X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng%2Fcommands%2Frotate.c;h=ec816315789b83ec5cedb9602db88dc2394fa522;hp=68bc59a33bf59037cd827a694637a530f5724c06;hb=f6e30a8ad86ac3275c23a7dc7ee223c665b10c97;hpb=dbd512ea8aa5349849aeeccda6130aa9e28cf23c diff --git a/src/bin/lttng/commands/rotate.c b/src/bin/lttng/commands/rotate.c index 68bc59a33..ec8163157 100644 --- a/src/bin/lttng/commands/rotate.c +++ b/src/bin/lttng/commands/rotate.c @@ -1,18 +1,8 @@ /* - * Copyright (C) 2017 - Julien Desfossez + * Copyright (C) 2017 Julien Desfossez * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License, version 2 only, - * as published by the Free Software Foundation. + * SPDX-License-Identifier: GPL-2.0-only * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #define _LGPL_SOURCE @@ -31,8 +21,7 @@ #include #include "../command.h" -#include -#include +#include static char *opt_session_name; static int opt_no_wait; @@ -57,161 +46,15 @@ static struct poptOption long_options[] = { {0, 0, 0, 0, 0, 0, 0} }; -static int mi_output_rotate(const char *status, const char *path, - const char *session_name) -{ - int ret; - - if (!lttng_opt_mi) { - ret = 0; - goto end; - } - - ret = mi_lttng_writer_open_element(writer, - mi_lttng_element_rotation); - if (ret) { - goto end; - } - - ret = mi_lttng_writer_write_element_string(writer, - mi_lttng_element_session_name, session_name); - if (ret) { - goto end; - } - - ret = mi_lttng_writer_write_element_string(writer, - mi_lttng_element_rotate_status, status); - if (ret) { - goto end; - } - if (path) { - ret = mi_lttng_writer_write_element_string(writer, - config_element_path, path); - if (ret) { - goto end; - } - } - /* Close rotation element */ - ret = mi_lttng_writer_close_element(writer); - if (ret) { - goto end; - } - -end: - return ret; -} - -static int output_trace_archive_location( - const struct lttng_trace_archive_location *location, - const char *session_name) -{ - int ret = 0; - enum lttng_trace_archive_location_type location_type; - enum lttng_trace_archive_location_status status; - bool printed_location = false; - - location_type = lttng_trace_archive_location_get_type(location); - - _MSG("Trace chunk archive for session %s is now readable", - session_name); - switch (location_type) { - case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL: - { - const char *absolute_path; - - status = lttng_trace_archive_location_local_get_absolute_path( - location, &absolute_path); - if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) { - ret = -1; - goto end; - } - MSG(" at %s", absolute_path); - ret = mi_output_rotate("completed", absolute_path, - session_name); - if (ret) { - goto end; - } - printed_location = true; - break; - } - case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY: - { - uint16_t control_port, data_port; - const char *host, *relative_path, *protocol_str; - enum lttng_trace_archive_location_relay_protocol_type protocol; - - /* Fetch all relay location parameters. */ - status = lttng_trace_archive_location_relay_get_protocol_type( - location, &protocol); - if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) { - ret = -1; - goto end; - } - - status = lttng_trace_archive_location_relay_get_host( - location, &host); - if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) { - ret = -1; - goto end; - } - - status = lttng_trace_archive_location_relay_get_control_port( - location, &control_port); - if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) { - ret = -1; - goto end; - } - - status = lttng_trace_archive_location_relay_get_data_port( - location, &data_port); - if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) { - ret = -1; - goto end; - } - - status = lttng_trace_archive_location_relay_get_relative_path( - location, &relative_path); - if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) { - ret = -1; - goto end; - } - - switch (protocol) { - case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP: - protocol_str = "tcp"; - break; - default: - protocol_str = "unknown"; - break; - } - - MSG(" on relay %s://%s/%s [control port %" PRIu16 ", data port %" - PRIu16 "]", protocol_str, host, - relative_path, control_port, data_port); - printed_location = true; - ret = mi_output_rotate("completed", relative_path, - session_name); - if (ret) { - goto end; - } - break; - } - default: - break; - } -end: - if (!printed_location) { - MSG(" at an unknown location"); - } - return ret; -} - static int rotate_tracing(char *session_name) { int ret; + enum cmd_error_code cmd_ret = CMD_SUCCESS; struct lttng_rotation_handle *handle = NULL; enum lttng_rotation_status rotation_status; enum lttng_rotation_state rotation_state = LTTNG_ROTATION_STATE_ONGOING; + const struct lttng_trace_archive_location *location = NULL; + bool print_location = true; DBG("Rotating the output files of session %s", session_name); @@ -220,89 +63,94 @@ static int rotate_tracing(char *session_name) switch (-ret) { case LTTNG_ERR_SESSION_NOT_STARTED: WARN("Tracing session %s not started yet", session_name); - break; + cmd_ret = CMD_WARNING; + goto end; default: ERR("%s", lttng_strerror(ret)); - break; + goto error; } + } + + if (opt_no_wait) { + rotation_state = LTTNG_ROTATION_STATE_ONGOING; + goto skip_wait; + } + + _MSG("Waiting for rotation to complete"); + ret = fflush(stdout); + if (ret) { + PERROR("fflush"); goto error; } - if (!opt_no_wait) { - _MSG("Waiting for rotation to complete"); - ret = fflush(stdout); - if (ret) { - PERROR("fflush"); + do { + 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."); goto error; } - do { - rotation_status = lttng_rotation_handle_get_state(handle, - &rotation_state); - if (rotation_status != LTTNG_ROTATION_STATUS_OK) { - ERR("Failed to query the state of the rotation"); + if (rotation_state == LTTNG_ROTATION_STATE_ONGOING) { + ret = usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US); + if (ret) { + PERROR("\nusleep"); goto error; } + _MSG("."); - /* - * Data sleep time before retrying (in usec). Don't - * sleep if the call returned value indicates - * availability. - */ - if (rotation_state == LTTNG_ROTATION_STATE_ONGOING) { - ret = usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME); - if (ret) { - PERROR("usleep"); - goto error; - } - _MSG("."); - - ret = fflush(stdout); - if (ret) { - PERROR("fflush"); - goto error; - } + ret = fflush(stdout); + if (ret) { + PERROR("\nfflush"); + goto error; } - } while (rotation_state == LTTNG_ROTATION_STATE_ONGOING); - MSG(""); - } + } + } while (rotation_state == LTTNG_ROTATION_STATE_ONGOING); + MSG(""); +skip_wait: switch (rotation_state) { case LTTNG_ROTATION_STATE_COMPLETED: - { - const struct lttng_trace_archive_location *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"); - goto error; - } - ret = output_trace_archive_location(location, session_name); - if (ret) { - goto error; + ERR("Failed to retrieve the rotation's completed chunk archive location."); + cmd_ret = CMD_ERROR; } - ret = CMD_SUCCESS; - goto end; - } + break; case LTTNG_ROTATION_STATE_EXPIRED: - MSG("Session %s rotated, but handle expired", session_name); - ret = mi_output_rotate("expired", NULL, session_name); - if (ret) { - goto error; - } - ret = CMD_SUCCESS; - goto end; + break; + case LTTNG_ROTATION_STATE_ERROR: + ERR("Failed to retrieve rotation state."); + goto error; + case LTTNG_ROTATION_STATE_ONGOING: + MSG("Rotation ongoing for session %s", session_name); + print_location = false; + break; default: - ERR("Unexpected rotation state received, aborting..."); + ERR("Unexpected rotation state encountered."); goto error; } -error: - ret = CMD_ERROR; + if (!lttng_opt_mi && print_location) { + ret = print_trace_archive_location(location, + session_name); + } else if (lttng_opt_mi) { + ret = mi_lttng_rotate(writer, session_name, rotation_state, + location); + } + + if (ret < 0) { + cmd_ret = CMD_ERROR; + } + end: lttng_rotation_handle_destroy(handle); - return ret; + return cmd_ret; +error: + cmd_ret = CMD_ERROR; + goto end; } /* @@ -312,7 +160,8 @@ end: */ int cmd_rotate(int argc, const char **argv) { - int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; + int opt, ret; + enum cmd_error_code cmd_ret = CMD_SUCCESS; int popt_ret; static poptContext pc; char *session_name = NULL; @@ -321,9 +170,8 @@ int cmd_rotate(int argc, const char **argv) pc = poptGetContext(NULL, argc, argv, long_options, 0); popt_ret = poptReadDefaultConfig(pc, 0); if (popt_ret) { - ret = CMD_ERROR; ERR("poptReadDefaultConfig"); - goto end; + goto error; } while ((opt = poptGetNextOpt(pc)) != -1) { @@ -335,7 +183,7 @@ int cmd_rotate(int argc, const char **argv) list_cmd_options(stdout, long_options); goto end; default: - ret = CMD_UNDEFINED; + cmd_ret = CMD_UNDEFINED; goto end; } } @@ -345,7 +193,7 @@ int cmd_rotate(int argc, const char **argv) if (!opt_session_name) { session_name = get_session_name(); if (!session_name) { - goto end; + goto error; } free_session_name = true; } else { @@ -356,79 +204,60 @@ int cmd_rotate(int argc, const char **argv) if (lttng_opt_mi) { writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); if (!writer) { - ret = -LTTNG_ERR_NOMEM; - goto end; + goto error; } /* Open rotate command */ ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_rotate); if (ret) { - ret = CMD_ERROR; - goto end; + goto error; } /* Open output element */ ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); if (ret) { - goto end; - } - - /* Open rotations element */ - ret = mi_lttng_writer_open_element(writer, - mi_lttng_element_rotations); - if (ret) { - goto end; + goto error; } - } - command_ret = rotate_tracing(session_name); - if (command_ret) { - success = 0; - } + cmd_ret = rotate_tracing(session_name); /* Mi closing */ if (lttng_opt_mi) { - /* Close rotations element */ + /* Close output element */ ret = mi_lttng_writer_close_element(writer); if (ret) { - goto end; - } - /* Close output element */ - ret = mi_lttng_writer_close_element(writer); - if (ret) { - goto end; + goto error; } /* Success ? */ ret = mi_lttng_writer_write_element_bool(writer, - mi_lttng_element_command_success, success); + mi_lttng_element_command_success, + cmd_ret == CMD_SUCCESS); if (ret) { - ret = CMD_ERROR; - goto end; + goto error; } /* Command element close */ ret = mi_lttng_writer_command_close(writer); if (ret) { - ret = CMD_ERROR; - goto end; + goto error; } } -end: /* Mi clean-up */ if (writer && mi_lttng_writer_destroy(writer)) { - /* Preserve original error code */ - ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; + goto error; } - - /* Overwrite ret if an error occurred with start_tracing */ - ret = command_ret ? command_ret : ret; +end: poptFreeContext(pc); if (free_session_name) { free(session_name); } - return ret; + + return cmd_ret; +error: + cmd_ret = CMD_ERROR; + goto end; }