2 * Copyright (C) 2019 - Jonathan Rajotte-Julien <jonathan.rajotte-julien@efficios.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/types.h>
27 #include <lttng/clear.h>
28 #include <lttng/clear-handle.h>
30 #include "../command.h"
32 #include <common/mi-lttng.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
34 #include <common/utils.h>
36 static int opt_clear_all
;
38 #ifdef LTTNG_EMBED_HELP
39 static const char help_msg
[] =
40 #include <lttng-clear.1.h>
45 static struct mi_writer
*writer
;
52 static struct poptOption long_options
[] = {
53 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
54 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
55 {"all", 'a', POPT_ARG_VAL
, &opt_clear_all
, 1, 0, 0},
56 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
63 static int clear_session(struct lttng_session
*session
)
65 enum lttng_clear_handle_status status
=
66 LTTNG_CLEAR_HANDLE_STATUS_OK
;
67 struct lttng_clear_handle
*handle
= NULL
;
68 enum lttng_error_code ret_code
;
69 bool printed_wait_msg
= false;
70 char *session_name
= NULL
;
73 ret
= lttng_clear_session(session
->name
, &handle
);
75 ERR("%s", lttng_strerror(ret
));
80 status
= lttng_clear_handle_wait_for_completion(handle
,
81 DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
/ USEC_PER_MSEC
);
83 case LTTNG_CLEAR_HANDLE_STATUS_TIMEOUT
:
84 if (!printed_wait_msg
) {
85 _MSG("Waiting for clear of session \"%s\"",
87 printed_wait_msg
= true;
92 case LTTNG_CLEAR_HANDLE_STATUS_COMPLETED
:
95 ERR("Failed to wait for the completion of clear for session \"%s\"",
100 } while (status
== LTTNG_CLEAR_HANDLE_STATUS_TIMEOUT
);
102 status
= lttng_clear_handle_get_result(handle
, &ret_code
);
103 if (status
!= LTTNG_CLEAR_HANDLE_STATUS_OK
) {
104 ERR("Failed to get the result of session clear");
108 if (ret_code
!= LTTNG_OK
) {
113 MSG("%sSession \"%s\" cleared", printed_wait_msg
? "\n" : "",
115 printed_wait_msg
= false;
118 ret
= mi_lttng_session(writer
, session
, 0);
127 if (printed_wait_msg
) {
130 lttng_clear_handle_destroy(handle
);
138 * Call clear_session for each registered sessions
140 static int clear_all_sessions(struct lttng_session
*sessions
, int count
)
142 int i
, ret
= CMD_SUCCESS
;
145 MSG("No session found, nothing to do.");
146 } else if (count
< 0) {
147 ERR("%s", lttng_strerror(ret
));
151 for (i
= 0; i
< count
; i
++) {
152 ret
= clear_session(&sessions
[i
]);
162 * The 'clear <options>' first level command
164 int cmd_clear(int argc
, const char **argv
)
167 int ret
= CMD_SUCCESS
, i
, command_ret
= CMD_SUCCESS
, success
= 1;
168 static poptContext pc
;
169 char *session_name
= NULL
;
170 const char *leftover
= NULL
;
172 struct lttng_session
*sessions
= NULL
;
176 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
177 poptReadDefaultConfig(pc
, 0);
179 while ((opt
= poptGetNextOpt(pc
)) != -1) {
184 case OPT_LIST_OPTIONS
:
185 list_cmd_options(stdout
, long_options
);
196 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
198 ret
= -LTTNG_ERR_NOMEM
;
202 /* Open command element */
203 ret
= mi_lttng_writer_command_open(writer
,
204 mi_lttng_element_command_clear
);
210 /* Open output element */
211 ret
= mi_lttng_writer_open_element(writer
,
212 mi_lttng_element_command_output
);
218 /* For validation and semantic purpose we open a sessions element */
219 ret
= mi_lttng_sessions_open(writer
);
226 if (!opt_clear_all
) {
228 * popt expects us to free this even if it returns a const char *.
229 * See https://www.mail-archive.com/popt-devel@rpm5.org/msg00193.html
230 * Force cast to char * allowing later freeing if necessary.
232 session_name
= (char *) poptGetArg(pc
);
235 /* No session name specified, lookup default */
236 session_name
= get_session_name();
237 if (session_name
== NULL
) {
238 command_ret
= CMD_ERROR
;
247 leftover
= poptGetArg(pc
);
249 ERR("Unknown argument: %s", leftover
);
255 /* Recuperate all sessions for further operation */
256 count
= lttng_list_sessions(&sessions
);
258 ERR("%s", lttng_strerror(count
));
259 command_ret
= CMD_ERROR
;
264 /* Ignore session name in case all sessions are to be cleaned */
266 command_ret
= clear_all_sessions(sessions
, count
);
268 ERR("%s", lttng_strerror(command_ret
));
272 /* Find the corresponding lttng_session struct */
274 for (i
= 0; i
< count
; i
++) {
275 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
277 command_ret
= clear_session(&sessions
[i
]);
279 ERR("%s", lttng_strerror(command_ret
));
286 ERR("Session name %s not found", session_name
);
287 command_ret
= LTTNG_ERR_SESS_NOT_FOUND
;
296 /* Close sessions and output element element */
297 ret
= mi_lttng_close_multi_element(writer
, 2);
304 ret
= mi_lttng_writer_write_element_bool(writer
,
305 mi_lttng_element_command_success
, success
);
311 /* Command element close */
312 ret
= mi_lttng_writer_command_close(writer
);
320 if (writer
&& mi_lttng_writer_destroy(writer
)) {
321 /* Preserve original error code */
322 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
328 /* Overwrite ret if an error occurred during clear_session/all */
329 ret
= command_ret
? command_ret
: ret
;