Commit | Line | Data |
---|---|---|
d68c9a04 JD |
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> | |
dd73d57b | 35 | #include <lttng/location.h> |
d68c9a04 JD |
36 | |
37 | static char *opt_session_name; | |
38 | static int opt_no_wait; | |
39 | static struct mi_writer *writer; | |
40 | ||
3ae5c539 JD |
41 | #ifdef LTTNG_EMBED_HELP |
42 | static const char help_msg[] = | |
43 | #include <lttng-rotate.1.h> | |
44 | ; | |
45 | #endif | |
46 | ||
d68c9a04 JD |
47 | enum { |
48 | OPT_HELP = 1, | |
49 | OPT_LIST_OPTIONS, | |
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 | {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0}, | |
57 | {0, 0, 0, 0, 0, 0, 0} | |
58 | }; | |
59 | ||
60 | static int mi_output_rotate(const char *status, const char *path, | |
61 | const char *session_name) | |
62 | { | |
63 | int ret; | |
64 | ||
65 | if (!lttng_opt_mi) { | |
66 | ret = 0; | |
67 | goto end; | |
68 | } | |
69 | ||
70 | ret = mi_lttng_writer_open_element(writer, | |
71 | mi_lttng_element_rotation); | |
72 | if (ret) { | |
73 | goto end; | |
74 | } | |
75 | ||
76 | ret = mi_lttng_writer_write_element_string(writer, | |
77 | mi_lttng_element_session_name, session_name); | |
78 | if (ret) { | |
79 | goto end; | |
80 | } | |
81 | ||
82 | ret = mi_lttng_writer_write_element_string(writer, | |
83 | mi_lttng_element_rotate_status, status); | |
84 | if (ret) { | |
85 | goto end; | |
86 | } | |
87 | if (path) { | |
88 | ret = mi_lttng_writer_write_element_string(writer, | |
89 | config_element_path, path); | |
90 | if (ret) { | |
91 | goto end; | |
92 | } | |
93 | } | |
94 | /* Close rotation element */ | |
95 | ret = mi_lttng_writer_close_element(writer); | |
96 | if (ret) { | |
97 | goto end; | |
98 | } | |
99 | ||
100 | end: | |
101 | return ret; | |
102 | } | |
103 | ||
dd73d57b JG |
104 | static int output_trace_archive_location( |
105 | const struct lttng_trace_archive_location *location, | |
106 | const char *session_name) | |
107 | { | |
108 | int ret = 0; | |
109 | enum lttng_trace_archive_location_type location_type; | |
110 | enum lttng_trace_archive_location_status status; | |
111 | bool printed_location = false; | |
112 | ||
113 | location_type = lttng_trace_archive_location_get_type(location); | |
114 | ||
115 | _MSG("Trace chunk archive for session %s is now readable", | |
116 | session_name); | |
117 | switch (location_type) { | |
118 | case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL: | |
119 | { | |
120 | const char *absolute_path; | |
121 | ||
122 | status = lttng_trace_archive_location_local_get_absolute_path( | |
123 | location, &absolute_path); | |
124 | if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) { | |
125 | ret = -1; | |
126 | goto end; | |
127 | } | |
128 | MSG(" at %s", absolute_path); | |
129 | ret = mi_output_rotate("completed", absolute_path, | |
130 | session_name); | |
131 | if (ret) { | |
132 | goto end; | |
133 | } | |
134 | printed_location = true; | |
135 | break; | |
136 | } | |
137 | case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY: | |
138 | { | |
139 | uint16_t control_port, data_port; | |
140 | const char *host, *relative_path, *protocol_str; | |
141 | enum lttng_trace_archive_location_relay_protocol_type protocol; | |
142 | ||
143 | /* Fetch all relay location parameters. */ | |
144 | status = lttng_trace_archive_location_relay_get_protocol_type( | |
145 | location, &protocol); | |
146 | if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) { | |
147 | ret = -1; | |
148 | goto end; | |
149 | } | |
150 | ||
151 | status = lttng_trace_archive_location_relay_get_host( | |
152 | location, &host); | |
153 | if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) { | |
154 | ret = -1; | |
155 | goto end; | |
156 | } | |
157 | ||
158 | status = lttng_trace_archive_location_relay_get_control_port( | |
159 | location, &control_port); | |
160 | if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) { | |
161 | ret = -1; | |
162 | goto end; | |
163 | } | |
164 | ||
165 | status = lttng_trace_archive_location_relay_get_data_port( | |
166 | location, &data_port); | |
167 | if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) { | |
168 | ret = -1; | |
169 | goto end; | |
170 | } | |
171 | ||
172 | status = lttng_trace_archive_location_relay_get_relative_path( | |
173 | location, &relative_path); | |
174 | if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) { | |
175 | ret = -1; | |
176 | goto end; | |
177 | } | |
178 | ||
179 | switch (protocol) { | |
180 | case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP: | |
181 | protocol_str = "tcp"; | |
182 | break; | |
183 | default: | |
184 | protocol_str = "unknown"; | |
185 | break; | |
186 | } | |
187 | ||
188 | MSG(" on relay %s://%s/%s [control port %" PRIu16 ", data port %" | |
189 | PRIu16 "]", protocol_str, host, | |
190 | relative_path, control_port, data_port); | |
191 | printed_location = true; | |
192 | ret = mi_output_rotate("completed", relative_path, | |
193 | session_name); | |
194 | if (ret) { | |
195 | goto end; | |
196 | } | |
197 | break; | |
198 | } | |
199 | default: | |
200 | break; | |
201 | } | |
202 | end: | |
203 | if (!printed_location) { | |
204 | MSG(" at an unknown location"); | |
205 | } | |
206 | return ret; | |
207 | } | |
208 | ||
d68c9a04 JD |
209 | static int rotate_tracing(char *session_name) |
210 | { | |
211 | int ret; | |
d68c9a04 JD |
212 | struct lttng_rotation_handle *handle = NULL; |
213 | enum lttng_rotation_status rotation_status; | |
214 | enum lttng_rotation_state rotation_state = LTTNG_ROTATION_STATE_ONGOING; | |
215 | ||
216 | DBG("Rotating the output files of session %s", session_name); | |
217 | ||
dbd512ea | 218 | ret = lttng_rotate_session(session_name, NULL, &handle); |
d68c9a04 JD |
219 | if (ret < 0) { |
220 | switch (-ret) { | |
221 | case LTTNG_ERR_SESSION_NOT_STARTED: | |
222 | WARN("Tracing session %s not started yet", session_name); | |
223 | break; | |
224 | default: | |
225 | ERR("%s", lttng_strerror(ret)); | |
226 | break; | |
227 | } | |
228 | goto error; | |
229 | } | |
230 | ||
231 | if (!opt_no_wait) { | |
232 | _MSG("Waiting for rotation to complete"); | |
233 | ret = fflush(stdout); | |
234 | if (ret) { | |
235 | PERROR("fflush"); | |
236 | goto error; | |
237 | } | |
238 | ||
239 | do { | |
240 | rotation_status = lttng_rotation_handle_get_state(handle, | |
241 | &rotation_state); | |
242 | if (rotation_status != LTTNG_ROTATION_STATUS_OK) { | |
243 | ERR("Failed to query the state of the rotation"); | |
244 | goto error; | |
245 | } | |
246 | ||
247 | /* | |
248 | * Data sleep time before retrying (in usec). Don't | |
249 | * sleep if the call returned value indicates | |
250 | * availability. | |
251 | */ | |
252 | if (rotation_state == LTTNG_ROTATION_STATE_ONGOING) { | |
253 | ret = usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME); | |
254 | if (ret) { | |
255 | PERROR("usleep"); | |
256 | goto error; | |
257 | } | |
258 | _MSG("."); | |
259 | ||
260 | ret = fflush(stdout); | |
261 | if (ret) { | |
262 | PERROR("fflush"); | |
263 | goto error; | |
264 | } | |
265 | } | |
266 | } while (rotation_state == LTTNG_ROTATION_STATE_ONGOING); | |
267 | MSG(""); | |
268 | } | |
269 | ||
270 | switch (rotation_state) { | |
271 | case LTTNG_ROTATION_STATE_COMPLETED: | |
272 | { | |
dd73d57b | 273 | const struct lttng_trace_archive_location *location; |
d68c9a04 | 274 | |
dd73d57b JG |
275 | rotation_status = lttng_rotation_handle_get_archive_location( |
276 | handle, &location); | |
d68c9a04 JD |
277 | if (rotation_status != LTTNG_ROTATION_STATUS_OK) { |
278 | ERR("Failed to retrieve the rotation's completed chunk archive location"); | |
279 | goto error; | |
280 | } | |
dd73d57b | 281 | ret = output_trace_archive_location(location, session_name); |
d68c9a04 JD |
282 | if (ret) { |
283 | goto error; | |
284 | } | |
285 | ret = CMD_SUCCESS; | |
286 | goto end; | |
287 | } | |
288 | case LTTNG_ROTATION_STATE_EXPIRED: | |
289 | MSG("Session %s rotated, but handle expired", session_name); | |
290 | ret = mi_output_rotate("expired", NULL, session_name); | |
291 | if (ret) { | |
292 | goto error; | |
293 | } | |
294 | ret = CMD_SUCCESS; | |
295 | goto end; | |
296 | default: | |
297 | ERR("Unexpected rotation state received, aborting..."); | |
298 | goto error; | |
299 | } | |
300 | ||
301 | error: | |
302 | ret = CMD_ERROR; | |
303 | end: | |
304 | lttng_rotation_handle_destroy(handle); | |
d68c9a04 JD |
305 | return ret; |
306 | } | |
307 | ||
308 | /* | |
309 | * cmd_rotate | |
310 | * | |
311 | * The 'rotate <options>' first level command | |
312 | */ | |
313 | int cmd_rotate(int argc, const char **argv) | |
314 | { | |
315 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; | |
316 | int popt_ret; | |
317 | static poptContext pc; | |
318 | char *session_name = NULL; | |
319 | bool free_session_name = false; | |
320 | ||
321 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
322 | popt_ret = poptReadDefaultConfig(pc, 0); | |
323 | if (popt_ret) { | |
324 | ret = CMD_ERROR; | |
325 | ERR("poptReadDefaultConfig"); | |
326 | goto end; | |
327 | } | |
328 | ||
329 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
330 | switch (opt) { | |
331 | case OPT_HELP: | |
332 | SHOW_HELP(); | |
333 | goto end; | |
334 | case OPT_LIST_OPTIONS: | |
335 | list_cmd_options(stdout, long_options); | |
336 | goto end; | |
337 | default: | |
338 | ret = CMD_UNDEFINED; | |
339 | goto end; | |
340 | } | |
341 | } | |
342 | ||
343 | opt_session_name = (char*) poptGetArg(pc); | |
344 | ||
345 | if (!opt_session_name) { | |
346 | session_name = get_session_name(); | |
347 | if (!session_name) { | |
348 | goto end; | |
349 | } | |
350 | free_session_name = true; | |
351 | } else { | |
352 | session_name = opt_session_name; | |
353 | } | |
354 | ||
355 | /* Mi check */ | |
356 | if (lttng_opt_mi) { | |
357 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
358 | if (!writer) { | |
359 | ret = -LTTNG_ERR_NOMEM; | |
360 | goto end; | |
361 | } | |
362 | ||
363 | /* Open rotate command */ | |
364 | ret = mi_lttng_writer_command_open(writer, | |
365 | mi_lttng_element_command_rotate); | |
366 | if (ret) { | |
367 | ret = CMD_ERROR; | |
368 | goto end; | |
369 | } | |
370 | ||
371 | /* Open output element */ | |
372 | ret = mi_lttng_writer_open_element(writer, | |
373 | mi_lttng_element_command_output); | |
374 | if (ret) { | |
375 | goto end; | |
376 | } | |
377 | ||
378 | /* Open rotations element */ | |
379 | ret = mi_lttng_writer_open_element(writer, | |
66ea93b1 | 380 | mi_lttng_element_rotation_schedules); |
d68c9a04 JD |
381 | if (ret) { |
382 | goto end; | |
383 | } | |
384 | ||
385 | } | |
386 | ||
387 | command_ret = rotate_tracing(session_name); | |
388 | if (command_ret) { | |
389 | success = 0; | |
390 | } | |
391 | ||
392 | /* Mi closing */ | |
393 | if (lttng_opt_mi) { | |
66ea93b1 | 394 | /* Close rotations element */ |
d68c9a04 JD |
395 | ret = mi_lttng_writer_close_element(writer); |
396 | if (ret) { | |
397 | goto end; | |
398 | } | |
66ea93b1 | 399 | /* Close output element */ |
d68c9a04 JD |
400 | ret = mi_lttng_writer_close_element(writer); |
401 | if (ret) { | |
402 | goto end; | |
403 | } | |
404 | /* Success ? */ | |
405 | ret = mi_lttng_writer_write_element_bool(writer, | |
406 | mi_lttng_element_command_success, success); | |
407 | if (ret) { | |
408 | ret = CMD_ERROR; | |
409 | goto end; | |
410 | } | |
411 | ||
412 | /* Command element close */ | |
413 | ret = mi_lttng_writer_command_close(writer); | |
414 | if (ret) { | |
415 | ret = CMD_ERROR; | |
416 | goto end; | |
417 | } | |
418 | } | |
419 | ||
420 | end: | |
421 | /* Mi clean-up */ | |
422 | if (writer && mi_lttng_writer_destroy(writer)) { | |
423 | /* Preserve original error code */ | |
424 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
425 | } | |
426 | ||
427 | /* Overwrite ret if an error occurred with start_tracing */ | |
428 | ret = command_ret ? command_ret : ret; | |
429 | poptFreeContext(pc); | |
430 | if (free_session_name) { | |
431 | free(session_name); | |
432 | } | |
433 | return ret; | |
434 | } |