Fix: destroy called twice on quit pipe
[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
d14d33bf
AM
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
f3ed775e
DG
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
6c1c0768 18#define _LGPL_SOURCE
f3ed775e
DG
19#include <limits.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
1dac0189 26#include <errno.h>
f3ed775e 27
9908810a 28#include <common/common.h>
feb0f3e5 29#include <common/utils.h>
10a8a223 30
beb8c75a 31#include "conf.h"
f3ed775e
DG
32
33/*
28eee19b
FG
34 * Returns the path with '/CONFIG_FILENAME' added to it;
35 * path will be NULL if an error occurs.
f3ed775e 36 */
58a97671 37char *config_get_file_path(char *path)
f3ed775e
DG
38{
39 int ret;
40 char *file_path;
41
42 ret = asprintf(&file_path, "%s/%s", path, CONFIG_FILENAME);
43 if (ret < 0) {
44 ERR("Fail allocating config file path");
487b253b 45 file_path = NULL;
f3ed775e
DG
46 }
47
48 return file_path;
49}
50
51/*
28eee19b
FG
52 * Returns an open FILE pointer to the config file;
53 * on error, NULL is returned.
f3ed775e
DG
54 */
55static FILE *open_config(char *path, const char *mode)
56{
57 FILE *fp = NULL;
58 char *file_path;
59
58a97671 60 file_path = config_get_file_path(path);
f3ed775e
DG
61 if (file_path == NULL) {
62 goto error;
63 }
64
65 fp = fopen(file_path, mode);
66 if (fp == NULL) {
f3ed775e
DG
67 goto error;
68 }
69
70error:
0e428499 71 free(file_path);
f3ed775e
DG
72 return fp;
73}
74
75/*
28eee19b
FG
76 * Creates the empty config file at the path.
77 * On success, returns 0;
78 * on error, returns -1.
f3ed775e
DG
79 */
80static int create_config_file(char *path)
81{
82 int ret;
83 FILE *fp;
84
85 fp = open_config(path, "w+");
86 if (fp == NULL) {
87 ERR("Unable to create config file");
88 ret = -1;
89 goto error;
90 }
91
92 ret = fclose(fp);
93
94error:
95 return ret;
96}
97
f3ed775e 98/*
28eee19b
FG
99 * Append data to the config file in file_path
100 * On success, returns 0;
101 * on error, returns -1.
f3ed775e 102 */
a079cd4d 103static int write_config(char *file_path, size_t size, char *data)
f3ed775e
DG
104{
105 FILE *fp;
a079cd4d
MD
106 size_t len;
107 int ret = 0;
f3ed775e
DG
108
109 fp = open_config(file_path, "a");
110 if (fp == NULL) {
a079cd4d
MD
111 ret = -1;
112 goto end;
f3ed775e
DG
113 }
114
115 /* Write session name into config file */
a079cd4d 116 len = fwrite(data, size, 1, fp);
27089920 117 if (len != 1) {
a079cd4d
MD
118 ret = -1;
119 }
f66c074c
DG
120 if (fclose(fp)) {
121 PERROR("close write_config");
122 }
a079cd4d
MD
123end:
124 return ret;
f3ed775e
DG
125}
126
f3ed775e 127/*
28eee19b 128 * Destroys directory config and file config.
f3ed775e
DG
129 */
130void config_destroy(char *path)
131{
132 int ret;
133 char *config_path;
134
58a97671
DG
135 config_path = config_get_file_path(path);
136 if (config_path == NULL) {
137 return;
138 }
f3ed775e 139
d6a07e7d
FG
140 if (!config_exists(config_path)) {
141 goto end;
142 }
143
144 DBG("Removing %s\n", config_path);
f3ed775e
DG
145 ret = remove(config_path);
146 if (ret < 0) {
6f04ed72 147 PERROR("remove config file");
f3ed775e 148 }
d6a07e7d 149end:
f3ed775e
DG
150 free(config_path);
151}
152
d6a07e7d 153/*
28eee19b 154 * Destroys the default config
d6a07e7d 155 */
d6a07e7d
FG
156void config_destroy_default(void)
157{
feb0f3e5 158 char *path = utils_get_home_dir();
d6a07e7d
FG
159 if (path == NULL) {
160 return;
161 }
162 config_destroy(path);
163}
164
165/*
28eee19b 166 * Returns 1 if config exists, 0 otherwise
d6a07e7d
FG
167 */
168int config_exists(const char *path)
169{
170 int ret;
171 struct stat info;
172
173 ret = stat(path, &info);
174 if (ret < 0) {
175 return 0;
176 }
177 return S_ISREG(info.st_mode) || S_ISDIR(info.st_mode);
178}
179
1dac0189
PPM
180static
181int _config_read_session_name(char *path, char **name)
f3ed775e 182{
1dac0189 183 int ret = 0;
f3ed775e
DG
184 FILE *fp;
185 char var[NAME_MAX], *session_name;
8ab7c0d9
MD
186#if (NAME_MAX == 255)
187#define NAME_MAX_SCANF_IS_A_BROKEN_API "254"
188#endif
f3ed775e 189
9908810a 190 session_name = zmalloc(NAME_MAX);
27089920 191 if (session_name == NULL) {
1dac0189 192 ret = -ENOMEM;
27089920
TD
193 ERR("Out of memory");
194 goto error;
195 }
196
f3ed775e
DG
197 fp = open_config(path, "r");
198 if (fp == NULL) {
1dac0189 199 ret = -ENOENT;
f3ed775e
DG
200 goto error;
201 }
202
f3ed775e 203 while (!feof(fp)) {
8ab7c0d9
MD
204 if ((ret = fscanf(fp, "%" NAME_MAX_SCANF_IS_A_BROKEN_API
205 "[^'=']=%" NAME_MAX_SCANF_IS_A_BROKEN_API "s\n",
206 var, session_name)) != 2) {
f3ed775e
DG
207 if (ret == -1) {
208 ERR("Missing session=NAME in config file.");
00f36863 209 goto error_close;
f3ed775e
DG
210 }
211 continue;
212 }
213
214 if (strcmp(var, "session") == 0) {
215 goto found;
216 }
217 }
218
00f36863 219error_close:
1dac0189 220 if (fclose(fp) < 0) {
f66c074c
DG
221 PERROR("close config read session name");
222 }
f3ed775e 223error:
1dac0189
PPM
224 free(session_name);
225 return ret;
f3ed775e 226found:
1dac0189
PPM
227 *name = session_name;
228 if (fclose(fp) < 0) {
f66c074c
DG
229 PERROR("close config read session name found");
230 }
1dac0189
PPM
231 return ret;
232}
233
234/*
235 * Returns the session name from the config file.
236 *
237 * The caller is responsible for freeing the returned string.
238 * On error, NULL is returned.
239 */
240char *config_read_session_name(char *path)
241{
242 int ret;
243 char *name = NULL;
244
245 ret = _config_read_session_name(path, &name);
246 if (ret == -ENOENT) {
247 const char *home_dir = utils_get_home_dir();
248
249 ERR("Can't find valid lttng config %s/.lttngrc", home_dir);
250 MSG("Did you create a session? (lttng create <my_session>)");
251 }
252
253 return name;
254}
255
256/*
257 * Returns the session name from the config file. (no warnings/errors emitted)
258 *
259 * The caller is responsible for freeing the returned string.
260 * On error, NULL is returned.
261 */
262char *config_read_session_name_quiet(char *path)
263{
264 char *name = NULL;
f3ed775e 265
1dac0189
PPM
266 (void) _config_read_session_name(path, &name);
267 return name;
f3ed775e
DG
268}
269
270/*
28eee19b
FG
271 * Write session name option to the config file.
272 * On success, returns 0;
273 * on error, returns -1.
f3ed775e
DG
274 */
275int config_add_session_name(char *path, char *name)
276{
277 int ret;
487b253b
DG
278 char *attr = "session=";
279 /* Max name len accepted plus attribute's len and the NULL byte. */
280 char session_name[NAME_MAX + strlen(attr) + 1];
f3ed775e 281
27089920
TD
282 /*
283 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
284 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
285 */
487b253b
DG
286 ret = snprintf(session_name, sizeof(session_name), "%s%s\n", attr, name);
287 if (ret < 0) {
27089920 288 ret = -1;
f3ed775e
DG
289 goto error;
290 }
a079cd4d 291 ret = write_config(path, ret, session_name);
f3ed775e
DG
292error:
293 return ret;
294}
295
f3ed775e 296/*
28eee19b
FG
297 * Init configuration directory and file.
298 * On success, returns 0;
299 * on error, returns -1.
f3ed775e 300 */
58a97671 301int config_init(char *session_name)
f3ed775e
DG
302{
303 int ret;
58a97671 304 char *path;
f3ed775e 305
feb0f3e5 306 path = utils_get_home_dir();
58a97671
DG
307 if (path == NULL) {
308 ret = -1;
f3ed775e
DG
309 goto error;
310 }
311
312 /* Create default config file */
313 ret = create_config_file(path);
314 if (ret < 0) {
315 goto error;
316 }
317
58a97671
DG
318 ret = config_add_session_name(path, session_name);
319 if (ret < 0) {
320 goto error;
321 }
322
f3ed775e
DG
323 DBG("Init config session in %s", path);
324
325error:
326 return ret;
327}
This page took 0.060005 seconds and 4 git commands to generate.