2 * Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
20 #include <common/sessiond-comm/sessiond-comm.h>
21 #include <common/mi-lttng.h>
23 #include "../command.h"
24 #include <lttng/lttng.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
, 0, OPT_HELP
, 0, 0},
43 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
44 {"no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, 0, 0},
48 static int rotate_tracing(char *session_name
)
51 enum cmd_error_code cmd_ret
= CMD_SUCCESS
;
52 struct lttng_rotation_handle
*handle
= NULL
;
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
= NULL
;
56 bool print_location
= true;
58 DBG("Rotating the output files of session %s", session_name
);
60 ret
= lttng_rotate_session(session_name
, NULL
, &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
,
88 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
90 ERR("Failed to query the state of the rotation.");
94 if (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
95 ret
= usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
102 ret
= fflush(stdout
);
108 } while (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
);
112 switch (rotation_state
) {
113 case LTTNG_ROTATION_STATE_COMPLETED
:
114 rotation_status
= lttng_rotation_handle_get_archive_location(
116 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
117 ERR("Failed to retrieve the rotation's completed chunk archive location.");
121 case LTTNG_ROTATION_STATE_EXPIRED
:
123 case LTTNG_ROTATION_STATE_ERROR
:
124 ERR("Failed to retrieve rotation state.");
126 case LTTNG_ROTATION_STATE_ONGOING
:
127 MSG("Rotation ongoing for session %s", session_name
);
128 print_location
= false;
131 ERR("Unexpected rotation state encountered.");
135 if (!lttng_opt_mi
&& print_location
) {
136 ret
= print_trace_archive_location(location
,
138 } else if (lttng_opt_mi
) {
139 ret
= mi_lttng_rotate(writer
, session_name
, rotation_state
,
148 lttng_rotation_handle_destroy(handle
);
158 * The 'rotate <options>' first level command
160 int cmd_rotate(int argc
, const char **argv
)
163 enum cmd_error_code cmd_ret
= CMD_SUCCESS
;
165 static poptContext pc
;
166 const char *arg_session_name
= NULL
;
167 char *session_name
= NULL
;
169 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
170 popt_ret
= poptReadDefaultConfig(pc
, 0);
172 ERR("poptReadDefaultConfig");
177 while ((opt
= poptGetNextOpt(pc
)) != -1) {
182 case OPT_LIST_OPTIONS
:
183 list_cmd_options(stdout
, long_options
);
186 cmd_ret
= CMD_UNDEFINED
;
191 arg_session_name
= poptGetArg(pc
);
192 if (arg_session_name
== NULL
) {
193 session_name
= get_session_name();
195 session_name
= strdup(arg_session_name
);
196 if (session_name
== NULL
) {
197 PERROR("Failed to copy session name");
201 if (session_name
== NULL
) {
208 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
214 /* Open rotate command */
215 ret
= mi_lttng_writer_command_open(writer
,
216 mi_lttng_element_command_rotate
);
222 /* Open output element */
223 ret
= mi_lttng_writer_open_element(writer
,
224 mi_lttng_element_command_output
);
231 cmd_ret
= rotate_tracing(session_name
);
235 /* Close output element */
236 ret
= mi_lttng_writer_close_element(writer
);
242 ret
= mi_lttng_writer_write_element_bool(writer
,
243 mi_lttng_element_command_success
,
244 cmd_ret
== CMD_SUCCESS
);
250 /* Command element close */
251 ret
= mi_lttng_writer_command_close(writer
);
259 if (writer
&& mi_lttng_writer_destroy(writer
)) {