Fix: wrong casting of return value
[lttng-tools.git] / src / common / config / config.c
index 13af2f117e7bf7306cdab4930d45a711da5312ab..cdc044d6dc72fda892f9f2d3239140714ad61eb0 100644 (file)
@@ -191,54 +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 };
 
+       /* First, try system-wide conf. file. */
+       path = DEFAULT_DAEMON_SYSTEM_CONFIGPATH;
+
+       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);
+       }
+
+       /* Second is the user local configuration. */
+       path = utils_get_home_dir();
+       if (path) {
+               char fullpath[PATH_MAX];
+
+               ret = snprintf(fullpath, sizeof(fullpath),
+                               DEFAULT_DAEMON_HOME_CONFIGPATH, path);
+               if (ret < 0) {
+                       PERROR("snprintf user conf. path");
+                       goto error;
+               }
+
+               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);
+               }
+       }
+
+       /* Final path is the one that the user might have provided. */
        if (override_path) {
                config_file = fopen(override_path, "r");
                if (config_file) {
-                       DBG("Loaded daemon configuration file at %s",
-                               override_path);
+                       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 end;
-               }
-       } else {
-               char *path = utils_get_home_dir();
-
-               /* Try to open the user's daemon configuration file */
-               if (path) {
-                       ret = asprintf(&path, DEFAULT_DAEMON_HOME_CONFIGPATH, path);
-                       if (ret < 0) {
-                               goto end;
-                       }
-
-                       ret = 0;
-                       config_file = fopen(path, "r");
-                       if (config_file) {
-                               DBG("Loaded daemon configuration file at %s", path);
-                       }
-
-                       free(path);
-               }
-
-               /* Try to open the system daemon configuration file */
-               if (!config_file) {
-                       config_file = fopen(DEFAULT_DAEMON_HOME_CONFIGPATH, "r");
+                       goto error;
                }
        }
 
-       if (!config_file) {
-               DBG("No daemon configuration file found.");
-               goto end;
-       }
-
-       ret = ini_parse_file(config_file,
-                       (ini_entry_handler) config_entry_handler_filter, (void *) &filter);
+       /* Everything went well. */
+       ret = 0;
 
-end:
+error:
        return ret;
 }
 
@@ -568,13 +587,50 @@ void fini_session_config_validation_ctx(
        memset(ctx, 0, sizeof(struct session_config_validation_ctx));
 }
 
+static
+char *get_session_config_xsd_path()
+{
+       char *xsd_path;
+       const char *base_path = getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV);
+       size_t base_path_len;
+       size_t max_path_len;
+
+       if (!base_path) {
+               base_path = DEFAULT_SESSION_CONFIG_XSD_PATH;
+       }
+
+       base_path_len = strlen(base_path);
+       max_path_len = base_path_len +
+               sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME) + 1;
+       xsd_path = zmalloc(max_path_len);
+       if (!xsd_path) {
+               goto end;
+       }
+
+       strncpy(xsd_path, base_path, max_path_len);
+       if (xsd_path[base_path_len - 1] != '/') {
+               xsd_path[base_path_len++] = '/';
+       }
+
+       strncpy(xsd_path + base_path_len, DEFAULT_SESSION_CONFIG_XSD_FILENAME,
+               max_path_len - base_path_len);
+end:
+       return xsd_path;
+}
+
 static
 int init_session_config_validation_ctx(
        struct session_config_validation_ctx *ctx)
 {
        int ret;
+       char *xsd_path = get_session_config_xsd_path();
 
-       ctx->parser_ctx = xmlSchemaNewParserCtxt(DEFAULT_SESSION_CONFIG_XSD_PATH);
+       if (!xsd_path) {
+               ret = -LTTNG_ERR_NOMEM;
+               goto end;
+       }
+
+       ctx->parser_ctx = xmlSchemaNewParserCtxt(xsd_path);
        if (!ctx->parser_ctx) {
                ERR("XSD parser context creation failed");
                ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
@@ -606,6 +662,7 @@ end:
                fini_session_config_validation_ctx(ctx);
        }
 
+       free(xsd_path);
        return ret;
 }
 
@@ -1222,11 +1279,12 @@ int create_session(const char *name,
 
                /* network destination */
                if (live_timer_interval && live_timer_interval != UINT64_MAX) {
-                       const char *url = output.control_uri ?
-                               output.control_uri : output.data_uri;
-
-                       /* URL has to be provided, even if we'll overwrite it after. */
-                       ret = lttng_create_session_live(name, url, live_timer_interval);
+                       /*
+                        * URLs are provided for sure since the test above make sure that
+                        * with a live timer the data and control URIs are provided. So,
+                        * NULL is passed here and will be set right after.
+                        */
+                       ret = lttng_create_session_live(name, NULL, live_timer_interval);
                } else {
                        ret = lttng_create_session(name, NULL);
                }
@@ -2266,6 +2324,11 @@ domain_init_error:
                }
        }
 end:
+       if (ret < 0) {
+               ERR("Failed to load session %s: %s", name, lttng_strerror(ret));
+               lttng_destroy_session(name);
+       }
+
        free(kernel_domain);
        free(ust_domain);
        free(jul_domain);
@@ -2341,7 +2404,14 @@ int load_session_from_path(const char *path, const char *session_name,
 
        directory = opendir(path);
        if (!directory) {
-               if (errno != ENOTDIR) {
+               switch (errno) {
+               case ENOTDIR:
+                       /* Try the file loading. */
+                       break;
+               case ENOENT:
+                       ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
+                       goto end;
+               default:
                        ret = -LTTNG_ERR_LOAD_IO_FAIL;
                        goto end;
                }
@@ -2385,7 +2455,7 @@ int load_session_from_path(const char *path, const char *session_name,
                                continue;
                        }
 
-                       if (path_len + file_name_len > PATH_MAX) {
+                       if (path_len + file_name_len >= PATH_MAX) {
                                continue;
                        }
 
@@ -2456,9 +2526,10 @@ int config_load_session(const char *path, const char *session_name,
                                goto end;
                        }
 
-                       ret = load_session_from_path(path, NULL,
+                       ret = load_session_from_path(path, session_name,
                                &validation_ctx, 0);
-                       if (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT) {
+                       if (!ret || (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT)) {
+                               /* Session found or an error occured */
                                free(path);
                                goto end;
                        }
@@ -2467,9 +2538,10 @@ int config_load_session(const char *path, const char *session_name,
                }
 
                /* Try system session configuration path */
-               ret = load_session_from_path(DEFAULT_SESSION_SYSTEM_CONFIGPATH, NULL,
-                               &validation_ctx, 0);
-               if (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT) {
+               ret = load_session_from_path(DEFAULT_SESSION_SYSTEM_CONFIGPATH,
+                       session_name, &validation_ctx, 0);
+               if (!ret || (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT)) {
+                       /* Session found or an error occured */
                        goto end;
                }
        } else {
@@ -2496,5 +2568,12 @@ int config_load_session(const char *path, const char *session_name,
        }
 end:
        fini_session_config_validation_ctx(&validation_ctx);
+       if (ret == -LTTNG_ERR_LOAD_SESSION_NOENT && !session_name && !path) {
+               /*
+                * Don't report an error if no sessions are found when called
+                * without a session_name or a search path.
+                */
+               ret = 0;
+       }
        return ret;
 }
This page took 0.027608 seconds and 4 git commands to generate.