2 * Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
19 #include <common/sessiond-comm/sessiond-comm.h>
20 #include <common/mi-lttng.h>
22 #include "../command.h"
23 #include <lttng/lttng.h>
25 static char *opt_session_name
;
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 char *session_name
= NULL
;
167 bool free_session_name
= false;
169 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
170 popt_ret
= poptReadDefaultConfig(pc
, 0);
172 ERR("poptReadDefaultConfig");
176 while ((opt
= poptGetNextOpt(pc
)) != -1) {
181 case OPT_LIST_OPTIONS
:
182 list_cmd_options(stdout
, long_options
);
185 cmd_ret
= CMD_UNDEFINED
;
190 opt_session_name
= (char*) poptGetArg(pc
);
192 if (!opt_session_name
) {
193 session_name
= get_session_name();
197 free_session_name
= true;
199 session_name
= opt_session_name
;
204 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
209 /* Open rotate command */
210 ret
= mi_lttng_writer_command_open(writer
,
211 mi_lttng_element_command_rotate
);
216 /* Open output element */
217 ret
= mi_lttng_writer_open_element(writer
,
218 mi_lttng_element_command_output
);
224 cmd_ret
= (cmd_error_code
) rotate_tracing(session_name
);
228 /* Close output element */
229 ret
= mi_lttng_writer_close_element(writer
);
234 ret
= mi_lttng_writer_write_element_bool(writer
,
235 mi_lttng_element_command_success
,
236 cmd_ret
== CMD_SUCCESS
);
241 /* Command element close */
242 ret
= mi_lttng_writer_command_close(writer
);
249 if (writer
&& mi_lttng_writer_destroy(writer
)) {
254 if (free_session_name
) {