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 const char *schedule_type_str
[] = {
63 [LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
] = "periodic",
64 [LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
] = "size-based",
67 static enum cmd_error_code
add_schedule(const char *session_name
,
68 enum lttng_rotation_schedule_type schedule_type
, uint64_t value
)
70 enum cmd_error_code ret
= CMD_SUCCESS
;
71 struct lttng_rotation_schedule
*schedule
= NULL
;
72 enum lttng_rotation_status status
;
73 const char *schedule_type_name
;
75 switch (schedule_type
) {
76 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
77 schedule
= lttng_rotation_schedule_periodic_create();
82 status
= lttng_rotation_schedule_periodic_set_period(schedule
,
85 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
86 schedule
= lttng_rotation_schedule_size_threshold_create();
91 status
= lttng_rotation_schedule_size_threshold_set_threshold(
95 ERR("Unknown schedule type");
99 schedule_type_name
= schedule_type_str
[schedule_type
];
102 case LTTNG_ROTATION_STATUS_OK
:
104 case LTTNG_ROTATION_STATUS_INVALID
:
105 ERR("Invalid value for %s option", schedule_type_name
);
109 ERR("Unknown error occured setting %s rotation schedule",
115 status
= lttng_session_add_rotation_schedule(session_name
, schedule
);
117 case LTTNG_ROTATION_STATUS_OK
:
119 switch (schedule_type
) {
120 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
121 MSG("Enabled %s rotations every %" PRIu64
" µs on session %s",
122 schedule_type_name
, value
, session_name
);
124 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
125 MSG("Enabled %s rotations every %" PRIu64
" bytes written on session %s",
126 schedule_type_name
, value
, session_name
);
132 case LTTNG_ROTATION_STATUS_INVALID
:
133 ERR("Invalid parameter for %s rotation schedule",
137 case LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET
:
138 ERR("A %s rotation schedule is already set on session %s",
143 case LTTNG_ROTATION_STATUS_ERROR
:
145 ERR("Failed to enable %s rotation schedule on session %s",
155 mi_ret
= mi_lttng_rotation_schedule_result(writer
,
156 schedule
, ret
== CMD_SUCCESS
);
164 lttng_rotation_schedule_destroy(schedule
);
169 * cmd_enable_rotation
171 * The 'enable-rotation <options>' first level command
173 int cmd_enable_rotation(int argc
, const char **argv
)
175 int popt_ret
, opt
, ret
= 0;
176 enum cmd_error_code cmd_ret
= CMD_SUCCESS
;
177 static poptContext pc
;
178 char *session_name
= NULL
;
179 char *opt_arg
= NULL
;
180 bool free_session_name
= false;
181 uint64_t timer_us
= 0, size_bytes
= 0;
182 bool periodic_rotation
= false, size_rotation
= false;
184 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
185 popt_ret
= poptReadDefaultConfig(pc
, 0);
187 ERR("poptReadDefaultConfig");
191 while ((opt
= poptGetNextOpt(pc
)) != -1) {
196 case OPT_LIST_OPTIONS
:
197 list_cmd_options(stdout
, long_options
);
201 opt_arg
= poptGetOptArg(pc
);
202 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
203 ERR("Invalid value for --timer option: %s", opt_arg
);
206 if (utils_parse_time_suffix(opt_arg
, &timer_us
) < 0) {
207 ERR("Invalid value for --timer option: %s", opt_arg
);
210 if (periodic_rotation
) {
211 ERR("Only one periodic rotation schedule may be set on a session.");
214 periodic_rotation
= true;
218 opt_arg
= poptGetOptArg(pc
);
219 if (utils_parse_size_suffix(opt_arg
, &size_bytes
) < 0) {
220 ERR("Invalid value for --size option: %s", opt_arg
);
224 ERR("Only one size-based rotation schedule may be set on a session.");
227 size_rotation
= true;
230 cmd_ret
= CMD_UNDEFINED
;
235 if (opt_session_name
== NULL
) {
236 session_name
= get_session_name();
237 if (session_name
== NULL
) {
240 free_session_name
= true;
242 session_name
= opt_session_name
;
247 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
252 /* Open command element */
253 ret
= mi_lttng_writer_command_open(writer
,
254 mi_lttng_element_command_enable_rotation
);
259 /* Open output element */
260 ret
= mi_lttng_writer_open_element(writer
,
261 mi_lttng_element_command_output
);
267 if (!periodic_rotation
&& !size_rotation
) {
268 ERR("No session rotation schedule parameter provided.");
274 ret
= mi_lttng_writer_open_element(writer
,
275 mi_lttng_element_rotation_schedule_results
);
280 ret
= mi_lttng_writer_write_element_string(writer
,
281 mi_lttng_element_session_name
,
288 if (periodic_rotation
) {
290 * Continue processing even on error as multiple schedules can
291 * be specified at once.
293 cmd_ret
= add_schedule(session_name
,
294 LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
,
299 enum cmd_error_code tmp_ret
;
301 /* Don't overwrite cmd_ret if it already indicates an error. */
302 tmp_ret
= add_schedule(session_name
,
303 LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
,
305 cmd_ret
= cmd_ret
? cmd_ret
: tmp_ret
;
309 /* Close rotation schedule results element */
310 ret
= mi_lttng_writer_close_element(writer
);
319 /* Close output element */
320 ret
= mi_lttng_writer_close_element(writer
);
326 ret
= mi_lttng_writer_write_element_bool(writer
,
327 mi_lttng_element_command_success
,
328 cmd_ret
== CMD_SUCCESS
);
333 /* Command element close */
334 ret
= mi_lttng_writer_command_close(writer
);
341 (void) mi_lttng_writer_destroy(writer
);
343 if (free_session_name
) {