Size-based 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
38enum {
39 OPT_HELP = 1,
40 OPT_LIST_OPTIONS,
41 OPT_TIMER,
90936dcf 42 OPT_SIZE,
259c2674
JD
43};
44
45static struct poptOption long_options[] = {
46 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
47 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
48 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
49 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
50 {"timer", 0, POPT_ARG_NONE, 0, OPT_TIMER, 0, 0},
90936dcf 51 {"size", 0, POPT_ARG_NONE, 0, OPT_SIZE, 0, 0},
259c2674
JD
52 {0, 0, 0, 0, 0, 0, 0}
53};
54
90936dcf 55static int setup_rotate(char *session_name, uint64_t timer, uint64_t size)
259c2674
JD
56{
57 int ret = 0;
58 struct lttng_rotation_schedule_attr *attr = NULL;
59
60 attr = lttng_rotation_schedule_attr_create();
61 if (!attr) {
62 goto error;
63 }
64
65 if (lttng_opt_mi) {
66 /* Open rotation_schedule element */
67 ret = mi_lttng_writer_open_element(writer,
68 config_element_rotation_schedule);
69 if (ret) {
70 goto error;
71 }
72 }
73
74 ret = lttng_rotation_schedule_attr_set_session_name(attr, session_name);
75 if (ret < 0) {
76 goto error;
77 }
78
79 if (lttng_opt_mi) {
80 ret = mi_lttng_writer_write_element_string(writer,
81 mi_lttng_element_session_name, session_name);
82 if (ret) {
83 goto error;
84 }
85 }
86
87 if (timer == -1ULL) {
88 lttng_rotation_schedule_attr_set_timer_period(attr, timer);
89 MSG("Disabling rotation timer on session %s", session_name);
90 }
90936dcf
JD
91 if (size == -1ULL) {
92 lttng_rotation_schedule_attr_set_size(attr, size);
93 MSG("Disabling rotation based on size on session %s", session_name);
94 }
259c2674
JD
95
96 ret = lttng_rotation_set_schedule(attr);
97 if (ret) {
98 ERR("%s", lttng_strerror(ret));
99 if (lttng_opt_mi) {
100 ret = mi_lttng_writer_write_element_string(writer,
101 mi_lttng_element_rotate_status, "error");
102 if (ret) {
103 goto end;
104 }
105 /* Close rotation_schedule element */
106 ret = mi_lttng_writer_close_element(writer);
107 if (ret) {
108 goto end;
109 }
110 }
111 goto error;
112 }
113
114 if (lttng_opt_mi) {
115 ret = mi_lttng_writer_write_element_string(writer,
116 mi_lttng_element_rotate_status, "success");
117 if (ret) {
118 goto end;
119 }
120
121 /* Close rotation_schedule element */
122 ret = mi_lttng_writer_close_element(writer);
123 if (ret) {
124 goto end;
125 }
126 }
127
128 ret = 0;
129 goto end;
130
131error:
132 ret = -1;
133end:
134 return ret;
135}
136
137/*
138 * cmd_disable_rotation
139 *
140 * The 'enable-rotation <options>' first level command
141 */
142int cmd_disable_rotation(int argc, const char **argv)
143{
144 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
145 int popt_ret;
146 static poptContext pc;
147 char *session_name = NULL;
148 bool free_session_name = false;
90936dcf 149 uint64_t timer = 0, size = 0;
259c2674
JD
150
151 pc = poptGetContext(NULL, argc, argv, long_options, 0);
152 popt_ret = poptReadDefaultConfig(pc, 0);
153 if (popt_ret) {
154 ret = CMD_ERROR;
155 ERR("poptReadDefaultConfig");
156 goto end;
157 }
158
159 while ((opt = poptGetNextOpt(pc)) != -1) {
160 switch (opt) {
161 case OPT_HELP:
162 SHOW_HELP();
163 goto end;
164 case OPT_LIST_OPTIONS:
165 list_cmd_options(stdout, long_options);
166 goto end;
167 case OPT_TIMER:
168 timer = -1ULL;
169 break;
90936dcf
JD
170 case OPT_SIZE:
171 size = -1ULL;
172 break;
259c2674
JD
173 default:
174 ret = CMD_UNDEFINED;
175 goto end;
176 }
177 }
178
179 if (opt_session_name == NULL) {
180 session_name = get_session_name();
181 if (session_name == NULL) {
182 goto end;
183 }
184 free_session_name = true;
185 } else {
186 session_name = opt_session_name;
187 }
188
189 /* Mi check */
190 if (lttng_opt_mi) {
191 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
192 if (!writer) {
193 ret = -LTTNG_ERR_NOMEM;
194 goto end;
195 }
196
197 /* Open command element */
198 ret = mi_lttng_writer_command_open(writer,
199 mi_lttng_element_command_disable_rotation);
200 if (ret) {
201 ret = CMD_ERROR;
202 goto end;
203 }
204
205 /* Open output element */
206 ret = mi_lttng_writer_open_element(writer,
207 mi_lttng_element_command_output);
208 if (ret) {
209 ret = CMD_ERROR;
210 goto end;
211 }
212 }
213
214 /* No config options, just rotate the session now */
90936dcf
JD
215 if (timer == 0 && size == 0) {
216 ERR("Missing timer period (--timer) or size limit (--size) option");
259c2674
JD
217 success = 0;
218 command_ret = -1;
219 } else {
90936dcf 220 command_ret = setup_rotate(session_name, timer, size);
259c2674
JD
221 }
222
223 if (command_ret) {
224 ERR("%s", lttng_strerror(command_ret));
225 success = 0;
226 }
227
228 /* Mi closing */
229 if (lttng_opt_mi) {
230 /* Close output element */
231 ret = mi_lttng_writer_close_element(writer);
232 if (ret) {
233 goto end;
234 }
235
236 /* Success ? */
237 ret = mi_lttng_writer_write_element_bool(writer,
238 mi_lttng_element_command_success, success);
239 if (ret) {
240 ret = CMD_ERROR;
241 goto end;
242 }
243
244 /* Command element close */
245 ret = mi_lttng_writer_command_close(writer);
246 if (ret) {
247 ret = CMD_ERROR;
248 goto end;
249 }
250 }
251
252end:
253 /* Mi clean-up */
254 if (writer && mi_lttng_writer_destroy(writer)) {
255 /* Preserve original error code */
256 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
257 }
258
259 /* Overwrite ret if an error occurred with start_tracing */
260 ret = command_ret ? command_ret : ret;
261 poptFreeContext(pc);
262 if (free_session_name) {
263 free(session_name);
264 }
265 return ret;
266}
This page took 0.034056 seconds and 5 git commands to generate.