X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng%2Fcommands%2Fdestroy.c;h=f26da066977da67bc0fe945d55a7162cfc08196d;hp=151686ff9061b157fa9b33aadd8c6924550e534b;hb=f73fabfda365d22e7dd180fb1614e37c446fbd9e;hpb=10a8a2237343699e3923d87e24dbf2d7fe225377 diff --git a/src/bin/lttng/commands/destroy.c b/src/bin/lttng/commands/destroy.c index 151686ff9..f26da0669 100644 --- a/src/bin/lttng/commands/destroy.c +++ b/src/bin/lttng/commands/destroy.c @@ -1,19 +1,18 @@ /* * Copyright (C) 2011 - David Goulet * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; only version 2 - * of the License. + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2 only, + * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #define _GNU_SOURCE @@ -25,19 +24,23 @@ #include #include -#include "../cmd.h" -#include "../conf.h" -#include "../utils.h" +#include "../command.h" + +#include static char *opt_session_name; +static int opt_destroy_all; enum { OPT_HELP = 1, + OPT_LIST_OPTIONS, }; static struct poptOption long_options[] = { /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, + {"all", 'a', POPT_ARG_VAL, &opt_destroy_all, 1, 0, 0}, + {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, {0, 0, 0, 0, 0, 0, 0} }; @@ -46,57 +49,66 @@ static struct poptOption long_options[] = { */ static void usage(FILE *ofp) { - fprintf(ofp, "usage: lttng destroy [options] [NAME]\n"); + fprintf(ofp, "usage: lttng destroy [NAME] [OPTIONS]\n"); fprintf(ofp, "\n"); fprintf(ofp, "Where NAME is an optional session name. If not specified, lttng will\n"); fprintf(ofp, "get it from the configuration directory (.lttng).\n"); fprintf(ofp, "\n"); + fprintf(ofp, "Options:\n"); fprintf(ofp, " -h, --help Show this help\n"); + fprintf(ofp, " -a, --all Destroy all sessions\n"); + fprintf(ofp, " --list-options Simple listing of options\n"); fprintf(ofp, "\n"); } /* - * Destroy a session removing the config directory and unregistering to the - * session daemon. + * destroy_session + * + * Unregister the provided session to the session daemon. On success, removes + * the default configuration. */ -static int destroy_session() +static int destroy_session(const char *session_name) { int ret; - char *session_name, *path; - - if (opt_session_name == NULL) { - session_name = get_session_name(); - if (session_name == NULL) { - ret = CMD_ERROR; - goto error; - } - } else { - session_name = opt_session_name; - } ret = lttng_destroy_session(session_name); if (ret < 0) { - goto free_name; - } - - path = config_get_default_path(); - if (path == NULL) { - ret = CMD_FATAL; - goto free_name; - } - - if (opt_session_name == NULL) { - config_destroy(path); - MSG("Session %s destroyed at %s", session_name, path); - } else { - MSG("Session %s destroyed", session_name); + switch (-ret) { + case LTTNG_ERR_SESS_NOT_FOUND: + WARN("Session name %s not found", session_name); + break; + default: + break; + } + goto error; } + MSG("Session %s destroyed", session_name); + config_destroy_default(); ret = CMD_SUCCESS; +error: + return ret; +} -free_name: - if (opt_session_name == NULL) { - free(session_name); +/* + * destroy_all_sessions + * + * Call destroy_sessions for each registered sessions + */ +static int destroy_all_sessions() +{ + int count, i, ret = CMD_SUCCESS; + struct lttng_session *sessions; + + count = lttng_list_sessions(&sessions); + if (count == 0) { + MSG("No session found, nothing to do."); + } + for (i = 0; i < count; i++) { + ret = destroy_session(sessions[i].name); + if (ret < 0) { + goto error; + } } error: return ret; @@ -107,8 +119,10 @@ error: */ int cmd_destroy(int argc, const char **argv) { - int opt, ret = CMD_SUCCESS; + int opt; + int ret = CMD_SUCCESS; static poptContext pc; + char *session_name = NULL; pc = poptGetContext(NULL, argc, argv, long_options, 0); poptReadDefaultConfig(pc, 0); @@ -116,19 +130,45 @@ int cmd_destroy(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stderr); - goto end; + usage(stdout); + break; + case OPT_LIST_OPTIONS: + list_cmd_options(stdout, long_options); + break; default: usage(stderr); ret = CMD_UNDEFINED; - goto end; + break; } + goto end; } - opt_session_name = (char*) poptGetArg(pc); + /* Ignore session name in case all sessions are to be destroyed */ + if (opt_destroy_all) { + ret = destroy_all_sessions(); + goto end; + } + + opt_session_name = (char *) poptGetArg(pc); + + if (opt_session_name == NULL) { + /* No session name specified, lookup default */ + session_name = get_session_name(); + if (session_name == NULL) { + ret = CMD_ERROR; + goto end; + } + } else { + session_name = opt_session_name; + } - ret = destroy_session(); + ret = destroy_session(session_name); end: + if (opt_session_name == NULL) { + free(session_name); + } + + poptFreeContext(pc); return ret; }