2 * Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
19 #include <common/sessiond-comm/sessiond-comm.h>
20 #include <common/mi-lttng.h>
21 #include <common/utils.h>
23 #include "../command.h"
24 #include <lttng/lttng.h>
26 static char *opt_session_name
;
27 static struct mi_writer
*writer
;
29 #ifdef LTTNG_EMBED_HELP
30 static const char help_msg
[] =
31 #include <lttng-enable-rotation.1.h>
42 static struct poptOption long_options
[] = {
43 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
44 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
45 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
46 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
47 {"timer", 0, POPT_ARG_INT
, 0, OPT_TIMER
, 0, 0},
48 {"size", 0, POPT_ARG_INT
, 0, OPT_SIZE
, 0, 0},
52 static const char *schedule_type_str
[] = {
57 static enum cmd_error_code
add_schedule(const char *session_name
,
58 enum lttng_rotation_schedule_type schedule_type
, uint64_t value
)
60 enum cmd_error_code ret
= CMD_SUCCESS
;
61 struct lttng_rotation_schedule
*schedule
= NULL
;
62 enum lttng_rotation_status status
;
63 const char *schedule_type_name
;
65 switch (schedule_type
) {
66 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
67 schedule
= lttng_rotation_schedule_periodic_create();
72 status
= lttng_rotation_schedule_periodic_set_period(schedule
,
75 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
76 schedule
= lttng_rotation_schedule_size_threshold_create();
81 status
= lttng_rotation_schedule_size_threshold_set_threshold(
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",
105 status
= lttng_session_add_rotation_schedule(session_name
, schedule
);
107 case LTTNG_ROTATION_STATUS_OK
:
109 switch (schedule_type
) {
110 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
111 MSG("Enabled %s rotations every %" PRIu64
" %s on session %s",
112 schedule_type_name
, value
, USEC_UNIT
, session_name
);
114 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
115 MSG("Enabled %s rotations every %" PRIu64
" bytes written on session %s",
116 schedule_type_name
, value
, session_name
);
122 case LTTNG_ROTATION_STATUS_INVALID
:
123 ERR("Invalid parameter for %s rotation schedule",
127 case LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET
:
128 ERR("A %s rotation schedule is already set on session %s",
133 case LTTNG_ROTATION_STATUS_ERROR
:
135 ERR("Failed to enable %s rotation schedule on session %s",
145 mi_ret
= mi_lttng_rotation_schedule_result(writer
,
146 schedule
, ret
== CMD_SUCCESS
);
154 lttng_rotation_schedule_destroy(schedule
);
159 * cmd_enable_rotation
161 * The 'enable-rotation <options>' first level command
163 int cmd_enable_rotation(int argc
, const char **argv
)
165 int popt_ret
, opt
, ret
= 0;
166 enum cmd_error_code cmd_ret
= CMD_SUCCESS
;
167 static poptContext pc
;
168 char *session_name
= NULL
;
169 char *opt_arg
= NULL
;
170 bool free_session_name
= false;
171 uint64_t timer_us
= 0, size_bytes
= 0;
172 bool periodic_rotation
= false, size_rotation
= false;
174 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
175 popt_ret
= poptReadDefaultConfig(pc
, 0);
177 ERR("poptReadDefaultConfig");
181 while ((opt
= poptGetNextOpt(pc
)) != -1) {
186 case OPT_LIST_OPTIONS
:
187 list_cmd_options(stdout
, long_options
);
191 opt_arg
= poptGetOptArg(pc
);
192 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
193 ERR("Invalid value for --timer option: %s", opt_arg
);
196 if (utils_parse_time_suffix(opt_arg
, &timer_us
) < 0) {
197 ERR("Invalid value for --timer option: %s", opt_arg
);
200 if (periodic_rotation
) {
201 ERR("Only one periodic rotation schedule may be set on a session.");
204 periodic_rotation
= true;
208 opt_arg
= poptGetOptArg(pc
);
209 if (utils_parse_size_suffix(opt_arg
, &size_bytes
) < 0) {
210 ERR("Invalid value for --size option: %s", opt_arg
);
214 ERR("Only one size-based rotation schedule may be set on a session.");
217 size_rotation
= true;
220 cmd_ret
= CMD_UNDEFINED
;
225 if (opt_session_name
== NULL
) {
226 session_name
= get_session_name();
227 if (session_name
== NULL
) {
230 free_session_name
= true;
232 session_name
= opt_session_name
;
237 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
242 /* Open command element */
243 ret
= mi_lttng_writer_command_open(writer
,
244 mi_lttng_element_command_enable_rotation
);
249 /* Open output element */
250 ret
= mi_lttng_writer_open_element(writer
,
251 mi_lttng_element_command_output
);
257 if (!periodic_rotation
&& !size_rotation
) {
258 ERR("No session rotation schedule parameter provided.");
264 ret
= mi_lttng_writer_open_element(writer
,
265 mi_lttng_element_rotation_schedule_results
);
270 ret
= mi_lttng_writer_write_element_string(writer
,
271 mi_lttng_element_session_name
,
278 if (periodic_rotation
) {
280 * Continue processing even on error as multiple schedules can
281 * be specified at once.
283 cmd_ret
= add_schedule(session_name
,
284 LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
,
289 enum cmd_error_code tmp_ret
;
291 /* Don't overwrite cmd_ret if it already indicates an error. */
292 tmp_ret
= add_schedule(session_name
,
293 LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
,
295 cmd_ret
= cmd_ret
? cmd_ret
: tmp_ret
;
299 /* Close rotation schedule results element */
300 ret
= mi_lttng_writer_close_element(writer
);
309 /* Close output element */
310 ret
= mi_lttng_writer_close_element(writer
);
316 ret
= mi_lttng_writer_write_element_bool(writer
,
317 mi_lttng_element_command_success
,
318 cmd_ret
== CMD_SUCCESS
);
323 /* Command element close */
324 ret
= mi_lttng_writer_command_close(writer
);
331 (void) mi_lttng_writer_destroy(writer
);
333 if (free_session_name
) {