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