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
;
39 static struct mi_writer
*writer
;
46 static struct poptOption long_options
[] = {
47 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
48 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
49 {"all", 'a', POPT_ARG_VAL
, &opt_destroy_all
, 1, 0, 0},
50 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
51 {"no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, 0, 0},
58 * Unregister the provided session to the session daemon. On success, removes
59 * the default configuration.
61 static int destroy_session(struct lttng_session
*session
)
64 char *session_name
= NULL
;
65 bool session_was_stopped
;
67 ret
= lttng_stop_tracing_no_wait(session
->name
);
68 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
69 ERR("%s", lttng_strerror(ret
));
71 session_was_stopped
= ret
== -LTTNG_ERR_TRACE_ALREADY_STOPPED
;
73 _MSG("Waiting for data availability");
76 ret
= lttng_data_pending(session
->name
);
78 /* Return the data available call error. */
83 * Data sleep time before retrying (in usec). Don't sleep if the call
84 * returned value indicates availability.
87 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
94 if (!session_was_stopped
) {
96 * Don't print the event and packet loss warnings since the user
97 * already saw them when stopping the trace.
99 print_session_stats(session
->name
);
102 ret
= lttng_destroy_session_no_wait(session
->name
);
107 MSG("Session %s destroyed", session
->name
);
109 session_name
= get_session_name_quiet();
110 if (session_name
&& !strncmp(session
->name
, session_name
, NAME_MAX
)) {
111 config_destroy_default();
115 ret
= mi_lttng_session(writer
, session
, 0);
129 * destroy_all_sessions
131 * Call destroy_sessions for each registered sessions
133 static int destroy_all_sessions(struct lttng_session
*sessions
, int count
)
135 int i
, ret
= CMD_SUCCESS
;
138 MSG("No session found, nothing to do.");
139 } else if (count
< 0) {
140 ERR("%s", lttng_strerror(ret
));
144 for (i
= 0; i
< count
; i
++) {
145 ret
= destroy_session(&sessions
[i
]);
155 * The 'destroy <options>' first level command
157 int cmd_destroy(int argc
, const char **argv
)
160 int ret
= CMD_SUCCESS
, i
, command_ret
= CMD_SUCCESS
, success
= 1;
161 static poptContext pc
;
162 char *session_name
= NULL
;
164 struct lttng_session
*sessions
;
168 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
169 poptReadDefaultConfig(pc
, 0);
171 while ((opt
= poptGetNextOpt(pc
)) != -1) {
176 case OPT_LIST_OPTIONS
:
177 list_cmd_options(stdout
, long_options
);
188 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
190 ret
= -LTTNG_ERR_NOMEM
;
194 /* Open command element */
195 ret
= mi_lttng_writer_command_open(writer
,
196 mi_lttng_element_command_destroy
);
202 /* Open output element */
203 ret
= mi_lttng_writer_open_element(writer
,
204 mi_lttng_element_command_output
);
210 /* For validation and semantic purpose we open a sessions element */
211 ret
= mi_lttng_sessions_open(writer
);
218 /* Recuperate all sessions for further operation */
219 count
= lttng_list_sessions(&sessions
);
226 /* Ignore session name in case all sessions are to be destroyed */
227 if (opt_destroy_all
) {
228 command_ret
= destroy_all_sessions(sessions
, count
);
233 opt_session_name
= (char *) poptGetArg(pc
);
235 if (!opt_session_name
) {
236 /* No session name specified, lookup default */
237 session_name
= get_session_name();
238 if (session_name
== NULL
) {
239 command_ret
= CMD_ERROR
;
244 session_name
= opt_session_name
;
247 /* Find the corresponding lttng_session struct */
249 for (i
= 0; i
< count
; i
++) {
250 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
252 command_ret
= destroy_session(&sessions
[i
]);
261 ERR("Session name %s not found", session_name
);
262 command_ret
= LTTNG_ERR_SESS_NOT_FOUND
;
271 /* Close sessions and output element element */
272 ret
= mi_lttng_close_multi_element(writer
, 2);
279 ret
= mi_lttng_writer_write_element_bool(writer
,
280 mi_lttng_element_command_success
, success
);
286 /* Command element close */
287 ret
= mi_lttng_writer_command_close(writer
);
295 if (writer
&& mi_lttng_writer_destroy(writer
)) {
296 /* Preserve original error code */
297 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
300 if (opt_session_name
== NULL
) {
304 /* Overwrite ret if an error occurred during destroy_session/all */
305 ret
= command_ret
? command_ret
: ret
;