c2e5580fc56dc68cbbe6a9d4e592aee6e0b38f5b
[lttng-tools.git] / src / bin / lttng / commands / rotate.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
27 #include <common/sessiond-comm/sessiond-comm.h>
28 #include <common/mi-lttng.h>
29
30 #include "../command.h"
31 #include <lttng/rotate.h>
32
33 static char *opt_session_name;
34 static int opt_no_wait;
35 static struct mi_writer *writer;
36
37 enum {
38 OPT_HELP = 1,
39 OPT_LIST_OPTIONS,
40 };
41
42 static struct poptOption long_options[] = {
43 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
44 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
45 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
46 {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0},
47 {0, 0, 0, 0, 0, 0, 0}
48 };
49
50 static int mi_print_session(char *session_name, int enabled)
51 {
52 int ret;
53
54 /* Open session element */
55 ret = mi_lttng_writer_open_element(writer, config_element_session);
56 if (ret) {
57 goto end;
58 }
59
60 /* Print session name element */
61 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
62 session_name);
63 if (ret) {
64 goto end;
65 }
66
67 ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled,
68 enabled);
69 if (ret) {
70 goto end;
71 }
72
73 /* Close session element */
74 ret = mi_lttng_writer_close_element(writer);
75
76 end:
77 return ret;
78 }
79
80 static int rotate_tracing(void)
81 {
82 int ret;
83 char *session_name = NULL, *path = NULL;
84 struct lttng_rotate_session_attr *attr = NULL;
85 struct lttng_rotate_session_handle *handle = NULL;
86 enum lttng_rotate_status rotate_status;
87
88 attr = lttng_rotate_session_attr_create();
89 if (!attr) {
90 goto error;
91 }
92
93 if (opt_session_name == NULL) {
94 session_name = get_session_name();
95 if (session_name == NULL) {
96 goto error;
97 }
98 } else {
99 session_name = opt_session_name;
100 }
101
102 ret = lttng_rotate_session_attr_set_session_name(attr, session_name);
103 if (ret < 0) {
104 goto error;
105 }
106
107 DBG("Rotating the output files of session %s", session_name);
108
109 ret = lttng_rotate_session(attr, &handle);
110 if (ret < 0) {
111 switch (-ret) {
112 case LTTNG_ERR_SESSION_NOT_STARTED:
113 WARN("Tracing session %s not started yet", session_name);
114 break;
115 default:
116 ERR("%s", lttng_strerror(ret));
117 break;
118 }
119 goto error;
120 }
121
122 if (!opt_no_wait) {
123 _MSG("Waiting for data availability");
124 fflush(stdout);
125 do {
126 ret = lttng_rotate_session_pending(handle);
127 if (ret < 0) {
128 goto error;
129 }
130
131 /*
132 * Data sleep time before retrying (in usec). Don't sleep if the call
133 * returned value indicates availability.
134 */
135 if (ret) {
136 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
137 _MSG(".");
138 fflush(stdout);
139 }
140 } while (ret == 1);
141 MSG("");
142 }
143
144 rotate_status = lttng_rotate_session_get_status(handle);
145 switch(rotate_status) {
146 case LTTNG_ROTATE_COMPLETED:
147 lttng_rotate_session_get_output_path(handle, &path);
148 MSG("Output files of session %s rotated to %s", session_name, path);
149 ret = CMD_SUCCESS;
150 goto end;
151 case LTTNG_ROTATE_STARTED:
152 MSG("Rotation started for session %s", session_name);
153 free(path);
154 if (lttng_opt_mi) {
155 ret = mi_print_session(session_name, 1);
156 if (ret) {
157 ret = CMD_ERROR;
158 goto error;
159 }
160 }
161
162 ret = CMD_SUCCESS;
163 goto end;
164 case LTTNG_ROTATE_EXPIRED:
165 MSG("Output files of session %s rotated, but handle expired", session_name);
166 if (lttng_opt_mi) {
167 ret = mi_print_session(session_name, 1);
168 if (ret) {
169 ret = CMD_ERROR;
170 goto error;
171 }
172 }
173
174 ret = CMD_SUCCESS;
175 goto end;
176 case LTTNG_ROTATE_ERROR:
177 MSG("An error occurred with the rotation of session %s", session_name);
178 if (lttng_opt_mi) {
179 ret = mi_print_session(session_name, 1);
180 if (ret) {
181 ret = CMD_ERROR;
182 goto error;
183 }
184 }
185
186 ret = CMD_SUCCESS;
187 goto end;
188 case LTTNG_ROTATE_EMPTY:
189 MSG("Empty session, nothing to rotate.");
190 ret = CMD_SUCCESS;
191 goto end;
192 }
193
194 error:
195 ret = CMD_ERROR;
196 end:
197 if (opt_session_name == NULL) {
198 free(session_name);
199 }
200 lttng_rotate_session_handle_destroy(handle);
201 lttng_rotate_session_attr_destroy(attr);
202 return ret;
203 }
204
205 /*
206 * cmd_rotate
207 *
208 * The 'rotate <options>' first level command
209 */
210 int cmd_rotate(int argc, const char **argv)
211 {
212 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
213 static poptContext pc;
214
215 pc = poptGetContext(NULL, argc, argv, long_options, 0);
216 poptReadDefaultConfig(pc, 0);
217
218 while ((opt = poptGetNextOpt(pc)) != -1) {
219 switch (opt) {
220 case OPT_HELP:
221 SHOW_HELP();
222 goto end;
223 case OPT_LIST_OPTIONS:
224 list_cmd_options(stdout, long_options);
225 goto end;
226 default:
227 ret = CMD_UNDEFINED;
228 goto end;
229 }
230 }
231
232 opt_session_name = (char*) poptGetArg(pc);
233
234 /* Mi check */
235 if (lttng_opt_mi) {
236 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
237 if (!writer) {
238 ret = -LTTNG_ERR_NOMEM;
239 goto end;
240 }
241
242 /* Open command element */
243 ret = mi_lttng_writer_command_open(writer,
244 mi_lttng_element_command_start);
245 if (ret) {
246 ret = CMD_ERROR;
247 goto end;
248 }
249
250 /* Open output element */
251 ret = mi_lttng_writer_open_element(writer,
252 mi_lttng_element_command_output);
253 if (ret) {
254 ret = CMD_ERROR;
255 goto end;
256 }
257
258 /*
259 * Open sessions element
260 * For validation purpose
261 */
262 ret = mi_lttng_writer_open_element(writer,
263 config_element_sessions);
264 if (ret) {
265 ret = CMD_ERROR;
266 goto end;
267 }
268 }
269
270 command_ret = rotate_tracing();
271 if (command_ret) {
272 success = 0;
273 }
274
275 /* Mi closing */
276 if (lttng_opt_mi) {
277 /* Close sessions and output element */
278 ret = mi_lttng_close_multi_element(writer, 2);
279 if (ret) {
280 ret = CMD_ERROR;
281 goto end;
282 }
283
284 /* Success ? */
285 ret = mi_lttng_writer_write_element_bool(writer,
286 mi_lttng_element_command_success, success);
287 if (ret) {
288 ret = CMD_ERROR;
289 goto end;
290 }
291
292 /* Command element close */
293 ret = mi_lttng_writer_command_close(writer);
294 if (ret) {
295 ret = CMD_ERROR;
296 goto end;
297 }
298 }
299
300 end:
301 /* Mi clean-up */
302 if (writer && mi_lttng_writer_destroy(writer)) {
303 /* Preserve original error code */
304 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
305 }
306
307 /* Overwrite ret if an error occurred with start_tracing */
308 ret = command_ret ? command_ret : ret;
309 poptFreeContext(pc);
310 return ret;
311 }
This page took 0.03769 seconds and 4 git commands to generate.