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 if (file_path) {
70 free(file_path);
71 }
72 return fp;
73 }
74
75 /*
76 * Creates the empty config file at the path.
77 * On success, returns 0;
78 * on error, returns -1.
79 */
80 static int create_config_file(char *path)
81 {
82 int ret;
83 FILE *fp;
84
85 fp = open_config(path, "w+");
86 if (fp == NULL) {
87 ERR("Unable to create config file");
88 ret = -1;
89 goto error;
90 }
91
92 ret = fclose(fp);
93
94 error:
95 return ret;
96 }
97
98 /*
99 * Append data to the config file in file_path
100 * On success, returns 0;
101 * on error, returns -1.
102 */
103 static int write_config(char *file_path, size_t size, char *data)
104 {
105 FILE *fp;
106 size_t len;
107 int ret = 0;
108
109 fp = open_config(file_path, "a");
110 if (fp == NULL) {
111 ret = -1;
112 goto end;
113 }
114
115 /* Write session name into config file */
116 len = fwrite(data, size, 1, fp);
117 if (len != 1) {
118 ret = -1;
119 }
120 if (fclose(fp)) {
121 PERROR("close write_config");
122 }
123 end:
124 return ret;
125 }
126
127 /*
128 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
129 */
130 char *config_get_default_path(void)
131 {
132 return getenv("HOME");
133 }
134
135 /*
136 * Destroys directory config and file config.
137 */
138 void config_destroy(char *path)
139 {
140 int ret;
141 char *config_path;
142
143 config_path = config_get_file_path(path);
144 if (config_path == NULL) {
145 return;
146 }
147
148 if (!config_exists(config_path)) {
149 goto end;
150 }
151
152 DBG("Removing %s\n", config_path);
153 ret = remove(config_path);
154 if (ret < 0) {
155 perror("remove config file");
156 }
157 end:
158 free(config_path);
159 }
160
161 /*
162 * Destroys the default config
163 */
164 void config_destroy_default(void)
165 {
166 char *path = config_get_default_path();
167 if (path == NULL) {
168 return;
169 }
170 config_destroy(path);
171 }
172
173 /*
174 * Returns 1 if config exists, 0 otherwise
175 */
176 int config_exists(const char *path)
177 {
178 int ret;
179 struct stat info;
180
181 ret = stat(path, &info);
182 if (ret < 0) {
183 return 0;
184 }
185 return S_ISREG(info.st_mode) || S_ISDIR(info.st_mode);
186 }
187
188 /*
189 * Returns the session name from the config file.
190 * The caller is responsible for freeing the returned string.
191 * On error, NULL is returned.
192 */
193 char *config_read_session_name(char *path)
194 {
195 int ret;
196 FILE *fp;
197 char var[NAME_MAX], *session_name;
198
199 session_name = malloc(NAME_MAX);
200 if (session_name == NULL) {
201 ERR("Out of memory");
202 goto error;
203 }
204
205 fp = open_config(path, "r");
206 if (fp == NULL) {
207 ERR("Can't find valid lttng config %s/.lttngrc", path);
208 MSG("Did you create a session? (lttng create <my_session>)");
209 goto error;
210 }
211
212 while (!feof(fp)) {
213 if ((ret = fscanf(fp, "%[^'=']=%s\n", var, session_name)) != 2) {
214 if (ret == -1) {
215 ERR("Missing session=NAME in config file.");
216 goto error_close;
217 }
218 continue;
219 }
220
221 if (strcmp(var, "session") == 0) {
222 goto found;
223 }
224 }
225
226 error_close:
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.035731 seconds and 4 git commands to generate.