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