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.
24 #include <sys/types.h>
27 #include <common/error.h>
28 #include <common/utils.h>
33 * Returns the path with '/CONFIG_FILENAME' added to it;
34 * path will be NULL if an error occurs.
36 char *config_get_file_path(char *path
)
41 ret
= asprintf(&file_path
, "%s/%s", path
, CONFIG_FILENAME
);
43 ERR("Fail allocating config file path");
51 * Returns an open FILE pointer to the config file;
52 * on error, NULL is returned.
54 static FILE *open_config(char *path
, const char *mode
)
59 file_path
= config_get_file_path(path
);
60 if (file_path
== NULL
) {
64 fp
= fopen(file_path
, mode
);
75 * Creates the empty config file at the path.
76 * On success, returns 0;
77 * on error, returns -1.
79 static int create_config_file(char *path
)
84 fp
= open_config(path
, "w+");
86 ERR("Unable to create config file");
98 * Append data to the config file in file_path
99 * On success, returns 0;
100 * on error, returns -1.
102 static int write_config(char *file_path
, size_t size
, char *data
)
108 fp
= open_config(file_path
, "a");
114 /* Write session name into config file */
115 len
= fwrite(data
, size
, 1, fp
);
120 PERROR("close write_config");
127 * Destroys directory config and file config.
129 void config_destroy(char *path
)
134 config_path
= config_get_file_path(path
);
135 if (config_path
== NULL
) {
139 if (!config_exists(config_path
)) {
143 DBG("Removing %s\n", config_path
);
144 ret
= remove(config_path
);
146 perror("remove config file");
153 * Destroys the default config
155 void config_destroy_default(void)
157 char *path
= utils_get_home_dir();
161 config_destroy(path
);
165 * Returns 1 if config exists, 0 otherwise
167 int config_exists(const char *path
)
172 ret
= stat(path
, &info
);
176 return S_ISREG(info
.st_mode
) || S_ISDIR(info
.st_mode
);
180 * Returns the session name from the config file.
181 * The caller is responsible for freeing the returned string.
182 * On error, NULL is returned.
184 char *config_read_session_name(char *path
)
188 char var
[NAME_MAX
], *session_name
;
189 #if (NAME_MAX == 255)
190 #define NAME_MAX_SCANF_IS_A_BROKEN_API "254"
193 session_name
= malloc(NAME_MAX
);
194 if (session_name
== NULL
) {
195 ERR("Out of memory");
199 fp
= open_config(path
, "r");
201 ERR("Can't find valid lttng config %s/.lttngrc", path
);
202 MSG("Did you create a session? (lttng create <my_session>)");
208 if ((ret
= fscanf(fp
, "%" NAME_MAX_SCANF_IS_A_BROKEN_API
209 "[^'=']=%" NAME_MAX_SCANF_IS_A_BROKEN_API
"s\n",
210 var
, session_name
)) != 2) {
212 ERR("Missing session=NAME in config file.");
218 if (strcmp(var
, "session") == 0) {
227 PERROR("close config read session name");
236 PERROR("close config read session name found");
243 * Write session name option to the config file.
244 * On success, returns 0;
245 * on error, returns -1.
247 int config_add_session_name(char *path
, char *name
)
250 char *attr
= "session=";
251 /* Max name len accepted plus attribute's len and the NULL byte. */
252 char session_name
[NAME_MAX
+ strlen(attr
) + 1];
255 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
256 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
258 ret
= snprintf(session_name
, sizeof(session_name
), "%s%s\n", attr
, name
);
263 ret
= write_config(path
, ret
, session_name
);
269 * Init configuration directory and file.
270 * On success, returns 0;
271 * on error, returns -1.
273 int config_init(char *session_name
)
278 path
= utils_get_home_dir();
284 /* Create default config file */
285 ret
= create_config_file(path
);
290 ret
= config_add_session_name(path
, session_name
);
295 DBG("Init config session in %s", path
);