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_immediate_attr
*attr
= NULL
;
213 struct lttng_rotation_handle
*handle
= NULL
;
214 enum lttng_rotation_status rotation_status
;
215 enum lttng_rotation_state rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
217 DBG("Rotating the output files of session %s", session_name
);
219 attr
= lttng_rotation_immediate_attr_create();
224 ret
= lttng_rotation_immediate_attr_set_session_name(attr
, session_name
);
226 ERR("Session name exceeds the maximal allowed length");
230 ret
= lttng_rotate_session(attr
, &handle
);
233 case LTTNG_ERR_SESSION_NOT_STARTED
:
234 WARN("Tracing session %s not started yet", session_name
);
237 ERR("%s", lttng_strerror(ret
));
244 _MSG("Waiting for rotation to complete");
245 ret
= fflush(stdout
);
252 rotation_status
= lttng_rotation_handle_get_state(handle
,
254 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
255 ERR("Failed to query the state of the rotation");
260 * Data sleep time before retrying (in usec). Don't
261 * sleep if the call returned value indicates
264 if (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
265 ret
= usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
272 ret
= fflush(stdout
);
278 } while (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
);
282 switch (rotation_state
) {
283 case LTTNG_ROTATION_STATE_COMPLETED
:
285 const struct lttng_trace_archive_location
*location
;
287 rotation_status
= lttng_rotation_handle_get_archive_location(
289 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
290 ERR("Failed to retrieve the rotation's completed chunk archive location");
293 ret
= output_trace_archive_location(location
, session_name
);
300 case LTTNG_ROTATION_STATE_EXPIRED
:
301 MSG("Session %s rotated, but handle expired", session_name
);
302 ret
= mi_output_rotate("expired", NULL
, session_name
);
309 ERR("Unexpected rotation state received, aborting...");
316 lttng_rotation_handle_destroy(handle
);
317 lttng_rotation_immediate_attr_destroy(attr
);
324 * The 'rotate <options>' first level command
326 int cmd_rotate(int argc
, const char **argv
)
328 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
330 static poptContext pc
;
331 char *session_name
= NULL
;
332 bool free_session_name
= false;
334 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
335 popt_ret
= poptReadDefaultConfig(pc
, 0);
338 ERR("poptReadDefaultConfig");
342 while ((opt
= poptGetNextOpt(pc
)) != -1) {
347 case OPT_LIST_OPTIONS
:
348 list_cmd_options(stdout
, long_options
);
356 opt_session_name
= (char*) poptGetArg(pc
);
358 if (!opt_session_name
) {
359 session_name
= get_session_name();
363 free_session_name
= true;
365 session_name
= opt_session_name
;
370 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
372 ret
= -LTTNG_ERR_NOMEM
;
376 /* Open rotate command */
377 ret
= mi_lttng_writer_command_open(writer
,
378 mi_lttng_element_command_rotate
);
384 /* Open output element */
385 ret
= mi_lttng_writer_open_element(writer
,
386 mi_lttng_element_command_output
);
391 /* Open rotations element */
392 ret
= mi_lttng_writer_open_element(writer
,
393 mi_lttng_element_rotations
);
400 command_ret
= rotate_tracing(session_name
);
407 /* Close rotations element */
408 ret
= mi_lttng_writer_close_element(writer
);
412 /* Close output element */
413 ret
= mi_lttng_writer_close_element(writer
);
418 ret
= mi_lttng_writer_write_element_bool(writer
,
419 mi_lttng_element_command_success
, success
);
425 /* Command element close */
426 ret
= mi_lttng_writer_command_close(writer
);
435 if (writer
&& mi_lttng_writer_destroy(writer
)) {
436 /* Preserve original error code */
437 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
440 /* Overwrite ret if an error occurred with start_tracing */
441 ret
= command_ret
? command_ret
: ret
;
443 if (free_session_name
) {