X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Fconfig%2Fsession-config.cpp;h=8f96b7f21e638a122fef392a4a5ed0e7dd3a86bf;hb=fbc3f258425648821da7264860ca32cc24db0915;hp=4d57d581a8548674b34cc1093dec81690110fba9;hpb=3afa94aeca5a0daae40fd7b6cc96b7e4c150c7d8;p=lttng-tools.git diff --git a/src/common/config/session-config.cpp b/src/common/config/session-config.cpp index 4d57d581a..8f96b7f21 100644 --- a/src/common/config/session-config.cpp +++ b/src/common/config/session-config.cpp @@ -1,7 +1,7 @@ /* * Copyright (C) 2013 Jérémie Galarneau * - * SPDX-License-Identifier: GPL-2.0-only + * SPDX-License-Identifier: LGPL-2.1-only * */ @@ -18,12 +18,12 @@ #include #include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #include #include #include @@ -34,30 +34,20 @@ #include #include -#include "session-config.h" -#include "config-internal.h" +#include "session-config.hpp" +#include "config-internal.hpp" #define CONFIG_USERSPACE_PROBE_LOOKUP_METHOD_NAME_MAX_LEN 7 -struct handler_filter_args { - const char* section; - config_entry_handler_cb handler; - void *user_data; -}; - +namespace { struct session_config_validation_ctx { xmlSchemaParserCtxtPtr parser_ctx; xmlSchemaPtr schema; xmlSchemaValidCtxtPtr schema_validation_ctx; }; +} /* namespace */ const char * const config_element_all = "all"; -LTTNG_EXPORT const char *config_str_yes = "yes"; -LTTNG_EXPORT const char *config_str_true = "true"; -LTTNG_EXPORT const char *config_str_on = "on"; -LTTNG_EXPORT const char *config_str_no = "no"; -LTTNG_EXPORT const char *config_str_false = "false"; -LTTNG_EXPORT const char *config_str_off = "off"; LTTNG_EXPORT const char *config_xml_encoding = "UTF-8"; LTTNG_EXPORT size_t config_xml_encoding_bytes_per_char = 2; /* Size of the encoding's largest character */ LTTNG_EXPORT const char *config_xml_indent_string = "\t"; @@ -245,157 +235,14 @@ enum process_event_node_phase { ENABLE = 1, }; +namespace { struct consumer_output { int enabled; char *path; char *control_uri; char *data_uri; }; - -static int config_entry_handler_filter(struct handler_filter_args *args, - const char *section, const char *name, const char *value) -{ - int ret = 0; - struct config_entry entry = { section, name, value }; - - LTTNG_ASSERT(args); - - if (!section || !name || !value) { - ret = -EIO; - goto end; - } - - if (args->section) { - if (strcmp(args->section, section)) { - goto end; - } - } - - ret = args->handler(&entry, args->user_data); -end: - return ret; -} - -int config_get_section_entries(const char *override_path, const char *section, - config_entry_handler_cb handler, void *user_data) -{ - int ret = 0; - const 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("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; - } - } - - /* Everything went well. */ - ret = 0; - -error: - return ret; -} - -int config_parse_value(const char *value) -{ - int i, ret = 0; - char *endptr, *lower_str; - size_t len; - unsigned long v; - - len = strlen(value); - if (!len) { - ret = -1; - goto end; - } - - v = strtoul(value, &endptr, 10); - if (endptr != value) { - ret = v; - goto end; - } - - lower_str = (char *) zmalloc(len + 1); - if (!lower_str) { - PERROR("zmalloc"); - ret = -errno; - goto end; - } - - for (i = 0; i < len; i++) { - lower_str[i] = tolower(value[i]); - } - - if (!strcmp(lower_str, config_str_yes) || - !strcmp(lower_str, config_str_true) || - !strcmp(lower_str, config_str_on)) { - ret = 1; - } else if (!strcmp(lower_str, config_str_no) || - !strcmp(lower_str, config_str_false) || - !strcmp(lower_str, config_str_off)) { - ret = 0; - } else { - ret = -1; - } - - free(lower_str); -end: - return ret; -} +} /* namespace */ /* * Returns a xmlChar string which must be released using xmlFree(). @@ -444,7 +291,7 @@ struct config_writer *config_writer_create(int fd_output, int indent) struct config_writer *writer; xmlOutputBufferPtr buffer; - writer = (config_writer *) zmalloc(sizeof(struct config_writer)); + writer = zmalloc(); if (!writer) { PERROR("zmalloc config_writer_create"); goto end; @@ -685,8 +532,8 @@ end: return ret >= 0 ? 0 : ret; } -static -void xml_error_handler(void *ctx, const char *format, ...) +static ATTR_FORMAT_PRINTF(2, 3) +void xml_error_handler(void *ctx __attribute__((unused)), const char *format, ...) { char *errMsg; va_list args; @@ -738,7 +585,7 @@ char *get_session_config_xsd_path(void) base_path_len = strlen(base_path); max_path_len = base_path_len + sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME) + 1; - xsd_path = (char *) zmalloc(max_path_len); + xsd_path = zmalloc(max_path_len); if (!xsd_path) { goto end; } @@ -1302,65 +1149,46 @@ end: return ret; } -static -int create_session_net_output(const char *name, const char *control_uri, - const char *data_uri) -{ - int ret; - struct lttng_handle *handle; - const char *uri = NULL; - - LTTNG_ASSERT(name); - - handle = lttng_create_handle(name, NULL); - if (!handle) { - ret = -LTTNG_ERR_NOMEM; - goto end; - } - - if (!control_uri || !data_uri) { - uri = control_uri ? control_uri : data_uri; - control_uri = uri; - data_uri = uri; - } - - ret = lttng_set_consumer_url(handle, control_uri, data_uri); - lttng_destroy_handle(handle); -end: - return ret; -} - static int create_snapshot_session(const char *session_name, xmlNodePtr output_node, const struct config_load_session_override_attr *overrides) { int ret; + enum lttng_error_code ret_code; xmlNodePtr node = NULL; xmlNodePtr snapshot_output_list_node; xmlNodePtr snapshot_output_node; + struct lttng_session_descriptor *session_descriptor = nullptr; LTTNG_ASSERT(session_name); + LTTNG_ASSERT(output_node); - ret = lttng_create_session_snapshot(session_name, NULL); - if (ret) { + /* + * Use a descriptor without output since consumer output size is not + * exposed by the session descriptor api. + */ + session_descriptor = lttng_session_descriptor_snapshot_create(session_name); + if (session_descriptor == nullptr) { + ret = -LTTNG_ERR_NOMEM; goto end; } - if (!output_node) { + ret_code = lttng_create_session_ext(session_descriptor); + if (ret_code != LTTNG_OK) { + ret = -ret_code; goto end; } snapshot_output_list_node = xmlFirstElementChild(output_node); /* Parse and create snapshot outputs */ - for (snapshot_output_node = xmlFirstElementChild(snapshot_output_list_node); snapshot_output_node; snapshot_output_node = xmlNextElementSibling(snapshot_output_node)) { char *name = NULL; uint64_t max_size = UINT64_MAX; - struct consumer_output output = { 0 }; + struct consumer_output output = {}; struct lttng_snapshot_output *snapshot_output = NULL; const char *control_uri = NULL; const char *data_uri = NULL; @@ -1476,6 +1304,7 @@ error_snapshot_output: } } end: + lttng_session_descriptor_destroy(session_descriptor); return ret; } @@ -1485,12 +1314,14 @@ int create_session(const char *name, uint64_t live_timer_interval, const struct config_load_session_override_attr *overrides) { - int ret; - struct consumer_output output = { 0 }; + int ret = 0; + enum lttng_error_code ret_code; + struct consumer_output output = {}; xmlNodePtr consumer_output_node; const char *control_uri = NULL; const char *data_uri = NULL; const char *path = NULL; + struct lttng_session_descriptor *session_descriptor = nullptr; LTTNG_ASSERT(name); @@ -1540,7 +1371,6 @@ int create_session(const char *name, } } - if (live_timer_interval != UINT64_MAX && !control_uri && !data_uri) { ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; goto end; @@ -1554,30 +1384,36 @@ int create_session(const char *name, * 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); + session_descriptor = lttng_session_descriptor_live_network_create( + name, control_uri, data_uri, live_timer_interval); } else { - ret = lttng_create_session(name, NULL); - } - if (ret) { - goto end; - } - - ret = create_session_net_output(name, control_uri, data_uri); - if (ret) { - goto end; + session_descriptor = lttng_session_descriptor_network_create( + name, control_uri, data_uri); } + } else if (path != nullptr) { + session_descriptor = lttng_session_descriptor_local_create(name, path); } else { - /* either local output or no output */ - ret = lttng_create_session(name, path); - if (ret) { - goto end; - } + /* No output */ + session_descriptor = lttng_session_descriptor_create(name); + } + + if (session_descriptor == nullptr) { + ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; + goto end; } + + ret_code = lttng_create_session_ext(session_descriptor); + if (ret_code != LTTNG_OK) { + ret = -ret_code; + goto end; + } + end: free(output.path); free(output.control_uri); free(output.data_uri); + lttng_session_descriptor_destroy(session_descriptor); return ret; } @@ -2013,7 +1849,7 @@ int process_event_node(xmlNodePtr event_node, struct lttng_handle *handle, continue; } - exclusions = (char **) zmalloc(exclusion_count * sizeof(char *)); + exclusions = calloc(exclusion_count); if (!exclusions) { exclusion_count = 0; ret = -LTTNG_ERR_NOMEM; @@ -3556,9 +3392,8 @@ int process_session_node(xmlNodePtr session_node, const char *session_name, /* Init domains to create the session handles */ for (node = xmlFirstElementChild(domains_node); node; node = xmlNextElementSibling(node)) { - struct lttng_domain *domain; + lttng_domain *domain = zmalloc(); - domain = (lttng_domain *) zmalloc(sizeof(*domain)); if (!domain) { ret = -LTTNG_ERR_NOMEM; goto error; @@ -4016,7 +3851,7 @@ int config_load_session(const char *path, const char *session_name, int ret; bool session_loaded = false; const char *path_ptr = NULL; - struct session_config_validation_ctx validation_ctx = { 0 }; + struct session_config_validation_ctx validation_ctx = {}; ret = init_session_config_validation_ctx(&validation_ctx); if (ret) {