Fix: lttng: poptGetArg doesn't provide string ownership
[lttng-tools.git] / src / bin / lttng / commands / save.c
index d90ae06344179d8013e398c59a2a4c57e00049d5..2801978759d23f146e0097b85796a7dc4d613c79 100644 (file)
@@ -1,21 +1,11 @@
 /*
- * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
- * 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 <inttypes.h>
 #include <popt.h>
 #include <stdio.h>
 #include <lttng/save.h>
 
 static char *opt_output_path;
-static int opt_force;
-static int opt_save_all;
+static bool opt_force;
+static bool opt_save_all;
+static struct mi_writer *writer;
+
+#ifdef LTTNG_EMBED_HELP
+static const char help_msg[] =
+#include <lttng-save.1.h>
+;
+#endif
 
 enum {
        OPT_HELP = 1,
        OPT_ALL,
        OPT_FORCE,
+       OPT_LIST_OPTIONS,
 };
 
 static struct poptOption save_opts[] = {
        /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
-       {"help",        'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
-       {"all",         'a', POPT_ARG_NONE, 0, OPT_ALL, 0, 0},
-       {"output-path", 'o', POPT_ARG_STRING, &opt_output_path, 0, 0, 0},
-       {"force",       'f', POPT_ARG_NONE, 0, OPT_FORCE, 0, 0},
+       {"help",        'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL},
+       {"all",         'a', POPT_ARG_NONE, NULL, OPT_ALL, NULL, NULL},
+       {"output-path", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL},
+       {"force",       'f', POPT_ARG_NONE, NULL, OPT_FORCE, NULL, NULL},
+       {"list-options",  0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
        {0, 0, 0, 0, 0, 0, 0}
 };
 
-static struct mi_writer *writer;
-
-/*
- * usage
- */
-static void usage(FILE *ofp)
-{
-       fprintf(ofp, "usage: lttng save [OPTIONS] [SESSION]\n");
-       fprintf(ofp, "\n");
-       fprintf(ofp, "Options:\n");
-       fprintf(ofp, "  -h, --help           Show this help\n");
-       fprintf(ofp, "  -a, --all            Save all sessions (default)\n");
-       fprintf(ofp, "  -o, --output-path    Output path of the session configuration(s)\n");
-       fprintf(ofp, "  -f, --force          Overwrite existing session configuration(s)\n");
-}
-
 static int mi_partial_session(const char *session_name)
 {
        int ret;
@@ -134,7 +117,7 @@ int cmd_save(int argc, const char **argv)
 {
        int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success;
        int opt;
-       const char *session_name = NULL;
+       const char *arg_session_name = NULL, *leftover = NULL;
        poptContext pc;
        struct lttng_save_session_attr *attr;
 
@@ -144,38 +127,47 @@ int cmd_save(int argc, const char **argv)
        while ((opt = poptGetNextOpt(pc)) != -1) {
                switch (opt) {
                case OPT_HELP:
-                       usage(stdout);
+                       SHOW_HELP();
                        goto end;
                case OPT_ALL:
-                       opt_save_all = 1;
+                       opt_save_all = true;
                        break;
                case OPT_FORCE:
-                       opt_force = 1;
+                       opt_force = true;
                        break;
+               case OPT_LIST_OPTIONS:
+                       list_cmd_options(stdout, save_opts);
+                       goto end;
                default:
-                       usage(stderr);
                        ret = CMD_UNDEFINED;
                        goto end;
                }
        }
 
        if (!opt_save_all) {
-               session_name = poptGetArg(pc);
-               if (session_name) {
-                       DBG2("Session name: %s", session_name);
+               arg_session_name = poptGetArg(pc);
+               if (arg_session_name) {
+                       DBG2("Session name: %s", arg_session_name);
                } else {
                        /* default to opt_save_all */
-                       opt_save_all = 1;
+                       opt_save_all = true;
                }
        }
 
+       leftover = poptGetArg(pc);
+       if (leftover) {
+               ERR("Unknown argument: %s", leftover);
+               ret = CMD_ERROR;
+               goto end;
+       }
+
        attr = lttng_save_session_attr_create();
        if (!attr) {
                ret = CMD_FATAL;
                goto end_destroy;
        }
 
-       if (lttng_save_session_attr_set_session_name(attr, session_name)) {
+       if (lttng_save_session_attr_set_session_name(attr, arg_session_name)) {
                ret = CMD_ERROR;
                goto end_destroy;
        }
@@ -221,12 +213,12 @@ int cmd_save(int argc, const char **argv)
                success = 0;
        } else {
                /* Inform the user of what just happened on success. */
-               if (session_name && opt_output_path) {
-                       MSG("Session %s saved successfully in %s.", session_name,
+               if (arg_session_name && opt_output_path) {
+                       MSG("Session %s saved successfully in %s.", arg_session_name,
                                        opt_output_path);
-               } else if (session_name && !opt_output_path) {
-                       MSG("Session %s saved successfully.", session_name);
-               } else if (!session_name && opt_output_path) {
+               } else if (arg_session_name && !opt_output_path) {
+                       MSG("Session %s saved successfully.", arg_session_name);
+               } else if (!arg_session_name && opt_output_path) {
                        MSG("All sessions have been saved successfully in %s.",
                                        opt_output_path);
                } else {
@@ -238,7 +230,7 @@ int cmd_save(int argc, const char **argv)
        /* Mi Printing and closing */
        if (lttng_opt_mi) {
                /* Mi print */
-               ret = mi_save_print(session_name);
+               ret = mi_save_print(arg_session_name);
                if (ret) {
                        ret = CMD_ERROR;
                        goto end_destroy;
This page took 0.026376 seconds and 4 git commands to generate.