| 1 | /* |
| 2 | * Copyright (C) 2014 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 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. |
| 7 | * |
| 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. |
| 12 | * |
| 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. |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #include <inttypes.h> |
| 20 | #include <popt.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <assert.h> |
| 25 | |
| 26 | #include <common/config/config.h> |
| 27 | #include "../command.h" |
| 28 | |
| 29 | static char *opt_input_path; |
| 30 | static char *opt_session_name; |
| 31 | static int opt_force; |
| 32 | static int opt_load_all; |
| 33 | |
| 34 | enum { |
| 35 | OPT_HELP = 1, |
| 36 | OPT_ALL, |
| 37 | OPT_FORCE, |
| 38 | }; |
| 39 | |
| 40 | static struct poptOption load_opts[] = { |
| 41 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 42 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
| 43 | {"all", 'a', POPT_ARG_NONE, 0, OPT_ALL, 0, 0}, |
| 44 | {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, |
| 45 | {"input-path", 'i', POPT_ARG_STRING, &opt_input_path, 0, 0, 0}, |
| 46 | {"force", 'f', POPT_ARG_NONE, 0, OPT_FORCE, 0, 0}, |
| 47 | {0, 0, 0, 0, 0, 0, 0} |
| 48 | }; |
| 49 | |
| 50 | /* |
| 51 | * usage |
| 52 | */ |
| 53 | static void usage(FILE *ofp) |
| 54 | { |
| 55 | fprintf(ofp, "usage: lttng load [OPTIONS]\n"); |
| 56 | fprintf(ofp, "\n"); |
| 57 | fprintf(ofp, "Options:\n"); |
| 58 | fprintf(ofp, " -h, --help Show this help\n"); |
| 59 | fprintf(ofp, " -a, --all Load all sessions (default)\n"); |
| 60 | fprintf(ofp, " -s, --session Load a specific session\n"); |
| 61 | fprintf(ofp, " -i, --input-path Input path of the session configuration(s)\n"); |
| 62 | fprintf(ofp, " -f, --force Override existing session configuration(s)\n"); |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * The 'load <options>' first level command |
| 67 | */ |
| 68 | int cmd_load(int argc, const char **argv) |
| 69 | { |
| 70 | int ret = CMD_SUCCESS; |
| 71 | int opt; |
| 72 | poptContext pc; |
| 73 | |
| 74 | pc = poptGetContext(NULL, argc, argv, load_opts, 0); |
| 75 | poptReadDefaultConfig(pc, 0); |
| 76 | |
| 77 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 78 | switch (opt) { |
| 79 | case OPT_HELP: |
| 80 | usage(stdout); |
| 81 | goto end; |
| 82 | case OPT_ALL: |
| 83 | opt_load_all = 1; |
| 84 | break; |
| 85 | case OPT_FORCE: |
| 86 | opt_force = 1; |
| 87 | break; |
| 88 | default: |
| 89 | usage(stderr); |
| 90 | ret = CMD_UNDEFINED; |
| 91 | goto end; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if (opt_session_name && opt_load_all) { |
| 96 | WARN("Conflicting session options, loading session %s", |
| 97 | opt_session_name); |
| 98 | } |
| 99 | |
| 100 | ret = config_load_session(opt_input_path, opt_session_name, opt_force); |
| 101 | if (ret) { |
| 102 | ret = CMD_ERROR; |
| 103 | } |
| 104 | end: |
| 105 | poptFreeContext(pc); |
| 106 | return ret; |
| 107 | } |