Fix: load all possible path for sessiond conf file
[lttng-tools.git] / src / common / config / config.c
index 829a645bbb18820b2ad1237574a9fe229596b6cd..cdc044d6dc72fda892f9f2d3239140714ad61eb0 100644 (file)
@@ -191,55 +191,73 @@ int config_get_section_entries(const char *override_path, const char *section,
                config_entry_handler_cb handler, void *user_data)
 {
        int ret = 0;
+       char *path;
        FILE *config_file = NULL;
        struct handler_filter_args filter = { section, handler, user_data };
 
-       if (override_path) {
-               config_file = fopen(override_path, "r");
-               if (config_file) {
-                       DBG("Loaded daemon configuration file at %s",
-                               override_path);
-               } else {
-                       ERR("Failed to open daemon configuration file at %s",
-                               override_path);
-                       ret = -ENOENT;
-                       goto end;
-               }
-       } else {
-               char *path = utils_get_home_dir();
+       /* First, try system-wide conf. file. */
+       path = DEFAULT_DAEMON_SYSTEM_CONFIGPATH;
 
-               /* Try to open the user's daemon configuration file */
-               if (path) {
-                       ret = asprintf(&path, DEFAULT_DAEMON_HOME_CONFIGPATH, path);
-                       if (ret < 0) {
-                               goto end;
-                       }
+       config_file = fopen(path, "r");
+       if (config_file) {
+               DBG("Loading daemon conf file at %s", path);
+               /*
+                * Return value is not very important here since error or not, we
+                * continue and try the next possible conf. file.
+                */
+               (void) ini_parse_file(config_file,
+                               (ini_entry_handler) config_entry_handler_filter,
+                               (void *) &filter);
+               fclose(config_file);
+       }
 
-                       ret = 0;
-                       config_file = fopen(path, "r");
-                       if (config_file) {
-                               DBG("Loaded daemon configuration file at %s", path);
-                       }
+       /* Second is the user local configuration. */
+       path = utils_get_home_dir();
+       if (path) {
+               char fullpath[PATH_MAX];
 
-                       free(path);
+               ret = snprintf(fullpath, sizeof(fullpath),
+                               DEFAULT_DAEMON_HOME_CONFIGPATH, path);
+               if (ret < 0) {
+                       PERROR("snprintf user conf. path");
+                       goto error;
                }
 
-               /* Try to open the system daemon configuration file */
-               if (!config_file) {
-                       config_file = fopen(DEFAULT_DAEMON_HOME_CONFIGPATH, "r");
+               config_file = fopen(fullpath, "r");
+               if (config_file) {
+                       DBG("Loading daemon user conf file at %s", path);
+                       /*
+                        * Return value is not very important here since error or not, we
+                        * continue and try the next possible conf. file.
+                        */
+                       (void) ini_parse_file(config_file,
+                                       (ini_entry_handler) config_entry_handler_filter,
+                                       (void *) &filter);
+                       fclose(config_file);
                }
        }
 
-       if (!config_file) {
-               DBG("No daemon configuration file found.");
-               goto end;
+       /* Final path is the one that the user might have provided. */
+       if (override_path) {
+               config_file = fopen(override_path, "r");
+               if (config_file) {
+                       DBG("Loading daemon command line conf file at %s", override_path);
+                       (void) ini_parse_file(config_file,
+                                       (ini_entry_handler) config_entry_handler_filter,
+                                       (void *) &filter);
+                       fclose(config_file);
+               } else {
+                       ERR("Failed to open daemon configuration file at %s",
+                               override_path);
+                       ret = -ENOENT;
+                       goto error;
+               }
        }
 
-       ret = ini_parse_file(config_file,
-                       (ini_entry_handler) config_entry_handler_filter, (void *) &filter);
+       /* Everything went well. */
+       ret = 0;
 
-       fclose(config_file);
-end:
+error:
        return ret;
 }
 
This page took 0.026785 seconds and 5 git commands to generate.