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>
31 #include <common/utils.h>
33 #include "../command.h"
34 #include <lttng/rotation.h>
36 static char *opt_session_name
;
37 static struct mi_writer
*writer
;
39 #ifdef LTTNG_EMBED_HELP
40 static const char help_msg
[] =
41 #include <lttng-enable-rotation.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 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
57 {"timer", 0, POPT_ARG_INT
, 0, OPT_TIMER
, 0, 0},
58 {"size", 0, POPT_ARG_INT
, 0, OPT_SIZE
, 0, 0},
62 static int setup_rotate(char *session_name
, uint64_t timer
, uint64_t size
)
65 struct lttng_rotation_schedule_attr
*attr
= NULL
;
67 attr
= lttng_rotation_schedule_attr_create();
72 ret
= lttng_rotation_schedule_attr_set_session_name(attr
, session_name
);
78 /* Open rotation_schedule element */
79 ret
= mi_lttng_writer_open_element(writer
,
80 config_element_rotation_schedule
);
85 ret
= mi_lttng_writer_write_element_string(writer
,
86 mi_lttng_element_session_name
, session_name
);
93 lttng_rotation_schedule_attr_set_timer_period(attr
, timer
);
94 MSG("Configuring session %s to rotate every %" PRIu64
" us",
97 ret
= mi_lttng_writer_write_element_unsigned_int(writer
,
98 config_element_rotation_timer_interval
, timer
);
105 lttng_rotation_schedule_attr_set_size(attr
, size
);
106 MSG("Configuring session %s to rotate every %" PRIu64
" bytes written",
109 ret
= mi_lttng_writer_write_element_unsigned_int(writer
,
110 config_element_rotation_size
, size
);
117 ret
= lttng_rotation_set_schedule(attr
);
119 ERR("%s", lttng_strerror(ret
));
121 ret
= mi_lttng_writer_write_element_string(writer
,
122 mi_lttng_element_rotate_status
, "error");
126 /* Close rotation_schedule element */
127 ret
= mi_lttng_writer_close_element(writer
);
136 ret
= mi_lttng_writer_write_element_string(writer
,
137 mi_lttng_element_rotate_status
, "success");
142 /* Close rotation_schedule element */
143 ret
= mi_lttng_writer_close_element(writer
);
155 lttng_rotation_schedule_attr_destroy(attr
);
160 * cmd_enable_rotation
162 * The 'enable-rotation <options>' first level command
164 int cmd_enable_rotation(int argc
, const char **argv
)
166 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
168 static poptContext pc
;
169 char *session_name
= NULL
;
170 char *opt_arg
= NULL
;
171 bool free_session_name
= false;
172 uint64_t timer
= 0, size
= 0;
174 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
175 popt_ret
= poptReadDefaultConfig(pc
, 0);
178 ERR("poptReadDefaultConfig");
182 while ((opt
= poptGetNextOpt(pc
)) != -1) {
187 case OPT_LIST_OPTIONS
:
188 list_cmd_options(stdout
, long_options
);
192 opt_arg
= poptGetOptArg(pc
);
193 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
194 ERR("Wrong value for --timer option: %s", opt_arg
);
198 if (utils_parse_time_suffix(opt_arg
, &timer
) < 0 || timer
== 0) {
199 ERR("Wrong value for --timer option: %s", opt_arg
);
203 DBG("Rotation timer set to %" PRIu64
, timer
);
207 opt_arg
= poptGetOptArg(pc
);
208 if (utils_parse_size_suffix(opt_arg
, &size
) < 0 || !size
) {
209 ERR("Wrong value for --size option: %s", opt_arg
);
213 DBG("Rotation size set to %" PRIu64
, size
);
221 if (opt_session_name
== NULL
) {
222 session_name
= get_session_name();
223 if (session_name
== NULL
) {
226 free_session_name
= true;
228 session_name
= opt_session_name
;
233 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
235 ret
= -LTTNG_ERR_NOMEM
;
239 /* Open command element */
240 ret
= mi_lttng_writer_command_open(writer
,
241 mi_lttng_element_command_enable_rotation
);
247 /* Open output element */
248 ret
= mi_lttng_writer_open_element(writer
,
249 mi_lttng_element_command_output
);
256 /* No config options, just rotate the session now */
257 if (timer
== 0 && size
== 0) {
258 ERR("No timer or size given");
262 command_ret
= setup_rotate(session_name
, timer
, size
);
266 ERR("%s", lttng_strerror(command_ret
));
272 /* Close output element */
273 ret
= mi_lttng_writer_close_element(writer
);
278 ret
= mi_lttng_writer_write_element_bool(writer
,
279 mi_lttng_element_command_success
, success
);
285 /* Command element close */
286 ret
= mi_lttng_writer_command_close(writer
);
295 if (writer
&& mi_lttng_writer_destroy(writer
)) {
296 /* Preserve original error code */
297 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
300 /* Overwrite ret if an error occurred with start_tracing */
301 ret
= command_ret
? command_ret
: ret
;
303 if (free_session_name
) {