Commit | Line | Data |
---|---|---|
c864d6d7 JG |
1 | /* |
2 | * Copyright (C) 2013 - 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 "../command.h" | |
27 | #include <lttng/save.h> | |
28 | ||
29 | static char *opt_output_path; | |
30 | static int opt_force; | |
31 | static int opt_save_all; | |
32 | ||
33 | enum { | |
34 | OPT_HELP = 1, | |
35 | OPT_ALL, | |
36 | OPT_FORCE, | |
37 | }; | |
38 | ||
39 | static struct poptOption save_opts[] = { | |
40 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
41 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, | |
42 | {"all", 'a', POPT_ARG_NONE, 0, OPT_ALL, 0, 0}, | |
43 | {"output-path", 'o', POPT_ARG_STRING, &opt_output_path, 0, 0, 0}, | |
44 | {"force", 'f', POPT_ARG_NONE, 0, OPT_FORCE, 0, 0}, | |
45 | {0, 0, 0, 0, 0, 0, 0} | |
46 | }; | |
47 | ||
48 | /* | |
49 | * usage | |
50 | */ | |
51 | static void usage(FILE *ofp) | |
52 | { | |
53 | fprintf(ofp, "usage: lttng save [OPTIONS] [SESSION]\n"); | |
54 | fprintf(ofp, "\n"); | |
55 | fprintf(ofp, "Options:\n"); | |
56 | fprintf(ofp, " -h, --help Show this help\n"); | |
57 | fprintf(ofp, " -a, --all Save all sessions (default)\n"); | |
58 | fprintf(ofp, " -o, --output-path Output path of the session configuration(s)\n"); | |
59 | fprintf(ofp, " -f, --force Overwrite existing session configuration(s)\n"); | |
60 | } | |
61 | ||
62 | /* | |
63 | * The 'save <options>' first level command | |
64 | */ | |
65 | int cmd_save(int argc, const char **argv) | |
66 | { | |
67 | int ret = CMD_SUCCESS; | |
68 | int opt; | |
69 | const char *session_name = NULL; | |
70 | poptContext pc; | |
71 | struct lttng_save_session_attr *attr; | |
72 | ||
73 | pc = poptGetContext(NULL, argc, argv, save_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_save_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_save_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("Session name: %s", session_name); | |
102 | } | |
103 | ||
104 | attr = lttng_save_session_attr_create(); | |
105 | if (!attr) { | |
106 | ret = CMD_FATAL; | |
107 | goto end_destroy; | |
108 | } | |
109 | ||
110 | if (lttng_save_session_attr_set_session_name(attr, session_name)) { | |
111 | ret = CMD_ERROR; | |
112 | goto end_destroy; | |
113 | } | |
114 | ||
115 | if (lttng_save_session_attr_set_overwrite(attr, opt_force)) { | |
116 | ret = CMD_ERROR; | |
117 | goto end_destroy; | |
118 | } | |
119 | ||
ab85fc7f | 120 | if (lttng_save_session_attr_set_output_url(attr, opt_output_path)) { |
c864d6d7 JG |
121 | ret = CMD_ERROR; |
122 | goto end_destroy; | |
123 | } | |
124 | ||
125 | ret = lttng_save_session(attr); | |
ab85fc7f DG |
126 | if (ret < 0) { |
127 | ERR("%s", lttng_strerror(ret)); | |
128 | } else { | |
129 | /* Inform the user of what just happened on success. */ | |
130 | if (session_name && opt_output_path) { | |
131 | MSG("Session %s saved successfully in %s.", session_name, | |
132 | opt_output_path); | |
133 | } else if (session_name && !opt_output_path) { | |
134 | MSG("Session %s saved successfully.", session_name); | |
135 | } else if (!session_name && opt_output_path) { | |
136 | MSG("All sessions have been saved successfully in %s.", | |
137 | opt_output_path); | |
138 | } else { | |
139 | MSG("All sessions have been saved successfully."); | |
c864d6d7 | 140 | } |
c864d6d7 JG |
141 | } |
142 | end_destroy: | |
143 | lttng_save_session_attr_destroy(attr); | |
144 | end: | |
145 | poptFreeContext(pc); | |
146 | return ret; | |
147 | } |