Add channel output method selection
[lttng-tools.git] / lttng / conf.c
CommitLineData
f3ed775e
DG
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
beb8c75a 28#include "conf.h"
f3ed775e
DG
29#include "lttngerr.h"
30
31/*
32 * get_config_file_path
33 *
34 * Return the path with '/CONFIG_FILENAME' added to it.
35 */
36static 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 */
54static 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
70error:
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 */
82static 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
96error:
97 return ret;
98}
99
100/*
101 * create_config_dir
102 *
103 * Create the empty config dir.
104 */
105static 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
122error:
123 return ret;
124}
125
126/*
127 * write_config
128 *
129 * Append data to the config file in file_path
130 */
131static 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
144error:
145 return;
146}
147
148/*
149 * config_get_default_path
150 *
151 * Return the default path to config directory which is the current working
152 * directory. User must free() the returned allocated string.
153 */
154char *config_get_default_path(void)
155{
156 char *alloc_path;
157
158 alloc_path = getcwd(NULL, 0);
159 if (alloc_path == NULL) {
160 perror("getcwd");
161 }
162
163 return alloc_path;
164}
165
166/*
167 * config_destroy
168 *
169 * Destroy directory config and file config.
170 */
171void config_destroy(char *path)
172{
173 int ret;
174 char *config_path;
175
176 config_path = get_config_file_path(path);
177
178 ret = remove(config_path);
179 if (ret < 0) {
180 perror("remove config file");
181 }
182
183 ret = rmdir(path);
184 if (ret < 0) {
185 perror("rmdir config dir");
186 }
187
188 free(config_path);
189}
190
191/*
192 * config_read_session_name
193 *
194 * Return sesson name from the config file.
195 */
196char *config_read_session_name(char *path)
197{
198 int ret;
199 FILE *fp;
200 char var[NAME_MAX], *session_name;
201
202 fp = open_config(path, "r");
203 if (fp == NULL) {
204 ERR("Can't find valid lttng config in %s", path);
205 goto error;
206 }
207
208 session_name = malloc(NAME_MAX);
209 while (!feof(fp)) {
210 if ((ret = fscanf(fp, "%[^'=']=%s\n", var, session_name)) != 2) {
211 if (ret == -1) {
212 ERR("Missing session=NAME in config file.");
213 goto error;
214 }
215 continue;
216 }
217
218 if (strcmp(var, "session") == 0) {
219 goto found;
220 }
221 }
222
223 fclose(fp);
224
225error:
226 return NULL;
227
228found:
229 fclose(fp);
230 return session_name;
231
232}
233
234/*
235 * config_add_session_name
236 *
237 * Write session name option to the config file.
238 */
239int config_add_session_name(char *path, char *name)
240{
241 int ret;
242 char session_name[NAME_MAX];
243
244 ret = snprintf(session_name, NAME_MAX, "session=%s\n", name);
245 if (ret < 0) {
246 goto error;
247 }
248
249 write_config(path, ret, session_name);
250 ret = 0;
251
252error:
253 return ret;
254}
255
256/*
257 * config_generate_dir_path
258 *
259 * Return allocated path string to path/CONFIG_DIRNAME.
260 */
261char *config_generate_dir_path(char *path)
262{
263 int ret;
264 char *new_path;
265
266 ret = asprintf(&new_path, "%s/%s", path, CONFIG_DIRNAME);
267 if (ret < 0) {
268 perror("config path problem");
269 goto error;
270 }
271
272error:
273 return new_path;
274}
275
276/*
277 * config_init
278 *
279 * Init configuration directory and file.
280 */
281int config_init(char *path)
282{
283 int ret;
284
285 /* Create config directory (.lttng) */
286 ret = create_config_dir(path);
287 if (ret < 0) {
288 goto error;
289 }
290
291 /* Create default config file */
292 ret = create_config_file(path);
293 if (ret < 0) {
294 goto error;
295 }
296
297 DBG("Init config session in %s", path);
298
299error:
300 return ret;
301}
This page took 0.048914 seconds and 4 git commands to generate.