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/clear.h>
18 #include <lttng/clear-handle.h>
20 #include "../command.h"
22 #include <common/mi-lttng.h>
23 #include <common/sessiond-comm/sessiond-comm.h>
24 #include <common/utils.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
, 0, OPT_HELP
, 0, 0},
45 {"all", 'a', POPT_ARG_VAL
, &opt_clear_all
, 1, 0, 0},
46 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
53 static int clear_session(struct lttng_session
*session
)
55 enum lttng_clear_handle_status status
=
56 LTTNG_CLEAR_HANDLE_STATUS_OK
;
57 struct lttng_clear_handle
*handle
= NULL
;
58 enum lttng_error_code ret_code
;
59 bool printed_wait_msg
= false;
60 char *session_name
= NULL
;
63 ret
= lttng_clear_session(session
->name
, &handle
);
65 ERR("%s", lttng_strerror(ret
));
70 status
= lttng_clear_handle_wait_for_completion(handle
,
71 DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
/ USEC_PER_MSEC
);
73 case LTTNG_CLEAR_HANDLE_STATUS_TIMEOUT
:
74 if (!printed_wait_msg
) {
75 _MSG("Waiting for clear of session \"%s\"",
77 printed_wait_msg
= true;
82 case LTTNG_CLEAR_HANDLE_STATUS_COMPLETED
:
85 ERR("Failed to wait for the completion of clear for session \"%s\"",
90 } while (status
== LTTNG_CLEAR_HANDLE_STATUS_TIMEOUT
);
92 status
= lttng_clear_handle_get_result(handle
, &ret_code
);
93 if (status
!= LTTNG_CLEAR_HANDLE_STATUS_OK
) {
94 ERR("Failed to get the result of session clear");
98 if (ret_code
!= LTTNG_OK
) {
103 MSG("%sSession \"%s\" cleared", printed_wait_msg
? "\n" : "",
105 printed_wait_msg
= false;
108 ret
= mi_lttng_session(writer
, session
, 0);
117 if (printed_wait_msg
) {
120 lttng_clear_handle_destroy(handle
);
128 * Call clear_session for each registered sessions
130 static int clear_all_sessions(struct lttng_session
*sessions
, int count
)
132 int i
, ret
= CMD_SUCCESS
;
135 MSG("No session found, nothing to do.");
136 } else if (count
< 0) {
137 ERR("%s", lttng_strerror(ret
));
141 for (i
= 0; i
< count
; i
++) {
142 ret
= clear_session(&sessions
[i
]);
152 * The 'clear <options>' first level command
154 int cmd_clear(int argc
, const char **argv
)
157 int ret
= CMD_SUCCESS
, i
, command_ret
= CMD_SUCCESS
, success
= 1;
158 static poptContext pc
;
159 char *session_name
= NULL
;
160 const char *leftover
= NULL
;
161 bool free_session_name
= false;
162 struct lttng_session
*sessions
= NULL
;
166 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
167 poptReadDefaultConfig(pc
, 0);
169 while ((opt
= poptGetNextOpt(pc
)) != -1) {
174 case OPT_LIST_OPTIONS
:
175 list_cmd_options(stdout
, long_options
);
186 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
188 ret
= -LTTNG_ERR_NOMEM
;
192 /* Open command element */
193 ret
= mi_lttng_writer_command_open(writer
,
194 mi_lttng_element_command_clear
);
200 /* Open output element */
201 ret
= mi_lttng_writer_open_element(writer
,
202 mi_lttng_element_command_output
);
208 /* For validation and semantic purpose we open a sessions element */
209 ret
= mi_lttng_sessions_open(writer
);
216 if (!opt_clear_all
) {
217 session_name
= (char *) poptGetArg(pc
);
219 /* No session name specified, lookup default */
220 session_name
= get_session_name();
221 if (session_name
== NULL
) {
222 command_ret
= CMD_ERROR
;
226 free_session_name
= true;
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
;
311 if (free_session_name
) {
315 /* Overwrite ret if an error occurred during clear_session/all */
316 ret
= command_ret
? command_ret
: ret
;