2 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
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_clear_all
;
28 #ifdef LTTNG_EMBED_HELP
29 static const char help_msg
[] =
30 #include <lttng-clear.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
, nullptr, OPT_HELP
, nullptr, nullptr },
45 { "all", 'a', POPT_ARG_VAL
, &opt_clear_all
, 1, nullptr, nullptr },
46 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
47 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
53 static int clear_session(struct lttng_session
*session
)
55 enum lttng_clear_handle_status status
= LTTNG_CLEAR_HANDLE_STATUS_OK
;
56 struct lttng_clear_handle
*handle
= nullptr;
57 enum lttng_error_code ret_code
;
58 bool printed_wait_msg
= false;
61 ret
= lttng_clear_session(session
->name
, &handle
);
63 ERR("%s", lttng_strerror(ret
));
68 status
= lttng_clear_handle_wait_for_completion(
69 handle
, DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
/ USEC_PER_MSEC
);
71 case LTTNG_CLEAR_HANDLE_STATUS_TIMEOUT
:
72 if (!printed_wait_msg
) {
73 _MSG("Waiting for clear of session \"%s\"", session
->name
);
74 printed_wait_msg
= true;
79 case LTTNG_CLEAR_HANDLE_STATUS_COMPLETED
:
82 ERR("Failed to wait for the completion of clear for session \"%s\"",
87 } while (status
== LTTNG_CLEAR_HANDLE_STATUS_TIMEOUT
);
89 status
= lttng_clear_handle_get_result(handle
, &ret_code
);
90 if (status
!= LTTNG_CLEAR_HANDLE_STATUS_OK
) {
91 ERR("Failed to get the result of session clear");
95 if (ret_code
!= LTTNG_OK
) {
100 MSG("%sSession \"%s\" cleared", printed_wait_msg
? "\n" : "", session
->name
);
101 printed_wait_msg
= false;
104 ret
= mi_lttng_session(writer
, session
, 0);
113 if (printed_wait_msg
) {
116 lttng_clear_handle_destroy(handle
);
123 * Call clear_session for each registered sessions
125 static int clear_all_sessions(struct lttng_session
*sessions
, int count
)
127 int i
, ret
= CMD_SUCCESS
;
130 MSG("No session found, nothing to do.");
131 } else if (count
< 0) {
132 ERR("%s", lttng_strerror(ret
));
136 for (i
= 0; i
< count
; i
++) {
137 ret
= clear_session(&sessions
[i
]);
147 * The 'clear <options>' first level command
149 int cmd_clear(int argc
, const char **argv
)
152 int ret
= CMD_SUCCESS
, i
, command_ret
= CMD_SUCCESS
, success
= 1;
153 static poptContext pc
;
154 char *session_name
= nullptr;
155 const char *arg_session_name
= nullptr;
156 const char *leftover
= nullptr;
157 struct lttng_session
*sessions
= nullptr;
161 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
162 poptReadDefaultConfig(pc
, 0);
164 while ((opt
= poptGetNextOpt(pc
)) != -1) {
169 case OPT_LIST_OPTIONS
:
170 list_cmd_options(stdout
, long_options
);
181 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
183 ret
= -LTTNG_ERR_NOMEM
;
187 /* Open command element */
188 ret
= mi_lttng_writer_command_open(writer
, mi_lttng_element_command_clear
);
194 /* Open output element */
195 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
);
201 /* For validation and semantic purpose we open a sessions element */
202 ret
= mi_lttng_sessions_open(writer
);
209 if (!opt_clear_all
) {
210 arg_session_name
= poptGetArg(pc
);
211 if (!arg_session_name
) {
212 /* No session name specified, lookup default */
213 session_name
= get_session_name();
215 session_name
= strdup(arg_session_name
);
216 if (session_name
== nullptr) {
217 PERROR("Failed to copy session name");
221 if (session_name
== nullptr) {
222 command_ret
= CMD_ERROR
;
228 leftover
= poptGetArg(pc
);
230 ERR("Unknown argument: %s", leftover
);
236 /* Recuperate all sessions for further operation */
237 count
= lttng_list_sessions(&sessions
);
239 ERR("%s", lttng_strerror(count
));
240 command_ret
= CMD_ERROR
;
245 /* Ignore session name in case all sessions are to be cleaned */
247 command_ret
= clear_all_sessions(sessions
, count
);
249 ERR("%s", lttng_strerror(command_ret
));
253 /* Find the corresponding lttng_session struct */
255 for (i
= 0; i
< count
; i
++) {
256 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
258 command_ret
= clear_session(&sessions
[i
]);
260 ERR("%s", lttng_strerror(command_ret
));
267 ERR("Session name %s not found", session_name
);
268 command_ret
= LTTNG_ERR_SESS_NOT_FOUND
;
277 /* Close sessions and output element element */
278 ret
= mi_lttng_close_multi_element(writer
, 2);
285 ret
= mi_lttng_writer_write_element_bool(
286 writer
, mi_lttng_element_command_success
, success
);
292 /* Command element close */
293 ret
= mi_lttng_writer_command_close(writer
);
301 if (writer
&& mi_lttng_writer_destroy(writer
)) {
302 /* Preserve original error code */
303 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
309 /* Overwrite ret if an error occurred during clear_session/all */
310 ret
= command_ret
? command_ret
: ret
;