2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
17 #include <common/compat/errno.h>
18 #include <common/common.h>
19 #include <common/utils.h>
24 * Returns the path with '/CONFIG_FILENAME' added to it;
25 * path will be NULL if an error occurs.
27 char *config_get_file_path(const char *path
)
32 ret
= asprintf(&file_path
, "%s/%s", path
, CONFIG_FILENAME
);
34 ERR("Fail allocating config file path");
42 * Returns an open FILE pointer to the config file;
43 * on error, NULL is returned.
45 static FILE *open_config(const char *path
, const char *mode
)
50 file_path
= config_get_file_path(path
);
51 if (file_path
== NULL
) {
55 fp
= fopen(file_path
, mode
);
66 * Creates the empty config file at the path.
67 * On success, returns 0;
68 * on error, returns -1.
70 static int create_config_file(const char *path
)
75 fp
= open_config(path
, "w+");
77 ERR("Unable to create config file");
89 * Append data to the config file in file_path
90 * On success, returns 0;
91 * on error, returns -1.
93 static int write_config(const char *file_path
, size_t size
, char *data
)
99 fp
= open_config(file_path
, "a");
105 /* Write session name into config file */
106 len
= fwrite(data
, size
, 1, fp
);
111 PERROR("close write_config");
118 * Destroys directory config and file config.
120 void config_destroy(const char *path
)
125 config_path
= config_get_file_path(path
);
126 if (config_path
== NULL
) {
130 if (!config_exists(config_path
)) {
134 DBG("Removing %s\n", config_path
);
135 ret
= remove(config_path
);
137 PERROR("remove config file");
144 * Destroys the default config
146 void config_destroy_default(void)
148 const char *path
= utils_get_home_dir();
152 config_destroy(path
);
156 * Returns 1 if config exists, 0 otherwise
158 int config_exists(const char *path
)
163 ret
= stat(path
, &info
);
167 return S_ISREG(info
.st_mode
) || S_ISDIR(info
.st_mode
);
171 int _config_read_session_name(const char *path
, char **name
)
175 char var
[NAME_MAX
], *session_name
;
177 #if (NAME_MAX == 255)
178 #define NAME_MAX_SCANF_IS_A_BROKEN_API "254"
181 session_name
= (char *) zmalloc(NAME_MAX
);
182 if (session_name
== NULL
) {
184 ERR("Out of memory");
188 fp
= open_config(path
, "r");
195 if ((ret
= fscanf(fp
, "%" NAME_MAX_SCANF_IS_A_BROKEN_API
196 "[^'=']=%" NAME_MAX_SCANF_IS_A_BROKEN_API
"s\n",
197 var
, session_name
)) != 2) {
199 ERR("Missing session=NAME in config file.");
205 if (strcmp(var
, "session") == 0) {
211 if (fclose(fp
) < 0) {
212 PERROR("close config read session name");
218 *name
= session_name
;
219 if (fclose(fp
) < 0) {
220 PERROR("close config read session name found");
226 * Returns the session name from the config file.
228 * The caller is responsible for freeing the returned string.
229 * On error, NULL is returned.
231 char *config_read_session_name(const char *path
)
236 ret
= _config_read_session_name(path
, &name
);
237 if (ret
== -ENOENT
) {
238 const char *home_dir
= utils_get_home_dir();
240 ERR("Can't find valid lttng config %s/.lttngrc", home_dir
);
241 MSG("Did you create a session? (lttng create <my_session>)");
248 * Returns the session name from the config file. (no warnings/errors emitted)
250 * The caller is responsible for freeing the returned string.
251 * On error, NULL is returned.
253 char *config_read_session_name_quiet(const char *path
)
257 (void) _config_read_session_name(path
, &name
);
262 * Write session name option to the config file.
263 * On success, returns 0;
264 * on error, returns -1.
266 int config_add_session_name(const char *path
, const char *name
)
269 const char *attr
= "session=";
270 /* Max name len accepted plus attribute's len and the NULL byte. */
271 char session_name
[NAME_MAX
+ strlen(attr
) + 1];
274 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
275 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
277 ret
= snprintf(session_name
, sizeof(session_name
), "%s%s\n", attr
, name
);
282 ret
= write_config(path
, ret
, session_name
);
288 * Init configuration directory and file.
289 * On success, returns 0;
290 * on error, returns -1.
292 int config_init(const char *session_name
)
297 path
= utils_get_home_dir();
303 /* Create default config file */
304 ret
= create_config_file(path
);
309 ret
= config_add_session_name(path
, session_name
);
314 DBG("Init config session in %s", path
);