Fix build system for valid make dist
[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
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
f3ed775e
DG
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/*
58a97671 32 * config_get_file_path
f3ed775e
DG
33 *
34 * Return the path with '/CONFIG_FILENAME' added to it.
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");
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
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) {
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
f3ed775e
DG
100/*
101 * write_config
102 *
103 * Append data to the config file in file_path
104 */
105static void write_config(char *file_path, size_t size, char *data)
106{
107 FILE *fp;
108
109 fp = open_config(file_path, "a");
110 if (fp == NULL) {
111 goto error;
112 }
113
114 /* Write session name into config file */
115 fwrite(data, size, 1, fp);
116 fclose(fp);
117
118error:
119 return;
120}
121
122/*
123 * config_get_default_path
124 *
58a97671 125 * Return the HOME directory path. Caller MUST NOT free(3) the return pointer.
f3ed775e
DG
126 */
127char *config_get_default_path(void)
128{
58a97671 129 return getenv("HOME");
f3ed775e
DG
130}
131
132/*
133 * config_destroy
134 *
135 * Destroy directory config and file config.
136 */
137void config_destroy(char *path)
138{
139 int ret;
140 char *config_path;
141
58a97671
DG
142 config_path = config_get_file_path(path);
143 if (config_path == NULL) {
144 return;
145 }
f3ed775e
DG
146
147 ret = remove(config_path);
148 if (ret < 0) {
149 perror("remove config file");
150 }
151
f3ed775e
DG
152 free(config_path);
153}
154
155/*
156 * config_read_session_name
157 *
158 * Return sesson name from the config file.
159 */
160char *config_read_session_name(char *path)
161{
162 int ret;
163 FILE *fp;
164 char var[NAME_MAX], *session_name;
165
166 fp = open_config(path, "r");
167 if (fp == NULL) {
0d63dd19 168 ERR("Can't find valid lttng config %s/.lttngrc", path);
f3ed775e
DG
169 goto error;
170 }
171
172 session_name = malloc(NAME_MAX);
173 while (!feof(fp)) {
174 if ((ret = fscanf(fp, "%[^'=']=%s\n", var, session_name)) != 2) {
175 if (ret == -1) {
176 ERR("Missing session=NAME in config file.");
177 goto error;
178 }
179 continue;
180 }
181
182 if (strcmp(var, "session") == 0) {
183 goto found;
184 }
185 }
186
187 fclose(fp);
188
189error:
190 return NULL;
191
192found:
193 fclose(fp);
194 return session_name;
195
196}
197
198/*
199 * config_add_session_name
200 *
201 * Write session name option to the config file.
202 */
203int config_add_session_name(char *path, char *name)
204{
205 int ret;
206 char session_name[NAME_MAX];
207
208 ret = snprintf(session_name, NAME_MAX, "session=%s\n", name);
209 if (ret < 0) {
210 goto error;
211 }
212
213 write_config(path, ret, session_name);
214 ret = 0;
215
216error:
217 return ret;
218}
219
f3ed775e
DG
220/*
221 * config_init
222 *
223 * Init configuration directory and file.
224 */
58a97671 225int config_init(char *session_name)
f3ed775e
DG
226{
227 int ret;
58a97671 228 char *path;
f3ed775e 229
58a97671
DG
230 path = config_get_default_path();
231 if (path == NULL) {
232 ret = -1;
f3ed775e
DG
233 goto error;
234 }
235
236 /* Create default config file */
237 ret = create_config_file(path);
238 if (ret < 0) {
239 goto error;
240 }
241
58a97671
DG
242 ret = config_add_session_name(path, session_name);
243 if (ret < 0) {
244 goto error;
245 }
246
f3ed775e
DG
247 DBG("Init config session in %s", path);
248
249error:
250 return ret;
251}
This page took 0.031205 seconds and 4 git commands to generate.