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.hpp>
20 #include <common/mi-lttng.hpp>
22 #include "../command.hpp"
23 #include <lttng/lttng.h>
25 static int opt_no_wait
;
26 static struct mi_writer
*writer
;
28 #ifdef LTTNG_EMBED_HELP
29 static const char help_msg
[] =
30 #include <lttng-rotate.1.h>
39 static struct poptOption long_options
[] = {
40 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
41 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
42 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
43 {"no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, 0, 0},
47 static int rotate_tracing(char *session_name
)
50 enum cmd_error_code cmd_ret
= CMD_SUCCESS
;
51 struct lttng_rotation_handle
*handle
= NULL
;
52 enum lttng_rotation_status rotation_status
;
53 enum lttng_rotation_state rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
54 const struct lttng_trace_archive_location
*location
= NULL
;
55 bool print_location
= true;
57 DBG("Rotating the output files of session %s", session_name
);
59 ret
= lttng_rotate_session(session_name
, NULL
, &handle
);
62 case LTTNG_ERR_SESSION_NOT_STARTED
:
63 WARN("Tracing session %s not started yet", session_name
);
64 cmd_ret
= CMD_WARNING
;
67 ERR("%s", lttng_strerror(ret
));
73 rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
77 _MSG("Waiting for rotation to complete");
85 rotation_status
= lttng_rotation_handle_get_state(handle
,
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(
115 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
116 ERR("Failed to retrieve the rotation's completed chunk archive location.");
120 case LTTNG_ROTATION_STATE_EXPIRED
:
122 case LTTNG_ROTATION_STATE_ERROR
:
123 ERR("Failed to retrieve rotation state.");
125 case LTTNG_ROTATION_STATE_ONGOING
:
126 MSG("Rotation ongoing for session %s", session_name
);
127 print_location
= false;
130 ERR("Unexpected rotation state encountered.");
134 if (!lttng_opt_mi
&& print_location
) {
135 ret
= print_trace_archive_location(location
,
137 } else if (lttng_opt_mi
) {
138 ret
= mi_lttng_rotate(writer
, session_name
, rotation_state
,
147 lttng_rotation_handle_destroy(handle
);
157 * The 'rotate <options>' first level command
159 int cmd_rotate(int argc
, const char **argv
)
162 enum cmd_error_code cmd_ret
= CMD_SUCCESS
;
164 static poptContext pc
;
165 const char *arg_session_name
= NULL
;
166 char *session_name
= NULL
;
168 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
169 popt_ret
= poptReadDefaultConfig(pc
, 0);
171 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 arg_session_name
= poptGetArg(pc
);
191 if (arg_session_name
== NULL
) {
192 session_name
= get_session_name();
194 session_name
= strdup(arg_session_name
);
195 if (session_name
== NULL
) {
196 PERROR("Failed to copy session name");
200 if (session_name
== NULL
) {
207 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
213 /* Open rotate command */
214 ret
= mi_lttng_writer_command_open(writer
,
215 mi_lttng_element_command_rotate
);
221 /* Open output element */
222 ret
= mi_lttng_writer_open_element(writer
,
223 mi_lttng_element_command_output
);
230 cmd_ret
= (cmd_error_code
) rotate_tracing(session_name
);
234 /* Close output element */
235 ret
= mi_lttng_writer_close_element(writer
);
241 ret
= mi_lttng_writer_write_element_bool(writer
,
242 mi_lttng_element_command_success
,
243 cmd_ret
== CMD_SUCCESS
);
249 /* Command element close */
250 ret
= mi_lttng_writer_command_close(writer
);
258 if (writer
&& mi_lttng_writer_destroy(writer
)) {