2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "../command.hpp"
11 #include <common/exception.hpp>
12 #include <common/mi-lttng.hpp>
13 #include <common/sessiond-comm/sessiond-comm.hpp>
14 #include <common/utils.hpp>
16 #include <lttng/lttng.h>
24 #include <sys/types.h>
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
;
45 static struct poptOption long_options
[] = {
46 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
47 { "help", 'h', POPT_ARG_NONE
, nullptr, OPT_HELP
, nullptr, nullptr },
48 { "all", 'a', POPT_ARG_NONE
, nullptr, OPT_ALL
, nullptr, nullptr },
49 { "glob", 'g', POPT_ARG_NONE
, nullptr, OPT_ENABLE_GLOB
, nullptr, nullptr },
50 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
51 { "no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, nullptr, nullptr },
52 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
58 * Unregister the provided session to the session daemon. On success, removes
59 * the default configuration.
61 static int destroy_session(const struct lttng_session
& session
)
64 char *session_name
= nullptr;
65 bool session_was_already_stopped
;
66 enum lttng_error_code ret_code
;
67 struct lttng_destruction_handle
*handle
= nullptr;
68 enum lttng_destruction_handle_status status
;
69 bool newline_needed
= false, printed_destroy_msg
= false;
70 enum lttng_rotation_state rotation_state
;
71 char *stats_str
= nullptr;
73 ret
= lttng_stop_tracing_no_wait(session
.name
);
74 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
75 ERR("%s", lttng_strerror(ret
));
78 session_was_already_stopped
= ret
== -LTTNG_ERR_TRACE_ALREADY_STOPPED
;
81 ret
= lttng_data_pending(session
.name
);
83 /* Return the data available call error. */
88 * Data sleep time before retrying (in usec). Don't
89 * sleep if the call returned value indicates
93 if (!printed_destroy_msg
) {
94 _MSG("Destroying session %s", session
.name
);
95 newline_needed
= true;
96 printed_destroy_msg
= true;
100 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
107 if (!session_was_already_stopped
) {
109 * Don't print the event and packet loss warnings since the user
110 * already saw them when stopping the trace.
112 ret
= get_session_stats_str(session
.name
, &stats_str
);
118 ret_code
= lttng_destroy_session_ext(session
.name
, &handle
);
119 if (ret_code
!= LTTNG_OK
) {
125 goto skip_wait_rotation
;
129 status
= lttng_destruction_handle_wait_for_completion(
130 handle
, DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
/ USEC_PER_MSEC
);
132 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
:
133 if (!printed_destroy_msg
) {
134 _MSG("Destroying session %s", session
.name
);
135 newline_needed
= true;
136 printed_destroy_msg
= true;
141 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
:
144 ERR("%sFailed to wait for the completion of the destruction of session \"%s\"",
145 newline_needed
? "\n" : "",
147 newline_needed
= false;
151 } while (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
);
153 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
154 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
155 ERR("%sFailed to get the result of session destruction",
156 newline_needed
? "\n" : "");
158 newline_needed
= false;
161 if (ret_code
!= LTTNG_OK
) {
166 status
= lttng_destruction_handle_get_rotation_state(handle
, &rotation_state
);
167 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
168 ERR("%sFailed to get rotation state from destruction handle",
169 newline_needed
? "\n" : "");
170 newline_needed
= false;
171 goto skip_wait_rotation
;
174 switch (rotation_state
) {
175 case LTTNG_ROTATION_STATE_NO_ROTATION
:
177 case LTTNG_ROTATION_STATE_COMPLETED
:
179 const struct lttng_trace_archive_location
*location
;
181 status
= lttng_destruction_handle_get_archive_location(handle
, &location
);
182 if (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
183 ret
= print_trace_archive_location(location
, session
.name
);
185 ERR("%sFailed to print the location of trace archive",
186 newline_needed
? "\n" : "");
187 newline_needed
= false;
188 goto skip_wait_rotation
;
195 ERR("%sFailed to get the location of the rotation performed during the session's destruction",
196 newline_needed
? "\n" : "");
197 newline_needed
= false;
198 goto skip_wait_rotation
;
201 MSG("%sSession %s destroyed", newline_needed
? "\n" : "", session
.name
);
202 newline_needed
= false;
204 MSG("%s", stats_str
);
207 session_name
= get_session_name_quiet();
208 if (session_name
&& !strncmp(session
.name
, session_name
, NAME_MAX
)) {
209 config_destroy_default();
213 ret
= mi_lttng_session(writer
, &session
, 0);
222 if (newline_needed
) {
225 lttng_destruction_handle_destroy(handle
);
231 static int destroy_sessions(const struct session_spec
& spec
)
234 auto sessions
= list_sessions(spec
);
235 int ret
= CMD_SUCCESS
;
237 if (sessions
.size() == 0) {
239 case session_spec::ALL
: /* fall throught */
240 case session_spec::GLOB_PATTERN
:
241 MSG("No session found, nothing to do.");
243 case session_spec::NAME
:
244 ERR("Session name %s not found", spec
.value
);
245 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
252 for (const auto& session
: sessions
) {
253 int const sub_ret
= destroy_session(session
);
255 if (sub_ret
!= CMD_SUCCESS
) {
256 ERR("%s during the destruction of session \"%s\"",
257 lttng_strerror(sub_ret
),
265 } catch (lttng::ctl::error
& error
) {
266 ERR("%s", lttng_strerror(error
.code()));
273 * The 'destroy <options>' first level command
275 int cmd_destroy(int argc
, const char **argv
)
278 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
280 static poptContext pc
;
281 const char *leftover
= nullptr;
282 struct session_spec spec
= {
283 .type
= session_spec::NAME
,
286 session_list
const sessions
;
288 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
289 poptReadDefaultConfig(pc
, 0);
291 while ((opt
= poptGetNextOpt(pc
)) != -1) {
296 case OPT_LIST_OPTIONS
:
297 list_cmd_options(stdout
, long_options
);
300 spec
.type
= session_spec::ALL
;
302 case OPT_ENABLE_GLOB
:
303 spec
.type
= session_spec::GLOB_PATTERN
;
313 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
315 ret
= -LTTNG_ERR_NOMEM
;
319 /* Open command element */
320 ret
= mi_lttng_writer_command_open(writer
, mi_lttng_element_command_destroy
);
326 /* Open output element */
327 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
);
333 /* For validation and semantic purpose we open a sessions element */
334 ret
= mi_lttng_sessions_open(writer
);
341 spec
.value
= poptGetArg(pc
);
343 command_ret
= destroy_sessions(spec
);
345 success
= command_ret
== CMD_SUCCESS
;
347 leftover
= poptGetArg(pc
);
349 ERR("Unknown argument: %s", leftover
);
356 /* Close sessions and output element element */
357 ret
= mi_lttng_close_multi_element(writer
, 2);
364 ret
= mi_lttng_writer_write_element_bool(
365 writer
, mi_lttng_element_command_success
, success
);
371 /* Command element close */
372 ret
= mi_lttng_writer_command_close(writer
);
380 if (writer
&& mi_lttng_writer_destroy(writer
)) {
381 /* Preserve original error code */
382 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
385 /* Overwrite ret if an error occurred during destroy_session/all */
386 ret
= command_ret
? command_ret
: ret
;