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/rotation.h>
25 #include <lttng/location.h>
27 static char *opt_session_name
;
28 static int opt_no_wait
;
29 static struct mi_writer
*writer
;
31 #ifdef LTTNG_EMBED_HELP
32 static const char help_msg
[] =
33 #include <lttng-rotate.1.h>
42 static struct poptOption long_options
[] = {
43 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
44 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
45 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
46 {"no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, 0, 0},
50 static int rotate_tracing(char *session_name
)
53 enum cmd_error_code cmd_ret
= CMD_SUCCESS
;
54 struct lttng_rotation_handle
*handle
= NULL
;
55 enum lttng_rotation_status rotation_status
;
56 enum lttng_rotation_state rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
57 const struct lttng_trace_archive_location
*location
= NULL
;
58 bool print_location
= true;
60 DBG("Rotating the output files of session %s", session_name
);
62 ret
= lttng_rotate_session(session_name
, NULL
, &handle
);
65 case LTTNG_ERR_SESSION_NOT_STARTED
:
66 WARN("Tracing session %s not started yet", session_name
);
67 cmd_ret
= CMD_WARNING
;
70 ERR("%s", lttng_strerror(ret
));
76 rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
80 _MSG("Waiting for rotation to complete");
88 rotation_status
= lttng_rotation_handle_get_state(handle
,
90 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
92 ERR("Failed to query the state of the rotation.");
96 if (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
97 ret
= usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
104 ret
= fflush(stdout
);
110 } while (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
);
114 switch (rotation_state
) {
115 case LTTNG_ROTATION_STATE_COMPLETED
:
116 rotation_status
= lttng_rotation_handle_get_archive_location(
118 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
119 ERR("Failed to retrieve the rotation's completed chunk archive location.");
123 case LTTNG_ROTATION_STATE_EXPIRED
:
125 case LTTNG_ROTATION_STATE_ERROR
:
126 ERR("Failed to retrieve rotation state.");
128 case LTTNG_ROTATION_STATE_ONGOING
:
129 MSG("Rotation ongoing for session %s", session_name
);
130 print_location
= false;
133 ERR("Unexpected rotation state encountered.");
137 if (!lttng_opt_mi
&& print_location
) {
138 ret
= print_trace_archive_location(location
,
140 } else if (lttng_opt_mi
) {
141 ret
= mi_lttng_rotate(writer
, session_name
, rotation_state
,
150 lttng_rotation_handle_destroy(handle
);
160 * The 'rotate <options>' first level command
162 int cmd_rotate(int argc
, const char **argv
)
165 enum cmd_error_code cmd_ret
= CMD_SUCCESS
;
167 static poptContext pc
;
168 char *session_name
= NULL
;
169 bool free_session_name
= false;
171 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
172 popt_ret
= poptReadDefaultConfig(pc
, 0);
174 ERR("poptReadDefaultConfig");
178 while ((opt
= poptGetNextOpt(pc
)) != -1) {
183 case OPT_LIST_OPTIONS
:
184 list_cmd_options(stdout
, long_options
);
187 cmd_ret
= CMD_UNDEFINED
;
192 opt_session_name
= (char*) poptGetArg(pc
);
194 if (!opt_session_name
) {
195 session_name
= get_session_name();
199 free_session_name
= true;
201 session_name
= opt_session_name
;
206 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
211 /* Open rotate command */
212 ret
= mi_lttng_writer_command_open(writer
,
213 mi_lttng_element_command_rotate
);
218 /* Open output element */
219 ret
= mi_lttng_writer_open_element(writer
,
220 mi_lttng_element_command_output
);
226 cmd_ret
= rotate_tracing(session_name
);
230 /* Close output element */
231 ret
= mi_lttng_writer_close_element(writer
);
236 ret
= mi_lttng_writer_write_element_bool(writer
,
237 mi_lttng_element_command_success
,
238 cmd_ret
== CMD_SUCCESS
);
243 /* Command element close */
244 ret
= mi_lttng_writer_command_close(writer
);
251 if (writer
&& mi_lttng_writer_destroy(writer
)) {
256 if (free_session_name
) {