Implement PID tracking for kernel tracing
[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 #define _LGPL_SOURCE
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 #include <common/common.h>
29 #include <common/utils.h>
30
31 #include "conf.h"
32
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 file_path = NULL;
46 }
47
48 return file_path;
49 }
50
51 /*
52 * Returns an open FILE pointer to the config file;
53 * on error, NULL is returned.
54 */
55 static FILE *open_config(char *path, const char *mode)
56 {
57 FILE *fp = NULL;
58 char *file_path;
59
60 file_path = config_get_file_path(path);
61 if (file_path == NULL) {
62 goto error;
63 }
64
65 fp = fopen(file_path, mode);
66 if (fp == NULL) {
67 goto error;
68 }
69
70 error:
71 free(file_path);
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 * Destroys directory config and file config.
129 */
130 void config_destroy(char *path)
131 {
132 int ret;
133 char *config_path;
134
135 config_path = config_get_file_path(path);
136 if (config_path == NULL) {
137 return;
138 }
139
140 if (!config_exists(config_path)) {
141 goto end;
142 }
143
144 DBG("Removing %s\n", config_path);
145 ret = remove(config_path);
146 if (ret < 0) {
147 PERROR("remove config file");
148 }
149 end:
150 free(config_path);
151 }
152
153 /*
154 * Destroys the default config
155 */
156 void config_destroy_default(void)
157 {
158 char *path = utils_get_home_dir();
159 if (path == NULL) {
160 return;
161 }
162 config_destroy(path);
163 }
164
165 /*
166 * Returns 1 if config exists, 0 otherwise
167 */
168 int config_exists(const char *path)
169 {
170 int ret;
171 struct stat info;
172
173 ret = stat(path, &info);
174 if (ret < 0) {
175 return 0;
176 }
177 return S_ISREG(info.st_mode) || S_ISDIR(info.st_mode);
178 }
179
180 /*
181 * Returns the session name from the config file.
182 * The caller is responsible for freeing the returned string.
183 * On error, NULL is returned.
184 */
185 char *config_read_session_name(char *path)
186 {
187 int ret;
188 FILE *fp;
189 char var[NAME_MAX], *session_name;
190 #if (NAME_MAX == 255)
191 #define NAME_MAX_SCANF_IS_A_BROKEN_API "254"
192 #endif
193
194 session_name = zmalloc(NAME_MAX);
195 if (session_name == NULL) {
196 ERR("Out of memory");
197 goto error;
198 }
199
200 fp = open_config(path, "r");
201 if (fp == NULL) {
202 ERR("Can't find valid lttng config %s/.lttngrc", path);
203 MSG("Did you create a session? (lttng create <my_session>)");
204 free(session_name);
205 goto error;
206 }
207
208 while (!feof(fp)) {
209 if ((ret = fscanf(fp, "%" NAME_MAX_SCANF_IS_A_BROKEN_API
210 "[^'=']=%" NAME_MAX_SCANF_IS_A_BROKEN_API "s\n",
211 var, session_name)) != 2) {
212 if (ret == -1) {
213 ERR("Missing session=NAME in config file.");
214 goto error_close;
215 }
216 continue;
217 }
218
219 if (strcmp(var, "session") == 0) {
220 goto found;
221 }
222 }
223
224 error_close:
225 free(session_name);
226 ret = fclose(fp);
227 if (ret < 0) {
228 PERROR("close config read session name");
229 }
230
231 error:
232 return NULL;
233
234 found:
235 ret = fclose(fp);
236 if (ret < 0) {
237 PERROR("close config read session name found");
238 }
239 return session_name;
240
241 }
242
243 /*
244 * Write session name option to the config file.
245 * On success, returns 0;
246 * on error, returns -1.
247 */
248 int config_add_session_name(char *path, char *name)
249 {
250 int ret;
251 char *attr = "session=";
252 /* Max name len accepted plus attribute's len and the NULL byte. */
253 char session_name[NAME_MAX + strlen(attr) + 1];
254
255 /*
256 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
257 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
258 */
259 ret = snprintf(session_name, sizeof(session_name), "%s%s\n", attr, name);
260 if (ret < 0) {
261 ret = -1;
262 goto error;
263 }
264 ret = write_config(path, ret, session_name);
265 error:
266 return ret;
267 }
268
269 /*
270 * Init configuration directory and file.
271 * On success, returns 0;
272 * on error, returns -1.
273 */
274 int config_init(char *session_name)
275 {
276 int ret;
277 char *path;
278
279 path = utils_get_home_dir();
280 if (path == NULL) {
281 ret = -1;
282 goto error;
283 }
284
285 /* Create default config file */
286 ret = create_config_file(path);
287 if (ret < 0) {
288 goto error;
289 }
290
291 ret = config_add_session_name(path, session_name);
292 if (ret < 0) {
293 goto error;
294 }
295
296 DBG("Init config session in %s", path);
297
298 error:
299 return ret;
300 }
This page took 0.036367 seconds and 5 git commands to generate.