2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "../command.hpp"
11 #include <common/mi-lttng.hpp>
12 #include <common/sessiond-comm/sessiond-comm.hpp>
13 #include <common/utils.hpp>
15 #include <lttng/lttng.h>
23 #include <sys/types.h>
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
, nullptr, OPT_HELP
, nullptr, nullptr },
46 { "all", 'a', POPT_ARG_VAL
, &opt_destroy_all
, 1, nullptr, nullptr },
47 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
48 { "no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, nullptr, nullptr },
49 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
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
= nullptr;
62 bool session_was_already_stopped
;
63 enum lttng_error_code ret_code
;
64 struct lttng_destruction_handle
*handle
= nullptr;
65 enum lttng_destruction_handle_status status
;
66 bool newline_needed
= false, printed_destroy_msg
= false;
67 enum lttng_rotation_state rotation_state
;
68 char *stats_str
= nullptr;
70 ret
= lttng_stop_tracing_no_wait(session
->name
);
71 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
72 ERR("%s", lttng_strerror(ret
));
75 session_was_already_stopped
= ret
== -LTTNG_ERR_TRACE_ALREADY_STOPPED
;
78 ret
= lttng_data_pending(session
->name
);
80 /* Return the data available call error. */
85 * Data sleep time before retrying (in usec). Don't
86 * sleep if the call returned value indicates
90 if (!printed_destroy_msg
) {
91 _MSG("Destroying session %s", session
->name
);
92 newline_needed
= true;
93 printed_destroy_msg
= true;
97 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
104 if (!session_was_already_stopped
) {
106 * Don't print the event and packet loss warnings since the user
107 * already saw them when stopping the trace.
109 ret
= get_session_stats_str(session
->name
, &stats_str
);
115 ret_code
= lttng_destroy_session_ext(session
->name
, &handle
);
116 if (ret_code
!= LTTNG_OK
) {
122 goto skip_wait_rotation
;
126 status
= lttng_destruction_handle_wait_for_completion(
127 handle
, DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
/ USEC_PER_MSEC
);
129 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
:
130 if (!printed_destroy_msg
) {
131 _MSG("Destroying session %s", session
->name
);
132 newline_needed
= true;
133 printed_destroy_msg
= true;
138 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
:
141 ERR("%sFailed to wait for the completion of the destruction of session \"%s\"",
142 newline_needed
? "\n" : "",
144 newline_needed
= false;
148 } while (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
);
150 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
151 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
152 ERR("%sFailed to get the result of session destruction",
153 newline_needed
? "\n" : "");
155 newline_needed
= false;
158 if (ret_code
!= LTTNG_OK
) {
163 status
= lttng_destruction_handle_get_rotation_state(handle
, &rotation_state
);
164 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
165 ERR("%sFailed to get rotation state from destruction handle",
166 newline_needed
? "\n" : "");
167 newline_needed
= false;
168 goto skip_wait_rotation
;
171 switch (rotation_state
) {
172 case LTTNG_ROTATION_STATE_NO_ROTATION
:
174 case LTTNG_ROTATION_STATE_COMPLETED
:
176 const struct lttng_trace_archive_location
*location
;
178 status
= lttng_destruction_handle_get_archive_location(handle
, &location
);
179 if (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
180 ret
= print_trace_archive_location(location
, session
->name
);
182 ERR("%sFailed to print the location of trace archive",
183 newline_needed
? "\n" : "");
184 newline_needed
= false;
185 goto skip_wait_rotation
;
192 ERR("%sFailed to get the location of the rotation performed during the session's destruction",
193 newline_needed
? "\n" : "");
194 newline_needed
= false;
195 goto skip_wait_rotation
;
198 MSG("%sSession %s destroyed", newline_needed
? "\n" : "", session
->name
);
199 newline_needed
= false;
201 MSG("%s", stats_str
);
204 session_name
= get_session_name_quiet();
205 if (session_name
&& !strncmp(session
->name
, session_name
, NAME_MAX
)) {
206 config_destroy_default();
210 ret
= mi_lttng_session(writer
, session
, 0);
219 if (newline_needed
) {
222 lttng_destruction_handle_destroy(handle
);
229 * destroy_all_sessions
231 * Call destroy_sessions for each registered sessions
233 static int destroy_all_sessions(struct lttng_session
*sessions
, int count
)
236 bool error_occurred
= false;
238 LTTNG_ASSERT(count
>= 0);
240 MSG("No session found, nothing to do.");
243 for (i
= 0; i
< count
; i
++) {
244 int ret
= destroy_session(&sessions
[i
]);
247 ERR("%s during the destruction of session \"%s\"",
250 /* Continue to next session. */
251 error_occurred
= true;
255 return error_occurred
? CMD_ERROR
: CMD_SUCCESS
;
259 * The 'destroy <options>' first level command
261 int cmd_destroy(int argc
, const char **argv
)
264 int ret
= CMD_SUCCESS
, i
, command_ret
= CMD_SUCCESS
, success
= 1;
265 static poptContext pc
;
266 char *session_name
= nullptr;
267 const char *arg_session_name
= nullptr;
268 const char *leftover
= nullptr;
270 struct lttng_session
*sessions
= nullptr;
274 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
275 poptReadDefaultConfig(pc
, 0);
277 while ((opt
= poptGetNextOpt(pc
)) != -1) {
282 case OPT_LIST_OPTIONS
:
283 list_cmd_options(stdout
, long_options
);
294 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
296 ret
= -LTTNG_ERR_NOMEM
;
300 /* Open command element */
301 ret
= mi_lttng_writer_command_open(writer
, mi_lttng_element_command_destroy
);
307 /* Open output element */
308 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
);
314 /* For validation and semantic purpose we open a sessions element */
315 ret
= mi_lttng_sessions_open(writer
);
322 /* Recuperate all sessions for further operation */
323 count
= lttng_list_sessions(&sessions
);
325 ERR("%s", lttng_strerror(count
));
326 command_ret
= CMD_ERROR
;
331 /* Ignore session name in case all sessions are to be destroyed */
332 if (opt_destroy_all
) {
333 command_ret
= destroy_all_sessions(sessions
, count
);
338 arg_session_name
= poptGetArg(pc
);
340 if (!arg_session_name
) {
341 /* No session name specified, lookup default */
342 session_name
= get_session_name();
344 session_name
= strdup(arg_session_name
);
345 if (session_name
== nullptr) {
346 PERROR("Failed to copy session name");
350 if (session_name
== nullptr) {
351 command_ret
= CMD_ERROR
;
356 /* Find the corresponding lttng_session struct */
358 for (i
= 0; i
< count
; i
++) {
359 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
361 command_ret
= destroy_session(&sessions
[i
]);
364 ERR("%s during the destruction of session \"%s\"",
365 lttng_strerror(command_ret
),
372 ERR("Session name %s not found", session_name
);
373 command_ret
= LTTNG_ERR_SESS_NOT_FOUND
;
379 leftover
= poptGetArg(pc
);
381 ERR("Unknown argument: %s", leftover
);
390 /* Close sessions and output element element */
391 ret
= mi_lttng_close_multi_element(writer
, 2);
398 ret
= mi_lttng_writer_write_element_bool(
399 writer
, mi_lttng_element_command_success
, success
);
405 /* Command element close */
406 ret
= mi_lttng_writer_command_close(writer
);
414 if (writer
&& mi_lttng_writer_destroy(writer
)) {
415 /* Preserve original error code */
416 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
422 /* Overwrite ret if an error occurred during destroy_session/all */
423 ret
= command_ret
? command_ret
: ret
;