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