Fix: error out on leftover arguments
[lttng-tools.git] / src / bin / lttng / commands / save.c
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 _LGPL_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/mi-lttng.h>
27
28 #include "../command.h"
29 #include <lttng/save.h>
30
31 static char *opt_output_path;
32 static bool opt_force;
33 static bool opt_save_all;
34 static struct mi_writer *writer;
35
36 enum {
37 OPT_HELP = 1,
38 OPT_ALL,
39 OPT_FORCE,
40 OPT_LIST_OPTIONS,
41 };
42
43 static struct poptOption save_opts[] = {
44 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
45 {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL},
46 {"all", 'a', POPT_ARG_NONE, NULL, OPT_ALL, NULL, NULL},
47 {"output-path", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL},
48 {"force", 'f', POPT_ARG_NONE, NULL, OPT_FORCE, NULL, NULL},
49 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
50 {0, 0, 0, 0, 0, 0, 0}
51 };
52
53 static int mi_partial_session(const char *session_name)
54 {
55 int ret;
56 assert(writer);
57 assert(session_name);
58
59 /* Open session element */
60 ret = mi_lttng_writer_open_element(writer, config_element_session);
61 if (ret) {
62 goto end;
63 }
64
65 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
66 session_name);
67 if (ret) {
68 goto end;
69 }
70
71 /* Closing session element */
72 ret = mi_lttng_writer_close_element(writer);
73 end:
74 return ret;
75 }
76
77 /*
78 * Mi print of save command
79 */
80 static int mi_save_print(const char *session_name)
81 {
82 int ret;
83 assert(writer);
84
85 if (opt_save_all) {
86 /* We use a wildcard to represent all sessions */
87 session_name = "*";
88 }
89
90 /* Print save element */
91 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_save);
92 if (ret) {
93 goto end;
94 }
95
96 /* Print session element */
97 ret = mi_partial_session(session_name);
98 if (ret) {
99 goto end;
100 }
101
102 /* Path element */
103 if (opt_output_path) {
104 ret = mi_lttng_writer_write_element_string(writer, config_element_path,
105 opt_output_path);
106 if (ret) {
107 goto end;
108 }
109 }
110
111 /* Close save element */
112 ret = mi_lttng_writer_close_element(writer);
113 end:
114 return ret;
115 }
116
117 /*
118 * The 'save <options>' first level command
119 */
120 int cmd_save(int argc, const char **argv)
121 {
122 int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success;
123 int opt;
124 const char *session_name = NULL, *leftover = NULL;
125 poptContext pc;
126 struct lttng_save_session_attr *attr;
127
128 pc = poptGetContext(NULL, argc, argv, save_opts, 0);
129 poptReadDefaultConfig(pc, 0);
130
131 while ((opt = poptGetNextOpt(pc)) != -1) {
132 switch (opt) {
133 case OPT_HELP:
134 SHOW_HELP();
135 goto end;
136 case OPT_ALL:
137 opt_save_all = true;
138 break;
139 case OPT_FORCE:
140 opt_force = true;
141 break;
142 case OPT_LIST_OPTIONS:
143 list_cmd_options(stdout, save_opts);
144 goto end;
145 default:
146 ret = CMD_UNDEFINED;
147 goto end;
148 }
149 }
150
151 if (!opt_save_all) {
152 session_name = poptGetArg(pc);
153 if (session_name) {
154 DBG2("Session name: %s", session_name);
155 } else {
156 /* default to opt_save_all */
157 opt_save_all = true;
158 }
159 }
160
161 leftover = poptGetArg(pc);
162 if (leftover) {
163 ERR("Unknown argument: %s", leftover);
164 ret = CMD_ERROR;
165 goto end;
166 }
167
168 attr = lttng_save_session_attr_create();
169 if (!attr) {
170 ret = CMD_FATAL;
171 goto end_destroy;
172 }
173
174 if (lttng_save_session_attr_set_session_name(attr, session_name)) {
175 ret = CMD_ERROR;
176 goto end_destroy;
177 }
178
179 if (lttng_save_session_attr_set_overwrite(attr, opt_force)) {
180 ret = CMD_ERROR;
181 goto end_destroy;
182 }
183
184 if (lttng_save_session_attr_set_output_url(attr, opt_output_path)) {
185 ret = CMD_ERROR;
186 goto end_destroy;
187 }
188
189 /* Mi check */
190 if (lttng_opt_mi) {
191 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
192 if (!writer) {
193 ret = -LTTNG_ERR_NOMEM;
194 goto end_destroy;
195 }
196
197 /* Open command element */
198 ret = mi_lttng_writer_command_open(writer,
199 mi_lttng_element_command_save);
200 if (ret) {
201 ret = CMD_ERROR;
202 goto end_destroy;
203 }
204
205 /* Open output element */
206 ret = mi_lttng_writer_open_element(writer,
207 mi_lttng_element_command_output);
208 if (ret) {
209 ret = CMD_ERROR;
210 goto end_destroy;
211 }
212 }
213
214 command_ret = lttng_save_session(attr);
215 if (command_ret < 0) {
216 ERR("%s", lttng_strerror(command_ret));
217 success = 0;
218 } else {
219 /* Inform the user of what just happened on success. */
220 if (session_name && opt_output_path) {
221 MSG("Session %s saved successfully in %s.", session_name,
222 opt_output_path);
223 } else if (session_name && !opt_output_path) {
224 MSG("Session %s saved successfully.", session_name);
225 } else if (!session_name && opt_output_path) {
226 MSG("All sessions have been saved successfully in %s.",
227 opt_output_path);
228 } else {
229 MSG("All sessions have been saved successfully.");
230 }
231 success = 1;
232 }
233
234 /* Mi Printing and closing */
235 if (lttng_opt_mi) {
236 /* Mi print */
237 ret = mi_save_print(session_name);
238 if (ret) {
239 ret = CMD_ERROR;
240 goto end_destroy;
241 }
242
243 /* Close output element */
244 ret = mi_lttng_writer_close_element(writer);
245 if (ret) {
246 ret = CMD_ERROR;
247 goto end_destroy;
248 }
249
250 /* Success ? */
251 ret = mi_lttng_writer_write_element_bool(writer,
252 mi_lttng_element_command_success, success);
253 if (ret) {
254 ret = CMD_ERROR;
255 goto end_destroy;
256 }
257
258 /* Command element close */
259 ret = mi_lttng_writer_command_close(writer);
260 if (ret) {
261 ret = CMD_ERROR;
262 goto end_destroy;
263 }
264 }
265 end_destroy:
266 lttng_save_session_attr_destroy(attr);
267 end:
268 /* Mi clean-up */
269 if (writer && mi_lttng_writer_destroy(writer)) {
270 /* Preserve original error code */
271 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
272 }
273
274 /* Overwrite ret if command failed */
275 ret = command_ret ? -command_ret : ret;
276
277 poptFreeContext(pc);
278 return ret;
279 }
This page took 0.057369 seconds and 5 git commands to generate.