Clean-up: lttng: prepend `the_` to global variables in load.c
[lttng-tools.git] / src / bin / lttng / commands / load.c
index 8fa718ed522fced8bdb942c92f26be0f6b366360..7f5f1a0c59ac37daae5d11d73ca909a340926ab0 100644 (file)
@@ -1,18 +1,8 @@
 /*
- * Copyright (C) 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ * Copyright (C) 2014 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 _LGPL_SOURCE
 
 #include <common/mi-lttng.h>
 #include <common/config/session-config.h>
-#include <lttng/load.h>
+#include <lttng/lttng.h>
 
 #include "../command.h"
 
-static char *opt_input_path;
-static char *opt_override_url;
-static int opt_force;
-static int opt_load_all;
+static char *the_opt_input_path;
+static char *the_opt_override_url;
+static char *the_opt_override_session_name;
+static int the_opt_force;
+static int the_opt_load_all;
+
+static const char *the_session_name;
 
-static const char *session_name;
+#ifdef LTTNG_EMBED_HELP
+static const char help_msg[] =
+#include <lttng-load.1.h>
+;
+#endif
 
 enum {
        OPT_HELP = 1,
@@ -43,39 +40,40 @@ enum {
        OPT_LIST_OPTIONS,
 };
 
-static struct mi_writer *writer;
+static struct mi_writer *the_writer;
 
-static struct poptOption load_opts[] = {
+static struct poptOption the_load_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},
-       {"input-path",  'i',  POPT_ARG_STRING, &opt_input_path, 0, 0, 0},
-       {"force",       'f',  POPT_ARG_NONE, 0, OPT_FORCE, 0, 0},
-       {"override-url",'U',  POPT_ARG_STRING, &opt_override_url, 0, 0, 0},
-       {"list-options",  0,  POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
+       {"help",          'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
+       {"all",           'a', POPT_ARG_NONE, 0, OPT_ALL, 0, 0},
+       {"input-path",    'i', POPT_ARG_STRING, &the_opt_input_path, 0, 0, 0},
+       {"force",         'f', POPT_ARG_NONE, 0, OPT_FORCE, 0, 0},
+       {"override-url",    0, POPT_ARG_STRING, &the_opt_override_url, 0, 0, 0},
+       {"override-name",   0, POPT_ARG_STRING, &the_opt_override_session_name, 0, 0, 0},
+       {"list-options",    0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
        {0, 0, 0, 0, 0, 0, 0}
 };
 
 static int mi_partial_session(const char *session_name)
 {
        int ret;
-       assert(writer);
+       assert(the_writer);
        assert(session_name);
 
        /* Open session element */
-       ret = mi_lttng_writer_open_element(writer, config_element_session);
+       ret = mi_lttng_writer_open_element(the_writer, config_element_session);
        if (ret) {
                goto end;
        }
 
-       ret = mi_lttng_writer_write_element_string(writer, config_element_name,
+       ret = mi_lttng_writer_write_element_string(the_writer, config_element_name,
                        session_name);
        if (ret) {
                goto end;
        }
 
        /* Closing session element */
-       ret = mi_lttng_writer_close_element(writer);
+       ret = mi_lttng_writer_close_element(the_writer);
 end:
        return ret;
 }
