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 | ||
d68c9a04 JD |
60 | static int rotate_tracing(char *session_name) |
61 | { | |
62 | int ret; | |
91c4d516 | 63 | enum cmd_error_code cmd_ret = CMD_SUCCESS; |
d68c9a04 JD |
64 | struct lttng_rotation_handle *handle = NULL; |
65 | enum lttng_rotation_status rotation_status; | |
66 | enum lttng_rotation_state rotation_state = LTTNG_ROTATION_STATE_ONGOING; | |
91c4d516 JG |
67 | const struct lttng_trace_archive_location *location = NULL; |
68 | bool print_location = true; | |
d68c9a04 JD |
69 | |
70 | DBG("Rotating the output files of session %s", session_name); | |
71 | ||
dbd512ea | 72 | ret = lttng_rotate_session(session_name, NULL, &handle); |
d68c9a04 JD |
73 | if (ret < 0) { |
74 | switch (-ret) { | |
75 | case LTTNG_ERR_SESSION_NOT_STARTED: | |
76 | WARN("Tracing session %s not started yet", session_name); | |
91c4d516 JG |
77 | cmd_ret = CMD_WARNING; |
78 | goto end; | |
d68c9a04 JD |
79 | default: |
80 | ERR("%s", lttng_strerror(ret)); | |
91c4d516 | 81 | goto error; |
d68c9a04 | 82 | } |
91c4d516 JG |
83 | } |
84 | ||
85 | if (opt_no_wait) { | |
86 | rotation_state = LTTNG_ROTATION_STATE_ONGOING; | |
87 | goto skip_wait; | |
88 | } | |
89 | ||
90 | _MSG("Waiting for rotation to complete"); | |
91 | ret = fflush(stdout); | |
92 | if (ret) { | |
93 | PERROR("fflush"); | |
d68c9a04 JD |
94 | goto error; |
95 | } | |
96 | ||
91c4d516 JG |
97 | do { |
98 | rotation_status = lttng_rotation_handle_get_state(handle, | |
99 | &rotation_state); | |
100 | if (rotation_status != LTTNG_ROTATION_STATUS_OK) { | |
e5148e25 | 101 | MSG(""); |
91c4d516 | 102 | ERR("Failed to query the state of the rotation."); |
d68c9a04 JD |
103 | goto error; |
104 | } | |
105 | ||
91c4d516 | 106 | if (rotation_state == LTTNG_ROTATION_STATE_ONGOING) { |
ea0f4f56 | 107 | ret = usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US); |
91c4d516 | 108 | if (ret) { |
e5148e25 | 109 | PERROR("\nusleep"); |
d68c9a04 JD |
110 | goto error; |
111 | } | |
91c4d516 | 112 | _MSG("."); |
d68c9a04 | 113 | |
91c4d516 JG |
114 | ret = fflush(stdout); |
115 | if (ret) { | |
e5148e25 | 116 | PERROR("\nfflush"); |
91c4d516 | 117 | goto error; |
d68c9a04 | 118 | } |
91c4d516 JG |
119 | } |
120 | } while (rotation_state == LTTNG_ROTATION_STATE_ONGOING); | |
121 | MSG(""); | |
d68c9a04 | 122 | |
91c4d516 | 123 | skip_wait: |
d68c9a04 JD |
124 | switch (rotation_state) { |
125 | case LTTNG_ROTATION_STATE_COMPLETED: | |
dd73d57b JG |
126 | rotation_status = lttng_rotation_handle_get_archive_location( |
127 | handle, &location); | |
d68c9a04 | 128 | if (rotation_status != LTTNG_ROTATION_STATUS_OK) { |
91c4d516 JG |
129 | ERR("Failed to retrieve the rotation's completed chunk archive location."); |
130 | cmd_ret = CMD_ERROR; | |
d68c9a04 | 131 | } |
91c4d516 | 132 | break; |
d68c9a04 | 133 | case LTTNG_ROTATION_STATE_EXPIRED: |
91c4d516 JG |
134 | break; |
135 | case LTTNG_ROTATION_STATE_ERROR: | |
136 | ERR("Failed to retrieve rotation state."); | |
91c4d516 JG |
137 | goto error; |
138 | case LTTNG_ROTATION_STATE_ONGOING: | |
139 | MSG("Rotation ongoing for session %s", session_name); | |
140 | print_location = false; | |
141 | break; | |
d68c9a04 | 142 | default: |
91c4d516 | 143 | ERR("Unexpected rotation state encountered."); |
d68c9a04 JD |
144 | goto error; |
145 | } | |
146 | ||
91c4d516 | 147 | if (!lttng_opt_mi && print_location) { |
8bb32452 | 148 | ret = print_trace_archive_location(location, |
91c4d516 JG |
149 | session_name); |
150 | } else if (lttng_opt_mi) { | |
151 | ret = mi_lttng_rotate(writer, session_name, rotation_state, | |
152 | location); | |
153 | } | |
154 | ||
155 | if (ret < 0) { | |
156 | cmd_ret = CMD_ERROR; | |
157 | } | |
158 | ||
d68c9a04 JD |
159 | end: |
160 | lttng_rotation_handle_destroy(handle); | |
91c4d516 JG |
161 | return cmd_ret; |
162 | error: | |
163 | cmd_ret = CMD_ERROR; | |
164 | goto end; | |
d68c9a04 JD |
165 | } |
166 | ||
167 | /* | |
168 | * cmd_rotate | |
169 | * | |
170 | * The 'rotate <options>' first level command | |
171 | */ | |
172 | int cmd_rotate(int argc, const char **argv) | |
173 | { | |
91c4d516 JG |
174 | int opt, ret; |
175 | enum cmd_error_code cmd_ret = CMD_SUCCESS; | |
d68c9a04 JD |
176 | int popt_ret; |
177 | static poptContext pc; | |
178 | char *session_name = NULL; | |
179 | bool free_session_name = false; | |
180 | ||
181 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
182 | popt_ret = poptReadDefaultConfig(pc, 0); | |
183 | if (popt_ret) { | |
d68c9a04 | 184 | ERR("poptReadDefaultConfig"); |
91c4d516 | 185 | goto error; |
d68c9a04 JD |
186 | } |
187 | ||
188 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
189 | switch (opt) { | |
190 | case OPT_HELP: | |
191 | SHOW_HELP(); | |
192 | goto end; | |
193 | case OPT_LIST_OPTIONS: | |
194 | list_cmd_options(stdout, long_options); | |
195 | goto end; | |
196 | default: | |
91c4d516 | 197 | cmd_ret = CMD_UNDEFINED; |
d68c9a04 JD |
198 | goto end; |
199 | } | |
200 | } | |
201 | ||
202 | opt_session_name = (char*) poptGetArg(pc); | |
203 | ||
204 | if (!opt_session_name) { | |
205 | session_name = get_session_name(); | |
206 | if (!session_name) { | |
91c4d516 | 207 | goto error; |
d68c9a04 JD |
208 | } |
209 | free_session_name = true; | |
210 | } else { | |
211 | session_name = opt_session_name; | |
212 | } | |
213 | ||
214 | /* Mi check */ | |
215 | if (lttng_opt_mi) { | |
216 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
217 | if (!writer) { | |
91c4d516 | 218 | goto error; |
d68c9a04 JD |
219 | } |
220 | ||
221 | /* Open rotate command */ | |
222 | ret = mi_lttng_writer_command_open(writer, | |
223 | mi_lttng_element_command_rotate); | |
224 | if (ret) { | |
91c4d516 | 225 | goto error; |
d68c9a04 JD |
226 | } |
227 | ||
228 | /* Open output element */ | |
229 | ret = mi_lttng_writer_open_element(writer, | |
230 | mi_lttng_element_command_output); | |
231 | if (ret) { | |
91c4d516 | 232 | goto error; |
d68c9a04 | 233 | } |
d68c9a04 JD |
234 | } |
235 | ||
91c4d516 | 236 | cmd_ret = rotate_tracing(session_name); |
d68c9a04 JD |
237 | |
238 | /* Mi closing */ | |
239 | if (lttng_opt_mi) { | |
66ea93b1 | 240 | /* Close output element */ |
d68c9a04 JD |
241 | ret = mi_lttng_writer_close_element(writer); |
242 | if (ret) { | |
91c4d516 | 243 | goto error; |
d68c9a04 JD |
244 | } |
245 | /* Success ? */ | |
246 | ret = mi_lttng_writer_write_element_bool(writer, | |
91c4d516 JG |
247 | mi_lttng_element_command_success, |
248 | cmd_ret == CMD_SUCCESS); | |
d68c9a04 | 249 | if (ret) { |
91c4d516 | 250 | goto error; |
d68c9a04 JD |
251 | } |
252 | ||
253 | /* Command element close */ | |
254 | ret = mi_lttng_writer_command_close(writer); | |
255 | if (ret) { | |
91c4d516 | 256 | goto error; |
d68c9a04 JD |
257 | } |
258 | } | |
259 | ||
d68c9a04 JD |
260 | /* Mi clean-up */ |
261 | if (writer && mi_lttng_writer_destroy(writer)) { | |
91c4d516 | 262 | goto error; |
d68c9a04 | 263 | } |
91c4d516 | 264 | end: |
d68c9a04 JD |
265 | poptFreeContext(pc); |
266 | if (free_session_name) { | |
267 | free(session_name); | |
268 | } | |
91c4d516 JG |
269 | |
270 | return cmd_ret; | |
271 | error: | |
272 | cmd_ret = CMD_ERROR; | |
273 | goto end; | |
d68c9a04 | 274 | } |