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