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