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