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