Commit | Line | Data |
---|---|---|
d68c9a04 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com> |
d68c9a04 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
d68c9a04 | 5 | * |
d68c9a04 JD |
6 | */ |
7 | ||
8 | #define _LGPL_SOURCE | |
9 | #include <popt.h> | |
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | #include <string.h> | |
13 | #include <sys/stat.h> | |
14 | #include <sys/types.h> | |
15 | #include <unistd.h> | |
16 | #include <inttypes.h> | |
17 | #include <ctype.h> | |
18 | #include <assert.h> | |
19 | ||
20 | #include <common/sessiond-comm/sessiond-comm.h> | |
21 | #include <common/mi-lttng.h> | |
22 | ||
23 | #include "../command.h" | |
050dd639 | 24 | #include <lttng/lttng.h> |
d68c9a04 JD |
25 | |
26 | static char *opt_session_name; | |
27 | static int opt_no_wait; | |
28 | static struct mi_writer *writer; | |
29 | ||
3ae5c539 JD |
30 | #ifdef LTTNG_EMBED_HELP |
31 | static const char help_msg[] = | |
32 | #include <lttng-rotate.1.h> | |
33 | ; | |
34 | #endif | |
35 | ||
d68c9a04 JD |
36 | enum { |
37 | OPT_HELP = 1, | |
38 | OPT_LIST_OPTIONS, | |
39 | }; | |
40 | ||
41 | static struct poptOption long_options[] = { | |
42 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
43 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, | |
44 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, | |
45 | {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0}, | |
46 | {0, 0, 0, 0, 0, 0, 0} | |
47 | }; | |
48 | ||
d68c9a04 JD |
49 | static int rotate_tracing(char *session_name) |
50 | { | |
51 | int ret; | |
91c4d516 | 52 | enum cmd_error_code cmd_ret = CMD_SUCCESS; |
d68c9a04 JD |
53 | struct lttng_rotation_handle *handle = NULL; |
54 | enum lttng_rotation_status rotation_status; | |
55 | enum lttng_rotation_state rotation_state = LTTNG_ROTATION_STATE_ONGOING; | |
91c4d516 JG |
56 | const struct lttng_trace_archive_location *location = NULL; |
57 | bool print_location = true; | |
d68c9a04 JD |
58 | |
59 | DBG("Rotating the output files of session %s", session_name); | |
60 | ||
dbd512ea | 61 | ret = lttng_rotate_session(session_name, NULL, &handle); |
d68c9a04 JD |
62 | if (ret < 0) { |
63 | switch (-ret) { | |
64 | case LTTNG_ERR_SESSION_NOT_STARTED: | |
65 | WARN("Tracing session %s not started yet", session_name); | |
91c4d516 JG |
66 | cmd_ret = CMD_WARNING; |
67 | goto end; | |
d68c9a04 JD |
68 | default: |
69 | ERR("%s", lttng_strerror(ret)); | |
91c4d516 | 70 | goto error; |
d68c9a04 | 71 | } |
91c4d516 JG |
72 | } |
73 | ||
74 | if (opt_no_wait) { | |
75 | rotation_state = LTTNG_ROTATION_STATE_ONGOING; | |
76 | goto skip_wait; | |
77 | } | |
78 | ||
79 | _MSG("Waiting for rotation to complete"); | |
80 | ret = fflush(stdout); | |
81 | if (ret) { | |
82 | PERROR("fflush"); | |
d68c9a04 JD |
83 | goto error; |
84 | } | |
85 | ||
91c4d516 JG |
86 | do { |
87 | rotation_status = lttng_rotation_handle_get_state(handle, | |
88 | &rotation_state); | |
89 | if (rotation_status != LTTNG_ROTATION_STATUS_OK) { | |
d2956687 | 90 | MSG(""); |
91c4d516 | 91 | ERR("Failed to query the state of the rotation."); |
d68c9a04 JD |
92 | goto error; |
93 | } | |
94 | ||
91c4d516 | 95 | if (rotation_state == LTTNG_ROTATION_STATE_ONGOING) { |
c8f61fd4 | 96 | ret = usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US); |
91c4d516 | 97 | if (ret) { |
d2956687 | 98 | PERROR("\nusleep"); |
d68c9a04 JD |
99 | goto error; |
100 | } | |
91c4d516 | 101 | _MSG("."); |
d68c9a04 | 102 | |
91c4d516 JG |
103 | ret = fflush(stdout); |
104 | if (ret) { | |
d2956687 | 105 | PERROR("\nfflush"); |
91c4d516 | 106 | goto error; |
d68c9a04 | 107 | } |
91c4d516 JG |
108 | } |
109 | } while (rotation_state == LTTNG_ROTATION_STATE_ONGOING); | |
110 | MSG(""); | |
d68c9a04 | 111 | |
91c4d516 | 112 | skip_wait: |
d68c9a04 JD |
113 | switch (rotation_state) { |
114 | case LTTNG_ROTATION_STATE_COMPLETED: | |
dd73d57b JG |
115 | rotation_status = lttng_rotation_handle_get_archive_location( |
116 | handle, &location); | |
d68c9a04 | 117 | if (rotation_status != LTTNG_ROTATION_STATUS_OK) { |
91c4d516 JG |
118 | ERR("Failed to retrieve the rotation's completed chunk archive location."); |
119 | cmd_ret = CMD_ERROR; | |
d68c9a04 | 120 | } |
91c4d516 | 121 | break; |
d68c9a04 | 122 | case LTTNG_ROTATION_STATE_EXPIRED: |
91c4d516 JG |
123 | break; |
124 | case LTTNG_ROTATION_STATE_ERROR: | |
125 | ERR("Failed to retrieve rotation state."); | |
91c4d516 JG |
126 | goto error; |
127 | case LTTNG_ROTATION_STATE_ONGOING: | |
128 | MSG("Rotation ongoing for session %s", session_name); | |
129 | print_location = false; | |
130 | break; | |
d68c9a04 | 131 | default: |
91c4d516 | 132 | ERR("Unexpected rotation state encountered."); |
d68c9a04 JD |
133 | goto error; |
134 | } | |
135 | ||
91c4d516 | 136 | if (!lttng_opt_mi && print_location) { |
bbbfd849 | 137 | ret = print_trace_archive_location(location, |
91c4d516 JG |
138 | session_name); |
139 | } else if (lttng_opt_mi) { | |
140 | ret = mi_lttng_rotate(writer, session_name, rotation_state, | |
141 | location); | |
142 | } | |
143 | ||
144 | if (ret < 0) { | |
145 | cmd_ret = CMD_ERROR; | |
146 | } | |
147 | ||
d68c9a04 JD |
148 | end: |
149 | lttng_rotation_handle_destroy(handle); | |
91c4d516 JG |
150 | return cmd_ret; |
151 | error: | |
152 | cmd_ret = CMD_ERROR; | |
153 | goto end; | |
d68c9a04 JD |
154 | } |
155 | ||
156 | /* | |
157 | * cmd_rotate | |
158 | * | |
159 | * The 'rotate <options>' first level command | |
160 | */ | |
161 | int cmd_rotate(int argc, const char **argv) | |
162 | { | |
91c4d516 JG |
163 | int opt, ret; |
164 | enum cmd_error_code cmd_ret = CMD_SUCCESS; | |
d68c9a04 JD |
165 | int popt_ret; |
166 | static poptContext pc; | |
167 | char *session_name = NULL; | |
168 | bool free_session_name = false; | |
169 | ||
170 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
171 | popt_ret = poptReadDefaultConfig(pc, 0); | |
172 | if (popt_ret) { | |
d68c9a04 | 173 | ERR("poptReadDefaultConfig"); |
91c4d516 | 174 | goto error; |
d68c9a04 JD |
175 | } |
176 | ||
177 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
178 | switch (opt) { | |
179 | case OPT_HELP: | |
180 | SHOW_HELP(); | |
181 | goto end; | |
182 | case OPT_LIST_OPTIONS: | |
183 | list_cmd_options(stdout, long_options); | |
184 | goto end; | |
185 | default: | |
91c4d516 | 186 | cmd_ret = CMD_UNDEFINED; |
d68c9a04 JD |
187 | goto end; |
188 | } | |
189 | } | |
190 | ||
191 | opt_session_name = (char*) poptGetArg(pc); | |
192 | ||
193 | if (!opt_session_name) { | |
194 | session_name = get_session_name(); | |
195 | if (!session_name) { | |
91c4d516 | 196 | goto error; |
d68c9a04 JD |
197 | } |
198 | free_session_name = true; | |
199 | } else { | |
200 | session_name = opt_session_name; | |
201 | } | |
202 | ||
203 | /* Mi check */ | |
204 | if (lttng_opt_mi) { | |
205 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
206 | if (!writer) { | |
91c4d516 | 207 | goto error; |
d68c9a04 JD |
208 | } |
209 | ||
210 | /* Open rotate command */ | |
211 | ret = mi_lttng_writer_command_open(writer, | |
212 | mi_lttng_element_command_rotate); | |
213 | if (ret) { | |
91c4d516 | 214 | goto error; |
d68c9a04 JD |
215 | } |
216 | ||
217 | /* Open output element */ | |
218 | ret = mi_lttng_writer_open_element(writer, | |
219 | mi_lttng_element_command_output); | |
220 | if (ret) { | |
91c4d516 | 221 | goto error; |
d68c9a04 | 222 | } |
d68c9a04 JD |
223 | } |
224 | ||
91c4d516 | 225 | cmd_ret = rotate_tracing(session_name); |
d68c9a04 JD |
226 | |
227 | /* Mi closing */ | |
228 | if (lttng_opt_mi) { | |
66ea93b1 | 229 | /* Close output element */ |
d68c9a04 JD |
230 | ret = mi_lttng_writer_close_element(writer); |
231 | if (ret) { | |
91c4d516 | 232 | goto error; |
d68c9a04 JD |
233 | } |
234 | /* Success ? */ | |
235 | ret = mi_lttng_writer_write_element_bool(writer, | |
91c4d516 JG |
236 | mi_lttng_element_command_success, |
237 | cmd_ret == CMD_SUCCESS); | |
d68c9a04 | 238 | if (ret) { |
91c4d516 | 239 | goto error; |
d68c9a04 JD |
240 | } |
241 | ||
242 | /* Command element close */ | |
243 | ret = mi_lttng_writer_command_close(writer); | |
244 | if (ret) { | |
91c4d516 | 245 | goto error; |
d68c9a04 JD |
246 | } |
247 | } | |
248 | ||
d68c9a04 JD |
249 | /* Mi clean-up */ |
250 | if (writer && mi_lttng_writer_destroy(writer)) { | |
91c4d516 | 251 | goto error; |
d68c9a04 | 252 | } |
91c4d516 | 253 | end: |
d68c9a04 JD |
254 | poptFreeContext(pc); |
255 | if (free_session_name) { | |
256 | free(session_name); | |
257 | } | |
91c4d516 JG |
258 | |
259 | return cmd_ret; | |
260 | error: | |
261 | cmd_ret = CMD_ERROR; | |
262 | goto end; | |
d68c9a04 | 263 | } |