2 * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/types.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
31 #include <common/mi-lttng.h>
33 #include "../command.h"
34 #include <lttng/rotation.h>
35 #include <lttng/location.h>
37 static char *opt_session_name
;
38 static int opt_no_wait
;
39 static struct mi_writer
*writer
;
41 #ifdef LTTNG_EMBED_HELP
42 static const char help_msg
[] =
43 #include <lttng-rotate.1.h>
52 static struct poptOption long_options
[] = {
53 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
54 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
55 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
56 {"no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, 0, 0},
60 static int mi_output_rotate(const char *status
, const char *path
,
61 const char *session_name
)
70 ret
= mi_lttng_writer_open_element(writer
,
71 mi_lttng_element_rotation
);
76 ret
= mi_lttng_writer_write_element_string(writer
,
77 mi_lttng_element_session_name
, session_name
);
82 ret
= mi_lttng_writer_write_element_string(writer
,
83 mi_lttng_element_rotate_status
, status
);
88 ret
= mi_lttng_writer_write_element_string(writer
,
89 config_element_path
, path
);
94 /* Close rotation element */
95 ret
= mi_lttng_writer_close_element(writer
);
104 static int output_trace_archive_location(
105 const struct lttng_trace_archive_location
*location
,
106 const char *session_name
)
109 enum lttng_trace_archive_location_type location_type
;
110 enum lttng_trace_archive_location_status status
;
111 bool printed_location
= false;
113 location_type
= lttng_trace_archive_location_get_type(location
);
115 _MSG("Trace chunk archive for session %s is now readable",
117 switch (location_type
) {
118 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
120 const char *absolute_path
;
122 status
= lttng_trace_archive_location_local_get_absolute_path(
123 location
, &absolute_path
);
124 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
128 MSG(" at %s", absolute_path
);
129 ret
= mi_output_rotate("completed", absolute_path
,
134 printed_location
= true;
137 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
139 uint16_t control_port
, data_port
;
140 const char *host
, *relative_path
, *protocol_str
;
141 enum lttng_trace_archive_location_relay_protocol_type protocol
;
143 /* Fetch all relay location parameters. */
144 status
= lttng_trace_archive_location_relay_get_protocol_type(
145 location
, &protocol
);
146 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
151 status
= lttng_trace_archive_location_relay_get_host(
153 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
158 status
= lttng_trace_archive_location_relay_get_control_port(
159 location
, &control_port
);
160 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
165 status
= lttng_trace_archive_location_relay_get_data_port(
166 location
, &data_port
);
167 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
172 status
= lttng_trace_archive_location_relay_get_relative_path(
173 location
, &relative_path
);
174 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
180 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
:
181 protocol_str
= "tcp";
184 protocol_str
= "unknown";
188 MSG(" on relay %s://%s/%s [control port %" PRIu16
", data port %"
189 PRIu16
"]", protocol_str
, host
,
190 relative_path
, control_port
, data_port
);
191 printed_location
= true;
192 ret
= mi_output_rotate("completed", relative_path
,
203 if (!printed_location
) {
204 MSG(" at an unknown location");
209 static int rotate_tracing(char *session_name
)
212 struct lttng_rotation_handle
*handle
= NULL
;
213 enum lttng_rotation_status rotation_status
;
214 enum lttng_rotation_state rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
216 DBG("Rotating the output files of session %s", session_name
);
218 ret
= lttng_rotate_session(session_name
, NULL
, &handle
);
221 case LTTNG_ERR_SESSION_NOT_STARTED
:
222 WARN("Tracing session %s not started yet", session_name
);
225 ERR("%s", lttng_strerror(ret
));
232 _MSG("Waiting for rotation to complete");
233 ret
= fflush(stdout
);
240 rotation_status
= lttng_rotation_handle_get_state(handle
,
242 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
243 ERR("Failed to query the state of the rotation");
248 * Data sleep time before retrying (in usec). Don't
249 * sleep if the call returned value indicates
252 if (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
253 ret
= usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
260 ret
= fflush(stdout
);
266 } while (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
);
270 switch (rotation_state
) {
271 case LTTNG_ROTATION_STATE_COMPLETED
:
273 const struct lttng_trace_archive_location
*location
;
275 rotation_status
= lttng_rotation_handle_get_archive_location(
277 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
278 ERR("Failed to retrieve the rotation's completed chunk archive location");
281 ret
= output_trace_archive_location(location
, session_name
);
288 case LTTNG_ROTATION_STATE_EXPIRED
:
289 MSG("Session %s rotated, but handle expired", session_name
);
290 ret
= mi_output_rotate("expired", NULL
, session_name
);
297 ERR("Unexpected rotation state received, aborting...");
304 lttng_rotation_handle_destroy(handle
);
311 * The 'rotate <options>' first level command
313 int cmd_rotate(int argc
, const char **argv
)
315 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
317 static poptContext pc
;
318 char *session_name
= NULL
;
319 bool free_session_name
= false;
321 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
322 popt_ret
= poptReadDefaultConfig(pc
, 0);
325 ERR("poptReadDefaultConfig");
329 while ((opt
= poptGetNextOpt(pc
)) != -1) {
334 case OPT_LIST_OPTIONS
:
335 list_cmd_options(stdout
, long_options
);
343 opt_session_name
= (char*) poptGetArg(pc
);
345 if (!opt_session_name
) {
346 session_name
= get_session_name();
350 free_session_name
= true;
352 session_name
= opt_session_name
;
357 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
359 ret
= -LTTNG_ERR_NOMEM
;
363 /* Open rotate command */
364 ret
= mi_lttng_writer_command_open(writer
,
365 mi_lttng_element_command_rotate
);
371 /* Open output element */
372 ret
= mi_lttng_writer_open_element(writer
,
373 mi_lttng_element_command_output
);
378 /* Open rotations element */
379 ret
= mi_lttng_writer_open_element(writer
,
380 mi_lttng_element_rotation_schedules
);
387 command_ret
= rotate_tracing(session_name
);
394 /* Close rotations element */
395 ret
= mi_lttng_writer_close_element(writer
);
399 /* Close output element */
400 ret
= mi_lttng_writer_close_element(writer
);
405 ret
= mi_lttng_writer_write_element_bool(writer
,
406 mi_lttng_element_command_success
, success
);
412 /* Command element close */
413 ret
= mi_lttng_writer_command_close(writer
);
422 if (writer
&& mi_lttng_writer_destroy(writer
)) {
423 /* Preserve original error code */
424 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
427 /* Overwrite ret if an error occurred with start_tracing */
428 ret
= command_ret
? command_ret
: ret
;
430 if (free_session_name
) {