2 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
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_clear_all
;
27 #ifdef LTTNG_EMBED_HELP
28 static const char help_msg
[] =
29 #include <lttng-clear.1.h>
34 static struct mi_writer
*writer
;
41 static struct poptOption long_options
[] = {
42 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
43 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
44 {"all", 'a', POPT_ARG_VAL
, &opt_clear_all
, 1, 0, 0},
45 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
52 static int clear_session(struct lttng_session
*session
)
54 enum lttng_clear_handle_status status
=
55 LTTNG_CLEAR_HANDLE_STATUS_OK
;
56 struct lttng_clear_handle
*handle
= NULL
;
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(handle
,
69 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\"",
75 printed_wait_msg
= true;
80 case LTTNG_CLEAR_HANDLE_STATUS_COMPLETED
:
83 ERR("Failed to wait for the completion of clear for session \"%s\"",
88 } while (status
== LTTNG_CLEAR_HANDLE_STATUS_TIMEOUT
);
90 status
= lttng_clear_handle_get_result(handle
, &ret_code
);
91 if (status
!= LTTNG_CLEAR_HANDLE_STATUS_OK
) {
92 ERR("Failed to get the result of session clear");
96 if (ret_code
!= LTTNG_OK
) {
101 MSG("%sSession \"%s\" cleared", printed_wait_msg
? "\n" : "",
103 printed_wait_msg
= false;
106 ret
= mi_lttng_session(writer
, session
, 0);
115 if (printed_wait_msg
) {
118 lttng_clear_handle_destroy(handle
);
125 * Call clear_session for each registered sessions
127 static int clear_all_sessions(struct lttng_session
*sessions
, int count
)
129 int i
, ret
= CMD_SUCCESS
;
132 MSG("No session found, nothing to do.");
133 } else if (count
< 0) {
134 ERR("%s", lttng_strerror(ret
));
138 for (i
= 0; i
< count
; i
++) {
139 ret
= clear_session(&sessions
[i
]);
149 * The 'clear <options>' first level command
151 int cmd_clear(int argc
, const char **argv
)
154 int ret
= CMD_SUCCESS
, i
, command_ret
= CMD_SUCCESS
, success
= 1;
155 static poptContext pc
;
156 char *session_name
= NULL
;
157 const char *arg_session_name
= NULL
;
158 const char *leftover
= NULL
;
159 struct lttng_session
*sessions
= NULL
;
163 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
164 poptReadDefaultConfig(pc
, 0);
166 while ((opt
= poptGetNextOpt(pc
)) != -1) {
171 case OPT_LIST_OPTIONS
:
172 list_cmd_options(stdout
, long_options
);
183 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
185 ret
= -LTTNG_ERR_NOMEM
;
189 /* Open command element */
190 ret
= mi_lttng_writer_command_open(writer
,
191 mi_lttng_element_command_clear
);
197 /* Open output element */
198 ret
= mi_lttng_writer_open_element(writer
,
199 mi_lttng_element_command_output
);
205 /* For validation and semantic purpose we open a sessions element */
206 ret
= mi_lttng_sessions_open(writer
);
213 if (!opt_clear_all
) {
214 arg_session_name
= poptGetArg(pc
);
215 if (!arg_session_name
) {
216 /* No session name specified, lookup default */
217 session_name
= get_session_name();
219 session_name
= strdup(arg_session_name
);
220 if (session_name
== NULL
) {
221 PERROR("Failed to copy session name");
225 if (session_name
== NULL
) {
226 command_ret
= CMD_ERROR
;
232 leftover
= poptGetArg(pc
);
234 ERR("Unknown argument: %s", leftover
);
240 /* Recuperate all sessions for further operation */
241 count
= lttng_list_sessions(&sessions
);
243 ERR("%s", lttng_strerror(count
));
244 command_ret
= CMD_ERROR
;
249 /* Ignore session name in case all sessions are to be cleaned */
251 command_ret
= clear_all_sessions(sessions
, count
);
253 ERR("%s", lttng_strerror(command_ret
));
257 /* Find the corresponding lttng_session struct */
259 for (i
= 0; i
< count
; i
++) {
260 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
262 command_ret
= clear_session(&sessions
[i
]);
264 ERR("%s", lttng_strerror(command_ret
));
271 ERR("Session name %s not found", session_name
);
272 command_ret
= LTTNG_ERR_SESS_NOT_FOUND
;
281 /* Close sessions and output element element */
282 ret
= mi_lttng_close_multi_element(writer
, 2);
289 ret
= mi_lttng_writer_write_element_bool(writer
,
290 mi_lttng_element_command_success
, success
);
296 /* Command element close */
297 ret
= mi_lttng_writer_command_close(writer
);
305 if (writer
&& mi_lttng_writer_destroy(writer
)) {
306 /* Preserve original error code */
307 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
313 /* Overwrite ret if an error occurred during clear_session/all */
314 ret
= command_ret
? command_ret
: ret
;