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>
36 static char *opt_session_name
;
37 static int opt_no_wait
;
38 static struct mi_writer
*writer
;
40 #ifdef LTTNG_EMBED_HELP
41 static const char help_msg
[] =
42 #include <lttng-rotate.1.h>
51 static struct poptOption long_options
[] = {
52 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
53 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
54 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
55 {"no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, 0, 0},
59 static int mi_output_rotate(const char *status
, const char *path
,
60 const char *session_name
)
69 ret
= mi_lttng_writer_open_element(writer
,
70 mi_lttng_element_rotation
);
75 ret
= mi_lttng_writer_write_element_string(writer
,
76 mi_lttng_element_session_name
, session_name
);
81 ret
= mi_lttng_writer_write_element_string(writer
,
82 mi_lttng_element_rotate_status
, status
);
87 ret
= mi_lttng_writer_write_element_string(writer
,
88 config_element_path
, path
);
93 /* Close rotation element */
94 ret
= mi_lttng_writer_close_element(writer
);
103 static int rotate_tracing(char *session_name
)
106 struct lttng_rotation_immediate_attr
*attr
= NULL
;
107 struct lttng_rotation_handle
*handle
= NULL
;
108 enum lttng_rotation_status rotation_status
;
109 enum lttng_rotation_state rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
111 DBG("Rotating the output files of session %s", session_name
);
113 attr
= lttng_rotation_immediate_attr_create();
118 ret
= lttng_rotation_immediate_attr_set_session_name(attr
, session_name
);
120 ERR("Session name exceeds the maximal allowed length");
124 ret
= lttng_rotate_session(attr
, &handle
);
127 case LTTNG_ERR_SESSION_NOT_STARTED
:
128 WARN("Tracing session %s not started yet", session_name
);
131 ERR("%s", lttng_strerror(ret
));
138 _MSG("Waiting for rotation to complete");
139 ret
= fflush(stdout
);
146 rotation_status
= lttng_rotation_handle_get_state(handle
,
148 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
149 ERR("Failed to query the state of the rotation");
154 * Data sleep time before retrying (in usec). Don't
155 * sleep if the call returned value indicates
158 if (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
159 ret
= usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
166 ret
= fflush(stdout
);
172 } while (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
);
176 switch (rotation_state
) {
177 case LTTNG_ROTATION_STATE_COMPLETED
:
181 rotation_status
= lttng_rotation_handle_get_completed_archive_location(
183 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
184 ERR("Failed to retrieve the rotation's completed chunk archive location");
187 MSG("Trace chunk archive for session %s is now readable at %s",
189 ret
= mi_output_rotate("completed", path
, session_name
);
196 case LTTNG_ROTATION_STATE_EXPIRED
:
197 MSG("Session %s rotated, but handle expired", session_name
);
198 ret
= mi_output_rotate("expired", NULL
, session_name
);
205 ERR("Unexpected rotation state received, aborting...");
212 lttng_rotation_handle_destroy(handle
);
213 lttng_rotation_immediate_attr_destroy(attr
);
220 * The 'rotate <options>' first level command
222 int cmd_rotate(int argc
, const char **argv
)
224 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
226 static poptContext pc
;
227 char *session_name
= NULL
;
228 bool free_session_name
= false;
230 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
231 popt_ret
= poptReadDefaultConfig(pc
, 0);
234 ERR("poptReadDefaultConfig");
238 while ((opt
= poptGetNextOpt(pc
)) != -1) {
243 case OPT_LIST_OPTIONS
:
244 list_cmd_options(stdout
, long_options
);
252 opt_session_name
= (char*) poptGetArg(pc
);
254 if (!opt_session_name
) {
255 session_name
= get_session_name();
259 free_session_name
= true;
261 session_name
= opt_session_name
;
266 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
268 ret
= -LTTNG_ERR_NOMEM
;
272 /* Open rotate command */
273 ret
= mi_lttng_writer_command_open(writer
,
274 mi_lttng_element_command_rotate
);
280 /* Open output element */
281 ret
= mi_lttng_writer_open_element(writer
,
282 mi_lttng_element_command_output
);
287 /* Open rotations element */
288 ret
= mi_lttng_writer_open_element(writer
,
289 mi_lttng_element_rotations
);
296 command_ret
= rotate_tracing(session_name
);
303 /* Close rotations element */
304 ret
= mi_lttng_writer_close_element(writer
);
308 /* Close output element */
309 ret
= mi_lttng_writer_close_element(writer
);
314 ret
= mi_lttng_writer_write_element_bool(writer
,
315 mi_lttng_element_command_success
, success
);
321 /* Command element close */
322 ret
= mi_lttng_writer_command_close(writer
);
331 if (writer
&& mi_lttng_writer_destroy(writer
)) {
332 /* Preserve original error code */
333 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
336 /* Overwrite ret if an error occurred with start_tracing */
337 ret
= command_ret
? command_ret
: ret
;
339 if (free_session_name
) {