X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fbin%2Flttng%2Fcommands%2Frotate.cpp;h=9c4347e43191467e3de3b4ecd28b7576e1749dd9;hb=cd9adb8b829564212158943a0d279bb35322ab30;hp=339e39598891505d3b62eafffc123be901b445dd;hpb=c9e313bc594f40a86eed237dce222c0fc99c957f;p=lttng-tools.git diff --git a/src/bin/lttng/commands/rotate.cpp b/src/bin/lttng/commands/rotate.cpp index 339e39598..9c4347e43 100644 --- a/src/bin/lttng/commands/rotate.cpp +++ b/src/bin/lttng/commands/rotate.cpp @@ -6,6 +6,15 @@ */ #define _LGPL_SOURCE +#include "../command.hpp" + +#include +#include + +#include + +#include +#include #include #include #include @@ -13,23 +22,14 @@ #include #include #include -#include -#include -#include -#include - -#include "../command.hpp" -#include - -static char *opt_session_name; static int opt_no_wait; static struct mi_writer *writer; #ifdef LTTNG_EMBED_HELP static const char help_msg[] = #include -; + ; #endif enum { @@ -39,25 +39,25 @@ enum { static struct poptOption long_options[] = { /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ - {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, - {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, - {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0}, - {0, 0, 0, 0, 0, 0, 0} + { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr }, + { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr }, + { "no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, nullptr, nullptr }, + { nullptr, 0, 0, nullptr, 0, nullptr, nullptr } }; static int rotate_tracing(char *session_name) { int ret; enum cmd_error_code cmd_ret = CMD_SUCCESS; - struct lttng_rotation_handle *handle = NULL; + struct lttng_rotation_handle *handle = nullptr; enum lttng_rotation_status rotation_status; enum lttng_rotation_state rotation_state = LTTNG_ROTATION_STATE_ONGOING; - const struct lttng_trace_archive_location *location = NULL; + const struct lttng_trace_archive_location *location = nullptr; bool print_location = true; DBG("Rotating the output files of session %s", session_name); - ret = lttng_rotate_session(session_name, NULL, &handle); + ret = lttng_rotate_session(session_name, nullptr, &handle); if (ret < 0) { switch (-ret) { case LTTNG_ERR_SESSION_NOT_STARTED: @@ -83,8 +83,7 @@ static int rotate_tracing(char *session_name) } do { - rotation_status = lttng_rotation_handle_get_state(handle, - &rotation_state); + rotation_status = lttng_rotation_handle_get_state(handle, &rotation_state); if (rotation_status != LTTNG_ROTATION_STATUS_OK) { MSG(""); ERR("Failed to query the state of the rotation."); @@ -111,8 +110,7 @@ static int rotate_tracing(char *session_name) skip_wait: switch (rotation_state) { case LTTNG_ROTATION_STATE_COMPLETED: - rotation_status = lttng_rotation_handle_get_archive_location( - handle, &location); + rotation_status = lttng_rotation_handle_get_archive_location(handle, &location); if (rotation_status != LTTNG_ROTATION_STATUS_OK) { ERR("Failed to retrieve the rotation's completed chunk archive location."); cmd_ret = CMD_ERROR; @@ -133,11 +131,9 @@ skip_wait: } if (!lttng_opt_mi && print_location) { - ret = print_trace_archive_location(location, - session_name); + ret = print_trace_archive_location(location, session_name); } else if (lttng_opt_mi) { - ret = mi_lttng_rotate(writer, session_name, rotation_state, - location); + ret = mi_lttng_rotate(writer, session_name, rotation_state, location); } if (ret < 0) { @@ -163,14 +159,15 @@ int cmd_rotate(int argc, const char **argv) enum cmd_error_code cmd_ret = CMD_SUCCESS; int popt_ret; static poptContext pc; - char *session_name = NULL; - bool free_session_name = false; + const char *arg_session_name = nullptr; + char *session_name = nullptr; - pc = poptGetContext(NULL, argc, argv, long_options, 0); + pc = poptGetContext(nullptr, argc, argv, long_options, 0); popt_ret = poptReadDefaultConfig(pc, 0); if (popt_ret) { ERR("poptReadDefaultConfig"); - goto error; + cmd_ret = CMD_ERROR; + goto end; } while ((opt = poptGetNextOpt(pc)) != -1) { @@ -187,37 +184,41 @@ int cmd_rotate(int argc, const char **argv) } } - opt_session_name = (char*) poptGetArg(pc); - - if (!opt_session_name) { + arg_session_name = poptGetArg(pc); + if (arg_session_name == nullptr) { session_name = get_session_name(); - if (!session_name) { - goto error; - } - free_session_name = true; } else { - session_name = opt_session_name; + session_name = strdup(arg_session_name); + if (session_name == nullptr) { + PERROR("Failed to copy session name"); + } + } + + if (session_name == nullptr) { + cmd_ret = CMD_ERROR; + goto end; } /* Mi check */ if (lttng_opt_mi) { writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); if (!writer) { - goto error; + cmd_ret = CMD_ERROR; + goto end; } /* Open rotate command */ - ret = mi_lttng_writer_command_open(writer, - mi_lttng_element_command_rotate); + ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_rotate); if (ret) { - goto error; + cmd_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) { - goto error; + cmd_ret = CMD_ERROR; + goto end; } } @@ -228,35 +229,33 @@ int cmd_rotate(int argc, const char **argv) /* Close output element */ ret = mi_lttng_writer_close_element(writer); if (ret) { - goto error; + cmd_ret = CMD_ERROR; + goto end; } /* Success ? */ - ret = mi_lttng_writer_write_element_bool(writer, - mi_lttng_element_command_success, - cmd_ret == CMD_SUCCESS); + ret = mi_lttng_writer_write_element_bool( + writer, mi_lttng_element_command_success, cmd_ret == CMD_SUCCESS); if (ret) { - goto error; + cmd_ret = CMD_ERROR; + goto end; } /* Command element close */ ret = mi_lttng_writer_command_close(writer); if (ret) { - goto error; + cmd_ret = CMD_ERROR; + goto end; } } /* Mi clean-up */ if (writer && mi_lttng_writer_destroy(writer)) { - goto error; + cmd_ret = CMD_ERROR; + goto end; } end: + free(session_name); poptFreeContext(pc); - if (free_session_name) { - free(session_name); - } return cmd_ret; -error: - cmd_ret = CMD_ERROR; - goto end; }