Add trace directory option to lttng view
[lttng-tools.git] / src / bin / lttng / conf.c
CommitLineData
f3ed775e
DG
1/*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
f3ed775e
DG
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#define _GNU_SOURCE
20#include <limits.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
27
db758600 28#include <common/error.h>
10a8a223 29
beb8c75a 30#include "conf.h"
f3ed775e
DG
31
32/*
58a97671 33 * config_get_file_path
f3ed775e 34 *
27089920
TD
35 * Returns the path with '/CONFIG_FILENAME' added to it;
36 * path will be NULL if an error occurs.
f3ed775e 37 */
58a97671 38char *config_get_file_path(char *path)
f3ed775e
DG
39{
40 int ret;
41 char *file_path;
42
43 ret = asprintf(&file_path, "%s/%s", path, CONFIG_FILENAME);
44 if (ret < 0) {
45 ERR("Fail allocating config file path");
46 }
47
48 return file_path;
49}
50
51/*
52 * open_config
53 *
27089920
TD
54 * Returns an open FILE pointer to the config file;
55 * on error, NULL is returned.
f3ed775e
DG
56 */
57static FILE *open_config(char *path, const char *mode)
58{
59 FILE *fp = NULL;
60 char *file_path;
61
58a97671 62 file_path = config_get_file_path(path);
f3ed775e
DG
63 if (file_path == NULL) {
64 goto error;
65 }
66
67 fp = fopen(file_path, mode);
68 if (fp == NULL) {
f3ed775e
DG
69 goto error;
70 }
71
72error:
73 if (file_path) {
74 free(file_path);
75 }
76 return fp;
77}
78
79/*
80 * create_config_file
81 *
27089920
TD
82 * Creates the empty config file at the path.
83 * On success, returns 0;
84 * on error, returns -1.
f3ed775e
DG
85 */
86static int create_config_file(char *path)
87{
88 int ret;
89 FILE *fp;
90
91 fp = open_config(path, "w+");
92 if (fp == NULL) {
93 ERR("Unable to create config file");
94 ret = -1;
95 goto error;
96 }
97
98 ret = fclose(fp);
99
100error:
101 return ret;
102}
103
f3ed775e
DG
104/*
105 * write_config
106 *
107 * Append data to the config file in file_path
27089920
TD
108 * On success, returns 0;
109 * on error, returns -1.
f3ed775e 110 */
a079cd4d 111static int write_config(char *file_path, size_t size, char *data)
f3ed775e
DG
112{
113 FILE *fp;
a079cd4d
MD
114 size_t len;
115 int ret = 0;
f3ed775e
DG
116
117 fp = open_config(file_path, "a");
118 if (fp == NULL) {
a079cd4d
MD
119 ret = -1;
120 goto end;
f3ed775e
DG
121 }
122
123 /* Write session name into config file */
a079cd4d 124 len = fwrite(data, size, 1, fp);
27089920 125 if (len != 1) {
a079cd4d
MD
126 ret = -1;
127 }
f3ed775e 128 fclose(fp);
a079cd4d
MD
129end:
130 return ret;
f3ed775e
DG
131}
132
133/*
134 * config_get_default_path
135 *
27089920 136 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
f3ed775e
DG
137 */
138char *config_get_default_path(void)
139{
58a97671 140 return getenv("HOME");
f3ed775e
DG
141}
142
143/*
144 * config_destroy
145 *
27089920 146 * Destroys directory config and file config.
f3ed775e
DG
147 */
148void config_destroy(char *path)
149{
150 int ret;
151 char *config_path;
152
58a97671
DG
153 config_path = config_get_file_path(path);
154 if (config_path == NULL) {
155 return;
156 }
f3ed775e
DG
157
158 ret = remove(config_path);
159 if (ret < 0) {
160 perror("remove config file");
161 }
162
f3ed775e
DG
163 free(config_path);
164}
165
166/*
167 * config_read_session_name
168 *
27089920
TD
169 * Returns the session name from the config file.
170 * The caller is responsible for freeing the returned string.
171 * On error, NULL is returned.
f3ed775e
DG
172 */
173char *config_read_session_name(char *path)
174{
175 int ret;
176 FILE *fp;
177 char var[NAME_MAX], *session_name;
178
27089920
TD
179 session_name = malloc(NAME_MAX);
180 if (session_name == NULL) {
181 ERR("Out of memory");
182 goto error;
183 }
184
f3ed775e
DG
185 fp = open_config(path, "r");
186 if (fp == NULL) {
0d63dd19 187 ERR("Can't find valid lttng config %s/.lttngrc", path);
00f36863 188 MSG("Did you create a session? (lttng create <my_session>)");
f3ed775e
DG
189 goto error;
190 }
191
f3ed775e
DG
192 while (!feof(fp)) {
193 if ((ret = fscanf(fp, "%[^'=']=%s\n", var, session_name)) != 2) {
194 if (ret == -1) {
195 ERR("Missing session=NAME in config file.");
00f36863 196 goto error_close;
f3ed775e
DG
197 }
198 continue;
199 }
200
201 if (strcmp(var, "session") == 0) {
202 goto found;
203 }
204 }
205
00f36863 206error_close:
f3ed775e
DG
207 fclose(fp);
208
209error:
210 return NULL;
211
212found:
213 fclose(fp);
214 return session_name;
215
216}
217
218/*
219 * config_add_session_name
220 *
221 * Write session name option to the config file.
27089920
TD
222 * On success, returns 0;
223 * on error, returns -1.
f3ed775e
DG
224 */
225int config_add_session_name(char *path, char *name)
226{
227 int ret;
228 char session_name[NAME_MAX];
229
27089920
TD
230 /*
231 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
232 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
233 */
f3ed775e 234 ret = snprintf(session_name, NAME_MAX, "session=%s\n", name);
27089920
TD
235 if ((ret < 0) || (ret >= NAME_MAX)) {
236 ret = -1;
f3ed775e
DG
237 goto error;
238 }
a079cd4d 239 ret = write_config(path, ret, session_name);
f3ed775e
DG
240error:
241 return ret;
242}
243
f3ed775e
DG
244/*
245 * config_init
246 *
247 * Init configuration directory and file.
27089920
TD
248 * On success, returns 0;
249 * on error, returns -1.
f3ed775e 250 */
58a97671 251int config_init(char *session_name)
f3ed775e
DG
252{
253 int ret;
58a97671 254 char *path;
f3ed775e 255
58a97671
DG
256 path = config_get_default_path();
257 if (path == NULL) {
258 ret = -1;
f3ed775e
DG
259 goto error;
260 }
261
262 /* Create default config file */
263 ret = create_config_file(path);
264 if (ret < 0) {
265 goto error;
266 }
267
58a97671
DG
268 ret = config_add_session_name(path, session_name);
269 if (ret < 0) {
270 goto error;
271 }
272
f3ed775e
DG
273 DBG("Init config session in %s", path);
274
275error:
276 return ret;
277}
This page took 0.037519 seconds and 4 git commands to generate.