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
;
46 static struct poptOption long_options
[] = {
47 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
48 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
49 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
50 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
51 {"timer", 0, POPT_ARG_INT
, 0, OPT_TIMER
, 0, 0},
52 {"size", 0, POPT_ARG_INT
, 0, OPT_SIZE
, 0, 0},
56 static int setup_rotate(char *session_name
, uint64_t timer
, uint64_t size
)
59 struct lttng_rotation_schedule_attr
*attr
= NULL
;
61 attr
= lttng_rotation_schedule_attr_create();
66 ret
= lttng_rotation_schedule_attr_set_session_name(attr
, session_name
);
72 /* Open rotation_schedule element */
73 ret
= mi_lttng_writer_open_element(writer
,
74 config_element_rotation_schedule
);
79 ret
= mi_lttng_writer_write_element_string(writer
,
80 mi_lttng_element_session_name
, session_name
);
87 lttng_rotation_schedule_attr_set_timer_period(attr
, timer
);
88 MSG("Configuring session %s to rotate every %" PRIu64
" us",
91 ret
= mi_lttng_writer_write_element_unsigned_int(writer
,
92 config_element_rotation_timer_interval
, timer
);
99 lttng_rotation_schedule_attr_set_size(attr
, size
);
100 MSG("Configuring session %s to rotate every %" PRIu64
" bytes written",
103 ret
= mi_lttng_writer_write_element_unsigned_int(writer
,
104 config_element_rotation_size
, size
);
111 ret
= lttng_rotation_set_schedule(attr
);
113 ERR("%s", lttng_strerror(ret
));
115 ret
= mi_lttng_writer_write_element_string(writer
,
116 mi_lttng_element_rotate_status
, "error");
120 /* Close rotation_schedule element */
121 ret
= mi_lttng_writer_close_element(writer
);
130 ret
= mi_lttng_writer_write_element_string(writer
,
131 mi_lttng_element_rotate_status
, "success");
136 /* Close rotation_schedule element */
137 ret
= mi_lttng_writer_close_element(writer
);
153 * cmd_enable_rotation
155 * The 'enable-rotation <options>' first level command
157 int cmd_enable_rotation(int argc
, const char **argv
)
159 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
161 static poptContext pc
;
162 char *session_name
= NULL
;
163 char *opt_arg
= NULL
;
164 bool free_session_name
= false;
165 uint64_t timer
= 0, size
= 0;
167 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
168 popt_ret
= poptReadDefaultConfig(pc
, 0);
171 ERR("poptReadDefaultConfig");
175 while ((opt
= poptGetNextOpt(pc
)) != -1) {
180 case OPT_LIST_OPTIONS
:
181 list_cmd_options(stdout
, long_options
);
185 opt_arg
= poptGetOptArg(pc
);
186 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
187 ERR("Wrong value for --timer option: %s", opt_arg
);
191 if (utils_parse_time_suffix(opt_arg
, &timer
) < 0 || timer
== 0) {
192 ERR("Wrong value for --timer option: %s", opt_arg
);
196 DBG("Rotation timer set to %" PRIu64
, timer
);
200 opt_arg
= poptGetOptArg(pc
);
201 if (utils_parse_size_suffix(opt_arg
, &size
) < 0 || !size
) {
202 ERR("Wrong value for --size option: %s", opt_arg
);
206 DBG("Rotation size set to %" PRIu64
, size
);
214 if (opt_session_name
== NULL
) {
215 session_name
= get_session_name();
216 if (session_name
== NULL
) {
219 free_session_name
= true;
221 session_name
= opt_session_name
;
226 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
228 ret
= -LTTNG_ERR_NOMEM
;
232 /* Open command element */
233 ret
= mi_lttng_writer_command_open(writer
,
234 mi_lttng_element_command_enable_rotation
);
240 /* Open output element */
241 ret
= mi_lttng_writer_open_element(writer
,
242 mi_lttng_element_command_output
);
249 /* No config options, just rotate the session now */
250 if (timer
== 0 && size
== 0) {
251 ERR("No timer or size given");
255 command_ret
= setup_rotate(session_name
, timer
, size
);
259 ERR("%s", lttng_strerror(command_ret
));
265 /* Close output element */
266 ret
= mi_lttng_writer_close_element(writer
);
271 ret
= mi_lttng_writer_write_element_bool(writer
,
272 mi_lttng_element_command_success
, success
);
278 /* Command element close */
279 ret
= mi_lttng_writer_command_close(writer
);
288 if (writer
&& mi_lttng_writer_destroy(writer
)) {
289 /* Preserve original error code */
290 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
293 /* Overwrite ret if an error occurred with start_tracing */
294 ret
= command_ret
? command_ret
: ret
;
296 if (free_session_name
) {