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