Fix: ret variable is used instead of cmd_ret in disable-rotation
[lttng-tools.git] / src / bin / lttng / commands / disable_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
32#include "../command.h"
33#include <lttng/rotation.h>
34
35static char *opt_session_name;
36static struct mi_writer *writer;
37
3ae5c539
JD
38#ifdef LTTNG_EMBED_HELP
39static const char help_msg[] =
40#include <lttng-disable-rotation.1.h>
41;
42#endif
43
259c2674
JD
44enum {
45 OPT_HELP = 1,
46 OPT_LIST_OPTIONS,
47 OPT_TIMER,
90936dcf 48 OPT_SIZE,
259c2674
JD
49};
50
51static struct poptOption long_options[] = {
52 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
66ea93b1
JG
53 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
54 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
55 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
259c2674 56 {"timer", 0, POPT_ARG_NONE, 0, OPT_TIMER, 0, 0},
90936dcf 57 {"size", 0, POPT_ARG_NONE, 0, OPT_SIZE, 0, 0},
259c2674
JD
58 {0, 0, 0, 0, 0, 0, 0}
59};
60
66ea93b1
JG
61static const char *schedule_type_str[] = {
62 [LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC] = "periodic",
63 [LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD] = "size-based",
64};
65
66static const struct lttng_rotation_schedule *get_schedule(
67 const char *session_name,
68 const struct lttng_rotation_schedules *schedules,
69 enum lttng_rotation_schedule_type schedule_type)
259c2674 70{
66ea93b1
JG
71 unsigned int count, i;
72 enum lttng_rotation_status status;
73 const struct lttng_rotation_schedule *ret = NULL;
259c2674 74
66ea93b1
JG
75 status = lttng_rotation_schedules_get_count(schedules, &count);
76 if (status != LTTNG_ROTATION_STATUS_OK) {
77 ERR("Unable to determine the number of rotation schedules of session %s",
78 session_name);
79 goto end;
259c2674
JD
80 }
81
66ea93b1
JG
82 for (i = 0; i < count; i++) {
83 const struct lttng_rotation_schedule *schedule = NULL;
84
85 schedule = lttng_rotation_schedules_get_at_index(schedules, i);
86 if (!schedule) {
87 ERR("Unable to retrieve rotation schedule at index %u",
88 i);
89 goto end;
259c2674 90 }
259c2674 91
66ea93b1
JG
92 if (lttng_rotation_schedule_get_type(schedule) ==
93 schedule_type) {
94 ret = schedule;
95 break;
259c2674
JD
96 }
97 }
98
66ea93b1
JG
99 if (!ret) {
100 ERR("No %s rotation schedule active on session %s",
101 schedule_type_str[schedule_type], session_name);
259c2674 102 }
66ea93b1
JG
103end:
104 return ret;
105}
106
107static struct lttng_rotation_schedule *create_empty_schedule(
108 enum lttng_rotation_schedule_type type)
109{
110 struct lttng_rotation_schedule *schedule = NULL;
111
112 switch (type) {
113 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
114 schedule = lttng_rotation_schedule_periodic_create();
115 break;
116 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
117 schedule = lttng_rotation_schedule_size_threshold_create();
118 break;
119 default:
120 abort();
90936dcf 121 }
66ea93b1
JG
122 return schedule;
123}
259c2674 124
66ea93b1
JG
125static enum cmd_error_code remove_schedule(const char *session_name,
126 enum lttng_rotation_schedule_type schedule_type)
127{
128 enum cmd_error_code cmd_ret;
129 int ret;
130 const struct lttng_rotation_schedule *schedule = NULL;
131 struct lttng_rotation_schedules *schedules = NULL;
132 enum lttng_rotation_status status;
133 const char *schedule_type_name;
134 struct lttng_rotation_schedule *empty_schedule = NULL;
135
136 switch (schedule_type) {
137 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
138 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
139 break;
140 default:
141 ERR("Unknown schedule type");
142 abort();
143 }
144
145 schedule_type_name = schedule_type_str[schedule_type];
146
147 ret = lttng_session_list_rotation_schedules(session_name, &schedules);
148 if (ret != LTTNG_OK) {
149 ERR("Failed to list rotation schedules of session %s",
150 session_name);
259c2674
JD
151 goto error;
152 }
153
66ea93b1
JG
154 schedule = get_schedule(session_name, schedules, schedule_type);
155 if (!schedule) {
156 cmd_ret = CMD_ERROR;
157 /*
158 * get_schedule() logs its own errors.
159 * A temporaty schedule is created to serialize an MI rotation
160 * schedule descriptor of the appropriate type that has no
161 * attributes set.
162 */
163 empty_schedule = create_empty_schedule(schedule_type);
164 if (!empty_schedule) {
165 goto error;
259c2674 166 }
66ea93b1
JG
167 goto skip_removal;
168 }
259c2674 169
66ea93b1
JG
170 status = lttng_session_remove_rotation_schedule(session_name, schedule);
171 switch (status) {
172 case LTTNG_ROTATION_STATUS_OK:
173 MSG("Disabled %s rotation on session %s",
174 schedule_type_name, session_name);
175 cmd_ret = CMD_SUCCESS;
176 break;
177 case LTTNG_ROTATION_STATUS_SCHEDULE_NOT_SET:
178 ERR("No %s rotation schedule set on session %s",
179 schedule_type_name,
180 session_name);
181 cmd_ret = CMD_ERROR;
182 break;
183 case LTTNG_ROTATION_STATUS_ERROR:
184 case LTTNG_ROTATION_STATUS_INVALID:
185 default:
186 ERR("Failed to disable %s rotation schedule on session %s",
187 schedule_type_name, session_name);
188 cmd_ret = CMD_ERROR;
189 break;
259c2674
JD
190 }
191
66ea93b1
JG
192skip_removal:
193 if (lttng_opt_mi) {
194 ret = mi_lttng_rotation_schedule_result(writer,
195 schedule ? schedule : empty_schedule,
196 cmd_ret == CMD_SUCCESS);
197 if (ret < 0) {
198 goto error;
199 }
200 }
259c2674 201
259c2674 202end:
66ea93b1
JG
203 lttng_rotation_schedules_destroy(schedules);
204 lttng_rotation_schedule_destroy(empty_schedule);
205 return cmd_ret;
206error:
207 cmd_ret = CMD_ERROR;
208 goto end;
259c2674
JD
209}
210
211/*
212 * cmd_disable_rotation
213 *
66ea93b1 214 * The 'disable-rotation <options>' first level command
259c2674
JD
215 */
216int cmd_disable_rotation(int argc, const char **argv)
217{
66ea93b1
JG
218 int popt_ret, opt, ret = 0;
219 enum cmd_error_code cmd_ret = CMD_SUCCESS;
259c2674
JD
220 static poptContext pc;
221 char *session_name = NULL;
222 bool free_session_name = false;
66ea93b1 223 bool periodic_rotation = false, size_rotation = false;
259c2674
JD
224
225 pc = poptGetContext(NULL, argc, argv, long_options, 0);
226 popt_ret = poptReadDefaultConfig(pc, 0);
227 if (popt_ret) {
62140c46 228 cmd_ret = CMD_ERROR;
259c2674
JD
229 ERR("poptReadDefaultConfig");
230 goto end;
231 }
232
233 while ((opt = poptGetNextOpt(pc)) != -1) {
234 switch (opt) {
235 case OPT_HELP:
236 SHOW_HELP();
237 goto end;
238 case OPT_LIST_OPTIONS:
239 list_cmd_options(stdout, long_options);
240 goto end;
241 case OPT_TIMER:
66ea93b1 242 periodic_rotation = true;
259c2674 243 break;
90936dcf 244 case OPT_SIZE:
66ea93b1 245 size_rotation = true;
90936dcf 246 break;
259c2674 247 default:
62140c46 248 cmd_ret = CMD_UNDEFINED;
259c2674
JD
249 goto end;
250 }
251 }
252
253 if (opt_session_name == NULL) {
254 session_name = get_session_name();
255 if (session_name == NULL) {
66ea93b1 256 goto error;
259c2674
JD
257 }
258 free_session_name = true;
259 } else {
260 session_name = opt_session_name;
261 }
262
263 /* Mi check */
264 if (lttng_opt_mi) {
265 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
266 if (!writer) {
66ea93b1 267 goto error;
259c2674
JD
268 }
269
270 /* Open command element */
271 ret = mi_lttng_writer_command_open(writer,
272 mi_lttng_element_command_disable_rotation);
273 if (ret) {
66ea93b1 274 goto error;
259c2674
JD
275 }
276
277 /* Open output element */
278 ret = mi_lttng_writer_open_element(writer,
279 mi_lttng_element_command_output);
280 if (ret) {
66ea93b1 281 goto error;
259c2674
JD
282 }
283 }
284
66ea93b1
JG
285 if (!periodic_rotation && !size_rotation) {
286 ERR("No session rotation schedule type provided.");
287 cmd_ret = CMD_ERROR;
288 goto close_command;
259c2674
JD
289 }
290
66ea93b1
JG
291 if (lttng_opt_mi) {
292 ret = mi_lttng_writer_open_element(writer,
293 mi_lttng_element_rotation_schedule_results);
294 if (ret) {
295 goto error;
296 }
297
298 ret = mi_lttng_writer_write_element_string(writer,
299 mi_lttng_element_session_name,
300 session_name);
301 if (ret) {
302 goto error;
303 }
259c2674
JD
304 }
305
66ea93b1
JG
306 if (periodic_rotation) {
307 /*
308 * Continue processing even on error as multiple schedules can
309 * be specified at once.
310 */
311 cmd_ret = remove_schedule(session_name,
312 LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC);
313 }
314
315 if (size_rotation) {
a14a81cb 316 enum cmd_error_code tmp_ret;
66ea93b1
JG
317
318 /* Don't overwrite cmd_ret if it already indicates an error. */
319 tmp_ret = remove_schedule(session_name,
320 LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD);
321 cmd_ret = cmd_ret ? cmd_ret : tmp_ret;
322 }
323
324 if (lttng_opt_mi) {
325 /* Close rotation schedule results element */
326 ret = mi_lttng_writer_close_element(writer);
327 if (ret) {
328 goto error;
329 }
330 }
331
332close_command:
259c2674
JD
333 /* Mi closing */
334 if (lttng_opt_mi) {
335 /* Close output element */
336 ret = mi_lttng_writer_close_element(writer);
337 if (ret) {
66ea93b1 338 goto error;
259c2674
JD
339 }
340
341 /* Success ? */
342 ret = mi_lttng_writer_write_element_bool(writer,
66ea93b1
JG
343 mi_lttng_element_command_success,
344 cmd_ret == CMD_SUCCESS);
259c2674 345 if (ret) {
66ea93b1 346 goto error;
259c2674
JD
347 }
348
349 /* Command element close */
350 ret = mi_lttng_writer_command_close(writer);
351 if (ret) {
66ea93b1 352 goto error;
259c2674
JD
353 }
354 }
355
356end:
66ea93b1 357 (void) mi_lttng_writer_destroy(writer);
259c2674
JD
358 poptFreeContext(pc);
359 if (free_session_name) {
360 free(session_name);
361 }
66ea93b1
JG
362 return cmd_ret;
363error:
364 cmd_ret = CMD_ERROR;
365 goto end;
259c2674 366}
This page took 0.038901 seconds and 4 git commands to generate.