2 * Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "../command.hpp"
11 #include <common/mi-lttng.hpp>
12 #include <common/sessiond-comm/sessiond-comm.hpp>
13 #include <common/utils.hpp>
15 #include <lttng/lttng.h>
24 #include <sys/types.h>
27 static char *opt_session_name
;
28 static struct mi_writer
*writer
;
30 #ifdef LTTNG_EMBED_HELP
31 static const char help_msg
[] =
32 #include <lttng-enable-rotation.1.h>
43 static struct poptOption long_options
[] = {
44 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
45 { "help", 'h', POPT_ARG_NONE
, nullptr, OPT_HELP
, nullptr, nullptr },
46 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
47 { "session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, nullptr, nullptr },
48 { "timer", 0, POPT_ARG_INT
, nullptr, OPT_TIMER
, nullptr, nullptr },
49 { "size", 0, POPT_ARG_INT
, nullptr, OPT_SIZE
, nullptr, nullptr },
50 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
53 static const char *schedule_type_str
[] = {
58 static enum cmd_error_code
add_schedule(const char *session_name
,
59 enum lttng_rotation_schedule_type schedule_type
,
62 enum cmd_error_code ret
= CMD_SUCCESS
;
63 struct lttng_rotation_schedule
*schedule
= nullptr;
64 enum lttng_rotation_status status
;
65 const char *schedule_type_name
;
67 switch (schedule_type
) {
68 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
69 schedule
= lttng_rotation_schedule_periodic_create();
74 status
= lttng_rotation_schedule_periodic_set_period(schedule
, value
);
76 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
77 schedule
= lttng_rotation_schedule_size_threshold_create();
82 status
= lttng_rotation_schedule_size_threshold_set_threshold(schedule
, value
);
85 ERR("Unknown schedule type");
89 schedule_type_name
= schedule_type_str
[schedule_type
];
92 case LTTNG_ROTATION_STATUS_OK
:
94 case LTTNG_ROTATION_STATUS_INVALID
:
95 ERR("Invalid value for %s option", schedule_type_name
);
99 ERR("Unknown error occurred setting %s rotation schedule", schedule_type_name
);
104 status
= lttng_session_add_rotation_schedule(session_name
, schedule
);
106 case LTTNG_ROTATION_STATUS_OK
:
108 switch (schedule_type
) {
109 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
110 MSG("Enabled %s rotations every %" PRIu64
" %s on session %s",
116 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
117 MSG("Enabled %s rotations every %" PRIu64
" bytes written on session %s",
126 case LTTNG_ROTATION_STATUS_INVALID
:
127 ERR("Invalid parameter for %s rotation schedule", schedule_type_name
);
130 case LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET
:
131 ERR("A %s rotation schedule is already set on session %s",
136 case LTTNG_ROTATION_STATUS_ERROR
:
138 ERR("Failed to enable %s rotation schedule on session %s",
148 mi_ret
= mi_lttng_rotation_schedule_result(writer
, schedule
, ret
== CMD_SUCCESS
);
156 lttng_rotation_schedule_destroy(schedule
);
161 * cmd_enable_rotation
163 * The 'enable-rotation <options>' first level command
165 int cmd_enable_rotation(int argc
, const char **argv
)
167 int popt_ret
, opt
, ret
= 0;
168 enum cmd_error_code cmd_ret
= CMD_SUCCESS
;
169 static poptContext pc
;
170 char *session_name
= nullptr;
171 char *opt_arg
= nullptr;
172 bool free_session_name
= false;
173 uint64_t timer_us
= 0, size_bytes
= 0;
174 bool periodic_rotation
= false, size_rotation
= false;
176 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
177 popt_ret
= poptReadDefaultConfig(pc
, 0);
179 ERR("poptReadDefaultConfig");
183 while ((opt
= poptGetNextOpt(pc
)) != -1) {
192 case OPT_LIST_OPTIONS
:
193 list_cmd_options(stdout
, long_options
);
197 opt_arg
= poptGetOptArg(pc
);
198 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
199 ERR("Invalid value for --timer option: %s", opt_arg
);
202 if (utils_parse_time_suffix(opt_arg
, &timer_us
) < 0) {
203 ERR("Invalid value for --timer option: %s", opt_arg
);
206 if (periodic_rotation
) {
207 ERR("Only one periodic rotation schedule may be set on a session.");
210 periodic_rotation
= true;
214 opt_arg
= poptGetOptArg(pc
);
215 if (utils_parse_size_suffix(opt_arg
, &size_bytes
) < 0) {
216 ERR("Invalid value for --size option: %s", opt_arg
);
220 ERR("Only one size-based rotation schedule may be set on a session.");
223 size_rotation
= true;
226 cmd_ret
= CMD_UNDEFINED
;
231 if (opt_session_name
== nullptr) {
232 session_name
= get_session_name();
233 if (session_name
== nullptr) {
236 free_session_name
= true;
238 session_name
= opt_session_name
;
243 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
248 /* Open command element */
249 ret
= mi_lttng_writer_command_open(writer
,
250 mi_lttng_element_command_enable_rotation
);
255 /* Open output element */
256 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
);
262 if (!periodic_rotation
&& !size_rotation
) {
263 ERR("No session rotation schedule parameter provided.");
269 ret
= mi_lttng_writer_open_element(writer
,
270 mi_lttng_element_rotation_schedule_results
);
275 ret
= mi_lttng_writer_write_element_string(
276 writer
, mi_lttng_element_session_name
, session_name
);
282 if (periodic_rotation
) {
284 * Continue processing even on error as multiple schedules can
285 * be specified at once.
288 add_schedule(session_name
, LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
, timer_us
);
292 enum cmd_error_code tmp_ret
;
294 /* Don't overwrite cmd_ret if it already indicates an error. */
295 tmp_ret
= add_schedule(
296 session_name
, LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
, size_bytes
);
297 cmd_ret
= cmd_ret
? cmd_ret
: tmp_ret
;
301 /* Close rotation schedule results element */
302 ret
= mi_lttng_writer_close_element(writer
);
311 /* Close output element */
312 ret
= mi_lttng_writer_close_element(writer
);
318 ret
= mi_lttng_writer_write_element_bool(
319 writer
, mi_lttng_element_command_success
, cmd_ret
== CMD_SUCCESS
);
324 /* Command element close */
325 ret
= mi_lttng_writer_command_close(writer
);
332 (void) mi_lttng_writer_destroy(writer
);
334 if (free_session_name
) {