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
;
37 static struct mi_writer
*writer
;
44 static struct poptOption long_options
[] = {
45 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
46 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
47 {"all", 'a', POPT_ARG_VAL
, &opt_destroy_all
, 1, 0, 0},
48 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
55 static void usage(FILE *ofp
)
57 fprintf(ofp
, "usage: lttng destroy [NAME] [OPTIONS]\n");
59 fprintf(ofp
, "Where NAME is an optional session name. If not specified, lttng will\n");
60 fprintf(ofp
, "get it from the configuration directory (.lttng).\n");
62 fprintf(ofp
, "Options:\n");
63 fprintf(ofp
, " -h, --help Show this help\n");
64 fprintf(ofp
, " -a, --all Destroy all sessions\n");
65 fprintf(ofp
, " --list-options Simple listing of options\n");
72 * Unregister the provided session to the session daemon. On success, removes
73 * the default configuration.
75 static int destroy_session(struct lttng_session
*session
)
78 char *session_name
= NULL
;
80 ret
= lttng_destroy_session(session
->name
);
83 case LTTNG_ERR_SESS_NOT_FOUND
:
84 WARN("Session name %s not found", session
->name
);
87 ERR("%s", lttng_strerror(ret
));
93 MSG("Session %s destroyed", session
->name
);
95 session_name
= get_session_name_quiet();
96 if (session_name
&& !strncmp(session
->name
, session_name
, NAME_MAX
)) {
97 config_destroy_default();
101 ret
= mi_lttng_session(writer
, session
, 0);
115 * destroy_all_sessions
117 * Call destroy_sessions for each registered sessions
119 static int destroy_all_sessions(struct lttng_session
*sessions
, int count
)
121 int i
, ret
= CMD_SUCCESS
;
124 MSG("No session found, nothing to do.");
125 } else if (count
< 0) {
126 ERR("%s", lttng_strerror(ret
));
130 for (i
= 0; i
< count
; i
++) {
131 ret
= destroy_session(&sessions
[i
]);
141 * The 'destroy <options>' first level command
143 int cmd_destroy(int argc
, const char **argv
)
146 int ret
= CMD_SUCCESS
, i
, command_ret
= CMD_SUCCESS
, success
= 1;
147 static poptContext pc
;
148 char *session_name
= NULL
;
150 struct lttng_session
*sessions
;
154 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
155 poptReadDefaultConfig(pc
, 0);
157 while ((opt
= poptGetNextOpt(pc
)) != -1) {
162 case OPT_LIST_OPTIONS
:
163 list_cmd_options(stdout
, long_options
);
175 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
177 ret
= -LTTNG_ERR_NOMEM
;
181 /* Open command element */
182 ret
= mi_lttng_writer_command_open(writer
,
183 mi_lttng_element_command_destroy
);
189 /* Open output element */
190 ret
= mi_lttng_writer_open_element(writer
,
191 mi_lttng_element_command_output
);
197 /* For validation and semantic purpose we open a sessions element */
198 ret
= mi_lttng_sessions_open(writer
);
205 /* Recuperate all sessions for further operation */
206 count
= lttng_list_sessions(&sessions
);
213 /* Ignore session name in case all sessions are to be destroyed */
214 if (opt_destroy_all
) {
215 command_ret
= destroy_all_sessions(sessions
, count
);
220 opt_session_name
= (char *) poptGetArg(pc
);
222 if (!opt_session_name
) {
223 /* No session name specified, lookup default */
224 session_name
= get_session_name();
225 if (session_name
== NULL
) {
226 command_ret
= CMD_ERROR
;
231 session_name
= opt_session_name
;
234 /* Find the corresponding lttng_session struct */
236 for (i
= 0; i
< count
; i
++) {
237 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
239 command_ret
= destroy_session(&sessions
[i
]);
248 ERR("Session name %s not found", session_name
);
249 command_ret
= LTTNG_ERR_SESS_NOT_FOUND
;
258 /* Close sessions and output element element */
259 ret
= mi_lttng_close_multi_element(writer
, 2);
266 ret
= mi_lttng_writer_write_element_bool(writer
,
267 mi_lttng_element_command_success
, success
);
273 /* Command element close */
274 ret
= mi_lttng_writer_command_close(writer
);
282 if (writer
&& mi_lttng_writer_destroy(writer
)) {
283 /* Preserve original error code */
284 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
287 if (opt_session_name
== NULL
) {
291 /* Overwrite ret if an error occurred during destroy_session/all */
292 ret
= command_ret
? command_ret
: ret
;