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 | |
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 | ||
db758600 | 28 | #include <common/error.h> |
10a8a223 | 29 | |
beb8c75a | 30 | #include "conf.h" |
f3ed775e DG |
31 | |
32 | /* | |
58a97671 | 33 | * config_get_file_path |
f3ed775e DG |
34 | * |
35 | * Return the path with '/CONFIG_FILENAME' added to it. | |
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 | * | |
53 | * Return an open FILE pointer to the config file. | |
54 | */ | |
55 | static FILE *open_config(char *path, const char *mode) | |
56 | { | |
57 | FILE *fp = NULL; | |
58 | char *file_path; | |
59 | ||
58a97671 | 60 | file_path = config_get_file_path(path); |
f3ed775e DG |
61 | if (file_path == NULL) { |
62 | goto error; | |
63 | } | |
64 | ||
65 | fp = fopen(file_path, mode); | |
66 | if (fp == NULL) { | |
f3ed775e DG |
67 | goto error; |
68 | } | |
69 | ||
70 | error: | |
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 | */ | |
82 | static 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 | ||
96 | error: | |
97 | return ret; | |
98 | } | |
99 | ||
f3ed775e DG |
100 | /* |
101 | * write_config | |
102 | * | |
103 | * Append data to the config file in file_path | |
104 | */ | |
a079cd4d | 105 | static int write_config(char *file_path, size_t size, char *data) |
f3ed775e DG |
106 | { |
107 | FILE *fp; | |
a079cd4d MD |
108 | size_t len; |
109 | int ret = 0; | |
f3ed775e DG |
110 | |
111 | fp = open_config(file_path, "a"); | |
112 | if (fp == NULL) { | |
a079cd4d MD |
113 | ret = -1; |
114 | goto end; | |
f3ed775e DG |
115 | } |
116 | ||
117 | /* Write session name into config file */ | |
a079cd4d MD |
118 | len = fwrite(data, size, 1, fp); |
119 | if (len < 1) { | |
120 | ret = -1; | |
121 | } | |
f3ed775e | 122 | fclose(fp); |
a079cd4d MD |
123 | end: |
124 | return ret; | |
f3ed775e DG |
125 | } |
126 | ||
127 | /* | |
128 | * config_get_default_path | |
129 | * | |
58a97671 | 130 | * Return the HOME directory path. Caller MUST NOT free(3) the return pointer. |
f3ed775e DG |
131 | */ |
132 | char *config_get_default_path(void) | |
133 | { | |
58a97671 | 134 | return getenv("HOME"); |
f3ed775e DG |
135 | } |
136 | ||
137 | /* | |
138 | * config_destroy | |
139 | * | |
140 | * Destroy directory config and file config. | |
141 | */ | |
142 | void config_destroy(char *path) | |
143 | { | |
144 | int ret; | |
145 | char *config_path; | |
146 | ||
58a97671 DG |
147 | config_path = config_get_file_path(path); |
148 | if (config_path == NULL) { | |
149 | return; | |
150 | } | |
f3ed775e DG |
151 | |
152 | ret = remove(config_path); | |
153 | if (ret < 0) { | |
154 | perror("remove config file"); | |
155 | } | |
156 | ||
f3ed775e DG |
157 | free(config_path); |
158 | } | |
159 | ||
160 | /* | |
161 | * config_read_session_name | |
162 | * | |
163 | * Return sesson name from the config file. | |
164 | */ | |
165 | char *config_read_session_name(char *path) | |
166 | { | |
167 | int ret; | |
168 | FILE *fp; | |
169 | char var[NAME_MAX], *session_name; | |
170 | ||
171 | fp = open_config(path, "r"); | |
172 | if (fp == NULL) { | |
0d63dd19 | 173 | ERR("Can't find valid lttng config %s/.lttngrc", path); |
00f36863 | 174 | MSG("Did you create a session? (lttng create <my_session>)"); |
f3ed775e DG |
175 | goto error; |
176 | } | |
177 | ||
178 | session_name = malloc(NAME_MAX); | |
179 | while (!feof(fp)) { | |
180 | if ((ret = fscanf(fp, "%[^'=']=%s\n", var, session_name)) != 2) { | |
181 | if (ret == -1) { | |
182 | ERR("Missing session=NAME in config file."); | |
00f36863 | 183 | goto error_close; |
f3ed775e DG |
184 | } |
185 | continue; | |
186 | } | |
187 | ||
188 | if (strcmp(var, "session") == 0) { | |
189 | goto found; | |
190 | } | |
191 | } | |
192 | ||
00f36863 | 193 | error_close: |
f3ed775e DG |
194 | fclose(fp); |
195 | ||
196 | error: | |
197 | return NULL; | |
198 | ||
199 | found: | |
200 | fclose(fp); | |
201 | return session_name; | |
202 | ||
203 | } | |
204 | ||
205 | /* | |
206 | * config_add_session_name | |
207 | * | |
208 | * Write session name option to the config file. | |
209 | */ | |
210 | int config_add_session_name(char *path, char *name) | |
211 | { | |
212 | int ret; | |
213 | char session_name[NAME_MAX]; | |
214 | ||
215 | ret = snprintf(session_name, NAME_MAX, "session=%s\n", name); | |
216 | if (ret < 0) { | |
217 | goto error; | |
218 | } | |
a079cd4d | 219 | ret = write_config(path, ret, session_name); |
f3ed775e DG |
220 | error: |
221 | return ret; | |
222 | } | |
223 | ||
f3ed775e DG |
224 | /* |
225 | * config_init | |
226 | * | |
227 | * Init configuration directory and file. | |
228 | */ | |
58a97671 | 229 | int config_init(char *session_name) |
f3ed775e DG |
230 | { |
231 | int ret; | |
58a97671 | 232 | char *path; |
f3ed775e | 233 | |
58a97671 DG |
234 | path = config_get_default_path(); |
235 | if (path == NULL) { | |
236 | ret = -1; | |
f3ed775e DG |
237 | goto error; |
238 | } | |
239 | ||
240 | /* Create default config file */ | |
241 | ret = create_config_file(path); | |
242 | if (ret < 0) { | |
243 | goto error; | |
244 | } | |
245 | ||
58a97671 DG |
246 | ret = config_add_session_name(path, session_name); |
247 | if (ret < 0) { | |
248 | goto error; | |
249 | } | |
250 | ||
f3ed775e DG |
251 | DBG("Init config session in %s", path); |
252 | ||
253 | error: | |
254 | return ret; | |
255 | } |