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