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_already_stopped
;
63 enum lttng_error_code ret_code
;
64 struct lttng_destruction_handle
*handle
= NULL
;
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
= NULL
;
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",
93 newline_needed
= true;
94 printed_destroy_msg
= true;
98 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
105 if (!session_was_already_stopped
) {
107 * Don't print the event and packet loss warnings since the user
108 * already saw them when stopping the trace.
110 ret
= get_session_stats_str(session
->name
, &stats_str
);
116 ret_code
= lttng_destroy_session_ext(session
->name
, &handle
);
117 if (ret_code
!= LTTNG_OK
) {
123 goto skip_wait_rotation
;
127 status
= lttng_destruction_handle_wait_for_completion(
128 handle
, DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
/
131 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
:
132 if (!printed_destroy_msg
) {
133 _MSG("Destroying session %s", session
->name
);
134 newline_needed
= true;
135 printed_destroy_msg
= true;
140 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
:
143 ERR("%sFailed to wait for the completion of the destruction of session \"%s\"",
144 newline_needed
? "\n" : "",
146 newline_needed
= false;
150 } while (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
);
152 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
153 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
154 ERR("%sFailed to get the result of session destruction",
155 newline_needed
? "\n" : "");
157 newline_needed
= false;
160 if (ret_code
!= LTTNG_OK
) {
165 status
= lttng_destruction_handle_get_rotation_state(
166 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(
183 if (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
184 ret
= print_trace_archive_location(
185 location
, session
->name
);
187 ERR("%sFailed to print the location of trace archive",
188 newline_needed
? "\n" : "");
189 newline_needed
= false;
190 goto skip_wait_rotation
;
197 ERR("%sFailed to get the location of the rotation performed during the session's destruction",
198 newline_needed
? "\n" : "");
199 newline_needed
= false;
200 goto skip_wait_rotation
;
203 MSG("%sSession %s destroyed", newline_needed
? "\n" : "",
205 newline_needed
= false;
207 MSG("%s", stats_str
);
210 session_name
= get_session_name_quiet();
211 if (session_name
&& !strncmp(session
->name
, session_name
, NAME_MAX
)) {
212 config_destroy_default();
216 ret
= mi_lttng_session(writer
, session
, 0);
225 if (newline_needed
) {
228 lttng_destruction_handle_destroy(handle
);
235 * destroy_all_sessions
237 * Call destroy_sessions for each registered sessions
239 static int destroy_all_sessions(struct lttng_session
*sessions
, int count
)
242 bool error_occurred
= false;
244 LTTNG_ASSERT(count
>= 0);
246 MSG("No session found, nothing to do.");
249 for (i
= 0; i
< count
; i
++) {
250 int ret
= destroy_session(&sessions
[i
]);
253 ERR("%s during the destruction of session \"%s\"",
256 /* Continue to next session. */
257 error_occurred
= true;
261 return error_occurred
? CMD_ERROR
: CMD_SUCCESS
;
265 * The 'destroy <options>' first level command
267 int cmd_destroy(int argc
, const char **argv
)
270 int ret
= CMD_SUCCESS
, i
, command_ret
= CMD_SUCCESS
, success
= 1;
271 static poptContext pc
;
272 char *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 opt_session_name
= (char *) poptGetArg(pc
);
347 if (!opt_session_name
) {
348 /* No session name specified, lookup default */
349 session_name
= get_session_name();
350 if (session_name
== NULL
) {
351 command_ret
= CMD_ERROR
;
356 session_name
= opt_session_name
;
359 /* Find the corresponding lttng_session struct */
361 for (i
= 0; i
< count
; i
++) {
362 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
364 command_ret
= destroy_session(&sessions
[i
]);
367 ERR("%s during the destruction of session \"%s\"",
368 lttng_strerror(command_ret
),
375 ERR("Session name %s not found", session_name
);
376 command_ret
= LTTNG_ERR_SESS_NOT_FOUND
;
382 leftover
= poptGetArg(pc
);
384 ERR("Unknown argument: %s", leftover
);
393 /* Close sessions and output element element */
394 ret
= mi_lttng_close_multi_element(writer
, 2);
401 ret
= mi_lttng_writer_write_element_bool(writer
,
402 mi_lttng_element_command_success
, success
);
408 /* Command element close */
409 ret
= mi_lttng_writer_command_close(writer
);
417 if (writer
&& mi_lttng_writer_destroy(writer
)) {
418 /* Preserve original error code */
419 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
422 if (opt_session_name
== NULL
) {
428 /* Overwrite ret if an error occurred during destroy_session/all */
429 ret
= command_ret
? command_ret
: ret
;