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