Don't remove the config directory on destroy
[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 *
b082db07
DG
151 * Return the HOME directory path. The output is dup so the user MUST
152 * free(3) the returned string.
f3ed775e
DG
153 */
154char *config_get_default_path(void)
155{
b082db07 156 return strdup(getenv("HOME"));
f3ed775e
DG
157}
158
159/*
160 * config_destroy
161 *
162 * Destroy directory config and file config.
163 */
164void 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
f3ed775e
DG
176 free(config_path);
177}
178
179/*
180 * config_read_session_name
181 *
182 * Return sesson name from the config file.
183 */
184char *config_read_session_name(char *path)
185{
186 int ret;
187 FILE *fp;
188 char var[NAME_MAX], *session_name;
189
190 fp = open_config(path, "r");
191 if (fp == NULL) {
192 ERR("Can't find valid lttng config in %s", path);
193 goto error;
194 }
195
196 session_name = malloc(NAME_MAX);
197 while (!feof(fp)) {
198 if ((ret = fscanf(fp, "%[^'=']=%s\n", var, session_name)) != 2) {
199 if (ret == -1) {
200 ERR("Missing session=NAME in config file.");
201 goto error;
202 }
203 continue;
204 }
205
206 if (strcmp(var, "session") == 0) {
207 goto found;
208 }
209 }
210
211 fclose(fp);
212
213error:
214 return NULL;
215
216found:
217 fclose(fp);
218 return session_name;
219
220}
221
222/*
223 * config_add_session_name
224 *
225 * Write session name option to the config file.
226 */
227int config_add_session_name(char *path, char *name)
228{
229 int ret;
230 char session_name[NAME_MAX];
231
232 ret = snprintf(session_name, NAME_MAX, "session=%s\n", name);
233 if (ret < 0) {
234 goto error;
235 }
236
237 write_config(path, ret, session_name);
238 ret = 0;
239
240error:
241 return ret;
242}
243
244/*
245 * config_generate_dir_path
246 *
247 * Return allocated path string to path/CONFIG_DIRNAME.
248 */
249char *config_generate_dir_path(char *path)
250{
251 int ret;
252 char *new_path;
253
254 ret = asprintf(&new_path, "%s/%s", path, CONFIG_DIRNAME);
255 if (ret < 0) {
256 perror("config path problem");
257 goto error;
258 }
259
260error:
261 return new_path;
262}
263
264/*
265 * config_init
266 *
267 * Init configuration directory and file.
268 */
269int config_init(char *path)
270{
271 int ret;
272
273 /* Create config directory (.lttng) */
274 ret = create_config_dir(path);
275 if (ret < 0) {
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
285 DBG("Init config session in %s", path);
286
287error:
288 return ret;
289}
This page took 0.032515 seconds and 4 git commands to generate.