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