Commit | Line | Data |
---|---|---|
f3ed775e | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
f3ed775e | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
f3ed775e | 5 | * |
f3ed775e DG |
6 | */ |
7 | ||
6c1c0768 | 8 | #define _LGPL_SOURCE |
f3ed775e DG |
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> | |
20fb9e02 | 16 | #include <stdbool.h> |
bbbfd849 | 17 | #include <lttng/lttng.h> |
f3ed775e | 18 | |
c399183f | 19 | #include "../command.h" |
f3ed775e | 20 | |
65f25c66 | 21 | #include <common/mi-lttng.h> |
42224349 | 22 | #include <common/sessiond-comm/sessiond-comm.h> |
1dac0189 | 23 | #include <common/utils.h> |
42224349 | 24 | |
fd076c09 | 25 | static char *opt_session_name; |
b09ee5ba | 26 | static int opt_destroy_all; |
e20ee7c2 | 27 | static int opt_no_wait; |
f3ed775e | 28 | |
4fc83d94 PP |
29 | #ifdef LTTNG_EMBED_HELP |
30 | static const char help_msg[] = | |
31 | #include <lttng-destroy.1.h> | |
32 | ; | |
33 | #endif | |
34 | ||
65f25c66 JRJ |
35 | /* Mi writer */ |
36 | static struct mi_writer *writer; | |
37 | ||
f3ed775e DG |
38 | enum { |
39 | OPT_HELP = 1, | |
679b4943 | 40 | OPT_LIST_OPTIONS, |
f3ed775e DG |
41 | }; |
42 | ||
43 | static struct poptOption long_options[] = { | |
44 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
45 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, | |
b09ee5ba | 46 | {"all", 'a', POPT_ARG_VAL, &opt_destroy_all, 1, 0, 0}, |
679b4943 | 47 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
e20ee7c2 | 48 | {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0}, |
f3ed775e DG |
49 | {0, 0, 0, 0, 0, 0, 0} |
50 | }; | |
51 | ||
f3ed775e | 52 | /* |
b09ee5ba FG |
53 | * destroy_session |
54 | * | |
55 | * Unregister the provided session to the session daemon. On success, removes | |
56 | * the default configuration. | |
f3ed775e | 57 | */ |
65f25c66 | 58 | static int destroy_session(struct lttng_session *session) |
f3ed775e DG |
59 | { |
60 | int ret; | |
1dac0189 | 61 | char *session_name = NULL; |
20fb9e02 | 62 | bool session_was_stopped; |
bbbfd849 JG |
63 | enum lttng_error_code ret_code; |
64 | struct lttng_destruction_handle *handle = NULL; | |
65 | enum lttng_destruction_handle_status status; | |
66 | bool printed_wait_msg = false; | |
67 | enum lttng_rotation_state rotation_state; | |
f3ed775e | 68 | |
e20ee7c2 JD |
69 | ret = lttng_stop_tracing_no_wait(session->name); |
70 | if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) { | |
71 | ERR("%s", lttng_strerror(ret)); | |
72 | } | |
20fb9e02 | 73 | session_was_stopped = ret == -LTTNG_ERR_TRACE_ALREADY_STOPPED; |
e20ee7c2 | 74 | if (!opt_no_wait) { |
e20ee7c2 JD |
75 | do { |
76 | ret = lttng_data_pending(session->name); | |
77 | if (ret < 0) { | |
78 | /* Return the data available call error. */ | |
79 | goto error; | |
80 | } | |
81 | ||
82 | /* | |
83 | * Data sleep time before retrying (in usec). Don't sleep if the call | |
84 | * returned value indicates availability. | |
85 | */ | |
86 | if (ret) { | |
01f55be4 | 87 | if (!printed_wait_msg) { |
bbbfd849 JG |
88 | _MSG("Waiting for destruction of session \"%s\"", |
89 | session->name); | |
90 | printed_wait_msg = true; | |
01f55be4 JG |
91 | fflush(stdout); |
92 | } | |
93 | ||
c8f61fd4 | 94 | usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US); |
e20ee7c2 JD |
95 | _MSG("."); |
96 | fflush(stdout); | |
97 | } | |
98 | } while (ret != 0); | |
e20ee7c2 | 99 | } |
20fb9e02 JD |
100 | if (!session_was_stopped) { |
101 | /* | |
102 | * Don't print the event and packet loss warnings since the user | |
103 | * already saw them when stopping the trace. | |
104 | */ | |
105 | print_session_stats(session->name); | |
106 | } | |
e20ee7c2 | 107 | |
bbbfd849 JG |
108 | ret_code = lttng_destroy_session_ext(session->name, &handle); |
109 | if (ret_code != LTTNG_OK) { | |
110 | ret = -ret_code; | |
b09ee5ba | 111 | goto error; |
f3ed775e DG |
112 | } |
113 | ||
bbbfd849 JG |
114 | if (opt_no_wait) { |
115 | goto skip_wait_rotation; | |
116 | } | |
117 | ||
118 | do { | |
119 | status = lttng_destruction_handle_wait_for_completion(handle, | |
c8f61fd4 | 120 | DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US / USEC_PER_MSEC); |
bbbfd849 JG |
121 | switch (status) { |
122 | case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT: | |
123 | if (!printed_wait_msg) { | |
124 | _MSG("Waiting for destruction of session \"%s\"", | |
125 | session->name); | |
126 | printed_wait_msg = true; | |
127 | } | |
128 | _MSG("."); | |
129 | fflush(stdout); | |
130 | break; | |
131 | case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED: | |
132 | break; | |
133 | default: | |
134 | ERR("Failed to wait for the completion of the destruction of session \"%s\"", | |
135 | session->name); | |
136 | ret = -1; | |
137 | goto error; | |
138 | } | |
139 | } while (status == LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT); | |
140 | ||
141 | status = lttng_destruction_handle_get_result(handle, &ret_code); | |
142 | if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) { | |
143 | ERR("Failed to get the result of session destruction"); | |
144 | ret = -1; | |
145 | goto error; | |
146 | } | |
147 | if (ret_code != LTTNG_OK) { | |
3285a971 | 148 | ret = -ret_code; |
bbbfd849 JG |
149 | goto error; |
150 | } | |
151 | ||
152 | status = lttng_destruction_handle_get_rotation_state(handle, | |
153 | &rotation_state); | |
7d81aa9f JG |
154 | if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) { |
155 | ERR("Failed to get rotation state from destruction handle"); | |
156 | goto skip_wait_rotation; | |
157 | } | |
bbbfd849 JG |
158 | switch (rotation_state) { |
159 | case LTTNG_ROTATION_STATE_NO_ROTATION: | |
160 | break; | |
161 | case LTTNG_ROTATION_STATE_COMPLETED: | |
162 | { | |
163 | const struct lttng_trace_archive_location *location; | |
164 | ||
165 | status = lttng_destruction_handle_get_archive_location(handle, | |
166 | &location); | |
167 | if (status == LTTNG_DESTRUCTION_HANDLE_STATUS_OK) { | |
168 | if (printed_wait_msg) { | |
169 | MSG(""); | |
170 | printed_wait_msg = false; | |
171 | } | |
172 | ret = print_trace_archive_location(location, | |
173 | session->name); | |
174 | if (ret) { | |
175 | ERR("Failed to print the location of trace archive"); | |
176 | goto skip_wait_rotation; | |
177 | } | |
178 | break; | |
179 | } | |
180 | /* fall-through. */ | |
181 | } | |
182 | default: | |
183 | ERR("Failed to get the location of the rotation performed during the session's destruction"); | |
184 | goto skip_wait_rotation; | |
185 | } | |
186 | skip_wait_rotation: | |
187 | MSG("%sSession \"%s\" destroyed", printed_wait_msg ? "\n" : "", | |
188 | session->name); | |
f16edb77 | 189 | printed_wait_msg = false; |
1dac0189 PPM |
190 | |
191 | session_name = get_session_name_quiet(); | |
192 | if (session_name && !strncmp(session->name, session_name, NAME_MAX)) { | |
193 | config_destroy_default(); | |
194 | } | |
65f25c66 JRJ |
195 | |
196 | if (lttng_opt_mi) { | |
197 | ret = mi_lttng_session(writer, session, 0); | |
198 | if (ret) { | |
199 | ret = CMD_ERROR; | |
200 | goto error; | |
201 | } | |
202 | } | |
203 | ||
f3ed775e | 204 | ret = CMD_SUCCESS; |
b09ee5ba | 205 | error: |
f16edb77 JG |
206 | if (printed_wait_msg) { |
207 | MSG(""); | |
208 | } | |
bbbfd849 | 209 | lttng_destruction_handle_destroy(handle); |
1dac0189 | 210 | free(session_name); |
b09ee5ba FG |
211 | return ret; |
212 | } | |
f3ed775e | 213 | |
b09ee5ba FG |
214 | /* |
215 | * destroy_all_sessions | |
216 | * | |
217 | * Call destroy_sessions for each registered sessions | |
218 | */ | |
65f25c66 | 219 | static int destroy_all_sessions(struct lttng_session *sessions, int count) |
b09ee5ba | 220 | { |
3285a971 JG |
221 | int i; |
222 | bool error_occurred = false; | |
b09ee5ba | 223 | |
3285a971 | 224 | assert(count >= 0); |
b09ee5ba FG |
225 | if (count == 0) { |
226 | MSG("No session found, nothing to do."); | |
227 | } | |
60e835ca | 228 | |
b09ee5ba | 229 | for (i = 0; i < count; i++) { |
3285a971 JG |
230 | int ret = destroy_session(&sessions[i]); |
231 | ||
b09ee5ba | 232 | if (ret < 0) { |
3285a971 JG |
233 | ERR("%s during the destruction of session \"%s\"", |
234 | lttng_strerror(ret), | |
235 | sessions[i].name); | |
236 | /* Continue to next session. */ | |
237 | error_occurred = true; | |
b09ee5ba | 238 | } |
b73d0b29 | 239 | } |
3285a971 JG |
240 | |
241 | return error_occurred ? CMD_ERROR : CMD_SUCCESS; | |
f3ed775e DG |
242 | } |
243 | ||
244 | /* | |
843f5df9 | 245 | * The 'destroy <options>' first level command |
f3ed775e DG |
246 | */ |
247 | int cmd_destroy(int argc, const char **argv) | |
248 | { | |
b09ee5ba | 249 | int opt; |
65f25c66 | 250 | int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1; |
f3ed775e | 251 | static poptContext pc; |
b09ee5ba | 252 | char *session_name = NULL; |
68c7f6e5 | 253 | const char *leftover = NULL; |
f3ed775e | 254 | |
65f25c66 JRJ |
255 | struct lttng_session *sessions; |
256 | int count; | |
257 | int found; | |
258 | ||
f3ed775e DG |
259 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
260 | poptReadDefaultConfig(pc, 0); | |
261 | ||
262 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
263 | switch (opt) { | |
264 | case OPT_HELP: | |
4ba92f18 | 265 | SHOW_HELP(); |
b09ee5ba | 266 | break; |
679b4943 SM |
267 | case OPT_LIST_OPTIONS: |
268 | list_cmd_options(stdout, long_options); | |
b09ee5ba | 269 | break; |
f3ed775e | 270 | default: |
f3ed775e | 271 | ret = CMD_UNDEFINED; |
b09ee5ba | 272 | break; |
f3ed775e | 273 | } |
b09ee5ba | 274 | goto end; |
f3ed775e DG |
275 | } |
276 | ||
65f25c66 | 277 | /* Mi preparation */ |
c7e35b03 | 278 | if (lttng_opt_mi) { |
65f25c66 JRJ |
279 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
280 | if (!writer) { | |
281 | ret = -LTTNG_ERR_NOMEM; | |
282 | goto end; | |
283 | } | |
284 | ||
285 | /* Open command element */ | |
286 | ret = mi_lttng_writer_command_open(writer, | |
287 | mi_lttng_element_command_destroy); | |
288 | if (ret) { | |
289 | ret = CMD_ERROR; | |
290 | goto end; | |
291 | } | |
292 | ||
293 | /* Open output element */ | |
294 | ret = mi_lttng_writer_open_element(writer, | |
295 | mi_lttng_element_command_output); | |
296 | if (ret) { | |
297 | ret = CMD_ERROR; | |
298 | goto end; | |
299 | } | |
300 | ||
301 | /* For validation and semantic purpose we open a sessions element */ | |
302 | ret = mi_lttng_sessions_open(writer); | |
303 | if (ret) { | |
304 | ret = CMD_ERROR; | |
305 | goto end; | |
306 | } | |
307 | } | |
308 | ||
309 | /* Recuperate all sessions for further operation */ | |
310 | count = lttng_list_sessions(&sessions); | |
311 | if (count < 0) { | |
56c3ec9e JR |
312 | ERR("%s", lttng_strerror(count)); |
313 | command_ret = CMD_ERROR; | |
65f25c66 JRJ |
314 | success = 0; |
315 | goto mi_closing; | |
c7e35b03 JR |
316 | } |
317 | ||
fd076c09 | 318 | /* Ignore session name in case all sessions are to be destroyed */ |
b09ee5ba | 319 | if (opt_destroy_all) { |
65f25c66 JRJ |
320 | command_ret = destroy_all_sessions(sessions, count); |
321 | if (command_ret) { | |
322 | success = 0; | |
323 | } | |
324 | } else { | |
325 | opt_session_name = (char *) poptGetArg(pc); | |
326 | ||
1dac0189 | 327 | if (!opt_session_name) { |
65f25c66 JRJ |
328 | /* No session name specified, lookup default */ |
329 | session_name = get_session_name(); | |
330 | if (session_name == NULL) { | |
331 | command_ret = CMD_ERROR; | |
332 | success = 0; | |
333 | goto mi_closing; | |
334 | } | |
335 | } else { | |
336 | session_name = opt_session_name; | |
337 | } | |
fd076c09 | 338 | |
65f25c66 JRJ |
339 | /* Find the corresponding lttng_session struct */ |
340 | found = 0; | |
341 | for (i = 0; i < count; i++) { | |
342 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { | |
343 | found = 1; | |
344 | command_ret = destroy_session(&sessions[i]); | |
345 | if (command_ret) { | |
346 | success = 0; | |
3285a971 JG |
347 | ERR("%s during the destruction of session \"%s\"", |
348 | lttng_strerror(command_ret), | |
349 | sessions[i].name); | |
65f25c66 | 350 | } |
65f25c66 JRJ |
351 | } |
352 | } | |
353 | ||
354 | if (!found) { | |
355 | ERR("Session name %s not found", session_name); | |
356 | command_ret = LTTNG_ERR_SESS_NOT_FOUND; | |
357 | success = 0; | |
358 | goto mi_closing; | |
359 | } | |
360 | } | |
361 | ||
68c7f6e5 JD |
362 | leftover = poptGetArg(pc); |
363 | if (leftover) { | |
364 | ERR("Unknown argument: %s", leftover); | |
365 | ret = CMD_ERROR; | |
366 | success = 0; | |
367 | goto mi_closing; | |
368 | } | |
369 | ||
65f25c66 JRJ |
370 | mi_closing: |
371 | /* Mi closing */ | |
372 | if (lttng_opt_mi) { | |
373 | /* Close sessions and output element element */ | |
374 | ret = mi_lttng_close_multi_element(writer, 2); | |
375 | if (ret) { | |
fd076c09 | 376 | ret = CMD_ERROR; |
b09ee5ba FG |
377 | goto end; |
378 | } | |
fd076c09 | 379 | |
65f25c66 JRJ |
380 | /* Success ? */ |
381 | ret = mi_lttng_writer_write_element_bool(writer, | |
382 | mi_lttng_element_command_success, success); | |
383 | if (ret) { | |
384 | ret = CMD_ERROR; | |
385 | goto end; | |
386 | } | |
f3ed775e | 387 | |
65f25c66 JRJ |
388 | /* Command element close */ |
389 | ret = mi_lttng_writer_command_close(writer); | |
390 | if (ret) { | |
391 | ret = CMD_ERROR; | |
392 | goto end; | |
393 | } | |
394 | } | |
f3ed775e | 395 | end: |
65f25c66 JRJ |
396 | /* Mi clean-up */ |
397 | if (writer && mi_lttng_writer_destroy(writer)) { | |
398 | /* Preserve original error code */ | |
399 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
400 | } | |
401 | ||
fd076c09 | 402 | if (opt_session_name == NULL) { |
b09ee5ba FG |
403 | free(session_name); |
404 | } | |
fd076c09 | 405 | |
65f25c66 JRJ |
406 | /* Overwrite ret if an error occurred during destroy_session/all */ |
407 | ret = command_ret ? command_ret : ret; | |
408 | ||
fd076c09 | 409 | poptFreeContext(pc); |
f3ed775e DG |
410 | return ret; |
411 | } |