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>
27 #include "../command.h"
29 #include <common/mi-lttng.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
31 #include <common/utils.h>
33 static char *opt_session_name
;
34 static int opt_destroy_all
;
35 static int opt_no_wait
;
38 static struct mi_writer
*writer
;
45 static struct poptOption long_options
[] = {
46 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
47 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
48 {"all", 'a', POPT_ARG_VAL
, &opt_destroy_all
, 1, 0, 0},
49 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
50 {"no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, 0, 0},
57 static void usage(FILE *ofp
)
59 fprintf(ofp
, "usage: lttng destroy [NAME] [OPTIONS]\n");
61 fprintf(ofp
, "Where NAME is an optional session name. If not specified, lttng will\n");
62 fprintf(ofp
, "get it from the configuration directory (.lttng).\n");
64 fprintf(ofp
, "Options:\n");
65 fprintf(ofp
, " -h, --help Show this help\n");
66 fprintf(ofp
, " -a, --all Destroy all sessions\n");
67 fprintf(ofp
, " --list-options Simple listing of options\n");
68 fprintf(ofp
, " -n, --no-wait Don't wait for data availability\n");
75 * Unregister the provided session to the session daemon. On success, removes
76 * the default configuration.
78 static int destroy_session(struct lttng_session
*session
)
81 char *session_name
= NULL
;
83 ret
= lttng_stop_tracing_no_wait(session
->name
);
84 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
85 ERR("%s", lttng_strerror(ret
));
88 _MSG("Waiting for data availability");
91 ret
= lttng_data_pending(session
->name
);
93 /* Return the data available call error. */
98 * Data sleep time before retrying (in usec). Don't sleep if the call
99 * returned value indicates availability.
102 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
110 ret
= lttng_destroy_session_no_wait(session
->name
);
115 MSG("Session %s destroyed", session
->name
);
117 session_name
= get_session_name_quiet();
118 if (session_name
&& !strncmp(session
->name
, session_name
, NAME_MAX
)) {
119 config_destroy_default();
123 ret
= mi_lttng_session(writer
, session
, 0);
137 * destroy_all_sessions
139 * Call destroy_sessions for each registered sessions
141 static int destroy_all_sessions(struct lttng_session
*sessions
, int count
)
143 int i
, ret
= CMD_SUCCESS
;
146 MSG("No session found, nothing to do.");
147 } else if (count
< 0) {
148 ERR("%s", lttng_strerror(ret
));
152 for (i
= 0; i
< count
; i
++) {
153 ret
= destroy_session(&sessions
[i
]);
163 * The 'destroy <options>' first level command
165 int cmd_destroy(int argc
, const char **argv
)
168 int ret
= CMD_SUCCESS
, i
, command_ret
= CMD_SUCCESS
, success
= 1;
169 static poptContext pc
;
170 char *session_name
= NULL
;
172 struct lttng_session
*sessions
;
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
);
197 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
199 ret
= -LTTNG_ERR_NOMEM
;
203 /* Open command element */
204 ret
= mi_lttng_writer_command_open(writer
,
205 mi_lttng_element_command_destroy
);
211 /* Open output element */
212 ret
= mi_lttng_writer_open_element(writer
,
213 mi_lttng_element_command_output
);
219 /* For validation and semantic purpose we open a sessions element */
220 ret
= mi_lttng_sessions_open(writer
);
227 /* Recuperate all sessions for further operation */
228 count
= lttng_list_sessions(&sessions
);
235 /* Ignore session name in case all sessions are to be destroyed */
236 if (opt_destroy_all
) {
237 command_ret
= destroy_all_sessions(sessions
, count
);
242 opt_session_name
= (char *) poptGetArg(pc
);
244 if (!opt_session_name
) {
245 /* No session name specified, lookup default */
246 session_name
= get_session_name();
247 if (session_name
== NULL
) {
248 command_ret
= CMD_ERROR
;
253 session_name
= opt_session_name
;
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
= destroy_session(&sessions
[i
]);
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
;
309 if (opt_session_name
== NULL
) {
313 /* Overwrite ret if an error occurred during destroy_session/all */
314 ret
= command_ret
? command_ret
: ret
;