@@ -86,15 +84,15 @@ end:
 static int mi_load_print(const char *session_name)
 {
        int ret;
-       assert(writer);
+       assert(the_writer);
 
-       if (opt_load_all) {
+       if (the_opt_load_all) {
                /* We use a wildcard to represent all sessions */
                session_name = "*";
        }
 
        /* Print load element */
-       ret = mi_lttng_writer_open_element(writer, mi_lttng_element_load);
+       ret = mi_lttng_writer_open_element(the_writer, mi_lttng_element_load);
        if (ret) {
                goto end;
        }
@@ -106,17 +104,41 @@ static int mi_load_print(const char *session_name)
        }
 
        /* Path element */
-       if (opt_input_path) {
-               ret = mi_lttng_writer_write_element_string(writer, config_element_path,
-                               opt_input_path);
+       if (the_opt_input_path) {
+               ret = mi_lttng_writer_write_element_string(the_writer, config_element_path,
+                               the_opt_input_path);
+               if (ret) {
+                       goto end;
+               }
+       }
+
+       /* Print override elements */
+       ret = mi_lttng_writer_open_element(the_writer, mi_lttng_element_load_overrides);
+       if (ret) {
+               goto end;
+       }
+
+       /* Session name override element */
+       if (the_opt_override_session_name) {
+               ret = mi_lttng_writer_write_element_string(the_writer,
+                               config_element_name, the_opt_override_session_name);
                if (ret) {
                        goto end;
                }
        }
 
-       /* Close load element */
-       ret = mi_lttng_writer_close_element(writer);
+       /* Session url override element */
+       if (the_opt_override_url) {
+               ret = mi_lttng_writer_write_element_string(the_writer,
+                               mi_lttng_element_load_override_url,
+                               the_opt_override_url);
+               if (ret) {
+                       goto end;
+               }
+       }
 
+       /* Close override and load element */
+       ret = mi_lttng_close_multi_element(the_writer, 2);
 end:
        return ret;
 }
@@ -131,8 +153,9 @@ int cmd_load(int argc, const char **argv)
        poptContext pc;
        struct lttng_load_session_attr *session_attr = NULL;
        char *input_path = NULL;
+       const char *leftover = NULL;
 
-       pc = poptGetContext(NULL, argc, argv, load_opts, 0);
+       pc = poptGetContext(NULL, argc, argv, the_load_opts, 0);
        poptReadDefaultConfig(pc, 0);
 
        while ((opt = poptGetNextOpt(pc)) != -1) {
@@ -142,14 +165,14 @@ int cmd_load(int argc, const char **argv)
                        ret = CMD_SUCCESS;
                        goto end;
                case OPT_ALL:
-                       opt_load_all = 1;
+                       the_opt_load_all = 1;
                        break;
                case OPT_LIST_OPTIONS:
-                       list_cmd_options(stdout, load_opts);
+                       list_cmd_options(stdout, the_load_opts);
                        ret = CMD_SUCCESS;
                        goto end;
                case OPT_FORCE:
-                       opt_force = 1;
+                       the_opt_force = 1;
                        break;
                default:
                        ret = CMD_UNDEFINED;
@@ -164,26 +187,33 @@ int cmd_load(int argc, const char **argv)
                goto end;
        }
 
-       if (!opt_load_all) {
-               session_name = poptGetArg(pc);
-               if (session_name) {
-                       DBG2("Loading session name: %s", session_name);
+       if (!the_opt_load_all) {
+               the_session_name = poptGetArg(pc);
+               if (the_session_name) {
+                       DBG2("Loading session name: %s", the_session_name);
                } else {
                        /* Default to load_all */
-                       opt_load_all = 1;
+                       the_opt_load_all = 1;
                }
        }
 
+       leftover = poptGetArg(pc);
+       if (leftover) {
+               ERR("Unknown argument: %s", leftover);
+               ret = CMD_ERROR;
+               goto end;
+       }
+
        /* Mi check */
        if (lttng_opt_mi) {
-               writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
-               if (!writer) {
+               the_writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
+               if (!the_writer) {
                        ret = CMD_ERROR;
                        goto end;
                }
 
                /* Open command element */
-               ret = mi_lttng_writer_command_open(writer,
+               ret = mi_lttng_writer_command_open(the_writer,
                                mi_lttng_element_command_load);
                if (ret) {
                        ret = CMD_ERROR;
@@ -191,7 +221,7 @@ int cmd_load(int argc, const char **argv)
                }
 
                /* Open output element */
-               ret = mi_lttng_writer_open_element(writer,
+               ret = mi_lttng_writer_open_element(the_writer,
                                mi_lttng_element_command_output);
                if (ret) {
                        ret = CMD_ERROR;
@@ -212,8 +242,8 @@ int cmd_load(int argc, const char **argv)
         * lttng_load_session_attr_set_input_url only suppports absolute path.
         * Use realpath to resolve any relative path.
         * */
-       if (opt_input_path) {
-               input_path = realpath(opt_input_path, NULL);
+       if (the_opt_input_path) {
+               input_path = realpath(the_opt_input_path, NULL);
                if (!input_path) {
                        PERROR("Invalid input path");
                        ret = CMD_ERROR;
@@ -233,7 +263,7 @@ int cmd_load(int argc, const char **argv)
 
        /* Set the session name. NULL means all sessions should be loaded */
        ret = lttng_load_session_attr_set_session_name(session_attr,
-                       session_name);
+                       the_session_name);
        if (ret) {
                ERR("Invalid session name");
                ret = CMD_ERROR;
@@ -241,7 +271,7 @@ int cmd_load(int argc, const char **argv)
        }
 
        /* Set the overwrite attribute */
-       ret = lttng_load_session_attr_set_overwrite(session_attr, opt_force);
+       ret = lttng_load_session_attr_set_overwrite(session_attr, the_opt_force);
        if (ret) {
                ERR("Force argument could not be applied");
                ret = CMD_ERROR;
@@ -249,35 +279,56 @@ int cmd_load(int argc, const char **argv)
        }
 
        /* Set the overrides attributes if any */
-       if (opt_override_url) {
+       if (the_opt_override_url) {
                ret = lttng_load_session_attr_set_override_url(session_attr,
-                               opt_override_url);
+                               the_opt_override_url);
                if (ret) {
                        ERR("Url override is invalid");
                        goto end;
                }
        }
 
+       if (the_opt_override_session_name) {
+               if (the_opt_load_all) {
+                       ERR("Options --all and --override-name cannot be used simultaneously");
+                       ret = CMD_ERROR;
+                       goto end;
+               }
+               ret = lttng_load_session_attr_set_override_session_name(session_attr,
+                               the_opt_override_session_name);
+               if (ret) {
+                       ERR("Failed to set session name override");
+                       ret = CMD_ERROR;
+                       goto end;
+               }
+       }
+
        ret = lttng_load_session(session_attr);
        if (ret) {
                ERR("%s", lttng_strerror(ret));
                success = 0;
                ret = CMD_ERROR;
        } else {
-               if (opt_load_all) {
+               if (the_opt_load_all) {
                        MSG("All sessions have been loaded successfully");
-               } else if (session_name) {
-                       ret = config_init((char *) session_name);
+               } else if (the_session_name) {
+                       ret = config_init((char *) the_session_name);
                        if (ret < 0) {
-                               ret = CMD_WARNING;
+                               WARN("Could not set %s as the default session",
+                                               the_session_name);
                        }
-                       MSG("Session %s has been loaded successfully", session_name);
+                       MSG("Session %s has been loaded successfully", the_session_name);
                } else {
                        MSG("Session has been loaded successfully");
                }
 
-               if (opt_override_url) {
-                       MSG("Session output url overridden with %s", opt_override_url);
+               if (the_opt_override_session_name) {
+                       MSG("Session name overridden with %s",
+                                       the_opt_override_session_name);
+               }
+
+               if (the_opt_override_url) {
+                       MSG("Session output url overridden with %s", the_opt_override_url);
                }
                success = 1;
                ret = CMD_SUCCESS;
@@ -286,21 +337,21 @@ int cmd_load(int argc, const char **argv)
        /* Mi Printing and closing */
        if (lttng_opt_mi) {
                /* Mi print */
-               ret = mi_load_print(session_name);
+               ret = mi_load_print(the_session_name);
                if (ret) {
                        ret = CMD_ERROR;
                        goto end;
                }
 
                /* Close  output element */
-               ret = mi_lttng_writer_close_element(writer);
+               ret = mi_lttng_writer_close_element(the_writer);
                if (ret) {
                        ret = CMD_ERROR;
                        goto end;
                }
 
                /* Success ? */
-               ret = mi_lttng_writer_write_element_bool(writer,
+               ret = mi_lttng_writer_write_element_bool(the_writer,
                                mi_lttng_element_command_success, success);
                if (ret) {
                        ret = CMD_ERROR;
@@ -308,14 +359,14 @@ int cmd_load(int argc, const char **argv)
                }
 
                /* Command element close */
-               ret = mi_lttng_writer_command_close(writer);
+               ret = mi_lttng_writer_command_close(the_writer);
                if (ret) {
                        ret = CMD_ERROR;
                        goto end;
                }
        }
 end:
-       if (writer && mi_lttng_writer_destroy(writer)) {
+       if (the_writer && mi_lttng_writer_destroy(the_writer)) {
                ERR("Failed to destroy mi lttng writer");
        }
 
This page took 0.028416 seconds and 4 git commands to generate.