2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
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.
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.
25 #include <sys/types.h>
29 #include <common/common.h>
30 #include <common/utils.h>
35 * Returns the path with '/CONFIG_FILENAME' added to it;
36 * path will be NULL if an error occurs.
38 char *config_get_file_path(char *path
)
43 ret
= asprintf(&file_path
, "%s/%s", path
, CONFIG_FILENAME
);
45 ERR("Fail allocating config file path");
53 * Returns an open FILE pointer to the config file;
54 * on error, NULL is returned.
56 static FILE *open_config(char *path
, const char *mode
)
61 file_path
= config_get_file_path(path
);
62 if (file_path
== NULL
) {
66 fp
= fopen(file_path
, mode
);
77 * Creates the empty config file at the path.
78 * On success, returns 0;
79 * on error, returns -1.
81 static int create_config_file(char *path
)
86 fp
= open_config(path
, "w+");
88 ERR("Unable to create config file");
100 * Append data to the config file in file_path
101 * On success, returns 0;
102 * on error, returns -1.
104 static int write_config(char *file_path
, size_t size
, char *data
)
110 fp
= open_config(file_path
, "a");
116 /* Write session name into config file */
117 len
= fwrite(data
, size
, 1, fp
);
122 PERROR("close write_config");
129 * Destroys directory config and file config.
131 void config_destroy(char *path
)
136 config_path
= config_get_file_path(path
);
137 if (config_path
== NULL
) {
141 if (!config_exists(config_path
)) {
145 DBG("Removing %s\n", config_path
);
146 ret
= remove(config_path
);
148 PERROR("remove config file");
155 * Destroys the default config
157 void config_destroy_default(void)
159 char *path
= utils_get_home_dir();
163 config_destroy(path
);
167 * Returns 1 if config exists, 0 otherwise
169 int config_exists(const char *path
)
174 ret
= stat(path
, &info
);
178 return S_ISREG(info
.st_mode
) || S_ISDIR(info
.st_mode
);
182 int _config_read_session_name(char *path
, char **name
)
186 char var
[NAME_MAX
], *session_name
;
187 #if (NAME_MAX == 255)
188 #define NAME_MAX_SCANF_IS_A_BROKEN_API "254"
191 session_name
= zmalloc(NAME_MAX
);
192 if (session_name
== NULL
) {
194 ERR("Out of memory");
198 fp
= open_config(path
, "r");
205 if ((ret
= fscanf(fp
, "%" NAME_MAX_SCANF_IS_A_BROKEN_API
206 "[^'=']=%" NAME_MAX_SCANF_IS_A_BROKEN_API
"s\n",
207 var
, session_name
)) != 2) {
209 ERR("Missing session=NAME in config file.");
215 if (strcmp(var
, "session") == 0) {
221 if (fclose(fp
) < 0) {
222 PERROR("close config read session name");
228 *name
= session_name
;
229 if (fclose(fp
) < 0) {
230 PERROR("close config read session name found");
236 * Returns the session name from the config file.
238 * The caller is responsible for freeing the returned string.
239 * On error, NULL is returned.
241 char *config_read_session_name(char *path
)
246 ret
= _config_read_session_name(path
, &name
);
247 if (ret
== -ENOENT
) {
248 const char *home_dir
= utils_get_home_dir();
250 ERR("Can't find valid lttng config %s/.lttngrc", home_dir
);
251 MSG("Did you create a session? (lttng create <my_session>)");
258 * Returns the session name from the config file. (no warnings/errors emitted)
260 * The caller is responsible for freeing the returned string.
261 * On error, NULL is returned.
263 char *config_read_session_name_quiet(char *path
)
267 (void) _config_read_session_name(path
, &name
);
272 * Write session name option to the config file.
273 * On success, returns 0;
274 * on error, returns -1.
276 int config_add_session_name(char *path
, char *name
)
279 char *attr
= "session=";
280 /* Max name len accepted plus attribute's len and the NULL byte. */
281 char session_name
[NAME_MAX
+ strlen(attr
) + 1];
284 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
285 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
287 ret
= snprintf(session_name
, sizeof(session_name
), "%s%s\n", attr
, name
);
292 ret
= write_config(path
, ret
, session_name
);
298 * Init configuration directory and file.
299 * On success, returns 0;
300 * on error, returns -1.
302 int config_init(char *session_name
)
307 path
= utils_get_home_dir();
313 /* Create default config file */
314 ret
= create_config_file(path
);
319 ret
= config_add_session_name(path
, session_name
);
324 DBG("Init config session in %s", path
);