Fix: destroy schedule attr
[lttng-tools.git] / src / bin / lttng / commands / enable_rotation.c
CommitLineData
259c2674
JD
1/*
2 * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com>
3 *
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.
7 *
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.
12 *
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.
16 */
17
18#define _LGPL_SOURCE
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#include <inttypes.h>
27#include <ctype.h>
28
29#include <common/sessiond-comm/sessiond-comm.h>
30#include <common/mi-lttng.h>
31#include <common/utils.h>
32
33#include "../command.h"
34#include <lttng/rotation.h>
35
36static char *opt_session_name;
37static struct mi_writer *writer;
38
3ae5c539
JD
39#ifdef LTTNG_EMBED_HELP
40static const char help_msg[] =
41#include <lttng-enable-rotation.1.h>
42;
43#endif
44
259c2674
JD
45enum {
46 OPT_HELP = 1,
47 OPT_LIST_OPTIONS,
48 OPT_TIMER,
90936dcf 49 OPT_SIZE,
259c2674
JD
50};
51
52static 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},
90936dcf 58 {"size", 0, POPT_ARG_INT, 0, OPT_SIZE, 0, 0},
259c2674
JD
59 {0, 0, 0, 0, 0, 0, 0}
60};
61
90936dcf 62static int setup_rotate(char *session_name, uint64_t timer, uint64_t size)
259c2674
JD
63{
64 int ret = 0;
65 struct lttng_rotation_schedule_attr *attr = NULL;
66
67 attr = lttng_rotation_schedule_attr_create();
68 if (!attr) {
69 goto error;
70 }
71
72 ret = lttng_rotation_schedule_attr_set_session_name(attr, session_name);
73 if (ret < 0) {
74 goto error;
75 }
76
77 if (lttng_opt_mi) {
78 /* Open rotation_schedule element */
79 ret = mi_lttng_writer_open_element(writer,
80 config_element_rotation_schedule);
81 if (ret) {
82 goto error;
83 }
84
85 ret = mi_lttng_writer_write_element_string(writer,
86 mi_lttng_element_session_name, session_name);
87 if (ret) {
88 goto error;
89 }
90 }
91
92 if (timer) {
93 lttng_rotation_schedule_attr_set_timer_period(attr, timer);
94 MSG("Configuring session %s to rotate every %" PRIu64 " us",
95 session_name, timer);
96 if (lttng_opt_mi) {
97 ret = mi_lttng_writer_write_element_unsigned_int(writer,
98 config_element_rotation_timer_interval, timer);
99 if (ret) {
100 goto end;
101 }
102 }
103 }
90936dcf
JD
104 if (size) {
105 lttng_rotation_schedule_attr_set_size(attr, size);
106 MSG("Configuring session %s to rotate every %" PRIu64 " bytes written",
107 session_name, size);
108 if (lttng_opt_mi) {
109 ret = mi_lttng_writer_write_element_unsigned_int(writer,
110 config_element_rotation_size, size);
111 if (ret) {
112 goto end;
113 }
114 }
115 }
259c2674
JD
116
117 ret = lttng_rotation_set_schedule(attr);
118 if (ret) {
119 ERR("%s", lttng_strerror(ret));
120 if (lttng_opt_mi) {
121 ret = mi_lttng_writer_write_element_string(writer,
122 mi_lttng_element_rotate_status, "error");
123 if (ret) {
124 goto end;
125 }
126 /* Close rotation_schedule element */
127 ret = mi_lttng_writer_close_element(writer);
128 if (ret) {
129 goto end;
130 }
131 }
132 goto error;
133 }
134
135 if (lttng_opt_mi) {
136 ret = mi_lttng_writer_write_element_string(writer,
137 mi_lttng_element_rotate_status, "success");
138 if (ret) {
139 goto end;
140 }
141
142 /* Close rotation_schedule element */
143 ret = mi_lttng_writer_close_element(writer);
144 if (ret) {
145 goto end;
146 }
147 }
148
149 ret = 0;
150 goto end;
151
152error:
153 ret = -1;
154end:
4b7e956f 155 lttng_rotation_schedule_attr_destroy(attr);
259c2674
JD
156 return ret;
157}
158
159/*
160 * cmd_enable_rotation
161 *
162 * The 'enable-rotation <options>' first level command
163 */
164int cmd_enable_rotation(int argc, const char **argv)
165{
166 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
167 int popt_ret;
168 static poptContext pc;
169 char *session_name = NULL;
170 char *opt_arg = NULL;
259c2674 171 bool free_session_name = false;
90936dcf 172 uint64_t timer = 0, size = 0;
259c2674
JD
173
174 pc = poptGetContext(NULL, argc, argv, long_options, 0);
175 popt_ret = poptReadDefaultConfig(pc, 0);
176 if (popt_ret) {
177 ret = CMD_ERROR;
178 ERR("poptReadDefaultConfig");
179 goto end;
180 }
181
182 while ((opt = poptGetNextOpt(pc)) != -1) {
183 switch (opt) {
184 case OPT_HELP:
185 SHOW_HELP();
186 goto end;
187 case OPT_LIST_OPTIONS:
188 list_cmd_options(stdout, long_options);
189 goto end;
190 case OPT_TIMER:
191 errno = 0;
192 opt_arg = poptGetOptArg(pc);
193 if (errno != 0 || !isdigit(opt_arg[0])) {
194 ERR("Wrong value for --timer option: %s", opt_arg);
195 ret = CMD_ERROR;
196 goto end;
197 }
198 if (utils_parse_time_suffix(opt_arg, &timer) < 0 || timer == 0) {
199 ERR("Wrong value for --timer option: %s", opt_arg);
200 ret = CMD_ERROR;
201 goto end;
202 }
203 DBG("Rotation timer set to %" PRIu64, timer);
204 break;
90936dcf
JD
205 case OPT_SIZE:
206 errno = 0;
207 opt_arg = poptGetOptArg(pc);
208 if (utils_parse_size_suffix(opt_arg, &size) < 0 || !size) {
209 ERR("Wrong value for --size option: %s", opt_arg);
210 ret = CMD_ERROR;
211 goto end;
212 }
213 DBG("Rotation size set to %" PRIu64, size);
214 break;
259c2674
JD
215 default:
216 ret = CMD_UNDEFINED;
217 goto end;
218 }
219 }
220
221 if (opt_session_name == NULL) {
222 session_name = get_session_name();
223 if (session_name == NULL) {
224 goto end;
225 }
226 free_session_name = true;
227 } else {
228 session_name = opt_session_name;
229 }
230
231 /* Mi check */
232 if (lttng_opt_mi) {
233 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
234 if (!writer) {
235 ret = -LTTNG_ERR_NOMEM;
236 goto end;
237 }
238
239 /* Open command element */
240 ret = mi_lttng_writer_command_open(writer,
241 mi_lttng_element_command_enable_rotation);
242 if (ret) {
243 ret = CMD_ERROR;
244 goto end;
245 }
246
247 /* Open output element */
248 ret = mi_lttng_writer_open_element(writer,
249 mi_lttng_element_command_output);
250 if (ret) {
251 ret = CMD_ERROR;
252 goto end;
253 }
254 }
255
256 /* No config options, just rotate the session now */
90936dcf
JD
257 if (timer == 0 && size == 0) {
258 ERR("No timer or size given");
259c2674
JD
259 success = 0;
260 command_ret = -1;
261 } else {
90936dcf 262 command_ret = setup_rotate(session_name, timer, size);
259c2674
JD
263 }
264
265 if (command_ret) {
266 ERR("%s", lttng_strerror(command_ret));
267 success = 0;
268 }
269
270 /* Mi closing */
271 if (lttng_opt_mi) {
272 /* Close output element */
273 ret = mi_lttng_writer_close_element(writer);
274 if (ret) {
275 goto end;
276 }
277 /* Success ? */
278 ret = mi_lttng_writer_write_element_bool(writer,
279 mi_lttng_element_command_success, success);
280 if (ret) {
281 ret = CMD_ERROR;
282 goto end;
283 }
284
285 /* Command element close */
286 ret = mi_lttng_writer_command_close(writer);
287 if (ret) {
288 ret = CMD_ERROR;
289 goto end;
290 }
291 }
292
293end:
294 /* Mi clean-up */
295 if (writer && mi_lttng_writer_destroy(writer)) {
296 /* Preserve original error code */
297 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
298 }
299
300 /* Overwrite ret if an error occurred with start_tracing */
301 ret = command_ret ? command_ret : ret;
302 poptFreeContext(pc);
303 if (free_session_name) {
304 free(session_name);
305 }
306 return ret;
307}
This page took 0.034099 seconds and 4 git commands to generate.