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 _MSG("Destroying session %s", session
->name
);
91 newline_needed
= true;
92 printed_destroy_msg
= true;
95 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
102 if (!session_was_already_stopped
) {
104 * Don't print the event and packet loss warnings since the user
105 * already saw them when stopping the trace.
107 ret
= get_session_stats_str(session
->name
, &stats_str
);
113 ret_code
= lttng_destroy_session_ext(session
->name
, &handle
);
114 if (ret_code
!= LTTNG_OK
) {
120 goto skip_wait_rotation
;
124 status
= lttng_destruction_handle_wait_for_completion(
125 handle
, DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
/
128 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
:
129 if (!printed_destroy_msg
) {
130 _MSG("Destroying session %s", session
->name
);
131 newline_needed
= true;
132 printed_destroy_msg
= true;
137 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
:
140 ERR("%sFailed to wait for the completion of the destruction of session \"%s\"",
141 newline_needed
? "\n" : "",
143 newline_needed
= false;
147 } while (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
);
149 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
150 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
151 ERR("%sFailed to get the result of session destruction",
152 newline_needed
? "\n" : "");
154 newline_needed
= false;
157 if (ret_code
!= LTTNG_OK
) {
162 status
= lttng_destruction_handle_get_rotation_state(
163 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(
180 if (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
181 ret
= print_trace_archive_location(
182 location
, session
->name
);
184 ERR("%sFailed to print the location of trace archive",
185 newline_needed
? "\n" : "");
186 newline_needed
= false;
187 goto skip_wait_rotation
;
194 ERR("%sFailed to get the location of the rotation performed during the session's destruction",
195 newline_needed
? "\n" : "");
196 newline_needed
= false;
197 goto skip_wait_rotation
;
200 MSG("%sSession %s destroyed", newline_needed
? "\n" : "",
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
);
232 * destroy_all_sessions
234 * Call destroy_sessions for each registered sessions
236 static int destroy_all_sessions(struct lttng_session
*sessions
, int count
)
239 bool error_occurred
= false;
243 MSG("No session found, nothing to do.");
246 for (i
= 0; i
< count
; i
++) {
247 int ret
= destroy_session(&sessions
[i
]);
250 ERR("%s during the destruction of session \"%s\"",
253 /* Continue to next session. */
254 error_occurred
= true;
258 return error_occurred
? CMD_ERROR
: CMD_SUCCESS
;
262 * The 'destroy <options>' first level command
264 int cmd_destroy(int argc
, const char **argv
)
267 int ret
= CMD_SUCCESS
, i
, command_ret
= CMD_SUCCESS
, success
= 1;
268 static poptContext pc
;
269 char *session_name
= NULL
;
270 const char *leftover
= NULL
;
272 struct lttng_session
*sessions
;
276 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
277 poptReadDefaultConfig(pc
, 0);
279 while ((opt
= poptGetNextOpt(pc
)) != -1) {
284 case OPT_LIST_OPTIONS
:
285 list_cmd_options(stdout
, long_options
);
296 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
298 ret
= -LTTNG_ERR_NOMEM
;
302 /* Open command element */
303 ret
= mi_lttng_writer_command_open(writer
,
304 mi_lttng_element_command_destroy
);
310 /* Open output element */
311 ret
= mi_lttng_writer_open_element(writer
,
312 mi_lttng_element_command_output
);
318 /* For validation and semantic purpose we open a sessions element */
319 ret
= mi_lttng_sessions_open(writer
);
326 /* Recuperate all sessions for further operation */
327 count
= lttng_list_sessions(&sessions
);
329 ERR("%s", lttng_strerror(count
));
330 command_ret
= CMD_ERROR
;
335 /* Ignore session name in case all sessions are to be destroyed */
336 if (opt_destroy_all
) {
337 command_ret
= destroy_all_sessions(sessions
, count
);
342 opt_session_name
= (char *) poptGetArg(pc
);
344 if (!opt_session_name
) {
345 /* No session name specified, lookup default */
346 session_name
= get_session_name();
347 if (session_name
== NULL
) {
348 command_ret
= CMD_ERROR
;
353 session_name
= opt_session_name
;
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(writer
,
399 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
;
419 if (opt_session_name
== NULL
) {
423 /* Overwrite ret if an error occurred during destroy_session/all */
424 ret
= command_ret
? command_ret
: ret
;