1cf0907ad884bf204060e6bd6f8f7df79f7d0bb1
[lttng-tools.git] / 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 as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _GNU_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 "conf.h"
29 #include "lttngerr.h"
30
31 /*
32 * get_config_file_path
33 *
34 * Return the path with '/CONFIG_FILENAME' added to it.
35 */
36 static char *get_config_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 }
45
46 return file_path;
47 }
48
49 /*
50 * open_config
51 *
52 * Return an open FILE pointer to the config file.
53 */
54 static FILE *open_config(char *path, const char *mode)
55 {
56 FILE *fp = NULL;
57 char *file_path;
58
59 file_path = get_config_file_path(path);
60 if (file_path == NULL) {
61 goto error;
62 }
63
64 fp = fopen(file_path, mode);
65 if (fp == NULL) {
66 perror("config file");
67 goto error;
68 }
69
70 error:
71 if (file_path) {
72 free(file_path);
73 }
74 return fp;
75 }
76
77 /*
78 * create_config_file
79 *
80 * Create the empty config file a the path.
81 */
82 static int create_config_file(char *path)
83 {
84 int ret;
85 FILE *fp;
86
87 fp = open_config(path, "w+");
88 if (fp == NULL) {
89 ERR("Unable to create config file");
90 ret = -1;
91 goto error;
92 }
93
94 ret = fclose(fp);
95
96 error:
97 return ret;
98 }
99
100 /*
101 * create_config_dir
102 *
103 * Create the empty config dir.
104 */
105 static int create_config_dir(char *path)
106 {
107 int ret;
108
109 /* Create session directory .lttng */
110 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
111 if (ret < 0) {
112 if (errno == EEXIST) {
113 ERR("Session already exist at %s", path);
114 } else {
115 perror("mkdir config");
116 ERR("Couldn't init config directory at %s", path);
117 }
118 ret = -errno;
119 goto error;
120 }
121
122 error:
123 return ret;
124 }
125
126 /*
127 * write_config
128 *
129 * Append data to the config file in file_path
130 */
131 static void write_config(char *file_path, size_t size, char *data)
132 {
133 FILE *fp;
134
135 fp = open_config(file_path, "a");
136 if (fp == NULL) {
137 goto error;
138 }
139
140 /* Write session name into config file */
141 fwrite(data, size, 1, fp);
142 fclose(fp);
143
144 error:
145 return;
146 }
147
148 /*
149 * config_get_default_path
150 *
151 * Return the HOME directory path. The output is dup so the user MUST
152 * free(3) the returned string.
153 */
154 char *config_get_default_path(void)
155 {
156 return strdup(getenv("HOME"));
157 }
158
159 /*
160 * config_destroy
161 *
162 * Destroy directory config and file config.
163 */
164 void config_destroy(char *path)
165 {
166 int ret;
167 char *config_path;
168
169 config_path = get_config_file_path(path);
170
171 ret = remove(config_path);
172 if (ret < 0) {
173 perror("remove config file");
174 }
175
176 ret = rmdir(path);
177 if (ret < 0) {
178 perror("rmdir config dir");
179 }
180
181 free(config_path);
182 }
183
184 /*
185 * config_read_session_name
186 *
187 * Return sesson name from the config file.
188 */
189 char *config_read_session_name(char *path)
190 {
191 int ret;
192 FILE *fp;
193 char var[NAME_MAX], *session_name;
194
195 fp = open_config(path, "r");
196 if (fp == NULL) {
197 ERR("Can't find valid lttng config in %s", path);
198 goto error;
199 }
200
201 session_name = malloc(NAME_MAX);
202 while (!feof(fp)) {
203 if ((ret = fscanf(fp, "%[^'=']=%s\n", var, session_name)) != 2) {
204 if (ret == -1) {
205 ERR("Missing session=NAME in config file.");
206 goto error;
207 }
208 continue;
209 }
210
211 if (strcmp(var, "session") == 0) {
212 goto found;
213 }
214 }
215
216 fclose(fp);
217
218 error:
219 return NULL;
220
221 found:
222 fclose(fp);
223 return session_name;
224
225 }
226
227 /*
228 * config_add_session_name
229 *
230 * Write session name option to the config file.
231 */
232 int config_add_session_name(char *path, char *name)
233 {
234 int ret;
235 char session_name[NAME_MAX];
236
237 ret = snprintf(session_name, NAME_MAX, "session=%s\n", name);
238 if (ret < 0) {
239 goto error;
240 }
241
242 write_config(path, ret, session_name);
243 ret = 0;
244
245 error:
246 return ret;
247 }
248
249 /*
250 * config_generate_dir_path
251 *
252 * Return allocated path string to path/CONFIG_DIRNAME.
253 */
254 char *config_generate_dir_path(char *path)
255 {
256 int ret;
257 char *new_path;
258
259 ret = asprintf(&new_path, "%s/%s", path, CONFIG_DIRNAME);
260 if (ret < 0) {
261 perror("config path problem");
262 goto error;
263 }
264
265 error:
266 return new_path;
267 }
268
269 /*
270 * config_init
271 *
272 * Init configuration directory and file.
273 */
274 int config_init(char *path)
275 {
276 int ret;
277
278 /* Create config directory (.lttng) */
279 ret = create_config_dir(path);
280 if (ret < 0) {
281 goto error;
282 }
283
284 /* Create default config file */
285 ret = create_config_file(path);
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.034032 seconds and 3 git commands to generate.