X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Fconfig%2Fconfig.c;h=b8a32e6e1f803218e0ba1f0aea10668d865a7dde;hb=b2579dc14eacae00a596d8bb885620c9fa4e263d;hp=5e100a7ab6c9c6e66848f5010acc711ffba51b8d;hpb=dcf266c050443412ec370bf86ddb3fddd2809eb9;p=lttng-tools.git diff --git a/src/common/config/config.c b/src/common/config/config.c index 5e100a7ab..b8a32e6e1 100644 --- a/src/common/config/config.c +++ b/src/common/config/config.c @@ -31,7 +31,6 @@ #include #include #include -#include #include #include #include @@ -540,11 +539,11 @@ void xml_error_handler(void *ctx, const char *format, ...) va_start(args, format); ret = vasprintf(&errMsg, format, args); + va_end(args); if (ret == -1) { ERR("String allocation failed in xml error handler"); return; } - va_end(args); fprintf(stderr, "XML Error: %s", errMsg); free(errMsg); @@ -569,13 +568,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(); + + if (!xsd_path) { + ret = -LTTNG_ERR_NOMEM; + goto end; + } - ctx->parser_ctx = xmlSchemaNewParserCtxt(DEFAULT_SESSION_CONFIG_XSD_PATH); + ctx->parser_ctx = xmlSchemaNewParserCtxt(xsd_path); if (!ctx->parser_ctx) { ERR("XSD parser context creation failed"); ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; @@ -607,6 +643,7 @@ end: fini_session_config_validation_ctx(ctx); } + free(xsd_path); return ret; } @@ -1187,9 +1224,6 @@ int create_session(const char *name, xmlNodePtr consumer_output_node; assert(name); - assert(kernel_domain); - assert(ust_domain); - assert(jul_domain); if (output_node) { consumer_output_node = xmlFirstElementChild(output_node); @@ -1226,11 +1260,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); } @@ -1238,7 +1273,7 @@ int create_session(const char *name, goto end; } - for (i = 0; i < (sizeof(domains) / sizeof(*domain)); i++) { + for (i = 0; i < (sizeof(domains) / sizeof(domains[0])); i++) { domain = domains[i]; if (!domain) { continue; @@ -2176,7 +2211,7 @@ int process_session_node(xmlNodePtr session_node, const char *session_name, if (session_name && strcmp(name, session_name)) { /* This is not the session we are looking for */ - ret = -LTTNG_ERR_LOAD_SESSION_NOT_FOUND; + ret = -LTTNG_ERR_LOAD_SESSION_NOENT; goto end; } @@ -2198,12 +2233,24 @@ int process_session_node(xmlNodePtr session_node, const char *session_name, switch (domain->type) { case LTTNG_DOMAIN_KERNEL: + if (kernel_domain) { + /* Same domain seen twice, invalid! */ + goto domain_init_error; + } kernel_domain = domain; break; case LTTNG_DOMAIN_UST: + if (ust_domain) { + /* Same domain seen twice, invalid! */ + goto domain_init_error; + } ust_domain = domain; break; case LTTNG_DOMAIN_JUL: + if (jul_domain) { + /* Same domain seen twice, invalid! */ + goto domain_init_error; + } jul_domain = domain; break; default: @@ -2258,6 +2305,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); @@ -2316,7 +2368,7 @@ int load_session_from_file(const char *path, const char *session_name, end: xmlFreeDoc(doc); if (!ret) { - ret = session_found ? 0 : -LTTNG_ERR_LOAD_SESSION_NOT_FOUND; + ret = session_found ? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT; } return ret; } @@ -2326,18 +2378,26 @@ int load_session_from_path(const char *path, const char *session_name, struct session_config_validation_ctx *validation_ctx, int override) { int ret, session_found = !session_name; - struct stat sb; DIR *directory = NULL; assert(path); assert(validation_ctx); - ret = stat(path, &sb); - if (ret) { - ret = -LTTNG_ERR_LOAD_SESSION_NOENT; - goto end; + directory = opendir(path); + if (!directory) { + 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; + } } - if (S_ISDIR(sb.st_mode)) { + if (directory) { struct dirent *entry; struct dirent *result; char *file_path = NULL; @@ -2354,13 +2414,6 @@ int load_session_from_path(const char *path, const char *session_name, goto end; } - directory = opendir(path); - if (!directory) { - ret = -LTTNG_ERR_LOAD_IO_FAIL; - free(entry); - goto end; - } - file_path = zmalloc(PATH_MAX); if (!file_path) { ret = -LTTNG_ERR_NOMEM; @@ -2373,6 +2426,7 @@ int load_session_from_path(const char *path, const char *session_name, file_path[path_len++] = '/'; } + ret = 0; /* Search for *.lttng files */ while (!readdir_r(directory, entry, &result) && result) { size_t file_name_len = strlen(result->d_name); @@ -2423,7 +2477,7 @@ end: } if (!session_found) { - ret = -LTTNG_ERR_LOAD_SESSION_NOT_FOUND; + ret = -LTTNG_ERR_LOAD_SESSION_NOENT; } return ret; @@ -2453,9 +2507,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; } @@ -2464,9 +2519,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 { @@ -2493,5 +2549,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; }