Fix: ignore SIGPIPE
[lttng-tools.git] / src / bin / lttng / commands / load.c
CommitLineData
8c42d845
JG
1/*
2 * Copyright (C) 2014 - 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
8c42d845
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 26#include <common/mi-lttng.h>
f40ef1d5 27#include <common/config/session-config.h>
b6d4654a 28#include <lttng/load.h>
1734c658 29
8c42d845
JG
30#include "../command.h"
31
32static char *opt_input_path;
8c42d845
JG
33static int opt_force;
34static int opt_load_all;
35
11143783
DG
36static const char *session_name;
37
8c42d845
JG
38enum {
39 OPT_HELP = 1,
40 OPT_ALL,
41 OPT_FORCE,
13a810d5 42 OPT_LIST_OPTIONS,
8c42d845
JG
43};
44
1734c658
JRJ
45static struct mi_writer *writer;
46
8c42d845
JG
47static struct poptOption load_opts[] = {
48 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
49 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
50 {"all", 'a', POPT_ARG_NONE, 0, OPT_ALL, 0, 0},
8c42d845
JG
51 {"input-path", 'i', POPT_ARG_STRING, &opt_input_path, 0, 0, 0},
52 {"force", 'f', POPT_ARG_NONE, 0, OPT_FORCE, 0, 0},
13a810d5 53 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
8c42d845
JG
54 {0, 0, 0, 0, 0, 0, 0}
55};
56
1734c658
JRJ
57static int mi_partial_session(const char *session_name)
58{
59 int ret;
60 assert(writer);
61 assert(session_name);
62
63 /* Open session element */
64 ret = mi_lttng_writer_open_element(writer, config_element_session);
65 if (ret) {
66 goto end;
67 }
68
69 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
70 session_name);
71 if (ret) {
72 goto end;
73 }
74
75 /* Closing session element */
76 ret = mi_lttng_writer_close_element(writer);
77end:
78 return ret;
79}
80
81/*
82 * Mi print of load command
83 */
84static int mi_load_print(const char *session_name)
85{
86 int ret;
87 assert(writer);
88
89 if (opt_load_all) {
90 /* We use a wildcard to represent all sessions */
91 session_name = "*";
92 }
93
94 /* Print load element */
95 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_load);
96 if (ret) {
97 goto end;
98 }
99
100 /* Print session element */
101 ret = mi_partial_session(session_name);
102 if (ret) {
103 goto end;
104 }
105
106 /* Path element */
107 if (opt_input_path) {
108 ret = mi_lttng_writer_write_element_string(writer, config_element_path,
109 opt_input_path);
110 if (ret) {
111 goto end;
112 }
113 }
114
115 /* Close load element */
116 ret = mi_lttng_writer_close_element(writer);
117
118end:
119 return ret;
120}
121
8c42d845
JG
122/*
123 * The 'load <options>' first level command
124 */
125int cmd_load(int argc, const char **argv)
126{
b6d4654a 127 int ret, success;
8c42d845
JG
128 int opt;
129 poptContext pc;
b6d4654a
JR
130 struct lttng_load_session_attr *session_attr = NULL;
131 char *input_path = NULL;
8c42d845
JG
132
133 pc = poptGetContext(NULL, argc, argv, load_opts, 0);
134 poptReadDefaultConfig(pc, 0);
135
136 while ((opt = poptGetNextOpt(pc)) != -1) {
137 switch (opt) {
138 case OPT_HELP:
4ba92f18 139 SHOW_HELP();
b6d4654a 140 ret = CMD_SUCCESS;
8c42d845
JG
141 goto end;
142 case OPT_ALL:
143 opt_load_all = 1;
144 break;
13a810d5
DG
145 case OPT_LIST_OPTIONS:
146 list_cmd_options(stdout, load_opts);
b6d4654a 147 ret = CMD_SUCCESS;
13a810d5 148 goto end;
8c42d845
JG
149 case OPT_FORCE:
150 opt_force = 1;
151 break;
152 default:
8c42d845
JG
153 ret = CMD_UNDEFINED;
154 goto end;
155 }
156 }
157
11143783
DG
158 if (!opt_load_all) {
159 session_name = poptGetArg(pc);
732b768a
DG
160 if (session_name) {
161 DBG2("Loading session name: %s", session_name);
1734c658
JRJ
162 } else {
163 /* Default to load_all */
164 opt_load_all = 1;
11143783 165 }
8c42d845
JG
166 }
167
1734c658
JRJ
168 /* Mi check */
169 if (lttng_opt_mi) {
170 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
171 if (!writer) {
b6d4654a 172 ret = CMD_ERROR;
1734c658
JRJ
173 goto end;
174 }
175
176 /* Open command element */
177 ret = mi_lttng_writer_command_open(writer,
178 mi_lttng_element_command_load);
179 if (ret) {
180 ret = CMD_ERROR;
181 goto end;
182 }
183
184 /* Open output element */
185 ret = mi_lttng_writer_open_element(writer,
186 mi_lttng_element_command_output);
187 if (ret) {
188 ret = CMD_ERROR;
189 goto end;
190 }
191 }
192
b6d4654a
JR
193 /* Prepare load attributes */
194 session_attr = lttng_load_session_attr_create();
195 if (!session_attr) {
196 ERR("Failed to create load session attributes");
197 ret = CMD_ERROR;
198 goto end;
199 }
200
201 /*
202 * Set the input url
203 * lttng_load_session_attr_set_input_url only suppports absolute path.
204 * Use realpath to resolve any relative path.
205 * */
206 if (opt_input_path) {
207 input_path = realpath(opt_input_path, NULL);
208 if (!input_path) {
209 PERROR("Invalid input path");
210 ret = CMD_ERROR;
211 goto end;
212 }
213 } else {
214 input_path = NULL;
215 }
216
217 ret = lttng_load_session_attr_set_input_url(session_attr,
218 input_path);
219 if (ret) {
220 ERR("Invalid input path");
221 ret = CMD_ERROR;
222 goto end;
223 }
224
225 /* Set the session name. NULL means all sessions should be loaded */
226 ret = lttng_load_session_attr_set_session_name(session_attr,
227 session_name);
228 if (ret) {
229 ERR("Invalid session name");
230 ret = CMD_ERROR;
231 goto end;
232 }
233
234 /* Set the overwrite attribute */
235 ret = lttng_load_session_attr_set_overwrite(session_attr, opt_force);
236 if (ret) {
237 ERR("Force argument could not be applied");
238 ret = CMD_ERROR;
239 goto end;
240 }
241
242 ret = lttng_load_session(session_attr);
243 if (ret) {
244 ERR("%s", lttng_strerror(ret));
1734c658 245 success = 0;
b6d4654a 246 ret = CMD_ERROR;
279d6193
DG
247 } else {
248 if (opt_load_all) {
249 MSG("All sessions have been loaded successfully");
732b768a 250 } else if (session_name) {
b6d4654a 251 ret = config_init((char *) session_name);
8e525b95
DG
252 if (ret < 0) {
253 ret = CMD_WARNING;
254 }
279d6193 255 MSG("Session %s has been loaded successfully", session_name);
732b768a
DG
256 } else {
257 MSG("Session has been loaded successfully");
279d6193 258 }
1734c658 259 success = 1;
b6d4654a 260 ret = CMD_SUCCESS;
1734c658
JRJ
261 }
262
263 /* Mi Printing and closing */
264 if (lttng_opt_mi) {
265 /* Mi print */
266 ret = mi_load_print(session_name);
267 if (ret) {
268 ret = CMD_ERROR;
269 goto end;
270 }
271
272 /* Close output element */
273 ret = mi_lttng_writer_close_element(writer);
274 if (ret) {
275 ret = CMD_ERROR;
276 goto end;
277 }
278
279 /* Success ? */
280 ret = mi_lttng_writer_write_element_bool(writer,
281 mi_lttng_element_command_success, success);
282 if (ret) {
283 ret = CMD_ERROR;
284 goto end;
285 }
286
287 /* Command element close */
288 ret = mi_lttng_writer_command_close(writer);
289 if (ret) {
290 ret = CMD_ERROR;
291 goto end;
292 }
8c42d845
JG
293 }
294end:
1734c658 295 if (writer && mi_lttng_writer_destroy(writer)) {
b6d4654a 296 ERR("Failed to destroy mi lttng writer");
1734c658
JRJ
297 }
298
b6d4654a
JR
299 lttng_load_session_attr_destroy(session_attr);
300 free(input_path);
8c42d845
JG
301 poptFreeContext(pc);
302 return ret;
303}
This page took 0.051001 seconds and 5 git commands to generate.