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>
28 #include "../command.h"
30 #include <common/mi-lttng.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
32 #include <common/utils.h>
34 static char *opt_session_name
;
35 static int opt_clear_all
;
37 #ifdef LTTNG_EMBED_HELP
38 static const char help_msg
[] =
39 #include <lttng-clear.1.h>
44 static struct mi_writer
*writer
;
51 static struct poptOption long_options
[] = {
52 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
53 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
54 {"all", 'a', POPT_ARG_VAL
, &opt_clear_all
, 1, 0, 0},
55 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
63 static int clear_session(struct lttng_session
*session
)
66 char *session_name
= NULL
;
68 ret
= lttng_clear_session(session
->name
);
70 ERR("%s", lttng_strerror(ret
));
74 MSG("Session %s cleared", session
->name
);
77 ret
= mi_lttng_session(writer
, session
, 0);
93 * Call clear_session for each registered sessions
95 static int clear_all_sessions(struct lttng_session
*sessions
, int count
)
97 int i
, ret
= CMD_SUCCESS
;
100 MSG("No session found, nothing to do.");
101 } else if (count
< 0) {
102 ERR("%s", lttng_strerror(ret
));
106 for (i
= 0; i
< count
; i
++) {
107 ret
= clear_session(&sessions
[i
]);
117 * The 'clear <options>' first level command
119 int cmd_clear(int argc
, const char **argv
)
122 int ret
= CMD_SUCCESS
, i
, command_ret
= CMD_SUCCESS
, success
= 1;
123 static poptContext pc
;
124 char *session_name
= NULL
;
125 const char *leftover
= NULL
;
127 struct lttng_session
*sessions
;
131 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
132 poptReadDefaultConfig(pc
, 0);
134 while ((opt
= poptGetNextOpt(pc
)) != -1) {
139 case OPT_LIST_OPTIONS
:
140 list_cmd_options(stdout
, long_options
);
151 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
153 ret
= -LTTNG_ERR_NOMEM
;
157 /* Open command element */
158 ret
= mi_lttng_writer_command_open(writer
,
159 mi_lttng_element_command_clear
);
165 /* Open output element */
166 ret
= mi_lttng_writer_open_element(writer
,
167 mi_lttng_element_command_output
);
173 /* For validation and semantic purpose we open a sessions element */
174 ret
= mi_lttng_sessions_open(writer
);
181 if (!opt_clear_all
) {
182 opt_session_name
= (char *) poptGetArg(pc
);
184 if (!opt_session_name
) {
185 /* No session name specified, lookup default */
186 session_name
= get_session_name();
187 if (session_name
== NULL
) {
188 command_ret
= CMD_ERROR
;
193 session_name
= opt_session_name
;
199 leftover
= poptGetArg(pc
);
201 ERR("Unknown argument: %s", leftover
);
207 /* Recuperate all sessions for further operation */
208 count
= lttng_list_sessions(&sessions
);
210 ERR("%s", lttng_strerror(count
));
211 command_ret
= CMD_ERROR
;
216 /* Ignore session name in case all sessions are to be cleaned */
218 command_ret
= clear_all_sessions(sessions
, count
);
223 /* Find the corresponding lttng_session struct */
225 for (i
= 0; i
< count
; i
++) {
226 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
228 command_ret
= clear_session(&sessions
[i
]);
236 ERR("Session name %s not found", session_name
);
237 command_ret
= LTTNG_ERR_SESS_NOT_FOUND
;
247 /* Close sessions and output element element */
248 ret
= mi_lttng_close_multi_element(writer
, 2);
255 ret
= mi_lttng_writer_write_element_bool(writer
,
256 mi_lttng_element_command_success
, success
);
262 /* Command element close */
263 ret
= mi_lttng_writer_command_close(writer
);
271 if (writer
&& mi_lttng_writer_destroy(writer
)) {
272 /* Preserve original error code */
273 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
276 if (opt_session_name
== NULL
) {
280 /* Overwrite ret if an error occurred during clear_session/all */
281 ret
= command_ret
? command_ret
: ret
;