2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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_destroy_all
;
36 static int opt_no_wait
;
38 #ifdef LTTNG_EMBED_HELP
39 static const char help_msg
[] =
40 #include <lttng-destroy.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_destroy_all
, 1, 0, 0},
56 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
57 {"no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, 0, 0},
64 * Unregister the provided session to the session daemon. On success, removes
65 * the default configuration.
67 static int destroy_session(struct lttng_session
*session
)
70 char *session_name
= NULL
;
71 bool session_was_stopped
;
73 ret
= lttng_stop_tracing_no_wait(session
->name
);
74 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
75 ERR("%s", lttng_strerror(ret
));
77 session_was_stopped
= ret
== -LTTNG_ERR_TRACE_ALREADY_STOPPED
;
79 bool printed_wait_msg
= false;
82 ret
= lttng_data_pending(session
->name
);
84 /* Return the data available call error. */
89 * Data sleep time before retrying (in usec). Don't sleep if the call
90 * returned value indicates availability.
93 if (!printed_wait_msg
) {
94 _MSG("Waiting for data availability");
98 printed_wait_msg
= true;
99 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
104 if (printed_wait_msg
) {
108 if (!session_was_stopped
) {
110 * Don't print the event and packet loss warnings since the user
111 * already saw them when stopping the trace.
113 print_session_stats(session
->name
);
116 ret
= lttng_destroy_session_no_wait(session
->name
);
121 MSG("Session %s destroyed", session
->name
);
123 session_name
= get_session_name_quiet();
124 if (session_name
&& !strncmp(session
->name
, session_name
, NAME_MAX
)) {
125 config_destroy_default();
129 ret
= mi_lttng_session(writer
, session
, 0);
143 * destroy_all_sessions
145 * Call destroy_sessions for each registered sessions
147 static int destroy_all_sessions(struct lttng_session
*sessions
, int count
)
149 int i
, ret
= CMD_SUCCESS
;
152 MSG("No session found, nothing to do.");
153 } else if (count
< 0) {
154 ERR("%s", lttng_strerror(ret
));
158 for (i
= 0; i
< count
; i
++) {
159 ret
= destroy_session(&sessions
[i
]);
169 * The 'destroy <options>' first level command
171 int cmd_destroy(int argc
, const char **argv
)
174 int ret
= CMD_SUCCESS
, i
, command_ret
= CMD_SUCCESS
, success
= 1;
175 static poptContext pc
;
176 char *session_name
= NULL
;
177 const char *leftover
= NULL
;
179 struct lttng_session
*sessions
;
183 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
184 poptReadDefaultConfig(pc
, 0);
186 while ((opt
= poptGetNextOpt(pc
)) != -1) {
191 case OPT_LIST_OPTIONS
:
192 list_cmd_options(stdout
, long_options
);
203 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
205 ret
= -LTTNG_ERR_NOMEM
;
209 /* Open command element */
210 ret
= mi_lttng_writer_command_open(writer
,
211 mi_lttng_element_command_destroy
);
217 /* Open output element */
218 ret
= mi_lttng_writer_open_element(writer
,
219 mi_lttng_element_command_output
);
225 /* For validation and semantic purpose we open a sessions element */
226 ret
= mi_lttng_sessions_open(writer
);
233 /* Recuperate all sessions for further operation */
234 count
= lttng_list_sessions(&sessions
);
236 ERR("%s", lttng_strerror(count
));
237 command_ret
= CMD_ERROR
;
242 /* Ignore session name in case all sessions are to be destroyed */
243 if (opt_destroy_all
) {
244 command_ret
= destroy_all_sessions(sessions
, count
);
249 opt_session_name
= (char *) poptGetArg(pc
);
251 if (!opt_session_name
) {
252 /* No session name specified, lookup default */
253 session_name
= get_session_name();
254 if (session_name
== NULL
) {
255 command_ret
= CMD_ERROR
;
260 session_name
= opt_session_name
;
263 /* Find the corresponding lttng_session struct */
265 for (i
= 0; i
< count
; i
++) {
266 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
268 command_ret
= destroy_session(&sessions
[i
]);
277 ERR("Session name %s not found", session_name
);
278 command_ret
= LTTNG_ERR_SESS_NOT_FOUND
;
284 leftover
= poptGetArg(pc
);
286 ERR("Unknown argument: %s", leftover
);
295 /* Close sessions and output element element */
296 ret
= mi_lttng_close_multi_element(writer
, 2);
303 ret
= mi_lttng_writer_write_element_bool(writer
,
304 mi_lttng_element_command_success
, success
);
310 /* Command element close */
311 ret
= mi_lttng_writer_command_close(writer
);
319 if (writer
&& mi_lttng_writer_destroy(writer
)) {
320 /* Preserve original error code */
321 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
324 if (opt_session_name
== NULL
) {
328 /* Overwrite ret if an error occurred during destroy_session/all */
329 ret
= command_ret
? command_ret
: ret
;