X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fbin%2Flttng%2Fcommands%2Fset_session.c;h=fb42d14d5d5e5b931485383c71c291c52e30821e;hb=a0377dfefe40662ba7d68617bce6ff467114136c;hp=3196f12bdd3863373938267a4106241eaac9100c;hpb=6c1c0768320135c6936c371b09731851b508c023;p=lttng-tools.git diff --git a/src/bin/lttng/commands/set_session.c b/src/bin/lttng/commands/set_session.c index 3196f12bd..fb42d14d5 100644 --- a/src/bin/lttng/commands/set_session.c +++ b/src/bin/lttng/commands/set_session.c @@ -1,21 +1,10 @@ /* - * Copyright (C) 2011 - David Goulet + * 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, version 2 only, - * as published by the Free Software Foundation. + * SPDX-License-Identifier: GPL-2.0-only * - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#define _GNU_SOURCE #define _LGPL_SOURCE #include #include @@ -24,7 +13,6 @@ #include #include #include -#include #include @@ -32,6 +20,12 @@ static char *opt_session_name; +#ifdef LTTNG_EMBED_HELP +static const char help_msg[] = +#include +; +#endif + enum { OPT_HELP = 1, OPT_LIST_OPTIONS, @@ -46,19 +40,6 @@ static struct poptOption long_options[] = { {0, 0, 0, 0, 0, 0, 0} }; -/* - * usage - */ -static void usage(FILE *ofp) -{ - fprintf(ofp, "usage: lttng set-session NAME [OPTIONS]\n"); - fprintf(ofp, "\n"); - fprintf(ofp, "Options:\n"); - fprintf(ofp, " -h, --help Show this help\n"); - fprintf(ofp, " --list-options Simple listing of options\n"); - fprintf(ofp, "\n"); -} - /* * Print the necessary mi for a session and name. */ @@ -66,8 +47,8 @@ static int mi_print(char *session_name) { int ret; - assert(writer); - assert(session_name); + LTTNG_ASSERT(writer); + LTTNG_ASSERT(session_name); /* * Open a sessions element @@ -106,11 +87,34 @@ end: static int set_session(void) { int ret = CMD_SUCCESS; + int count, i; + unsigned int session_found = 0; + struct lttng_session *sessions; if (opt_session_name && strlen(opt_session_name) > NAME_MAX) { ERR("Session name too long. Length must be lower or equal to %d", NAME_MAX); ret = CMD_ERROR; + goto end; + } + + count = lttng_list_sessions(&sessions); + if (count < 0) { + ret = CMD_ERROR; + ERR("%s", lttng_strerror(count)); + goto end; + } + + for (i = 0; i < count; i++) { + if (strncmp(sessions[i].name, opt_session_name, NAME_MAX) == 0) { + session_found = 1; + break; + } + } + + if (!session_found) { + ERR("Session '%s' not found", opt_session_name); + ret = CMD_ERROR; goto error; } @@ -133,6 +137,8 @@ static int set_session(void) ret = CMD_SUCCESS; error: + free(sessions); +end: return ret; } @@ -150,13 +156,12 @@ int cmd_set_session(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout); + SHOW_HELP(); goto end; case OPT_LIST_OPTIONS: list_cmd_options(stdout, long_options); goto end; default: - usage(stderr); ret = CMD_UNDEFINED; goto end; } @@ -165,7 +170,6 @@ int cmd_set_session(int argc, const char **argv) opt_session_name = (char *) poptGetArg(pc); if (opt_session_name == NULL) { ERR("Missing session name"); - usage(stderr); ret = CMD_ERROR; goto end; } @@ -232,7 +236,7 @@ end: ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL; } - /* Overwrite ret if an error occured during set_session() */ + /* Overwrite ret if an error occurred during set_session() */ ret = command_ret ? command_ret : ret; poptFreeContext(pc);