2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
17 #include <lttng/lttng.h>
19 #include "../command.h"
21 #include <common/mi-lttng.h>
22 #include <common/sessiond-comm/sessiond-comm.h>
23 #include <common/utils.h>
25 static char *opt_session_name
;
26 static int opt_destroy_all
;
27 static int opt_no_wait
;
29 #ifdef LTTNG_EMBED_HELP
30 static const char help_msg
[] =
31 #include <lttng-destroy.1.h>
36 static struct mi_writer
*writer
;
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},
46 {"all", 'a', POPT_ARG_VAL
, &opt_destroy_all
, 1, 0, 0},
47 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
48 {"no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, 0, 0},
55 * Unregister the provided session to the session daemon. On success, removes
56 * the default configuration.
58 static int destroy_session(struct lttng_session
*session
)
61 char *session_name
= NULL
;
62 bool session_was_stopped
;
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
;
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
));
73 session_was_stopped
= ret
== -LTTNG_ERR_TRACE_ALREADY_STOPPED
;
76 ret
= lttng_data_pending(session
->name
);
78 /* Return the data available call error. */
83 * Data sleep time before retrying (in usec). Don't sleep if the call
84 * returned value indicates availability.
87 if (!printed_wait_msg
) {
88 _MSG("Waiting for destruction of session \"%s\"",
90 printed_wait_msg
= true;
94 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
100 if (!session_was_stopped
) {
102 * Don't print the event and packet loss warnings since the user
103 * already saw them when stopping the trace.
105 print_session_stats(session
->name
);
108 ret_code
= lttng_destroy_session_ext(session
->name
, &handle
);
109 if (ret_code
!= LTTNG_OK
) {
115 goto skip_wait_rotation
;
119 status
= lttng_destruction_handle_wait_for_completion(handle
,
120 DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
/ USEC_PER_MSEC
);
122 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
:
123 if (!printed_wait_msg
) {
124 _MSG("Waiting for destruction of session \"%s\"",
126 printed_wait_msg
= true;
131 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
:
134 ERR("Failed to wait for the completion of the destruction of session \"%s\"",
139 } while (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
);
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");
147 if (ret_code
!= LTTNG_OK
) {
152 status
= lttng_destruction_handle_get_rotation_state(handle
,
154 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
155 ERR("Failed to get rotation state from destruction handle");
156 goto skip_wait_rotation
;
158 switch (rotation_state
) {
159 case LTTNG_ROTATION_STATE_NO_ROTATION
:
161 case LTTNG_ROTATION_STATE_COMPLETED
:
163 const struct lttng_trace_archive_location
*location
;
165 status
= lttng_destruction_handle_get_archive_location(handle
,
167 if (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
168 if (printed_wait_msg
) {
170 printed_wait_msg
= false;
172 ret
= print_trace_archive_location(location
,
175 ERR("Failed to print the location of trace archive");
176 goto skip_wait_rotation
;
183 ERR("Failed to get the location of the rotation performed during the session's destruction");
184 goto skip_wait_rotation
;
187 MSG("%sSession \"%s\" destroyed", printed_wait_msg
? "\n" : "",
189 printed_wait_msg
= false;
191 session_name
= get_session_name_quiet();
192 if (session_name
&& !strncmp(session
->name
, session_name
, NAME_MAX
)) {
193 config_destroy_default();
197 ret
= mi_lttng_session(writer
, session
, 0);
206 if (printed_wait_msg
) {
209 lttng_destruction_handle_destroy(handle
);
215 * destroy_all_sessions
217 * Call destroy_sessions for each registered sessions
219 static int destroy_all_sessions(struct lttng_session
*sessions
, int count
)
222 bool error_occurred
= false;
226 MSG("No session found, nothing to do.");
229 for (i
= 0; i
< count
; i
++) {
230 int ret
= destroy_session(&sessions
[i
]);
233 ERR("%s during the destruction of session \"%s\"",
236 /* Continue to next session. */
237 error_occurred
= true;
241 return error_occurred
? CMD_ERROR
: CMD_SUCCESS
;
245 * The 'destroy <options>' first level command
247 int cmd_destroy(int argc
, const char **argv
)
250 int ret
= CMD_SUCCESS
, i
, command_ret
= CMD_SUCCESS
, success
= 1;
251 static poptContext pc
;
252 char *session_name
= NULL
;
253 const char *leftover
= NULL
;
255 struct lttng_session
*sessions
;
259 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
260 poptReadDefaultConfig(pc
, 0);
262 while ((opt
= poptGetNextOpt(pc
)) != -1) {
267 case OPT_LIST_OPTIONS
:
268 list_cmd_options(stdout
, long_options
);
279 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
281 ret
= -LTTNG_ERR_NOMEM
;
285 /* Open command element */
286 ret
= mi_lttng_writer_command_open(writer
,
287 mi_lttng_element_command_destroy
);
293 /* Open output element */
294 ret
= mi_lttng_writer_open_element(writer
,
295 mi_lttng_element_command_output
);
301 /* For validation and semantic purpose we open a sessions element */
302 ret
= mi_lttng_sessions_open(writer
);
309 /* Recuperate all sessions for further operation */
310 count
= lttng_list_sessions(&sessions
);
312 ERR("%s", lttng_strerror(count
));
313 command_ret
= CMD_ERROR
;
318 /* Ignore session name in case all sessions are to be destroyed */
319 if (opt_destroy_all
) {
320 command_ret
= destroy_all_sessions(sessions
, count
);
325 opt_session_name
= (char *) poptGetArg(pc
);
327 if (!opt_session_name
) {
328 /* No session name specified, lookup default */
329 session_name
= get_session_name();
330 if (session_name
== NULL
) {
331 command_ret
= CMD_ERROR
;
336 session_name
= opt_session_name
;
339 /* Find the corresponding lttng_session struct */
341 for (i
= 0; i
< count
; i
++) {
342 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
344 command_ret
= destroy_session(&sessions
[i
]);
347 ERR("%s during the destruction of session \"%s\"",
348 lttng_strerror(command_ret
),
355 ERR("Session name %s not found", session_name
);
356 command_ret
= LTTNG_ERR_SESS_NOT_FOUND
;
362 leftover
= poptGetArg(pc
);
364 ERR("Unknown argument: %s", leftover
);
373 /* Close sessions and output element element */
374 ret
= mi_lttng_close_multi_element(writer
, 2);
381 ret
= mi_lttng_writer_write_element_bool(writer
,
382 mi_lttng_element_command_success
, success
);
388 /* Command element close */
389 ret
= mi_lttng_writer_command_close(writer
);
397 if (writer
&& mi_lttng_writer_destroy(writer
)) {
398 /* Preserve original error code */
399 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
402 if (opt_session_name
== NULL
) {
406 /* Overwrite ret if an error occurred during destroy_session/all */
407 ret
= command_ret
? command_ret
: ret
;