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