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