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