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 | ||
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 | 36 | char *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 | */ | |
54 | static 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) { | |
f3ed775e DG |
66 | goto error; |
67 | } | |
68 | ||
69 | error: | |
70 | if (file_path) { | |
71 | free(file_path); | |
72 | } | |
73 | return fp; | |
74 | } | |
75 | ||
76 | /* | |
77 | * create_config_file | |
78 | * | |
79 | * Create the empty config file a the path. | |
80 | */ | |
81 | static int create_config_file(char *path) | |
82 | { | |
83 | int ret; | |
84 | FILE *fp; | |
85 | ||
86 | fp = open_config(path, "w+"); | |
87 | if (fp == NULL) { | |
88 | ERR("Unable to create config file"); | |
89 | ret = -1; | |
90 | goto error; | |
91 | } | |
92 | ||
93 | ret = fclose(fp); | |
94 | ||
95 | error: | |
96 | return ret; | |
97 | } | |
98 | ||
f3ed775e DG |
99 | /* |
100 | * write_config | |
101 | * | |
102 | * Append data to the config file in file_path | |
103 | */ | |
a079cd4d | 104 | static int write_config(char *file_path, size_t size, char *data) |
f3ed775e DG |
105 | { |
106 | FILE *fp; | |
a079cd4d MD |
107 | size_t len; |
108 | int ret = 0; | |
f3ed775e DG |
109 | |
110 | fp = open_config(file_path, "a"); | |
111 | if (fp == NULL) { | |
a079cd4d MD |
112 | ret = -1; |
113 | goto end; | |
f3ed775e DG |
114 | } |
115 | ||
116 | /* Write session name into config file */ | |
a079cd4d MD |
117 | len = fwrite(data, size, 1, fp); |
118 | if (len < 1) { | |
119 | ret = -1; | |
120 | } | |
f3ed775e | 121 | fclose(fp); |
a079cd4d MD |
122 | end: |
123 | return ret; | |
f3ed775e DG |
124 | } |
125 | ||
126 | /* | |
127 | * config_get_default_path | |
128 | * | |
58a97671 | 129 | * Return the HOME directory path. Caller MUST NOT free(3) the return pointer. |
f3ed775e DG |
130 | */ |
131 | char *config_get_default_path(void) | |
132 | { | |
58a97671 | 133 | return getenv("HOME"); |
f3ed775e DG |
134 | } |
135 | ||
136 | /* | |
137 | * config_destroy | |
138 | * | |
139 | * Destroy directory config and file config. | |
140 | */ | |
141 | void config_destroy(char *path) | |
142 | { | |
143 | int ret; | |
144 | char *config_path; | |
145 | ||
58a97671 DG |
146 | config_path = config_get_file_path(path); |
147 | if (config_path == NULL) { | |
148 | return; | |
149 | } | |
f3ed775e DG |
150 | |
151 | ret = remove(config_path); | |
152 | if (ret < 0) { | |
153 | perror("remove config file"); | |
154 | } | |
155 | ||
f3ed775e DG |
156 | free(config_path); |
157 | } | |
158 | ||
159 | /* | |
160 | * config_read_session_name | |
161 | * | |
162 | * Return sesson name from the config file. | |
163 | */ | |
164 | char *config_read_session_name(char *path) | |
165 | { | |
166 | int ret; | |
167 | FILE *fp; | |
168 | char var[NAME_MAX], *session_name; | |
169 | ||
170 | fp = open_config(path, "r"); | |
171 | if (fp == NULL) { | |
0d63dd19 | 172 | ERR("Can't find valid lttng config %s/.lttngrc", path); |
a9bddc02 | 173 | MSG("Did you create a session? (lttng create <my_sesion>)"); |
f3ed775e DG |
174 | goto error; |
175 | } | |
176 | ||
177 | session_name = malloc(NAME_MAX); | |
178 | while (!feof(fp)) { | |
179 | if ((ret = fscanf(fp, "%[^'=']=%s\n", var, session_name)) != 2) { | |
180 | if (ret == -1) { | |
181 | ERR("Missing session=NAME in config file."); | |
182 | goto error; | |
183 | } | |
184 | continue; | |
185 | } | |
186 | ||
187 | if (strcmp(var, "session") == 0) { | |
188 | goto found; | |
189 | } | |
190 | } | |
191 | ||
192 | fclose(fp); | |
193 | ||
194 | error: | |
195 | return NULL; | |
196 | ||
197 | found: | |
198 | fclose(fp); | |
199 | return session_name; | |
200 | ||
201 | } | |
202 | ||
203 | /* | |
204 | * config_add_session_name | |
205 | * | |
206 | * Write session name option to the config file. | |
207 | */ | |
208 | int config_add_session_name(char *path, char *name) | |
209 | { | |
210 | int ret; | |
211 | char session_name[NAME_MAX]; | |
212 | ||
213 | ret = snprintf(session_name, NAME_MAX, "session=%s\n", name); | |
214 | if (ret < 0) { | |
215 | goto error; | |
216 | } | |
a079cd4d | 217 | ret = write_config(path, ret, session_name); |
f3ed775e DG |
218 | error: |
219 | return ret; | |
220 | } | |
221 | ||
f3ed775e DG |
222 | /* |
223 | * config_init | |
224 | * | |
225 | * Init configuration directory and file. | |
226 | */ | |
58a97671 | 227 | int config_init(char *session_name) |
f3ed775e DG |
228 | { |
229 | int ret; | |
58a97671 | 230 | char *path; |
f3ed775e | 231 | |
58a97671 DG |
232 | path = config_get_default_path(); |
233 | if (path == NULL) { | |
234 | ret = -1; | |
f3ed775e DG |
235 | goto error; |
236 | } | |
237 | ||
238 | /* Create default config file */ | |
239 | ret = create_config_file(path); | |
240 | if (ret < 0) { | |
241 | goto error; | |
242 | } | |
243 | ||
58a97671 DG |
244 | ret = config_add_session_name(path, session_name); |
245 | if (ret < 0) { | |
246 | goto error; | |
247 | } | |
248 | ||
f3ed775e DG |
249 | DBG("Init config session in %s", path); |
250 | ||
251 | error: | |
252 | return ret; | |
253 | } |