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>
29 #include <common/sessiond-comm/sessiond-comm.h>
30 #include <common/mi-lttng.h>
32 #include "../command.h"
33 #include <lttng/rotation.h>
35 static char *opt_session_name
;
36 static struct mi_writer
*writer
;
38 #ifdef LTTNG_EMBED_HELP
39 static const char help_msg
[] =
40 #include <lttng-disable-rotation.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 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
56 {"timer", 0, POPT_ARG_NONE
, 0, OPT_TIMER
, 0, 0},
57 {"size", 0, POPT_ARG_NONE
, 0, OPT_SIZE
, 0, 0},
61 static int setup_rotate(char *session_name
, uint64_t timer
, uint64_t size
)
64 struct lttng_rotation_schedule_attr
*attr
= NULL
;
66 attr
= lttng_rotation_schedule_attr_create();
72 /* Open rotation_schedule element */
73 ret
= mi_lttng_writer_open_element(writer
,
74 config_element_rotation_schedule
);
80 ret
= lttng_rotation_schedule_attr_set_session_name(attr
, session_name
);
86 ret
= mi_lttng_writer_write_element_string(writer
,
87 mi_lttng_element_session_name
, session_name
);
94 lttng_rotation_schedule_attr_set_timer_period(attr
, timer
);
95 MSG("Disabling rotation timer on session %s", session_name
);
98 lttng_rotation_schedule_attr_set_size(attr
, size
);
99 MSG("Disabling rotation based on size on session %s", session_name
);
102 ret
= lttng_rotation_set_schedule(attr
);
104 ERR("%s", lttng_strerror(ret
));
106 ret
= mi_lttng_writer_write_element_string(writer
,
107 mi_lttng_element_rotate_status
, "error");
111 /* Close rotation_schedule element */
112 ret
= mi_lttng_writer_close_element(writer
);
121 ret
= mi_lttng_writer_write_element_string(writer
,
122 mi_lttng_element_rotate_status
, "success");
127 /* Close rotation_schedule element */
128 ret
= mi_lttng_writer_close_element(writer
);
144 * cmd_disable_rotation
146 * The 'enable-rotation <options>' first level command
148 int cmd_disable_rotation(int argc
, const char **argv
)
150 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
152 static poptContext pc
;
153 char *session_name
= NULL
;
154 bool free_session_name
= false;
155 uint64_t timer
= 0, size
= 0;
157 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
158 popt_ret
= poptReadDefaultConfig(pc
, 0);
161 ERR("poptReadDefaultConfig");
165 while ((opt
= poptGetNextOpt(pc
)) != -1) {
170 case OPT_LIST_OPTIONS
:
171 list_cmd_options(stdout
, long_options
);
185 if (opt_session_name
== NULL
) {
186 session_name
= get_session_name();
187 if (session_name
== NULL
) {
190 free_session_name
= true;
192 session_name
= opt_session_name
;
197 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
199 ret
= -LTTNG_ERR_NOMEM
;
203 /* Open command element */
204 ret
= mi_lttng_writer_command_open(writer
,
205 mi_lttng_element_command_disable_rotation
);
211 /* Open output element */
212 ret
= mi_lttng_writer_open_element(writer
,
213 mi_lttng_element_command_output
);
220 /* No config options, just rotate the session now */
221 if (timer
== 0 && size
== 0) {
222 ERR("Missing timer period (--timer) or size limit (--size) option");
226 command_ret
= setup_rotate(session_name
, timer
, size
);
230 ERR("%s", lttng_strerror(command_ret
));
236 /* Close output element */
237 ret
= mi_lttng_writer_close_element(writer
);
243 ret
= mi_lttng_writer_write_element_bool(writer
,
244 mi_lttng_element_command_success
, success
);
250 /* Command element close */
251 ret
= mi_lttng_writer_command_close(writer
);
260 if (writer
&& mi_lttng_writer_destroy(writer
)) {
261 /* Preserve original error code */
262 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
265 /* Overwrite ret if an error occurred with start_tracing */
266 ret
= command_ret
? command_ret
: ret
;
268 if (free_session_name
) {