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