2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
17 #include <lttng/lttng.h>
19 #include "../command.hpp"
21 #include <common/mi-lttng.hpp>
22 #include <common/sessiond-comm/sessiond-comm.hpp>
23 #include <common/utils.hpp>
25 static int opt_destroy_all
;
26 static int opt_no_wait
;
28 #ifdef LTTNG_EMBED_HELP
29 static const char help_msg
[] =
30 #include <lttng-destroy.1.h>
35 static struct mi_writer
*writer
;
42 static struct poptOption long_options
[] = {
43 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
44 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
45 {"all", 'a', POPT_ARG_VAL
, &opt_destroy_all
, 1, 0, 0},
46 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
47 {"no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, 0, 0},
54 * Unregister the provided session to the session daemon. On success, removes
55 * the default configuration.
57 static int destroy_session(struct lttng_session
*session
)
60 char *session_name
= NULL
;
61 bool session_was_already_stopped
;
62 enum lttng_error_code ret_code
;
63 struct lttng_destruction_handle
*handle
= NULL
;
64 enum lttng_destruction_handle_status status
;
65 bool newline_needed
= false, printed_destroy_msg
= false;
66 enum lttng_rotation_state rotation_state
;
67 char *stats_str
= NULL
;
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
));
74 session_was_already_stopped
= ret
== -LTTNG_ERR_TRACE_ALREADY_STOPPED
;
77 ret
= lttng_data_pending(session
->name
);
79 /* Return the data available call error. */
84 * Data sleep time before retrying (in usec). Don't
85 * sleep if the call returned value indicates
89 if (!printed_destroy_msg
) {
90 _MSG("Destroying session %s",
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
/
130 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
:
131 if (!printed_destroy_msg
) {
132 _MSG("Destroying session %s", session
->name
);
133 newline_needed
= true;
134 printed_destroy_msg
= true;
139 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
:
142 ERR("%sFailed to wait for the completion of the destruction of session \"%s\"",
143 newline_needed
? "\n" : "",
145 newline_needed
= false;
149 } while (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
);
151 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
152 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
153 ERR("%sFailed to get the result of session destruction",
154 newline_needed
? "\n" : "");
156 newline_needed
= false;
159 if (ret_code
!= LTTNG_OK
) {
164 status
= lttng_destruction_handle_get_rotation_state(
165 handle
, &rotation_state
);
166 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
167 ERR("%sFailed to get rotation state from destruction handle",
168 newline_needed
? "\n" : "");
169 newline_needed
= false;
170 goto skip_wait_rotation
;
173 switch (rotation_state
) {
174 case LTTNG_ROTATION_STATE_NO_ROTATION
:
176 case LTTNG_ROTATION_STATE_COMPLETED
:
178 const struct lttng_trace_archive_location
*location
;
180 status
= lttng_destruction_handle_get_archive_location(
182 if (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
183 ret
= print_trace_archive_location(
184 location
, session
->name
);
186 ERR("%sFailed to print the location of trace archive",
187 newline_needed
? "\n" : "");
188 newline_needed
= false;
189 goto skip_wait_rotation
;
196 ERR("%sFailed to get the location of the rotation performed during the session's destruction",
197 newline_needed
? "\n" : "");
198 newline_needed
= false;
199 goto skip_wait_rotation
;
202 MSG("%sSession %s destroyed", newline_needed
? "\n" : "",
204 newline_needed
= false;
206 MSG("%s", stats_str
);
209 session_name
= get_session_name_quiet();
210 if (session_name
&& !strncmp(session
->name
, session_name
, NAME_MAX
)) {
211 config_destroy_default();
215 ret
= mi_lttng_session(writer
, session
, 0);
224 if (newline_needed
) {
227 lttng_destruction_handle_destroy(handle
);
234 * destroy_all_sessions
236 * Call destroy_sessions for each registered sessions
238 static int destroy_all_sessions(struct lttng_session
*sessions
, int count
)
241 bool error_occurred
= false;
243 LTTNG_ASSERT(count
>= 0);
245 MSG("No session found, nothing to do.");
248 for (i
= 0; i
< count
; i
++) {
249 int ret
= destroy_session(&sessions
[i
]);
252 ERR("%s during the destruction of session \"%s\"",
255 /* Continue to next session. */
256 error_occurred
= true;
260 return error_occurred
? CMD_ERROR
: CMD_SUCCESS
;
264 * The 'destroy <options>' first level command
266 int cmd_destroy(int argc
, const char **argv
)
269 int ret
= CMD_SUCCESS
, i
, command_ret
= CMD_SUCCESS
, success
= 1;
270 static poptContext pc
;
271 char *session_name
= NULL
;
272 const char *arg_session_name
= NULL
;
273 const char *leftover
= NULL
;
275 struct lttng_session
*sessions
= NULL
;
279 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
280 poptReadDefaultConfig(pc
, 0);
282 while ((opt
= poptGetNextOpt(pc
)) != -1) {
287 case OPT_LIST_OPTIONS
:
288 list_cmd_options(stdout
, long_options
);
299 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
301 ret
= -LTTNG_ERR_NOMEM
;
305 /* Open command element */
306 ret
= mi_lttng_writer_command_open(writer
,
307 mi_lttng_element_command_destroy
);
313 /* Open output element */
314 ret
= mi_lttng_writer_open_element(writer
,
315 mi_lttng_element_command_output
);
321 /* For validation and semantic purpose we open a sessions element */
322 ret
= mi_lttng_sessions_open(writer
);
329 /* Recuperate all sessions for further operation */
330 count
= lttng_list_sessions(&sessions
);
332 ERR("%s", lttng_strerror(count
));
333 command_ret
= CMD_ERROR
;
338 /* Ignore session name in case all sessions are to be destroyed */
339 if (opt_destroy_all
) {
340 command_ret
= destroy_all_sessions(sessions
, count
);
345 arg_session_name
= poptGetArg(pc
);
347 if (!arg_session_name
) {
348 /* No session name specified, lookup default */
349 session_name
= get_session_name();
351 session_name
= strdup(arg_session_name
);
352 if (session_name
== NULL
) {
353 PERROR("Failed to copy session name");
357 if (session_name
== NULL
) {
358 command_ret
= CMD_ERROR
;
363 /* Find the corresponding lttng_session struct */
365 for (i
= 0; i
< count
; i
++) {
366 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
368 command_ret
= destroy_session(&sessions
[i
]);
371 ERR("%s during the destruction of session \"%s\"",
372 lttng_strerror(command_ret
),
379 ERR("Session name %s not found", session_name
);
380 command_ret
= LTTNG_ERR_SESS_NOT_FOUND
;
386 leftover
= poptGetArg(pc
);
388 ERR("Unknown argument: %s", leftover
);
397 /* Close sessions and output element element */
398 ret
= mi_lttng_close_multi_element(writer
, 2);
405 ret
= mi_lttng_writer_write_element_bool(writer
,
406 mi_lttng_element_command_success
, success
);
412 /* Command element close */
413 ret
= mi_lttng_writer_command_close(writer
);
421 if (writer
&& mi_lttng_writer_destroy(writer
)) {
422 /* Preserve original error code */
423 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
429 /* Overwrite ret if an error occurred during destroy_session/all */
430 ret
= command_ret
? command_ret
: ret
;