lttng rotate command
[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 #include <inttypes.h>
27 #include <ctype.h>
28 #include <assert.h>
29
30 #include <common/sessiond-comm/sessiond-comm.h>
31 #include <common/mi-lttng.h>
32
33 #include "../command.h"
34 #include <lttng/rotation.h>
35
36 static char *opt_session_name;
37 static int opt_no_wait;
38 static struct mi_writer *writer;
39
40 enum {
41 OPT_HELP = 1,
42 OPT_LIST_OPTIONS,
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 {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0},
50 {0, 0, 0, 0, 0, 0, 0}
51 };
52
53 static int mi_output_rotate(const char *status, const char *path,
54 const char *session_name)
55 {
56 int ret;
57
58 if (!lttng_opt_mi) {
59 ret = 0;
60 goto end;
61 }
62
63 ret = mi_lttng_writer_open_element(writer,
64 mi_lttng_element_rotation);
65 if (ret) {
66 goto end;
67 }
68
69 ret = mi_lttng_writer_write_element_string(writer,
70 mi_lttng_element_session_name, session_name);
71 if (ret) {
72 goto end;
73 }
74
75 ret = mi_lttng_writer_write_element_string(writer,
76 mi_lttng_element_rotate_status, status);
77 if (ret) {
78 goto end;
79 }
80 if (path) {
81 ret = mi_lttng_writer_write_element_string(writer,
82 config_element_path, path);
83 if (ret) {
84 goto end;
85 }
86 }
87 /* Close rotation element */
88 ret = mi_lttng_writer_close_element(writer);
89 if (ret) {
90 goto end;
91 }
92
93 end:
94 return ret;
95 }
96
97 static int rotate_tracing(char *session_name)
98 {
99 int ret;
100 struct lttng_rotation_immediate_attr *attr = NULL;
101 struct lttng_rotation_handle *handle = NULL;
102 enum lttng_rotation_status rotation_status;
103 enum lttng_rotation_state rotation_state = LTTNG_ROTATION_STATE_ONGOING;
104
105 DBG("Rotating the output files of session %s", session_name);
106
107 attr = lttng_rotation_immediate_attr_create();
108 if (!attr) {
109 goto error;
110 }
111
112 ret = lttng_rotation_immediate_attr_set_session_name(attr, session_name);
113 if (ret < 0) {
114 ERR("Session name exceeds the maximal allowed length");
115 goto error;
116 }
117
118 ret = lttng_rotate_session(attr, &handle);
119 if (ret < 0) {
120 switch (-ret) {
121 case LTTNG_ERR_SESSION_NOT_STARTED:
122 WARN("Tracing session %s not started yet", session_name);
123 break;
124 default:
125 ERR("%s", lttng_strerror(ret));
126 break;
127 }
128 goto error;
129 }
130
131 if (!opt_no_wait) {
132 _MSG("Waiting for rotation to complete");
133 ret = fflush(stdout);
134 if (ret) {
135 PERROR("fflush");
136 goto error;
137 }
138
139 do {
140 rotation_status = lttng_rotation_handle_get_state(handle,
141 &rotation_state);
142 if (rotation_status != LTTNG_ROTATION_STATUS_OK) {
143 ERR("Failed to query the state of the rotation");
144 goto error;
145 }
146
147 /*
148 * Data sleep time before retrying (in usec). Don't
149 * sleep if the call returned value indicates
150 * availability.
151 */
152 if (rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
153 ret = usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
154 if (ret) {
155 PERROR("usleep");
156 goto error;
157 }
158 _MSG(".");
159
160 ret = fflush(stdout);
161 if (ret) {
162 PERROR("fflush");
163 goto error;
164 }
165 }
166 } while (rotation_state == LTTNG_ROTATION_STATE_ONGOING);
167 MSG("");
168 }
169
170 switch (rotation_state) {
171 case LTTNG_ROTATION_STATE_COMPLETED:
172 {
173 const char *path;
174
175 rotation_status = lttng_rotation_handle_get_completed_archive_location(
176 handle, &path);
177 if (rotation_status != LTTNG_ROTATION_STATUS_OK) {
178 ERR("Failed to retrieve the rotation's completed chunk archive location");
179 goto error;
180 }
181 MSG("Trace chunk archive for session %s is now readable at %s",
182 session_name, path);
183 ret = mi_output_rotate("completed", path, session_name);
184 if (ret) {
185 goto error;
186 }
187 ret = CMD_SUCCESS;
188 goto end;
189 }
190 case LTTNG_ROTATION_STATE_EXPIRED:
191 MSG("Session %s rotated, but handle expired", session_name);
192 ret = mi_output_rotate("expired", NULL, session_name);
193 if (ret) {
194 goto error;
195 }
196 ret = CMD_SUCCESS;
197 goto end;
198 default:
199 ERR("Unexpected rotation state received, aborting...");
200 goto error;
201 }
202
203 error:
204 ret = CMD_ERROR;
205 end:
206 lttng_rotation_handle_destroy(handle);
207 lttng_rotation_immediate_attr_destroy(attr);
208 return ret;
209 }
210
211 /*
212 * cmd_rotate
213 *
214 * The 'rotate <options>' first level command
215 */
216 int cmd_rotate(int argc, const char **argv)
217 {
218 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
219 int popt_ret;
220 static poptContext pc;
221 char *session_name = NULL;
222 bool free_session_name = false;
223
224 pc = poptGetContext(NULL, argc, argv, long_options, 0);
225 popt_ret = poptReadDefaultConfig(pc, 0);
226 if (popt_ret) {
227 ret = CMD_ERROR;
228 ERR("poptReadDefaultConfig");
229 goto end;
230 }
231
232 while ((opt = poptGetNextOpt(pc)) != -1) {
233 switch (opt) {
234 case OPT_HELP:
235 SHOW_HELP();
236 goto end;
237 case OPT_LIST_OPTIONS:
238 list_cmd_options(stdout, long_options);
239 goto end;
240 default:
241 ret = CMD_UNDEFINED;
242 goto end;
243 }
244 }
245
246 opt_session_name = (char*) poptGetArg(pc);
247
248 if (!opt_session_name) {
249 session_name = get_session_name();
250 if (!session_name) {
251 goto end;
252 }
253 free_session_name = true;
254 } else {
255 session_name = opt_session_name;
256 }
257
258 /* Mi check */
259 if (lttng_opt_mi) {
260 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
261 if (!writer) {
262 ret = -LTTNG_ERR_NOMEM;
263 goto end;
264 }
265
266 /* Open rotate command */
267 ret = mi_lttng_writer_command_open(writer,
268 mi_lttng_element_command_rotate);
269 if (ret) {
270 ret = CMD_ERROR;
271 goto end;
272 }
273
274 /* Open output element */
275 ret = mi_lttng_writer_open_element(writer,
276 mi_lttng_element_command_output);
277 if (ret) {
278 goto end;
279 }
280
281 /* Open rotations element */
282 ret = mi_lttng_writer_open_element(writer,
283 mi_lttng_element_rotations);
284 if (ret) {
285 goto end;
286 }
287
288 }
289
290 command_ret = rotate_tracing(session_name);
291 if (command_ret) {
292 success = 0;
293 }
294
295 /* Mi closing */
296 if (lttng_opt_mi) {
297 /* Close rotations element */
298 ret = mi_lttng_writer_close_element(writer);
299 if (ret) {
300 goto end;
301 }
302 /* Close output element */
303 ret = mi_lttng_writer_close_element(writer);
304 if (ret) {
305 goto end;
306 }
307 /* Success ? */
308 ret = mi_lttng_writer_write_element_bool(writer,
309 mi_lttng_element_command_success, success);
310 if (ret) {
311 ret = CMD_ERROR;
312 goto end;
313 }
314
315 /* Command element close */
316 ret = mi_lttng_writer_command_close(writer);
317 if (ret) {
318 ret = CMD_ERROR;
319 goto end;
320 }
321 }
322
323 end:
324 /* Mi clean-up */
325 if (writer && mi_lttng_writer_destroy(writer)) {
326 /* Preserve original error code */
327 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
328 }
329
330 /* Overwrite ret if an error occurred with start_tracing */
331 ret = command_ret ? command_ret : ret;
332 poptFreeContext(pc);
333 if (free_session_name) {
334 free(session_name);
335 }
336 return ret;
337 }
This page took 0.051054 seconds and 5 git commands to generate.