Fix: lttng: enable-rotation: leak of command parameter
[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 <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.hpp>
20 #include <common/mi-lttng.hpp>
21 #include <common/utils.hpp>
22
23 #include "../command.hpp"
24 #include <lttng/lttng.h>
25
26 static char *opt_session_name;
27 static struct mi_writer *writer;
28
29 #ifdef LTTNG_EMBED_HELP
30 static const char help_msg[] =
31 #include <lttng-enable-rotation.1.h>
32 ;
33 #endif
34
35 enum {
36 OPT_HELP = 1,
37 OPT_LIST_OPTIONS,
38 OPT_TIMER,
39 OPT_SIZE,
40 };
41
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},
49 {0, 0, 0, 0, 0, 0, 0}
50 };
51
52 static const char *schedule_type_str[] = {
53 "size-based",
54 "periodic",
55 };
56
57 static 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;
64
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;
71 }
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;
80 }
81 status = lttng_rotation_schedule_size_threshold_set_threshold(
82 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",
100 schedule_type_name);
101 ret = CMD_ERROR;
102 goto end;
103 }
104
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:
111 MSG("Enabled %s rotations every %" PRIu64 " %s on session %s",
112 schedule_type_name, value, USEC_UNIT, session_name);
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();
120 }
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;
140 }
141
142 if (lttng_opt_mi) {
143 int mi_ret;
144
145 mi_ret = mi_lttng_rotation_schedule_result(writer,
146 schedule, ret == CMD_SUCCESS);
147 if (mi_ret < 0) {
148 ret = CMD_ERROR;
149 goto end;
150 }
151 }
152
153 end:
154 lttng_rotation_schedule_destroy(schedule);
155 return ret;
156 }
157
158 /*
159 * cmd_enable_rotation
160 *
161 * The 'enable-rotation <options>' first level command
162 */
163 int cmd_enable_rotation(int argc, const char **argv)
164 {
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;
173
174 pc = poptGetContext(NULL, argc, argv, long_options, 0);
175 popt_ret = poptReadDefaultConfig(pc, 0);
176 if (popt_ret) {
177 ERR("poptReadDefaultConfig");
178 goto error;
179 }
180
181 while ((opt = poptGetNextOpt(pc)) != -1) {
182 if (opt_arg) {
183 free(opt_arg);
184 opt_arg = nullptr;
185 }
186 switch (opt) {
187 case OPT_HELP:
188 SHOW_HELP();
189 goto end;
190 case OPT_LIST_OPTIONS:
191 list_cmd_options(stdout, long_options);
192 goto end;
193 case OPT_TIMER:
194 errno = 0;
195 opt_arg = poptGetOptArg(pc);
196 if (errno != 0 || !isdigit(opt_arg[0])) {
197 ERR("Invalid value for --timer option: %s", opt_arg);
198 goto error;
199 }
200 if (utils_parse_time_suffix(opt_arg, &timer_us) < 0) {
201 ERR("Invalid value for --timer option: %s", opt_arg);
202 goto error;
203 }
204 if (periodic_rotation) {
205 ERR("Only one periodic rotation schedule may be set on a session.");
206 goto error;
207 }
208 periodic_rotation = true;
209 break;
210 case OPT_SIZE:
211 errno = 0;
212 opt_arg = poptGetOptArg(pc);
213 if (utils_parse_size_suffix(opt_arg, &size_bytes) < 0) {
214 ERR("Invalid value for --size option: %s", opt_arg);
215 goto error;
216 }
217 if (size_rotation) {
218 ERR("Only one size-based rotation schedule may be set on a session.");
219 goto error;
220 }
221 size_rotation = true;
222 break;
223 default:
224 cmd_ret = CMD_UNDEFINED;
225 goto end;
226 }
227 }
228
229 if (opt_session_name == NULL) {
230 session_name = get_session_name();
231 if (session_name == NULL) {
232 goto error;
233 }
234 free_session_name = true;
235 } else {
236 session_name = opt_session_name;
237 }
238
239 /* Mi check */
240 if (lttng_opt_mi) {
241 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
242 if (!writer) {
243 goto error;
244 }
245
246 /* Open command element */
247 ret = mi_lttng_writer_command_open(writer,
248 mi_lttng_element_command_enable_rotation);
249 if (ret) {
250 goto error;
251 }
252
253 /* Open output element */
254 ret = mi_lttng_writer_open_element(writer,
255 mi_lttng_element_command_output);
256 if (ret) {
257 goto error;
258 }
259 }
260
261 if (!periodic_rotation && !size_rotation) {
262 ERR("No session rotation schedule parameter provided.");
263 cmd_ret = CMD_ERROR;
264 goto close_command;
265 }
266
267 if (lttng_opt_mi) {
268 ret = mi_lttng_writer_open_element(writer,
269 mi_lttng_element_rotation_schedule_results);
270 if (ret) {
271 goto error;
272 }
273
274 ret = mi_lttng_writer_write_element_string(writer,
275 mi_lttng_element_session_name,
276 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 = add_schedule(session_name,
288 LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC,
289 timer_us);
290 }
291
292 if (size_rotation) {
293 enum cmd_error_code tmp_ret;
294
295 /* Don't overwrite cmd_ret if it already indicates an error. */
296 tmp_ret = add_schedule(session_name,
297 LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD,
298 size_bytes);
299 cmd_ret = cmd_ret ? cmd_ret : tmp_ret;
300 }
301
302 if (lttng_opt_mi) {
303 /* Close rotation schedule results element */
304 ret = mi_lttng_writer_close_element(writer);
305 if (ret) {
306 goto error;
307 }
308 }
309
310 close_command:
311 /* Mi closing */
312 if (lttng_opt_mi) {
313 /* Close output element */
314 ret = mi_lttng_writer_close_element(writer);
315 if (ret) {
316 goto error;
317 }
318
319 /* Success ? */
320 ret = mi_lttng_writer_write_element_bool(writer,
321 mi_lttng_element_command_success,
322 cmd_ret == CMD_SUCCESS);
323 if (ret) {
324 goto error;
325 }
326
327 /* Command element close */
328 ret = mi_lttng_writer_command_close(writer);
329 if (ret) {
330 goto error;
331 }
332 }
333
334 end:
335 (void) mi_lttng_writer_destroy(writer);
336 poptFreeContext(pc);
337 if (free_session_name) {
338 free(session_name);
339 }
340 free(opt_arg);
341 return cmd_ret;
342
343 error:
344 cmd_ret = CMD_ERROR;
345 goto end;
346 }
This page took 0.039875 seconds and 4 git commands to generate.