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>
27 #include <common/sessiond-comm/sessiond-comm.h>
28 #include <common/mi-lttng.h>
30 #include "../command.h"
31 #include <lttng/rotate.h>
33 static char *opt_session_name
;
34 static int opt_no_wait
;
35 static struct mi_writer
*writer
;
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 mi_print_session(char *session_name
, int enabled
)
54 /* Open session element */
55 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
60 /* Print session name element */
61 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
67 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
,
73 /* Close session element */
74 ret
= mi_lttng_writer_close_element(writer
);
80 static int rotate_tracing(void)
83 char *session_name
= NULL
, *path
= NULL
;
84 struct lttng_rotate_session_attr
*attr
= NULL
;
85 struct lttng_rotate_session_handle
*handle
= NULL
;
86 enum lttng_rotate_status rotate_status
;
88 attr
= lttng_rotate_session_attr_create();
93 if (opt_session_name
== NULL
) {
94 session_name
= get_session_name();
95 if (session_name
== NULL
) {
99 session_name
= opt_session_name
;
102 ret
= lttng_rotate_session_attr_set_session_name(attr
, session_name
);
107 DBG("Rotating the output files of session %s", session_name
);
109 ret
= lttng_rotate_session(attr
, &handle
);
112 case LTTNG_ERR_SESSION_NOT_STARTED
:
113 WARN("Tracing session %s not started yet", session_name
);
116 ERR("%s", lttng_strerror(ret
));
123 _MSG("Waiting for data availability");
126 ret
= lttng_rotate_session_pending(handle
);
132 * Data sleep time before retrying (in usec). Don't sleep if the call
133 * returned value indicates availability.
136 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
144 rotate_status
= lttng_rotate_session_get_status(handle
);
145 switch(rotate_status
) {
146 case LTTNG_ROTATE_COMPLETED
:
147 lttng_rotate_session_get_output_path(handle
, &path
);
148 MSG("Output files of session %s rotated to %s", session_name
, path
);
151 case LTTNG_ROTATE_STARTED
:
152 MSG("Rotation started for session %s", session_name
);
155 ret
= mi_print_session(session_name
, 1);
164 case LTTNG_ROTATE_EXPIRED
:
165 MSG("Output files of session %s rotated, but handle expired", session_name
);
167 ret
= mi_print_session(session_name
, 1);
176 case LTTNG_ROTATE_ERROR
:
177 MSG("An error occurred with the rotation of session %s", session_name
);
179 ret
= mi_print_session(session_name
, 1);
188 case LTTNG_ROTATE_EMPTY
:
189 MSG("Empty session, nothing to rotate.");
197 if (opt_session_name
== NULL
) {
200 lttng_rotate_session_handle_destroy(handle
);
201 lttng_rotate_session_attr_destroy(attr
);
208 * The 'rotate <options>' first level command
210 int cmd_rotate(int argc
, const char **argv
)
212 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
213 static poptContext pc
;
215 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
216 poptReadDefaultConfig(pc
, 0);
218 while ((opt
= poptGetNextOpt(pc
)) != -1) {
223 case OPT_LIST_OPTIONS
:
224 list_cmd_options(stdout
, long_options
);
232 opt_session_name
= (char*) poptGetArg(pc
);
236 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
238 ret
= -LTTNG_ERR_NOMEM
;
242 /* Open command element */
243 ret
= mi_lttng_writer_command_open(writer
,
244 mi_lttng_element_command_start
);
250 /* Open output element */
251 ret
= mi_lttng_writer_open_element(writer
,
252 mi_lttng_element_command_output
);
259 * Open sessions element
260 * For validation purpose
262 ret
= mi_lttng_writer_open_element(writer
,
263 config_element_sessions
);
270 command_ret
= rotate_tracing();
277 /* Close sessions and output element */
278 ret
= mi_lttng_close_multi_element(writer
, 2);
285 ret
= mi_lttng_writer_write_element_bool(writer
,
286 mi_lttng_element_command_success
, success
);
292 /* Command element close */
293 ret
= mi_lttng_writer_command_close(writer
);
302 if (writer
&& mi_lttng_writer_destroy(writer
)) {
303 /* Preserve original error code */
304 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
307 /* Overwrite ret if an error occurred with start_tracing */
308 ret
= command_ret
? command_ret
: ret
;