2 * Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "../command.hpp"
11 #include <common/mi-lttng.hpp>
12 #include <common/sessiond-comm/sessiond-comm.hpp>
14 #include <lttng/lttng.h>
23 #include <sys/types.h>
26 static int opt_no_wait
;
27 static struct mi_writer
*writer
;
29 #ifdef LTTNG_EMBED_HELP
30 static const char help_msg
[] =
31 #include <lttng-rotate.1.h>
40 static struct poptOption long_options
[] = {
41 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
42 { "help", 'h', POPT_ARG_NONE
, nullptr, OPT_HELP
, nullptr, nullptr },
43 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
44 { "no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, nullptr, nullptr },
45 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
48 static int rotate_tracing(char *session_name
)
51 enum cmd_error_code cmd_ret
= CMD_SUCCESS
;
52 struct lttng_rotation_handle
*handle
= nullptr;
53 enum lttng_rotation_status rotation_status
;
54 enum lttng_rotation_state rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
55 const struct lttng_trace_archive_location
*location
= nullptr;
56 bool print_location
= true;
58 DBG("Rotating the output files of session %s", session_name
);
60 ret
= lttng_rotate_session(session_name
, nullptr, &handle
);
63 case LTTNG_ERR_SESSION_NOT_STARTED
:
64 WARN("Tracing session %s not started yet", session_name
);
65 cmd_ret
= CMD_WARNING
;
68 ERR("%s", lttng_strerror(ret
));
74 rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
78 _MSG("Waiting for rotation to complete");
86 rotation_status
= lttng_rotation_handle_get_state(handle
, &rotation_state
);
87 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
89 ERR("Failed to query the state of the rotation.");
93 if (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
94 ret
= usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
101 ret
= fflush(stdout
);
107 } while (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
);
111 switch (rotation_state
) {
112 case LTTNG_ROTATION_STATE_COMPLETED
:
113 rotation_status
= lttng_rotation_handle_get_archive_location(handle
, &location
);
114 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
115 ERR("Failed to retrieve the rotation's completed chunk archive location.");
119 case LTTNG_ROTATION_STATE_EXPIRED
:
121 case LTTNG_ROTATION_STATE_ERROR
:
122 ERR("Failed to retrieve rotation state.");
124 case LTTNG_ROTATION_STATE_ONGOING
:
125 MSG("Rotation ongoing for session %s", session_name
);
126 print_location
= false;
129 ERR("Unexpected rotation state encountered.");
133 if (!lttng_opt_mi
&& print_location
) {
134 ret
= print_trace_archive_location(location
, session_name
);
135 } else if (lttng_opt_mi
) {
136 ret
= mi_lttng_rotate(writer
, session_name
, rotation_state
, location
);
144 lttng_rotation_handle_destroy(handle
);
154 * The 'rotate <options>' first level command
156 int cmd_rotate(int argc
, const char **argv
)
159 enum cmd_error_code cmd_ret
= CMD_SUCCESS
;
161 static poptContext pc
;
162 const char *arg_session_name
= nullptr;
163 char *session_name
= nullptr;
165 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
166 popt_ret
= poptReadDefaultConfig(pc
, 0);
168 ERR("poptReadDefaultConfig");
173 while ((opt
= poptGetNextOpt(pc
)) != -1) {
178 case OPT_LIST_OPTIONS
:
179 list_cmd_options(stdout
, long_options
);
182 cmd_ret
= CMD_UNDEFINED
;
187 arg_session_name
= poptGetArg(pc
);
188 if (arg_session_name
== nullptr) {
189 session_name
= get_session_name();
191 session_name
= strdup(arg_session_name
);
192 if (session_name
== nullptr) {
193 PERROR("Failed to copy session name");
197 if (session_name
== nullptr) {
204 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
210 /* Open rotate command */
211 ret
= mi_lttng_writer_command_open(writer
, mi_lttng_element_command_rotate
);
217 /* Open output element */
218 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
);
225 cmd_ret
= (cmd_error_code
) rotate_tracing(session_name
);
229 /* Close output element */
230 ret
= mi_lttng_writer_close_element(writer
);
236 ret
= mi_lttng_writer_write_element_bool(
237 writer
, mi_lttng_element_command_success
, cmd_ret
== CMD_SUCCESS
);
243 /* Command element close */
244 ret
= mi_lttng_writer_command_close(writer
);
252 if (writer
&& mi_lttng_writer_destroy(writer
)) {