1d0e42bdc5f4ed1c60667ebb72cfbdcea297201d
[lttng-tools.git] / src / bin / lttng / commands / load.c
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 int opt_force;
31 static int opt_load_all;
32
33 static const char *session_name;
34
35 enum {
36 OPT_HELP = 1,
37 OPT_ALL,
38 OPT_FORCE,
39 };
40
41 static struct poptOption load_opts[] = {
42 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
43 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
44 {"all", 'a', POPT_ARG_NONE, 0, OPT_ALL, 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] [SESSION]\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, " -i, --input-path Input path of the session configuration(s)\n");
61 fprintf(ofp, " -f, --force Override existing session configuration(s)\n");
62 }
63
64 /*
65 * The 'load <options>' first level command
66 */
67 int cmd_load(int argc, const char **argv)
68 {
69 int ret = CMD_SUCCESS;
70 int opt;
71 poptContext pc;
72
73 pc = poptGetContext(NULL, argc, argv, load_opts, 0);
74 poptReadDefaultConfig(pc, 0);
75
76 while ((opt = poptGetNextOpt(pc)) != -1) {
77 switch (opt) {
78 case OPT_HELP:
79 usage(stdout);
80 goto end;
81 case OPT_ALL:
82 opt_load_all = 1;
83 break;
84 case OPT_FORCE:
85 opt_force = 1;
86 break;
87 default:
88 usage(stderr);
89 ret = CMD_UNDEFINED;
90 goto end;
91 }
92 }
93
94 if (!opt_load_all) {
95 session_name = poptGetArg(pc);
96 if (!session_name) {
97 ERR("A session name must be provided if the \"all\" option is not used.");
98 ret = CMD_ERROR;
99 goto end;
100 }
101 DBG2("Loading session name: %s", session_name);
102 }
103
104 ret = config_load_session(opt_input_path, session_name, opt_force);
105 if (ret) {
106 ERR("%s", lttng_strerror(ret));
107 ret = -ret;
108 } else {
109 if (opt_load_all) {
110 MSG("All sessions have been loaded successfully");
111 } else {
112 MSG("Session %s has been loaded successfully", session_name);
113 }
114 }
115 end:
116 poptFreeContext(pc);
117 return ret;
118 }
This page took 0.0376 seconds and 3 git commands to generate.