Fix: add missing includes for embedded help
[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 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 }
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 }
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
152 error:
153 ret = -1;
154 end:
155 return ret;
156 }
157
158 /*
159 * cmd_enable_rotation
160 *
161 * The 'enable-rotation <options>' first level command
162 */
163 int cmd_enable_rotation(int argc, const char **argv)
164 {
165 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
166 int popt_ret;
167 static poptContext pc;
168 char *session_name = NULL;
169 char *opt_arg = NULL;
170 bool free_session_name = false;
171 uint64_t timer = 0, size = 0;
172
173 pc = poptGetContext(NULL, argc, argv, long_options, 0);
174 popt_ret = poptReadDefaultConfig(pc, 0);
175 if (popt_ret) {
176 ret = CMD_ERROR;
177 ERR("poptReadDefaultConfig");
178 goto end;
179 }
180
181 while ((opt = poptGetNextOpt(pc)) != -1) {
182 switch (opt) {
183 case OPT_HELP:
184 SHOW_HELP();
185 goto end;
186 case OPT_LIST_OPTIONS:
187 list_cmd_options(stdout, long_options);
188 goto end;
189 case OPT_TIMER:
190 errno = 0;
191 opt_arg = poptGetOptArg(pc);
192 if (errno != 0 || !isdigit(opt_arg[0])) {
193 ERR("Wrong value for --timer option: %s", opt_arg);
194 ret = CMD_ERROR;
195 goto end;
196 }
197 if (utils_parse_time_suffix(opt_arg, &timer) < 0 || timer == 0) {
198 ERR("Wrong value for --timer option: %s", opt_arg);
199 ret = CMD_ERROR;
200 goto end;
201 }
202 DBG("Rotation timer set to %" PRIu64, timer);
203 break;
204 case OPT_SIZE:
205 errno = 0;
206 opt_arg = poptGetOptArg(pc);
207 if (utils_parse_size_suffix(opt_arg, &size) < 0 || !size) {
208 ERR("Wrong value for --size option: %s", opt_arg);
209 ret = CMD_ERROR;
210 goto end;
211 }
212 DBG("Rotation size set to %" PRIu64, size);
213 break;
214 default:
215 ret = CMD_UNDEFINED;
216 goto end;
217 }
218 }
219
220 if (opt_session_name == NULL) {
221 session_name = get_session_name();
222 if (session_name == NULL) {
223 goto end;
224 }
225 free_session_name = true;
226 } else {
227 session_name = opt_session_name;
228 }
229
230 /* Mi check */
231 if (lttng_opt_mi) {
232 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
233 if (!writer) {
234 ret = -LTTNG_ERR_NOMEM;
235 goto end;
236 }
237
238 /* Open command element */
239 ret = mi_lttng_writer_command_open(writer,
240 mi_lttng_element_command_enable_rotation);
241 if (ret) {
242 ret = CMD_ERROR;
243 goto end;
244 }
245
246 /* Open output element */
247 ret = mi_lttng_writer_open_element(writer,
248 mi_lttng_element_command_output);
249 if (ret) {
250 ret = CMD_ERROR;
251 goto end;
252 }
253 }
254
255 /* No config options, just rotate the session now */
256 if (timer == 0 && size == 0) {
257 ERR("No timer or size given");
258 success = 0;
259 command_ret = -1;
260 } else {
261 command_ret = setup_rotate(session_name, timer, size);
262 }
263
264 if (command_ret) {
265 ERR("%s", lttng_strerror(command_ret));
266 success = 0;
267 }
268
269 /* Mi closing */
270 if (lttng_opt_mi) {
271 /* Close output element */
272 ret = mi_lttng_writer_close_element(writer);
273 if (ret) {
274 goto end;
275 }
276 /* Success ? */
277 ret = mi_lttng_writer_write_element_bool(writer,
278 mi_lttng_element_command_success, success);
279 if (ret) {
280 ret = CMD_ERROR;
281 goto end;
282 }
283
284 /* Command element close */
285 ret = mi_lttng_writer_command_close(writer);
286 if (ret) {
287 ret = CMD_ERROR;
288 goto end;
289 }
290 }
291
292 end:
293 /* Mi clean-up */
294 if (writer && mi_lttng_writer_destroy(writer)) {
295 /* Preserve original error code */
296 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
297 }
298
299 /* Overwrite ret if an error occurred with start_tracing */
300 ret = command_ret ? command_ret : ret;
301 poptFreeContext(pc);
302 if (free_session_name) {
303 free(session_name);
304 }
305 return ret;
306 }
This page took 0.039462 seconds and 6 git commands to generate.