Use lttng_read/lttng_write wrappers
[lttng-tools.git] / src / bin / 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
d14d33bf
AM
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
f3ed775e
DG
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
db758600 27#include <common/error.h>
feb0f3e5 28#include <common/utils.h>
10a8a223 29
beb8c75a 30#include "conf.h"
f3ed775e
DG
31
32/*
28eee19b
FG
33 * Returns the path with '/CONFIG_FILENAME' added to it;
34 * path will be NULL if an error occurs.
f3ed775e 35 */
58a97671 36char *config_get_file_path(char *path)
f3ed775e
DG
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");
487b253b 44 file_path = NULL;
f3ed775e
DG
45 }
46
47 return file_path;
48}
49
50/*
28eee19b
FG
51 * Returns an open FILE pointer to the config file;
52 * on error, NULL is returned.
f3ed775e
DG
53 */
54static FILE *open_config(char *path, const char *mode)
55{
56 FILE *fp = NULL;
57 char *file_path;
58
58a97671 59 file_path = config_get_file_path(path);
f3ed775e
DG
60 if (file_path == NULL) {
61 goto error;
62 }
63
64 fp = fopen(file_path, mode);
65 if (fp == NULL) {
f3ed775e
DG
66 goto error;
67 }
68
69error:
0e428499 70 free(file_path);
f3ed775e
DG
71 return fp;
72}
73
74/*
28eee19b
FG
75 * Creates the empty config file at the path.
76 * On success, returns 0;
77 * on error, returns -1.
f3ed775e
DG
78 */
79static 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
93error:
94 return ret;
95}
96
f3ed775e 97/*
28eee19b
FG
98 * Append data to the config file in file_path
99 * On success, returns 0;
100 * on error, returns -1.
f3ed775e 101 */
a079cd4d 102static int write_config(char *file_path, size_t size, char *data)
f3ed775e
DG
103{
104 FILE *fp;
a079cd4d
MD
105 size_t len;
106 int ret = 0;
f3ed775e
DG
107
108 fp = open_config(file_path, "a");
109 if (fp == NULL) {
a079cd4d
MD
110 ret = -1;
111 goto end;
f3ed775e
DG
112 }
113
114 /* Write session name into config file */
a079cd4d 115 len = fwrite(data, size, 1, fp);
27089920 116 if (len != 1) {
a079cd4d
MD
117 ret = -1;
118 }
f66c074c
DG
119 if (fclose(fp)) {
120 PERROR("close write_config");
121 }
a079cd4d
MD
122end:
123 return ret;
f3ed775e
DG
124}
125
f3ed775e 126/*
28eee19b 127 * Destroys directory config and file config.
f3ed775e
DG
128 */
129void config_destroy(char *path)
130{
131 int ret;
132 char *config_path;
133
58a97671
DG
134 config_path = config_get_file_path(path);
135 if (config_path == NULL) {
136 return;
137 }
f3ed775e 138
d6a07e7d
FG
139 if (!config_exists(config_path)) {
140 goto end;
141 }
142
143 DBG("Removing %s\n", config_path);
f3ed775e
DG
144 ret = remove(config_path);
145 if (ret < 0) {
146 perror("remove config file");
147 }
d6a07e7d 148end:
f3ed775e
DG
149 free(config_path);
150}
151
d6a07e7d 152/*
28eee19b 153 * Destroys the default config
d6a07e7d 154 */
d6a07e7d
FG
155void config_destroy_default(void)
156{
feb0f3e5 157 char *path = utils_get_home_dir();
d6a07e7d
FG
158 if (path == NULL) {
159 return;
160 }
161 config_destroy(path);
162}
163
164/*
28eee19b 165 * Returns 1 if config exists, 0 otherwise
d6a07e7d
FG
166 */
167int 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
f3ed775e 179/*
28eee19b
FG
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.
f3ed775e
DG
183 */
184char *config_read_session_name(char *path)
185{
186 int ret;
187 FILE *fp;
188 char var[NAME_MAX], *session_name;
189
27089920
TD
190 session_name = malloc(NAME_MAX);
191 if (session_name == NULL) {
192 ERR("Out of memory");
193 goto error;
194 }
195
f3ed775e
DG
196 fp = open_config(path, "r");
197 if (fp == NULL) {
0d63dd19 198 ERR("Can't find valid lttng config %s/.lttngrc", path);
00f36863 199 MSG("Did you create a session? (lttng create <my_session>)");
dcee6f19 200 free(session_name);
f3ed775e
DG
201 goto error;
202 }
203
f3ed775e
DG
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.");
00f36863 208 goto error_close;
f3ed775e
DG
209 }
210 continue;
211 }
212
213 if (strcmp(var, "session") == 0) {
214 goto found;
215 }
216 }
217
00f36863 218error_close:
dcee6f19 219 free(session_name);
f66c074c
DG
220 ret = fclose(fp);
221 if (ret < 0) {
222 PERROR("close config read session name");
223 }
f3ed775e
DG
224
225error:
226 return NULL;
227
228found:
f66c074c
DG
229 ret = fclose(fp);
230 if (ret < 0) {
231 PERROR("close config read session name found");
232 }
f3ed775e
DG
233 return session_name;
234
235}
236
237/*
28eee19b
FG
238 * Write session name option to the config file.
239 * On success, returns 0;
240 * on error, returns -1.
f3ed775e
DG
241 */
242int config_add_session_name(char *path, char *name)
243{
244 int ret;
487b253b
DG
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];
f3ed775e 248
27089920
TD
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 */
487b253b
DG
253 ret = snprintf(session_name, sizeof(session_name), "%s%s\n", attr, name);
254 if (ret < 0) {
27089920 255 ret = -1;
f3ed775e
DG
256 goto error;
257 }
a079cd4d 258 ret = write_config(path, ret, session_name);
f3ed775e
DG
259error:
260 return ret;
261}
262
f3ed775e 263/*
28eee19b
FG
264 * Init configuration directory and file.
265 * On success, returns 0;
266 * on error, returns -1.
f3ed775e 267 */
58a97671 268int config_init(char *session_name)
f3ed775e
DG
269{
270 int ret;
58a97671 271 char *path;
f3ed775e 272
feb0f3e5 273 path = utils_get_home_dir();
58a97671
DG
274 if (path == NULL) {
275 ret = -1;
f3ed775e
DG
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
58a97671
DG
285 ret = config_add_session_name(path, session_name);
286 if (ret < 0) {
287 goto error;
288 }
289
f3ed775e
DG
290 DBG("Init config session in %s", path);
291
292error:
293 return ret;
294}
This page took 0.047964 seconds and 4 git commands to generate.