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