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