Commit | Line | Data |
---|---|---|
6e0be6cc 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 | ||
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 | } | |
189 | ||
190 | error: | |
191 | ret = CMD_ERROR; | |
192 | end: | |
193 | if (opt_session_name == NULL) { | |
194 | free(session_name); | |
195 | } | |
196 | lttng_rotate_session_handle_destroy(handle); | |
197 | lttng_rotate_session_attr_destroy(attr); | |
198 | return ret; | |
199 | } | |
200 | ||
201 | /* | |
202 | * cmd_rotate | |
203 | * | |
204 | * The 'rotate <options>' first level command | |
205 | */ | |
206 | int cmd_rotate(int argc, const char **argv) | |
207 | { | |
208 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; | |
209 | static poptContext pc; | |
210 | ||
211 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
212 | poptReadDefaultConfig(pc, 0); | |
213 | ||
214 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
215 | switch (opt) { | |
216 | case OPT_HELP: | |
217 | SHOW_HELP(); | |
218 | goto end; | |
219 | case OPT_LIST_OPTIONS: | |
220 | list_cmd_options(stdout, long_options); | |
221 | goto end; | |
222 | default: | |
223 | ret = CMD_UNDEFINED; | |
224 | goto end; | |
225 | } | |
226 | } | |
227 | ||
228 | opt_session_name = (char*) poptGetArg(pc); | |
229 | ||
230 | /* Mi check */ | |
231 | if (lttng_opt_mi) { | |
232 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
233 | if (!writer) { | |
234 | ret = -LTTNG_ERR_NOMEM; | |
235 | goto end; | |
236 | } | |
237 | ||
238 | /* Open command element */ | |
239 | ret = mi_lttng_writer_command_open(writer, | |
240 | mi_lttng_element_command_start); | |
241 | if (ret) { | |
242 | ret = CMD_ERROR; | |
243 | goto end; | |
244 | } | |
245 | ||
246 | /* Open output element */ | |
247 | ret = mi_lttng_writer_open_element(writer, | |
248 | mi_lttng_element_command_output); | |
249 | if (ret) { | |
250 | ret = CMD_ERROR; | |
251 | goto end; | |
252 | } | |
253 | ||
254 | /* | |
255 | * Open sessions element | |
256 | * For validation purpose | |
257 | */ | |
258 | ret = mi_lttng_writer_open_element(writer, | |
259 | config_element_sessions); | |
260 | if (ret) { | |
261 | ret = CMD_ERROR; | |
262 | goto end; | |
263 | } | |
264 | } | |
265 | ||
266 | command_ret = rotate_tracing(); | |
267 | if (command_ret) { | |
268 | success = 0; | |
269 | } | |
270 | ||
271 | /* Mi closing */ | |
272 | if (lttng_opt_mi) { | |
273 | /* Close sessions and output element */ | |
274 | ret = mi_lttng_close_multi_element(writer, 2); | |
275 | if (ret) { | |
276 | ret = CMD_ERROR; | |
277 | goto end; | |
278 | } | |
279 | ||
280 | /* Success ? */ | |
281 | ret = mi_lttng_writer_write_element_bool(writer, | |
282 | mi_lttng_element_command_success, success); | |
283 | if (ret) { | |
284 | ret = CMD_ERROR; | |
285 | goto end; | |
286 | } | |
287 | ||
288 | /* Command element close */ | |
289 | ret = mi_lttng_writer_command_close(writer); | |
290 | if (ret) { | |
291 | ret = CMD_ERROR; | |
292 | goto end; | |
293 | } | |
294 | } | |
295 | ||
296 | end: | |
297 | /* Mi clean-up */ | |
298 | if (writer && mi_lttng_writer_destroy(writer)) { | |
299 | /* Preserve original error code */ | |
300 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
301 | } | |
302 | ||
303 | /* Overwrite ret if an error occurred with start_tracing */ | |
304 | ret = command_ret ? command_ret : ret; | |
305 | poptFreeContext(pc); | |
306 | return ret; | |
307 | } |