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