Move to kernel style SPDX license identifiers
[lttng-tools.git] / src / bin / lttng / commands / enable_rotation.c
CommitLineData
259c2674 1/*
ab5be9fa 2 * Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com>
259c2674 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
259c2674 5 *
259c2674
JD
6 */
7
8#define _LGPL_SOURCE
9#include <popt.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <sys/stat.h>
14#include <sys/types.h>
15#include <unistd.h>
16#include <inttypes.h>
17#include <ctype.h>
18
19#include <common/sessiond-comm/sessiond-comm.h>
20#include <common/mi-lttng.h>
21#include <common/utils.h>
22
23#include "../command.h"
24#include <lttng/rotation.h>
25
26static char *opt_session_name;
27static struct mi_writer *writer;
28
3ae5c539
JD
29#ifdef LTTNG_EMBED_HELP
30static const char help_msg[] =
31#include <lttng-enable-rotation.1.h>
32;
33#endif
34
259c2674
JD
35enum {
36 OPT_HELP = 1,
37 OPT_LIST_OPTIONS,
38 OPT_TIMER,
90936dcf 39 OPT_SIZE,
259c2674
JD
40};
41
42static 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},
90936dcf 48 {"size", 0, POPT_ARG_INT, 0, OPT_SIZE, 0, 0},
259c2674
JD
49 {0, 0, 0, 0, 0, 0, 0}
50};
51
66ea93b1
JG
52static const char *schedule_type_str[] = {
53 [LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC] = "periodic",
54 [LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD] = "size-based",
55};
259c2674 56
66ea93b1
JG
57static enum cmd_error_code add_schedule(const char *session_name,
58 enum lttng_rotation_schedule_type schedule_type, uint64_t value)
59{
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;
259c2674 64
66ea93b1
JG
65 switch (schedule_type) {
66 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
67 schedule = lttng_rotation_schedule_periodic_create();
68 if (!schedule) {
69 ret = CMD_ERROR;
70 goto end;
259c2674 71 }
66ea93b1
JG
72 status = lttng_rotation_schedule_periodic_set_period(schedule,
73 value);
74 break;
75 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
76 schedule = lttng_rotation_schedule_size_threshold_create();
77 if (!schedule) {
78 ret = CMD_ERROR;
79 goto end;
259c2674 80 }
66ea93b1
JG
81 status = lttng_rotation_schedule_size_threshold_set_threshold(
82 schedule, value);
83 break;
84 default:
85 ERR("Unknown schedule type");
86 abort();
259c2674
JD
87 }
88
66ea93b1
JG
89 schedule_type_name = schedule_type_str[schedule_type];
90
91 switch (status) {
92 case LTTNG_ROTATION_STATUS_OK:
93 break;
94 case LTTNG_ROTATION_STATUS_INVALID:
95 ERR("Invalid value for %s option", schedule_type_name);
96 ret = CMD_ERROR;
97 goto end;
98 default:
a9577b76 99 ERR("Unknown error occurred setting %s rotation schedule",
66ea93b1
JG
100 schedule_type_name);
101 ret = CMD_ERROR;
102 goto end;
90936dcf 103 }
259c2674 104
66ea93b1
JG
105 status = lttng_session_add_rotation_schedule(session_name, schedule);
106 switch (status) {
107 case LTTNG_ROTATION_STATUS_OK:
108 ret = CMD_SUCCESS;
109 switch (schedule_type) {
110 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
2a1135fa
JG
111 MSG("Enabled %s rotations every %" PRIu64 " %s on session %s",
112 schedule_type_name, value, USEC_UNIT, session_name);
66ea93b1
JG
113 break;
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);
117 break;
118 default:
119 abort();
259c2674 120 }
66ea93b1
JG
121 break;
122 case LTTNG_ROTATION_STATUS_INVALID:
123 ERR("Invalid parameter for %s rotation schedule",
124 schedule_type_name);
125 ret = CMD_ERROR;
126 break;
127 case LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET:
128 ERR("A %s rotation schedule is already set on session %s",
129 schedule_type_name,
130 session_name);
131 ret = CMD_ERROR;
132 break;
133 case LTTNG_ROTATION_STATUS_ERROR:
134 default:
135 ERR("Failed to enable %s rotation schedule on session %s",
136 schedule_type_name,
137 session_name);
138 ret = CMD_ERROR;
139 break;
259c2674
JD
140 }
141
142 if (lttng_opt_mi) {
66ea93b1 143 int mi_ret;
259c2674 144
66ea93b1
JG
145 mi_ret = mi_lttng_rotation_schedule_result(writer,
146 schedule, ret == CMD_SUCCESS);
147 if (mi_ret < 0) {
148 ret = CMD_ERROR;
259c2674
JD
149 goto end;
150 }
151 }
152
259c2674 153end:
66ea93b1 154 lttng_rotation_schedule_destroy(schedule);
259c2674
JD
155 return ret;
156}
157
158/*
159 * cmd_enable_rotation
160 *
161 * The 'enable-rotation <options>' first level command
162 */
163int cmd_enable_rotation(int argc, const char **argv)
164{
66ea93b1
JG
165 int popt_ret, opt, ret = 0;
166 enum cmd_error_code cmd_ret = CMD_SUCCESS;
259c2674
JD
167 static poptContext pc;
168 char *session_name = NULL;
169 char *opt_arg = NULL;
259c2674 170 bool free_session_name = false;
66ea93b1
JG
171 uint64_t timer_us = 0, size_bytes = 0;
172 bool periodic_rotation = false, size_rotation = false;
259c2674
JD
173
174 pc = poptGetContext(NULL, argc, argv, long_options, 0);
175 popt_ret = poptReadDefaultConfig(pc, 0);
176 if (popt_ret) {
259c2674 177 ERR("poptReadDefaultConfig");
66ea93b1 178 goto error;
259c2674
JD
179 }
180
181 while ((opt = poptGetNextOpt(pc)) != -1) {
182 switch (opt) {
183 case OPT_HELP:
184 SHOW_HELP();
185 goto end;
186 case OPT_LIST_OPTIONS:
187 list_cmd_options(stdout, long_options);
188 goto end;
189 case OPT_TIMER:
190 errno = 0;
191 opt_arg = poptGetOptArg(pc);
192 if (errno != 0 || !isdigit(opt_arg[0])) {
66ea93b1
JG
193 ERR("Invalid value for --timer option: %s", opt_arg);
194 goto error;
195 }
196 if (utils_parse_time_suffix(opt_arg, &timer_us) < 0) {
197 ERR("Invalid value for --timer option: %s", opt_arg);
198 goto error;
259c2674 199 }
66ea93b1
JG
200 if (periodic_rotation) {
201 ERR("Only one periodic rotation schedule may be set on a session.");
202 goto error;
259c2674 203 }
66ea93b1 204 periodic_rotation = true;
259c2674 205 break;
90936dcf
JD
206 case OPT_SIZE:
207 errno = 0;
208 opt_arg = poptGetOptArg(pc);
66ea93b1
JG
209 if (utils_parse_size_suffix(opt_arg, &size_bytes) < 0) {
210 ERR("Invalid value for --size option: %s", opt_arg);
211 goto error;
90936dcf 212 }
66ea93b1
JG
213 if (size_rotation) {
214 ERR("Only one size-based rotation schedule may be set on a session.");
215 goto error;
216 }
217 size_rotation = true;
90936dcf 218 break;
259c2674 219 default:
66ea93b1 220 cmd_ret = CMD_UNDEFINED;
259c2674
JD
221 goto end;
222 }
223 }
224
225 if (opt_session_name == NULL) {
226 session_name = get_session_name();
227 if (session_name == NULL) {
66ea93b1 228 goto error;
259c2674
JD
229 }
230 free_session_name = true;
231 } else {
232 session_name = opt_session_name;
233 }
234
235 /* Mi check */
236 if (lttng_opt_mi) {
237 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
238 if (!writer) {
66ea93b1 239 goto error;
259c2674
JD
240 }
241
242 /* Open command element */
243 ret = mi_lttng_writer_command_open(writer,
244 mi_lttng_element_command_enable_rotation);
245 if (ret) {
66ea93b1 246 goto error;
259c2674
JD
247 }
248
249 /* Open output element */
250 ret = mi_lttng_writer_open_element(writer,
251 mi_lttng_element_command_output);
252 if (ret) {
66ea93b1 253 goto error;
259c2674
JD
254 }
255 }
256
66ea93b1
JG
257 if (!periodic_rotation && !size_rotation) {
258 ERR("No session rotation schedule parameter provided.");
259 cmd_ret = CMD_ERROR;
260 goto close_command;
259c2674
JD
261 }
262
66ea93b1
JG
263 if (lttng_opt_mi) {
264 ret = mi_lttng_writer_open_element(writer,
265 mi_lttng_element_rotation_schedule_results);
266 if (ret) {
267 goto error;
268 }
269
270 ret = mi_lttng_writer_write_element_string(writer,
271 mi_lttng_element_session_name,
272 session_name);
273 if (ret) {
274 goto error;
275 }
259c2674
JD
276 }
277
66ea93b1
JG
278 if (periodic_rotation) {
279 /*
280 * Continue processing even on error as multiple schedules can
281 * be specified at once.
282 */
283 cmd_ret = add_schedule(session_name,
284 LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC,
285 timer_us);
286 }
287
288 if (size_rotation) {
a14a81cb 289 enum cmd_error_code tmp_ret;
66ea93b1
JG
290
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,
294 size_bytes);
295 cmd_ret = cmd_ret ? cmd_ret : tmp_ret;
296 }
297
298 if (lttng_opt_mi) {
299 /* Close rotation schedule results element */
300 ret = mi_lttng_writer_close_element(writer);
301 if (ret) {
302 goto error;
303 }
304 }
305
306close_command:
259c2674
JD
307 /* Mi closing */
308 if (lttng_opt_mi) {
66ea93b1 309 /* Close output element */
259c2674
JD
310 ret = mi_lttng_writer_close_element(writer);
311 if (ret) {
66ea93b1 312 goto error;
259c2674 313 }
66ea93b1 314
259c2674
JD
315 /* Success ? */
316 ret = mi_lttng_writer_write_element_bool(writer,
66ea93b1
JG
317 mi_lttng_element_command_success,
318 cmd_ret == CMD_SUCCESS);
259c2674 319 if (ret) {
66ea93b1 320 goto error;
259c2674
JD
321 }
322
323 /* Command element close */
324 ret = mi_lttng_writer_command_close(writer);
325 if (ret) {
66ea93b1 326 goto error;
259c2674
JD
327 }
328 }
329
330end:
66ea93b1 331 (void) mi_lttng_writer_destroy(writer);
259c2674
JD
332 poptFreeContext(pc);
333 if (free_session_name) {
334 free(session_name);
335 }
66ea93b1
JG
336 return cmd_ret;
337
338error:
339 cmd_ret = CMD_ERROR;
340 goto end;
259c2674 341}
This page took 0.045355 seconds and 4 git commands to generate.