X-Git-Url: https://git.liburcu.org/?a=blobdiff_plain;f=src%2Fbin%2Flttng%2Fcommands%2Fcreate.cpp;h=dce13866f6dea7d2948149de093e49af65935bde;hb=cd9adb8b829564212158943a0d279bb35322ab30;hp=2385fd70e8216a8fd6ef7bc0b4817305e3f9a3b7;hpb=4971b7f0243bd3a7a661bcf1cfe95f0f8014b59b;p=lttng-tools.git diff --git a/src/bin/lttng/commands/create.cpp b/src/bin/lttng/commands/create.cpp index 2385fd70e..dce13866f 100644 --- a/src/bin/lttng/commands/create.cpp +++ b/src/bin/lttng/commands/create.cpp @@ -7,32 +7,31 @@ */ #define _LGPL_SOURCE +#include "../command.hpp" +#include "../utils.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include + #include #include +#include #include #include #include #include #include -#include -#include -#include #include - -#include - -#include "../command.h" -#include "../utils.h" - -#include -#include -#include -#include -#include -#include +#include static char *opt_output_path; -static char *opt_session_name; static char *opt_url; static char *opt_ctrl_url; static char *opt_data_url; @@ -45,7 +44,7 @@ static uint32_t opt_live_timer; #ifdef LTTNG_EMBED_HELP static const char help_msg[] = #include -; + ; #endif enum { @@ -64,18 +63,24 @@ enum output_type { static struct mi_writer *writer; static struct poptOption long_options[] = { /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ - {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL}, - {"output", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL}, - {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, - {"set-url", 'U', POPT_ARG_STRING, &opt_url, 0, 0, 0}, - {"ctrl-url", 'C', POPT_ARG_STRING, &opt_ctrl_url, 0, 0, 0}, - {"data-url", 'D', POPT_ARG_STRING, &opt_data_url, 0, 0, 0}, - {"no-output", 0, POPT_ARG_VAL, &opt_no_output, 1, 0, 0}, - {"no-consumer", 0, POPT_ARG_VAL, &opt_no_consumer, 1, 0, 0}, - {"snapshot", 0, POPT_ARG_VAL, &opt_snapshot, 1, 0, 0}, - {"live", 0, POPT_ARG_INT | POPT_ARGFLAG_OPTIONAL, 0, OPT_LIVE_TIMER, 0, 0}, - {"shm-path", 0, POPT_ARG_STRING, &opt_shm_path, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0} + { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr }, + { "output", 'o', POPT_ARG_STRING, &opt_output_path, 0, nullptr, nullptr }, + { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr }, + { "set-url", 'U', POPT_ARG_STRING, &opt_url, 0, nullptr, nullptr }, + { "ctrl-url", 'C', POPT_ARG_STRING, &opt_ctrl_url, 0, nullptr, nullptr }, + { "data-url", 'D', POPT_ARG_STRING, &opt_data_url, 0, nullptr, nullptr }, + { "no-output", 0, POPT_ARG_VAL, &opt_no_output, 1, nullptr, nullptr }, + { "no-consumer", 0, POPT_ARG_VAL, &opt_no_consumer, 1, nullptr, nullptr }, + { "snapshot", 0, POPT_ARG_VAL, &opt_snapshot, 1, nullptr, nullptr }, + { "live", + 0, + POPT_ARG_INT | POPT_ARGFLAG_OPTIONAL, + nullptr, + OPT_LIVE_TIMER, + nullptr, + nullptr }, + { "shm-path", 0, POPT_ARG_STRING, &opt_shm_path, 0, nullptr, nullptr }, + { nullptr, 0, 0, nullptr, 0, nullptr, nullptr } }; /* @@ -129,14 +134,13 @@ end: return ret; } -static -struct lttng_session_descriptor *create_session_descriptor(void) +static struct lttng_session_descriptor *create_session_descriptor(const char *session_name) { ssize_t uri_count; enum output_type output_type; - struct lttng_uri *uris = NULL; - struct lttng_session_descriptor *descriptor = NULL; - const char *uri_str1 = NULL, *uri_str2 = NULL; + struct lttng_uri *uris = nullptr; + struct lttng_session_descriptor *descriptor = nullptr; + const char *uri_str1 = nullptr, *uri_str2 = nullptr; char local_output_path[LTTNG_PATH_MAX] = {}; if (opt_no_output) { @@ -151,12 +155,12 @@ struct lttng_session_descriptor *create_session_descriptor(void) ERR("Failed to expand output path."); goto end; } - ret = lttng_strncpy(local_output_path, expanded_output_path, - sizeof(local_output_path)); + ret = lttng_strncpy( + local_output_path, expanded_output_path, sizeof(local_output_path)); free(expanded_output_path); if (ret) { ERR("Output path exceeds the maximal supported length (%zu bytes)", - sizeof(local_output_path)); + sizeof(local_output_path)); goto end; } } else if (opt_url || opt_ctrl_url) { @@ -178,11 +182,11 @@ struct lttng_session_descriptor *create_session_descriptor(void) ERR("Unrecognized URL format."); goto end; } - ret = lttng_strncpy(local_output_path, uris[0].dst.path, - sizeof(local_output_path)); + ret = lttng_strncpy( + local_output_path, uris[0].dst.path, sizeof(local_output_path)); if (ret) { ERR("Output path exceeds the maximal supported length (%zu bytes)", - sizeof(local_output_path)); + sizeof(local_output_path)); } break; case 2: @@ -202,48 +206,42 @@ struct lttng_session_descriptor *create_session_descriptor(void) case OUTPUT_UNSPECIFIED: case OUTPUT_LOCAL: descriptor = lttng_session_descriptor_snapshot_local_create( - opt_session_name, - output_type == OUTPUT_LOCAL ? - local_output_path : NULL); + session_name, + output_type == OUTPUT_LOCAL ? local_output_path : nullptr); break; case OUTPUT_NONE: - descriptor = lttng_session_descriptor_snapshot_create( - opt_session_name); + descriptor = lttng_session_descriptor_snapshot_create(session_name); break; case OUTPUT_NETWORK: descriptor = lttng_session_descriptor_snapshot_network_create( - opt_session_name, uri_str1, uri_str2); + session_name, uri_str1, uri_str2); break; default: abort(); } } else if (opt_live_timer) { /* Live session. */ - if (output_type != OUTPUT_UNSPECIFIED && - output_type != OUTPUT_NETWORK) { + if (output_type != OUTPUT_UNSPECIFIED && output_type != OUTPUT_NETWORK) { ERR("Unsupported output type specified for live session."); goto end; } descriptor = lttng_session_descriptor_live_network_create( - opt_session_name, uri_str1, uri_str2, - opt_live_timer); + session_name, uri_str1, uri_str2, opt_live_timer); } else { /* Regular session. */ switch (output_type) { case OUTPUT_UNSPECIFIED: case OUTPUT_LOCAL: descriptor = lttng_session_descriptor_local_create( - opt_session_name, - output_type == OUTPUT_LOCAL ? - local_output_path : NULL); + session_name, + output_type == OUTPUT_LOCAL ? local_output_path : nullptr); break; case OUTPUT_NONE: - descriptor = lttng_session_descriptor_create( - opt_session_name); + descriptor = lttng_session_descriptor_create(session_name); break; case OUTPUT_NETWORK: descriptor = lttng_session_descriptor_network_create( - opt_session_name, uri_str1, uri_str2); + session_name, uri_str1, uri_str2); break; default: abort(); @@ -256,16 +254,15 @@ struct lttng_session_descriptor *create_session_descriptor(void) * Auto-launch the relay daemon when a live session * is created using default URLs. */ - if (!opt_url && !opt_ctrl_url && !opt_data_url && - opt_live_timer && !check_relayd()) { + if (!opt_url && !opt_ctrl_url && !opt_data_url && opt_live_timer && + !check_relayd()) { int ret; - const char *pathname = opt_relayd_path ? : - INSTALL_BIN_PATH "/lttng-relayd"; + const char *pathname = opt_relayd_path ?: INSTALL_BIN_PATH "/lttng-relayd"; ret = spawn_relayd(pathname, 0); if (ret < 0) { lttng_session_descriptor_destroy(descriptor); - descriptor = NULL; + descriptor = nullptr; } } } @@ -280,22 +277,21 @@ end: * * Returns one of the CMD_* result constants. */ -static int create_session(void) +static int create_session(const char *session_name) { int ret, i; char shm_path[LTTNG_PATH_MAX] = {}; - struct lttng_session_descriptor *session_descriptor = NULL; + struct lttng_session_descriptor *session_descriptor = nullptr; enum lttng_session_descriptor_status descriptor_status; enum lttng_error_code ret_code; - struct lttng_session *sessions = NULL; - const struct lttng_session *created_session = NULL; + struct lttng_session *sessions = nullptr; + const struct lttng_session *created_session = nullptr; const char *created_session_name; /* Validate options. */ - if (opt_session_name) { - if (strlen(opt_session_name) > NAME_MAX) { - ERR("Session name too long. Length must be lower or equal to %d", - NAME_MAX); + if (session_name) { + if (strlen(session_name) > NAME_MAX) { + ERR("Session name too long. Length must be lower or equal to %d", NAME_MAX); ret = CMD_ERROR; goto error; } @@ -304,13 +300,14 @@ static int create_session(void) * Both are reserved for the default session name. See bug #449 to * understand why we need to check both here. */ - if ((strncmp(opt_session_name, DEFAULT_SESSION_NAME "-", - strlen(DEFAULT_SESSION_NAME) + 1) == 0) || - (strncmp(opt_session_name, DEFAULT_SESSION_NAME, - strlen(DEFAULT_SESSION_NAME)) == 0 && - strlen(opt_session_name) == strlen(DEFAULT_SESSION_NAME))) { + if ((strncmp(session_name, + DEFAULT_SESSION_NAME "-", + strlen(DEFAULT_SESSION_NAME) + 1) == 0) || + (strncmp(session_name, DEFAULT_SESSION_NAME, strlen(DEFAULT_SESSION_NAME)) == + 0 && + strlen(session_name) == strlen(DEFAULT_SESSION_NAME))) { ERR("%s is a reserved keyword for default session(s)", - DEFAULT_SESSION_NAME); + DEFAULT_SESSION_NAME); ret = CMD_ERROR; goto error; } @@ -328,7 +325,7 @@ static int create_session(void) goto error; } - session_descriptor = create_session_descriptor(); + session_descriptor = create_session_descriptor(session_name); if (!session_descriptor) { ret = CMD_ERROR; goto error; @@ -340,8 +337,8 @@ static int create_session(void) goto error; } - descriptor_status = lttng_session_descriptor_get_session_name( - session_descriptor, &created_session_name); + descriptor_status = lttng_session_descriptor_get_session_name(session_descriptor, + &created_session_name); if (descriptor_status != LTTNG_SESSION_DESCRIPTOR_STATUS_OK) { ERR("Failed to obtain created session name"); ret = CMD_ERROR; @@ -350,8 +347,7 @@ static int create_session(void) ret = lttng_list_sessions(&sessions); if (ret < 0) { - ERR("Failed to fetch properties of created session: %s", - lttng_strerror(ret)); + ERR("Failed to fetch properties of created session: %s", lttng_strerror(ret)); ret = CMD_ERROR; goto error; } @@ -374,15 +370,13 @@ static int create_session(void) * An auto-generated session name already includes the creation * timestamp. */ - if (opt_session_name) { + if (session_name) { uint64_t creation_time; struct tm *timeinfo; time_t creation_time_t; size_t strftime_ret; - ret_code = lttng_session_get_creation_time( - created_session, - &creation_time); + ret_code = lttng_session_get_creation_time(created_session, &creation_time); if (ret_code != LTTNG_OK) { ERR("%s", lttng_strerror(-ret_code)); ret = CMD_ERROR; @@ -396,8 +390,9 @@ static int create_session(void) goto error; } strftime_ret = strftime(datetime_suffix, - sizeof(datetime_suffix), - "-%Y%m%d-%H%M%S", timeinfo); + sizeof(datetime_suffix), + "-%Y%m%d-%H%M%S", + timeinfo); if (strftime_ret == 0) { ERR("Failed to format session creation time."); ret = CMD_ERROR; @@ -405,16 +400,18 @@ static int create_session(void) } } - ret = snprintf(shm_path, sizeof(shm_path), - "%s/%s%s", opt_shm_path, created_session_name, - datetime_suffix); + ret = snprintf(shm_path, + sizeof(shm_path), + "%s/%s%s", + opt_shm_path, + created_session_name, + datetime_suffix); if (ret < 0 || ret >= sizeof(shm_path)) { ERR("Failed to format the shared memory path."); ret = CMD_ERROR; goto error; } - ret = lttng_set_session_shm_path(created_session_name, - shm_path); + ret = lttng_set_session_shm_path(created_session_name, shm_path); if (ret < 0) { lttng_destroy_session(created_session_name); ret = CMD_ERROR; @@ -434,8 +431,7 @@ static int create_session(void) MSG("Traces will be output to %s", created_session->path); if (opt_live_timer) { - MSG("Live timer interval set to %u %s", opt_live_timer, - USEC_UNIT); + MSG("Live timer interval set to %u %s", opt_live_timer, USEC_UNIT); } } else if (opt_snapshot) { struct lttng_snapshot_output_list *list; @@ -450,12 +446,10 @@ static int create_session(void) } while ((iter = lttng_snapshot_output_list_get_next(list))) { - const char *url = NULL; + const char *url = nullptr; - url = lttng_snapshot_output_get_ctrl_url( - iter); - ret = lttng_strncpy(snapshot_url, url, - sizeof(snapshot_url)); + url = lttng_snapshot_output_get_ctrl_url(iter); + ret = lttng_strncpy(snapshot_url, url, sizeof(snapshot_url)); if (ret) { snapshot_url[0] = '\0'; ERR("Failed to retrieve snapshot output destination"); @@ -465,8 +459,7 @@ static int create_session(void) lttng_snapshot_output_list_destroy(list); if (*snapshot_url) { - MSG("Default snapshot output set to %s", - snapshot_url); + MSG("Default snapshot output set to %s", snapshot_url); } MSG("Every channel enabled for this session will be set to mmap output and default to overwrite mode."); } @@ -513,15 +506,14 @@ static int spawn_sessiond(const char *pathname) /* * Spawn session daemon in daemon mode. */ - execlp(pathname, "lttng-sessiond", - "--daemonize", NULL); + execlp(pathname, "lttng-sessiond", "--daemonize", NULL); /* execlp only returns if error happened */ if (errno == ENOENT) { ERR("No session daemon found. Use --sessiond-path."); } else { PERROR("execlp"); } - kill(getppid(), SIGTERM); /* wake parent */ + kill(getppid(), SIGTERM); /* wake parent */ exit(EXIT_FAILURE); } else if (pid > 0) { /* @@ -542,19 +534,18 @@ static int spawn_sessiond(const char *pathname) } if (WIFSIGNALED(status)) { - ERR("Session daemon was killed by signal %d", - WTERMSIG(status)); + ERR("Session daemon was killed by signal %d", WTERMSIG(status)); ret = -1; - goto end; + goto end; } else if (WIFEXITED(status)) { DBG("Session daemon terminated normally (exit status: %d)", - WEXITSTATUS(status)); + WEXITSTATUS(status)); if (WEXITSTATUS(status) != 0) { ERR("Session daemon terminated with an error (exit status: %d)", - WEXITSTATUS(status)); + WEXITSTATUS(status)); ret = -1; - goto end; + goto end; } break; } @@ -578,10 +569,10 @@ end: * the liblttngctl API for the check. If not, try to * spawn a daemon. */ -static int launch_sessiond(void) +static int launch_sessiond() { int ret; - const char *pathname = NULL; + const char *pathname = nullptr; ret = lttng_session_daemon_alive(); if (ret) { @@ -594,19 +585,19 @@ static int launch_sessiond(void) pathname = opt_sessiond_path; /* Try LTTNG_SESSIOND_PATH env variable */ - if (pathname == NULL) { + if (pathname == nullptr) { pathname = getenv(DEFAULT_SESSIOND_PATH_ENV); } /* Try with configured path */ - if (pathname == NULL) { + if (pathname == nullptr) { if (CONFIG_SESSIOND_BIN[0] != '\0') { pathname = CONFIG_SESSIOND_BIN; } } /* Try the default path */ - if (pathname == NULL) { + if (pathname == nullptr) { pathname = INSTALL_BIN_PATH "/lttng-sessiond"; } @@ -622,14 +613,12 @@ static int launch_sessiond(void) ret = spawn_sessiond(pathname); end: if (ret) { - ERR("Problem occurred while launching session daemon (%s)", - pathname); + ERR("Problem occurred while launching session daemon (%s)", pathname); } return ret; } -static -int validate_url_option_combination(void) +static int validate_url_option_combination() { int ret = 0; int used_count = 0; @@ -653,11 +642,12 @@ int validate_url_option_combination(void) int cmd_create(int argc, const char **argv) { int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; - char *opt_arg = NULL; - const char *leftover = NULL; + char *opt_arg = nullptr; + const char *arg_session_name = nullptr; + const char *leftover = nullptr; static poptContext pc; - pc = poptGetContext(NULL, argc, argv, long_options, 0); + pc = poptGetContext(nullptr, argc, argv, long_options, 0); poptReadDefaultConfig(pc, 0); while ((opt = poptGetNextOpt(pc)) != -1) { @@ -673,12 +663,17 @@ int cmd_create(int argc, const char **argv) uint64_t v; errno = 0; + if (opt_arg) { + free(opt_arg); + opt_arg = nullptr; + } + opt_arg = poptGetOptArg(pc); if (!opt_arg) { /* Set up default values. */ opt_live_timer = (uint32_t) DEFAULT_LTTNG_LIVE_TIMER; DBG("Session live timer interval set to default value %d", - opt_live_timer); + opt_live_timer); break; } @@ -740,22 +735,22 @@ int cmd_create(int argc, const char **argv) } /* Open command element */ - ret = mi_lttng_writer_command_open(writer, - mi_lttng_element_command_create); + ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_create); if (ret) { ret = CMD_ERROR; goto end; } /* Open output element */ - ret = mi_lttng_writer_open_element(writer, - mi_lttng_element_command_output); + ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); if (ret) { ret = CMD_ERROR; goto end; } } - opt_session_name = (char*) poptGetArg(pc); + + /* Get the optional session name argument. */ + arg_session_name = poptGetArg(pc); leftover = poptGetArg(pc); if (leftover) { @@ -764,7 +759,7 @@ int cmd_create(int argc, const char **argv) goto end; } - command_ret = create_session(); + command_ret = create_session(arg_session_name); if (command_ret) { success = 0; } @@ -778,8 +773,8 @@ int cmd_create(int argc, const char **argv) } /* Success ? */ - ret = mi_lttng_writer_write_element_bool(writer, - mi_lttng_element_command_success, success); + ret = mi_lttng_writer_write_element_bool( + writer, mi_lttng_element_command_success, success); if (ret) { ret = CMD_ERROR; goto end; @@ -803,6 +798,7 @@ end: /* Overwrite ret if an error occurred in create_session() */ ret = command_ret ? command_ret : ret; + free(opt_arg); poptFreeContext(pc); return ret; }