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