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