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