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 char *opt_session_name
;
27 static int opt_no_wait
;
28 static struct mi_writer
*writer
;
30 #ifdef LTTNG_EMBED_HELP
31 static const char help_msg
[] =
32 #include <lttng-rotate.1.h>
41 static struct poptOption long_options
[] = {
42 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
43 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
44 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
45 {"no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, 0, 0},
49 static int rotate_tracing(char *session_name
)
52 enum cmd_error_code cmd_ret
= CMD_SUCCESS
;
53 struct lttng_rotation_handle
*handle
= NULL
;
54 enum lttng_rotation_status rotation_status
;
55 enum lttng_rotation_state rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
56 const struct lttng_trace_archive_location
*location
= NULL
;
57 bool print_location
= true;
59 DBG("Rotating the output files of session %s", session_name
);
61 ret
= lttng_rotate_session(session_name
, NULL
, &handle
);
64 case LTTNG_ERR_SESSION_NOT_STARTED
:
65 WARN("Tracing session %s not started yet", session_name
);
66 cmd_ret
= CMD_WARNING
;
69 ERR("%s", lttng_strerror(ret
));
75 rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
79 _MSG("Waiting for rotation to complete");
87 rotation_status
= lttng_rotation_handle_get_state(handle
,
89 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
91 ERR("Failed to query the state of the rotation.");
95 if (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
96 ret
= usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
103 ret
= fflush(stdout
);
109 } while (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
);
113 switch (rotation_state
) {
114 case LTTNG_ROTATION_STATE_COMPLETED
:
115 rotation_status
= lttng_rotation_handle_get_archive_location(
117 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
118 ERR("Failed to retrieve the rotation's completed chunk archive location.");
122 case LTTNG_ROTATION_STATE_EXPIRED
:
124 case LTTNG_ROTATION_STATE_ERROR
:
125 ERR("Failed to retrieve rotation state.");
127 case LTTNG_ROTATION_STATE_ONGOING
:
128 MSG("Rotation ongoing for session %s", session_name
);
129 print_location
= false;
132 ERR("Unexpected rotation state encountered.");
136 if (!lttng_opt_mi
&& print_location
) {
137 ret
= print_trace_archive_location(location
,
139 } else if (lttng_opt_mi
) {
140 ret
= mi_lttng_rotate(writer
, session_name
, rotation_state
,
149 lttng_rotation_handle_destroy(handle
);
159 * The 'rotate <options>' first level command
161 int cmd_rotate(int argc
, const char **argv
)
164 enum cmd_error_code cmd_ret
= CMD_SUCCESS
;
166 static poptContext pc
;
167 char *session_name
= NULL
;
168 bool free_session_name
= false;
170 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
171 popt_ret
= poptReadDefaultConfig(pc
, 0);
173 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 opt_session_name
= (char*) poptGetArg(pc
);
193 if (!opt_session_name
) {
194 session_name
= get_session_name();
198 free_session_name
= true;
200 session_name
= opt_session_name
;
205 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
210 /* Open rotate command */
211 ret
= mi_lttng_writer_command_open(writer
,
212 mi_lttng_element_command_rotate
);
217 /* Open output element */
218 ret
= mi_lttng_writer_open_element(writer
,
219 mi_lttng_element_command_output
);
225 cmd_ret
= rotate_tracing(session_name
);
229 /* Close output element */
230 ret
= mi_lttng_writer_close_element(writer
);
235 ret
= mi_lttng_writer_write_element_bool(writer
,
236 mi_lttng_element_command_success
,
237 cmd_ret
== CMD_SUCCESS
);
242 /* Command element close */
243 ret
= mi_lttng_writer_command_close(writer
);
250 if (writer
&& mi_lttng_writer_destroy(writer
)) {
255 if (free_session_name
) {