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.
25 #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
;
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
},
56 static void usage(FILE *ofp
)
58 fprintf(ofp
, "usage: lttng destroy [NAME] [OPTIONS]\n");
60 fprintf(ofp
, "Where NAME is an optional session name. If not specified, lttng will\n");
61 fprintf(ofp
, "get it from the configuration directory (.lttng).\n");
63 fprintf(ofp
, "Options:\n");
64 fprintf(ofp
, " -h, --help Show this help\n");
65 fprintf(ofp
, " -a, --all Destroy all sessions\n");
66 fprintf(ofp
, " --list-options Simple listing of options\n");
73 * Unregister the provided session to the session daemon. On success, removes
74 * the default configuration.
76 static int destroy_session(struct lttng_session
*session
)
79 char *session_name
= NULL
;
81 ret
= lttng_destroy_session(session
->name
);
84 case LTTNG_ERR_SESS_NOT_FOUND
:
85 WARN("Session name %s not found", session
->name
);
88 ERR("%s", lttng_strerror(ret
));
94 MSG("Session %s destroyed", session
->name
);
96 session_name
= get_session_name_quiet();
97 if (session_name
&& !strncmp(session
->name
, session_name
, NAME_MAX
)) {
98 config_destroy_default();
102 ret
= mi_lttng_session(writer
, session
, 0);
116 * destroy_all_sessions
118 * Call destroy_sessions for each registered sessions
120 static int destroy_all_sessions(struct lttng_session
*sessions
, int count
)
122 int i
, ret
= CMD_SUCCESS
;
125 MSG("No session found, nothing to do.");
126 } else if (count
< 0) {
127 ERR("%s", lttng_strerror(ret
));
131 for (i
= 0; i
< count
; i
++) {
132 ret
= destroy_session(&sessions
[i
]);
142 * The 'destroy <options>' first level command
144 int cmd_destroy(int argc
, const char **argv
)
147 int ret
= CMD_SUCCESS
, i
, command_ret
= CMD_SUCCESS
, success
= 1;
148 static poptContext pc
;
149 char *session_name
= NULL
;
151 struct lttng_session
*sessions
;
155 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
156 poptReadDefaultConfig(pc
, 0);
158 while ((opt
= poptGetNextOpt(pc
)) != -1) {
163 case OPT_LIST_OPTIONS
:
164 list_cmd_options(stdout
, long_options
);
176 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
178 ret
= -LTTNG_ERR_NOMEM
;
182 /* Open command element */
183 ret
= mi_lttng_writer_command_open(writer
,
184 mi_lttng_element_command_destroy
);
190 /* Open output element */
191 ret
= mi_lttng_writer_open_element(writer
,
192 mi_lttng_element_command_output
);
198 /* For validation and semantic purpose we open a sessions element */
199 ret
= mi_lttng_sessions_open(writer
);
206 /* Recuperate all sessions for further operation */
207 count
= lttng_list_sessions(&sessions
);
214 /* Ignore session name in case all sessions are to be destroyed */
215 if (opt_destroy_all
) {
216 command_ret
= destroy_all_sessions(sessions
, count
);
221 opt_session_name
= (char *) poptGetArg(pc
);
223 if (!opt_session_name
) {
224 /* No session name specified, lookup default */
225 session_name
= get_session_name();
226 if (session_name
== NULL
) {
227 command_ret
= CMD_ERROR
;
232 session_name
= opt_session_name
;
235 /* Find the corresponding lttng_session struct */
237 for (i
= 0; i
< count
; i
++) {
238 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
240 command_ret
= destroy_session(&sessions
[i
]);
249 ERR("Session name %s not found", session_name
);
250 command_ret
= LTTNG_ERR_SESS_NOT_FOUND
;
259 /* Close sessions and output element element */
260 ret
= mi_lttng_close_multi_element(writer
, 2);
267 ret
= mi_lttng_writer_write_element_bool(writer
,
268 mi_lttng_element_command_success
, success
);
274 /* Command element close */
275 ret
= mi_lttng_writer_command_close(writer
);
283 if (writer
&& mi_lttng_writer_destroy(writer
)) {
284 /* Preserve original error code */
285 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
288 if (opt_session_name
== NULL
) {
292 /* Overwrite ret if an error occurred during destroy_session/all */
293 ret
= command_ret
? command_ret
: ret
;