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
;
45 static struct poptOption long_options
[] = {
46 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
47 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
48 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
49 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
50 {"timer", 0, POPT_ARG_NONE
, 0, OPT_TIMER
, 0, 0},
51 {"size", 0, POPT_ARG_NONE
, 0, OPT_SIZE
, 0, 0},
55 static int setup_rotate(char *session_name
, uint64_t timer
, uint64_t size
)
58 struct lttng_rotation_schedule_attr
*attr
= NULL
;
60 attr
= lttng_rotation_schedule_attr_create();
66 /* Open rotation_schedule element */
67 ret
= mi_lttng_writer_open_element(writer
,
68 config_element_rotation_schedule
);
74 ret
= lttng_rotation_schedule_attr_set_session_name(attr
, session_name
);
80 ret
= mi_lttng_writer_write_element_string(writer
,
81 mi_lttng_element_session_name
, session_name
);
88 lttng_rotation_schedule_attr_set_timer_period(attr
, timer
);
89 MSG("Disabling rotation timer on session %s", session_name
);
92 lttng_rotation_schedule_attr_set_size(attr
, size
);
93 MSG("Disabling rotation based on size on session %s", session_name
);
96 ret
= lttng_rotation_set_schedule(attr
);
98 ERR("%s", lttng_strerror(ret
));
100 ret
= mi_lttng_writer_write_element_string(writer
,
101 mi_lttng_element_rotate_status
, "error");
105 /* Close rotation_schedule element */
106 ret
= mi_lttng_writer_close_element(writer
);
115 ret
= mi_lttng_writer_write_element_string(writer
,
116 mi_lttng_element_rotate_status
, "success");
121 /* Close rotation_schedule element */
122 ret
= mi_lttng_writer_close_element(writer
);
138 * cmd_disable_rotation
140 * The 'enable-rotation <options>' first level command
142 int cmd_disable_rotation(int argc
, const char **argv
)
144 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
146 static poptContext pc
;
147 char *session_name
= NULL
;
148 bool free_session_name
= false;
149 uint64_t timer
= 0, size
= 0;
151 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
152 popt_ret
= poptReadDefaultConfig(pc
, 0);
155 ERR("poptReadDefaultConfig");
159 while ((opt
= poptGetNextOpt(pc
)) != -1) {
164 case OPT_LIST_OPTIONS
:
165 list_cmd_options(stdout
, long_options
);
179 if (opt_session_name
== NULL
) {
180 session_name
= get_session_name();
181 if (session_name
== NULL
) {
184 free_session_name
= true;
186 session_name
= opt_session_name
;
191 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
193 ret
= -LTTNG_ERR_NOMEM
;
197 /* Open command element */
198 ret
= mi_lttng_writer_command_open(writer
,
199 mi_lttng_element_command_disable_rotation
);
205 /* Open output element */
206 ret
= mi_lttng_writer_open_element(writer
,
207 mi_lttng_element_command_output
);
214 /* No config options, just rotate the session now */
215 if (timer
== 0 && size
== 0) {
216 ERR("Missing timer period (--timer) or size limit (--size) option");
220 command_ret
= setup_rotate(session_name
, timer
, size
);
224 ERR("%s", lttng_strerror(command_ret
));
230 /* Close output element */
231 ret
= mi_lttng_writer_close_element(writer
);
237 ret
= mi_lttng_writer_write_element_bool(writer
,
238 mi_lttng_element_command_success
, success
);
244 /* Command element close */
245 ret
= mi_lttng_writer_command_close(writer
);
254 if (writer
&& mi_lttng_writer_destroy(writer
)) {
255 /* Preserve original error code */
256 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
259 /* Overwrite ret if an error occurred with start_tracing */
260 ret
= command_ret
? command_ret
: ret
;
262 if (free_session_name
) {