Fix: session name max length was not handled correctly
[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 * Returns the path with '/CONFIG_FILENAME' added to it;
33 * path will be NULL if an error occurs.
34 */
35 char *config_get_file_path(char *path)
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 file_path = NULL;
44 }
45
46 return file_path;
47 }
48
49 /*
50 * Returns an open FILE pointer to the config file;
51 * on error, NULL is returned.
52 */
53 static FILE *open_config(char *path, const char *mode)
54 {
55 FILE *fp = NULL;
56 char *file_path;
57
58 file_path = config_get_file_path(path);
59 if (file_path == NULL) {
60 goto error;
61 }
62
63 fp = fopen(file_path, mode);
64 if (fp == NULL) {
65 goto error;
66 }
67
68 error:
69 free(file_path);
70 return fp;
71 }
72
73 /*
74 * Creates the empty config file at the path.
75 * On success, returns 0;
76 * on error, returns -1.
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
96 /*
97 * Append data to the config file in file_path
98 * On success, returns 0;
99 * on error, returns -1.
100 */
101 static int write_config(char *file_path, size_t size, char *data)
102 {
103 FILE *fp;
104 size_t len;
105 int ret = 0;
106
107 fp = open_config(file_path, "a");
108 if (fp == NULL) {
109 ret = -1;
110 goto end;
111 }
112
113 /* Write session name into config file */
114 len = fwrite(data, size, 1, fp);
115 if (len != 1) {
116 ret = -1;
117 }
118 if (fclose(fp)) {
119 PERROR("close write_config");
120 }
121 end:
122 return ret;
123 }
124
125 /*
126 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
127 */
128 char *config_get_default_path(void)
129 {
130 return getenv("HOME");
131 }
132
133 /*
134 * Destroys directory config and file config.
135 */
136 void config_destroy(char *path)
137 {
138 int ret;
139 char *config_path;
140
141 config_path = config_get_file_path(path);
142 if (config_path == NULL) {
143 return;
144 }
145
146 if (!config_exists(config_path)) {
147 goto end;
148 }
149
150 DBG("Removing %s\n", config_path);
151 ret = remove(config_path);
152 if (ret < 0) {
153 perror("remove config file");
154 }
155 end:
156 free(config_path);
157 }
158
159 /*
160 * Destroys the default config
161 */
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 /*
172 * Returns 1 if config exists, 0 otherwise
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
186 /*
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.
190 */
191 char *config_read_session_name(char *path)
192 {
193 int ret;
194 FILE *fp;
195 char var[NAME_MAX], *session_name;
196
197 session_name = malloc(NAME_MAX);
198 if (session_name == NULL) {
199 ERR("Out of memory");
200 goto error;
201 }
202
203 fp = open_config(path, "r");
204 if (fp == NULL) {
205 ERR("Can't find valid lttng config %s/.lttngrc", path);
206 MSG("Did you create a session? (lttng create <my_session>)");
207 free(session_name);
208 goto error;
209 }
210
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.");
215 goto error_close;
216 }
217 continue;
218 }
219
220 if (strcmp(var, "session") == 0) {
221 goto found;
222 }
223 }
224
225 error_close:
226 free(session_name);
227 ret = fclose(fp);
228 if (ret < 0) {
229 PERROR("close config read session name");
230 }
231
232 error:
233 return NULL;
234
235 found:
236 ret = fclose(fp);
237 if (ret < 0) {
238 PERROR("close config read session name found");
239 }
240 return session_name;
241
242 }
243
244 /*
245 * Write session name option to the config file.
246 * On success, returns 0;
247 * on error, returns -1.
248 */
249 int config_add_session_name(char *path, char *name)
250 {
251 int ret;
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];
255
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 */
260 ret = snprintf(session_name, sizeof(session_name), "%s%s\n", attr, name);
261 if (ret < 0) {
262 ret = -1;
263 goto error;
264 }
265 ret = write_config(path, ret, session_name);
266 error:
267 return ret;
268 }
269
270 /*
271 * Init configuration directory and file.
272 * On success, returns 0;
273 * on error, returns -1.
274 */
275 int config_init(char *session_name)
276 {
277 int ret;
278 char *path;
279
280 path = config_get_default_path();
281 if (path == NULL) {
282 ret = -1;
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
292 ret = config_add_session_name(path, session_name);
293 if (ret < 0) {
294 goto error;
295 }
296
297 DBG("Init config session in %s", path);
298
299 error:
300 return ret;
301 }
This page took 0.035923 seconds and 5 git commands to generate.