Add --override-url option to load command
[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>
ea6ebdf0 28#include <lttng/load.h>
1734c658 29
8c42d845
JG
30#include "../command.h"
31
32static char *opt_input_path;
92f8fa63 33static char *opt_override_url;
8c42d845
JG
34static int opt_force;
35static int opt_load_all;
36
11143783
DG
37static const char *session_name;
38
8c42d845
JG
39enum {
40 OPT_HELP = 1,
41 OPT_ALL,
42 OPT_FORCE,
13a810d5 43 OPT_LIST_OPTIONS,
8c42d845
JG
44};
45
1734c658
JRJ
46static struct mi_writer *writer;
47
8c42d845
JG
48static struct poptOption load_opts[] = {
49 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
50 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
51 {"all", 'a', POPT_ARG_NONE, 0, OPT_ALL, 0, 0},
8c42d845
JG
52 {"input-path", 'i', POPT_ARG_STRING, &opt_input_path, 0, 0, 0},
53 {"force", 'f', POPT_ARG_NONE, 0, OPT_FORCE, 0, 0},
92f8fa63 54 {"override-url", 'U', POPT_ARG_STRING, &opt_override_url, 0, 0, 0},
13a810d5 55 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
8c42d845
JG
56 {0, 0, 0, 0, 0, 0, 0}
57};
58
1734c658
JRJ
59static int mi_partial_session(const char *session_name)
60{
61 int ret;
62 assert(writer);
63 assert(session_name);
64
65 /* Open session element */
66 ret = mi_lttng_writer_open_element(writer, config_element_session);
67 if (ret) {
68 goto end;
69 }
70
71 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
72 session_name);
73 if (ret) {
74 goto end;
75 }
76
77 /* Closing session element */
78 ret = mi_lttng_writer_close_element(writer);
79end:
80 return ret;
81}
82
83/*
84 * Mi print of load command
85 */
86static int mi_load_print(const char *session_name)
87{
88 int ret;
89 assert(writer);
90
91 if (opt_load_all) {
92 /* We use a wildcard to represent all sessions */
93 session_name = "*";
94 }
95
96 /* Print load element */
97 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_load);
98 if (ret) {
99 goto end;
100 }
101
102 /* Print session element */
103 ret = mi_partial_session(session_name);
104 if (ret) {
105 goto end;
106 }
107
108 /* Path element */
109 if (opt_input_path) {
110 ret = mi_lttng_writer_write_element_string(writer, config_element_path,
111 opt_input_path);
112 if (ret) {
113 goto end;
114 }
115 }
116
117 /* Close load element */
118 ret = mi_lttng_writer_close_element(writer);
119
120end:
121 return ret;
122}
123
8c42d845
JG
124/*
125 * The 'load <options>' first level command
126 */
127int cmd_load(int argc, const char **argv)
128{
1734c658 129 int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success;
8c42d845
JG
130 int opt;
131 poptContext pc;
ea6ebdf0
JR
132 struct lttng_load_session_attr *session_attr = NULL;
133 char *input_path = NULL;
8c42d845
JG
134
135 pc = poptGetContext(NULL, argc, argv, load_opts, 0);
136 poptReadDefaultConfig(pc, 0);
137
138 while ((opt = poptGetNextOpt(pc)) != -1) {
139 switch (opt) {
140 case OPT_HELP:
4ba92f18 141 SHOW_HELP();
8c42d845
JG
142 goto end;
143 case OPT_ALL:
144 opt_load_all = 1;
145 break;
13a810d5
DG
146 case OPT_LIST_OPTIONS:
147 list_cmd_options(stdout, load_opts);
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) {
172 ret = -LTTNG_ERR_NOMEM;
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
ea6ebdf0
JR
193 /* Prepare load attributes */
194 session_attr = lttng_load_session_attr_create();
195 if (!session_attr) {
196 ERR("Load session attributes creation failed");
197 ret = -LTTNG_ERR_NOMEM;
198 goto end;
199 }
200
201 /*
202 * Set the input url
203 * lttng_load_session_attr_set_input_url support only 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("Input path is invalid");
210 }
211 } else {
212 input_path = NULL;
213 }
214
215 ret = lttng_load_session_attr_set_input_url(session_attr,
216 input_path);
217 if (ret) {
218 ERR("Input path is invalid");
219 goto end;
220 }
221
222 /* Set the session name. NULL means all sessions should be loaded */
223 ret = lttng_load_session_attr_set_session_name(session_attr,
224 session_name);
225 if (ret) {
226 ERR("Session name is invalid");
227 goto end;
228 }
229
230 /* Set the overwrite attribute */
231 ret = lttng_load_session_attr_set_overwrite(session_attr, opt_force);
232 if (ret) {
233 ERR("Force argument could not be applied");
234 goto end;
235 }
236
92f8fa63
JR
237 /* Set the overrides attributes if any */
238 if (opt_override_url) {
239 ret = lttng_load_session_attr_set_override_url(session_attr,
240 opt_override_url);
241 if (ret) {
242 ERR("Url override is invalid");
243 goto end;
244 }
245 }
246
ea6ebdf0 247 command_ret = lttng_load_session(session_attr);
1734c658
JRJ
248 if (command_ret) {
249 ERR("%s", lttng_strerror(command_ret));
250 success = 0;
279d6193
DG
251 } else {
252 if (opt_load_all) {
253 MSG("All sessions have been loaded successfully");
732b768a 254 } else if (session_name) {
8e525b95
DG
255 ret = config_init((char *)session_name);
256 if (ret < 0) {
257 ret = CMD_WARNING;
258 }
279d6193 259 MSG("Session %s has been loaded successfully", session_name);
732b768a
DG
260 } else {
261 MSG("Session has been loaded successfully");
279d6193 262 }
92f8fa63
JR
263
264 if (opt_override_url) {
265 MSG("Session output url overridden with %s", opt_override_url);
266 }
1734c658
JRJ
267 success = 1;
268 }
269
270 /* Mi Printing and closing */
271 if (lttng_opt_mi) {
272 /* Mi print */
273 ret = mi_load_print(session_name);
274 if (ret) {
275 ret = CMD_ERROR;
276 goto end;
277 }
278
279 /* Close output element */
280 ret = mi_lttng_writer_close_element(writer);
281 if (ret) {
282 ret = CMD_ERROR;
283 goto end;
284 }
285
286 /* Success ? */
287 ret = mi_lttng_writer_write_element_bool(writer,
288 mi_lttng_element_command_success, success);
289 if (ret) {
290 ret = CMD_ERROR;
291 goto end;
292 }
293
294 /* Command element close */
295 ret = mi_lttng_writer_command_close(writer);
296 if (ret) {
297 ret = CMD_ERROR;
298 goto end;
299 }
8c42d845
JG
300 }
301end:
1734c658
JRJ
302 if (writer && mi_lttng_writer_destroy(writer)) {
303 /* Preserve original error code */
304 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
305 }
306
307 /* Overwrite ret if the was an error with the load command */
308 ret = command_ret ? -command_ret : ret;
309
ea6ebdf0
JR
310 lttng_load_session_attr_destroy(session_attr);
311 free(input_path);
8c42d845
JG
312 poptFreeContext(pc);
313 return ret;
314}
This page took 0.051953 seconds and 5 git commands to generate.