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