2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include <common/common.hpp>
12 #include <common/compat/errno.hpp>
13 #include <common/utils.hpp>
20 #include <sys/types.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
== nullptr) {
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
== nullptr) {
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()
148 const char *path
= utils_get_home_dir();
149 if (path
== nullptr) {
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
);
170 static int _config_read_session_name(const char *path
, char **name
)
174 char var
[NAME_MAX
], *session_name
;
176 #if (NAME_MAX == 255)
177 #define NAME_MAX_SCANF_IS_A_BROKEN_API "254"
180 session_name
= calloc
<char>(NAME_MAX
);
181 if (session_name
== nullptr) {
183 ERR("Out of memory");
187 fp
= open_config(path
, "r");
194 if ((ret
= fscanf(fp
,
195 "%" NAME_MAX_SCANF_IS_A_BROKEN_API
196 "[^'=']=%" NAME_MAX_SCANF_IS_A_BROKEN_API
"s\n",
198 session_name
)) != 2) {
200 ERR("Missing session=NAME in config file.");
206 if (strcmp(var
, "session") == 0) {
212 if (fclose(fp
) < 0) {
213 PERROR("close config read session name");
219 *name
= session_name
;
220 if (fclose(fp
) < 0) {
221 PERROR("close config read session name found");
227 * Returns the session name from the config file.
229 * The caller is responsible for freeing the returned string.
230 * On error, NULL is returned.
232 char *config_read_session_name(const char *path
)
235 char *name
= nullptr;
237 ret
= _config_read_session_name(path
, &name
);
238 if (ret
== -ENOENT
) {
239 const char *home_dir
= utils_get_home_dir();
241 ERR("Can't find valid lttng config %s/.lttngrc", home_dir
);
242 MSG("Did you create a session? (lttng create <my_session>)");
249 * Returns the session name from the config file. (no warnings/errors emitted)
251 * The caller is responsible for freeing the returned string.
252 * On error, NULL is returned.
254 char *config_read_session_name_quiet(const char *path
)
256 char *name
= nullptr;
258 (void) _config_read_session_name(path
, &name
);
263 * Write session name option to the config file.
264 * On success, returns 0;
265 * on error, returns -1.
267 int config_add_session_name(const char *path
, const char *name
)
270 const char *attr
= "session=";
271 /* Max name len accepted plus attribute's len and the NULL byte. */
272 char session_name
[NAME_MAX
+ strlen(attr
) + 1];
275 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
276 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
278 ret
= snprintf(session_name
, sizeof(session_name
), "%s%s\n", attr
, name
);
283 ret
= write_config(path
, ret
, session_name
);
289 * Init configuration directory and file.
290 * On success, returns 0;
291 * on error, returns -1.
293 int config_init(const char *session_name
)
298 path
= utils_get_home_dir();
299 if (path
== nullptr) {
304 /* Create default config file */
305 ret
= create_config_file(path
);
310 ret
= config_add_session_name(path
, session_name
);
315 DBG("Init config session in %s", path
);