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.h"
21 #include <common/mi-lttng.h>
22 #include <common/sessiond-comm/sessiond-comm.h>
23 #include <common/utils.h>
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;
59 char *session_name
= NULL
;
62 ret
= lttng_clear_session(session
->name
, &handle
);
64 ERR("%s", lttng_strerror(ret
));
69 status
= lttng_clear_handle_wait_for_completion(handle
,
70 DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
/ USEC_PER_MSEC
);
72 case LTTNG_CLEAR_HANDLE_STATUS_TIMEOUT
:
73 if (!printed_wait_msg
) {
74 _MSG("Waiting for clear of session \"%s\"",
76 printed_wait_msg
= true;
81 case LTTNG_CLEAR_HANDLE_STATUS_COMPLETED
:
84 ERR("Failed to wait for the completion of clear for session \"%s\"",
89 } while (status
== LTTNG_CLEAR_HANDLE_STATUS_TIMEOUT
);
91 status
= lttng_clear_handle_get_result(handle
, &ret_code
);
92 if (status
!= LTTNG_CLEAR_HANDLE_STATUS_OK
) {
93 ERR("Failed to get the result of session clear");
97 if (ret_code
!= LTTNG_OK
) {
102 MSG("%sSession \"%s\" cleared", printed_wait_msg
? "\n" : "",
104 printed_wait_msg
= false;
107 ret
= mi_lttng_session(writer
, session
, 0);
116 if (printed_wait_msg
) {
119 lttng_clear_handle_destroy(handle
);
127 * Call clear_session for each registered sessions
129 static int clear_all_sessions(struct lttng_session
*sessions
, int count
)
131 int i
, ret
= CMD_SUCCESS
;
134 MSG("No session found, nothing to do.");
135 } else if (count
< 0) {
136 ERR("%s", lttng_strerror(ret
));
140 for (i
= 0; i
< count
; i
++) {
141 ret
= clear_session(&sessions
[i
]);
151 * The 'clear <options>' first level command
153 int cmd_clear(int argc
, const char **argv
)
156 int ret
= CMD_SUCCESS
, i
, command_ret
= CMD_SUCCESS
, success
= 1;
157 static poptContext pc
;
158 char *session_name
= NULL
;
159 const char *leftover
= NULL
;
160 bool free_session_name
= false;
161 struct lttng_session
*sessions
= NULL
;
165 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
166 poptReadDefaultConfig(pc
, 0);
168 while ((opt
= poptGetNextOpt(pc
)) != -1) {
173 case OPT_LIST_OPTIONS
:
174 list_cmd_options(stdout
, long_options
);
185 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
187 ret
= -LTTNG_ERR_NOMEM
;
191 /* Open command element */
192 ret
= mi_lttng_writer_command_open(writer
,
193 mi_lttng_element_command_clear
);
199 /* Open output element */
200 ret
= mi_lttng_writer_open_element(writer
,
201 mi_lttng_element_command_output
);
207 /* For validation and semantic purpose we open a sessions element */
208 ret
= mi_lttng_sessions_open(writer
);
215 if (!opt_clear_all
) {
216 session_name
= (char *) poptGetArg(pc
);
218 /* No session name specified, lookup default */
219 session_name
= get_session_name();
220 if (session_name
== NULL
) {
221 command_ret
= CMD_ERROR
;
225 free_session_name
= true;
231 leftover
= poptGetArg(pc
);
233 ERR("Unknown argument: %s", leftover
);
239 /* Recuperate all sessions for further operation */
240 count
= lttng_list_sessions(&sessions
);
242 ERR("%s", lttng_strerror(count
));
243 command_ret
= CMD_ERROR
;
248 /* Ignore session name in case all sessions are to be cleaned */
250 command_ret
= clear_all_sessions(sessions
, count
);
252 ERR("%s", lttng_strerror(command_ret
));
256 /* Find the corresponding lttng_session struct */
258 for (i
= 0; i
< count
; i
++) {
259 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
261 command_ret
= clear_session(&sessions
[i
]);
263 ERR("%s", lttng_strerror(command_ret
));
270 ERR("Session name %s not found", session_name
);
271 command_ret
= LTTNG_ERR_SESS_NOT_FOUND
;
280 /* Close sessions and output element element */
281 ret
= mi_lttng_close_multi_element(writer
, 2);
288 ret
= mi_lttng_writer_write_element_bool(writer
,
289 mi_lttng_element_command_success
, success
);
295 /* Command element close */
296 ret
= mi_lttng_writer_command_close(writer
);
304 if (writer
&& mi_lttng_writer_destroy(writer
)) {
305 /* Preserve original error code */
306 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
310 if (free_session_name
) {
314 /* Overwrite ret if an error occurred during clear_session/all */
315 ret
= command_ret
? command_ret
: ret
;