Fix: error out on leftover arguments
[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;
fe559816 33static char *opt_override_url;
e1f481f6 34static char *opt_override_session_name;
8c42d845
JG
35static int opt_force;
36static int opt_load_all;
37
11143783
DG
38static const char *session_name;
39
8c42d845
JG
40enum {
41 OPT_HELP = 1,
42 OPT_ALL,
43 OPT_FORCE,
13a810d5 44 OPT_LIST_OPTIONS,
8c42d845
JG
45};
46
1734c658
JRJ
47static struct mi_writer *writer;
48
8c42d845
JG
49static struct poptOption load_opts[] = {
50 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
e1f481f6
JR
51 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
52 {"all", 'a', POPT_ARG_NONE, 0, OPT_ALL, 0, 0},
53 {"input-path", 'i', POPT_ARG_STRING, &opt_input_path, 0, 0, 0},
54 {"force", 'f', POPT_ARG_NONE, 0, OPT_FORCE, 0, 0},
55 {"override-url", 0, POPT_ARG_STRING, &opt_override_url, 0, 0, 0},
56 {"override-name", 0, POPT_ARG_STRING, &opt_override_session_name, 0, 0, 0},
57 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
8c42d845
JG
58 {0, 0, 0, 0, 0, 0, 0}
59};
60
1734c658
JRJ
61static int mi_partial_session(const char *session_name)
62{
63 int ret;
64 assert(writer);
65 assert(session_name);
66
67 /* Open session element */
68 ret = mi_lttng_writer_open_element(writer, config_element_session);
69 if (ret) {
70 goto end;
71 }
72
73 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
74 session_name);
75 if (ret) {
76 goto end;
77 }
78
79 /* Closing session element */
80 ret = mi_lttng_writer_close_element(writer);
81end:
82 return ret;
83}
84
85/*
86 * Mi print of load command
87 */
88static int mi_load_print(const char *session_name)
89{
90 int ret;
91 assert(writer);
92
93 if (opt_load_all) {
94 /* We use a wildcard to represent all sessions */
95 session_name = "*";
96 }
97
98 /* Print load element */
99 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_load);
100 if (ret) {
101 goto end;
102 }
103
104 /* Print session element */
105 ret = mi_partial_session(session_name);
106 if (ret) {
107 goto end;
108 }
109
110 /* Path element */
111 if (opt_input_path) {
112 ret = mi_lttng_writer_write_element_string(writer, config_element_path,
113 opt_input_path);
114 if (ret) {
115 goto end;
116 }
117 }
118
c88dc4c7
JR
119 /* Print override elements */
120 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_load_overrides);
121 if (ret) {
122 goto end;
123 }
124
125 /* Session name override element */
126 if (opt_override_session_name) {
127 ret = mi_lttng_writer_write_element_string(writer,
128 config_element_name, opt_override_session_name);
129 if (ret) {
130 goto end;
131 }
132 }
133
134 /* Session url override element */
135 if (opt_override_url) {
136 ret = mi_lttng_writer_write_element_string(writer,
137 mi_lttng_element_load_override_url,
138 opt_override_url);
139 if (ret) {
140 goto end;
141 }
142 }
1734c658 143
c88dc4c7
JR
144 /* Close override and load element */
145 ret = mi_lttng_close_multi_element(writer, 2);
1734c658
JRJ
146end:
147 return ret;
148}
149
8c42d845
JG
150/*
151 * The 'load <options>' first level command
152 */
153int cmd_load(int argc, const char **argv)
154{
b6d4654a 155 int ret, success;
8c42d845
JG
156 int opt;
157 poptContext pc;
b6d4654a
JR
158 struct lttng_load_session_attr *session_attr = NULL;
159 char *input_path = NULL;
266d3dd0 160 const char *leftover = NULL;
8c42d845
JG
161
162 pc = poptGetContext(NULL, argc, argv, load_opts, 0);
163 poptReadDefaultConfig(pc, 0);
164
165 while ((opt = poptGetNextOpt(pc)) != -1) {
166 switch (opt) {
167 case OPT_HELP:
4ba92f18 168 SHOW_HELP();
b6d4654a 169 ret = CMD_SUCCESS;
8c42d845
JG
170 goto end;
171 case OPT_ALL:
172 opt_load_all = 1;
173 break;
13a810d5
DG
174 case OPT_LIST_OPTIONS:
175 list_cmd_options(stdout, load_opts);
b6d4654a 176 ret = CMD_SUCCESS;
13a810d5 177 goto end;
8c42d845
JG
178 case OPT_FORCE:
179 opt_force = 1;
180 break;
181 default:
8c42d845
JG
182 ret = CMD_UNDEFINED;
183 goto end;
184 }
185 }
186
623bc34c
JG
187 ret = lttng_session_daemon_alive();
188 if (!ret) {
189 ERR("No session daemon is available");
190 ret = CMD_ERROR;
191 goto end;
192 }
193
11143783
DG
194 if (!opt_load_all) {
195 session_name = poptGetArg(pc);
732b768a
DG
196 if (session_name) {
197 DBG2("Loading session name: %s", session_name);
1734c658
JRJ
198 } else {
199 /* Default to load_all */
200 opt_load_all = 1;
11143783 201 }
8c42d845
JG
202 }
203
266d3dd0
JD
204 leftover = poptGetArg(pc);
205 if (leftover) {
206 ERR("Unknown argument: %s", leftover);
207 ret = CMD_ERROR;
208 goto end;
209 }
210
1734c658
JRJ
211 /* Mi check */
212 if (lttng_opt_mi) {
213 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
214 if (!writer) {
b6d4654a 215 ret = CMD_ERROR;
1734c658
JRJ
216 goto end;
217 }
218
219 /* Open command element */
220 ret = mi_lttng_writer_command_open(writer,
221 mi_lttng_element_command_load);
222 if (ret) {
223 ret = CMD_ERROR;
224 goto end;
225 }
226
227 /* Open output element */
228 ret = mi_lttng_writer_open_element(writer,
229 mi_lttng_element_command_output);
230 if (ret) {
231 ret = CMD_ERROR;
232 goto end;
233 }
234 }
235
b6d4654a
JR
236 /* Prepare load attributes */
237 session_attr = lttng_load_session_attr_create();
238 if (!session_attr) {
239 ERR("Failed to create load session attributes");
240 ret = CMD_ERROR;
241 goto end;
242 }
243
244 /*
245 * Set the input url
246 * lttng_load_session_attr_set_input_url only suppports absolute path.
247 * Use realpath to resolve any relative path.
248 * */
249 if (opt_input_path) {
250 input_path = realpath(opt_input_path, NULL);
251 if (!input_path) {
252 PERROR("Invalid input path");
253 ret = CMD_ERROR;
254 goto end;
255 }
256 } else {
257 input_path = NULL;
258 }
259
260 ret = lttng_load_session_attr_set_input_url(session_attr,
261 input_path);
262 if (ret) {
263 ERR("Invalid input path");
264 ret = CMD_ERROR;
265 goto end;
266 }
267
268 /* Set the session name. NULL means all sessions should be loaded */
269 ret = lttng_load_session_attr_set_session_name(session_attr,
270 session_name);
271 if (ret) {
272 ERR("Invalid session name");
273 ret = CMD_ERROR;
274 goto end;
275 }
276
277 /* Set the overwrite attribute */
278 ret = lttng_load_session_attr_set_overwrite(session_attr, opt_force);
279 if (ret) {
280 ERR("Force argument could not be applied");
281 ret = CMD_ERROR;
282 goto end;
283 }
284
fe559816
JR
285 /* Set the overrides attributes if any */
286 if (opt_override_url) {
287 ret = lttng_load_session_attr_set_override_url(session_attr,
288 opt_override_url);
289 if (ret) {
290 ERR("Url override is invalid");
291 goto end;
292 }
293 }
294
e1f481f6
JR
295 if (opt_override_session_name) {
296 if (opt_load_all) {
297 ERR("Options --all and --override-name cannot be used simultaneously");
298 ret = CMD_ERROR;
299 goto end;
300 }
301 ret = lttng_load_session_attr_set_override_session_name(session_attr,
302 opt_override_session_name);
303 if (ret) {
304 ERR("Failed to set session name override");
305 ret = CMD_ERROR;
306 goto end;
307 }
308 }
309
b6d4654a
JR
310 ret = lttng_load_session(session_attr);
311 if (ret) {
312 ERR("%s", lttng_strerror(ret));
1734c658 313 success = 0;
b6d4654a 314 ret = CMD_ERROR;
279d6193
DG
315 } else {
316 if (opt_load_all) {
317 MSG("All sessions have been loaded successfully");
732b768a 318 } else if (session_name) {
b6d4654a 319 ret = config_init((char *) session_name);
8e525b95 320 if (ret < 0) {
96694f0b
JG
321 WARN("Could not set %s as the default session",
322 session_name);
8e525b95 323 }
279d6193 324 MSG("Session %s has been loaded successfully", session_name);
732b768a
DG
325 } else {
326 MSG("Session has been loaded successfully");
279d6193 327 }
fe559816 328
4bbf55e8
JR
329 if (opt_override_session_name) {
330 MSG("Session name overridden with %s",
331 opt_override_session_name);
332 }
333
fe559816
JR
334 if (opt_override_url) {
335 MSG("Session output url overridden with %s", opt_override_url);
336 }
1734c658 337 success = 1;
b6d4654a 338 ret = CMD_SUCCESS;
1734c658
JRJ
339 }
340
341 /* Mi Printing and closing */
342 if (lttng_opt_mi) {
343 /* Mi print */
344 ret = mi_load_print(session_name);
345 if (ret) {
346 ret = CMD_ERROR;
347 goto end;
348 }
349
350 /* Close output element */
351 ret = mi_lttng_writer_close_element(writer);
352 if (ret) {
353 ret = CMD_ERROR;
354 goto end;
355 }
356
357 /* Success ? */
358 ret = mi_lttng_writer_write_element_bool(writer,
359 mi_lttng_element_command_success, success);
360 if (ret) {
361 ret = CMD_ERROR;
362 goto end;
363 }
364
365 /* Command element close */
366 ret = mi_lttng_writer_command_close(writer);
367 if (ret) {
368 ret = CMD_ERROR;
369 goto end;
370 }
8c42d845
JG
371 }
372end:
1734c658 373 if (writer && mi_lttng_writer_destroy(writer)) {
b6d4654a 374 ERR("Failed to destroy mi lttng writer");
1734c658
JRJ
375 }
376
b6d4654a
JR
377 lttng_load_session_attr_destroy(session_attr);
378 free(input_path);
8c42d845
JG
379 poptFreeContext(pc);
380 return ret;
381}
This page took 0.056566 seconds and 5 git commands to generate.