2 * Copyright (C) 2014 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
9 #include "lttng-ctl-helper.hpp"
11 #include <common/compat/string.hpp>
12 #include <common/config/session-config.hpp>
13 #include <common/macros.hpp>
14 #include <common/sessiond-comm/sessiond-comm.hpp>
15 #include <common/uri.hpp>
17 #include <lttng/load-internal.hpp>
18 #include <lttng/load.h>
19 #include <lttng/lttng-error.h>
24 struct lttng_load_session_attr
*lttng_load_session_attr_create(void)
26 return zmalloc
<lttng_load_session_attr
>();
29 static void reset_load_session_attr_urls(struct lttng_load_session_attr
*attr
)
31 free(attr
->raw_override_url
);
32 free(attr
->raw_override_path_url
);
33 free(attr
->raw_override_ctrl_url
);
34 free(attr
->raw_override_data_url
);
35 if (attr
->override_attr
) {
36 free(attr
->override_attr
->path_url
);
37 free(attr
->override_attr
->ctrl_url
);
38 free(attr
->override_attr
->data_url
);
39 free(attr
->override_attr
->session_name
);
43 void lttng_load_session_attr_destroy(struct lttng_load_session_attr
*attr
)
46 reset_load_session_attr_urls(attr
);
47 free(attr
->override_attr
);
52 static int validate_attr(const struct lttng_load_session_attr
*attr
)
57 ret
= -LTTNG_ERR_INVALID
;
61 if (!attr
->override_attr
) {
66 * Refuse override name if the objective is to load multiple session
67 * since this operation is ambiguous while loading multiple session.
69 if (attr
->override_attr
->session_name
&& attr
->session_name
[0] == '\0') {
70 ret
= -LTTNG_ERR_INVALID
;
77 const char *lttng_load_session_attr_get_session_name(struct lttng_load_session_attr
*attr
)
79 const char *ret
= nullptr;
81 if (attr
&& attr
->session_name
[0]) {
82 ret
= attr
->session_name
;
88 const char *lttng_load_session_attr_get_input_url(struct lttng_load_session_attr
*attr
)
90 const char *ret
= nullptr;
92 if (attr
&& attr
->input_url
[0]) {
93 ret
= attr
->input_url
;
99 int lttng_load_session_attr_get_overwrite(struct lttng_load_session_attr
*attr
)
101 return attr
? attr
->overwrite
: -LTTNG_ERR_INVALID
;
104 const char *lttng_load_session_attr_get_override_ctrl_url(struct lttng_load_session_attr
*attr
)
106 const char *ret
= nullptr;
108 if (!attr
|| !attr
->override_attr
) {
112 ret
= attr
->raw_override_ctrl_url
;
117 const char *lttng_load_session_attr_get_override_data_url(struct lttng_load_session_attr
*attr
)
119 const char *ret
= nullptr;
121 if (!attr
|| !attr
->override_attr
) {
125 ret
= attr
->raw_override_data_url
;
130 const char *lttng_load_session_attr_get_override_url(struct lttng_load_session_attr
*attr
)
132 const char *ret
= nullptr;
134 if (!attr
|| !attr
->override_attr
) {
138 if ((attr
->override_attr
->path_url
||
139 (attr
->override_attr
->ctrl_url
&& attr
->override_attr
->data_url
))) {
140 ret
= attr
->raw_override_url
;
146 const char *lttng_load_session_attr_get_override_session_name(struct lttng_load_session_attr
*attr
)
148 const char *ret
= nullptr;
150 if (!attr
|| !attr
->override_attr
) {
154 ret
= attr
->override_attr
->session_name
;
159 int lttng_load_session_attr_set_session_name(struct lttng_load_session_attr
*attr
,
160 const char *session_name
)
165 ret
= -LTTNG_ERR_INVALID
;
172 len
= strlen(session_name
);
173 if (len
>= LTTNG_NAME_MAX
) {
174 ret
= -LTTNG_ERR_INVALID
;
178 ret
= lttng_strncpy(attr
->session_name
, session_name
, sizeof(attr
->session_name
));
180 ret
= -LTTNG_ERR_INVALID
;
184 attr
->session_name
[0] = '\0';
190 int lttng_load_session_attr_set_input_url(struct lttng_load_session_attr
*attr
, const char *url
)
195 struct lttng_uri
*uris
= nullptr;
198 ret
= -LTTNG_ERR_INVALID
;
203 attr
->input_url
[0] = '\0';
209 if (len
>= PATH_MAX
) {
210 ret
= -LTTNG_ERR_INVALID
;
214 size
= uri_parse_str_urls(url
, nullptr, &uris
);
215 if (size
<= 0 || uris
[0].dtype
!= LTTNG_DST_PATH
) {
216 ret
= -LTTNG_ERR_INVALID
;
220 /* Copy string plus the NULL terminated byte. */
221 ret
= lttng_strncpy(attr
->input_url
, uris
[0].dst
.path
, sizeof(attr
->input_url
));
223 ret
= -LTTNG_ERR_INVALID
;
233 int lttng_load_session_attr_set_overwrite(struct lttng_load_session_attr
*attr
, int overwrite
)
238 ret
= -LTTNG_ERR_INVALID
;
242 attr
->overwrite
= !!overwrite
;
247 int lttng_load_session_attr_set_override_ctrl_url(struct lttng_load_session_attr
*attr
,
252 struct lttng_uri
*uri
= nullptr;
253 char *url_str
= nullptr;
254 char *raw_str
= nullptr;
257 ret
= -LTTNG_ERR_INVALID
;
261 if (!attr
->override_attr
) {
262 attr
->override_attr
= zmalloc
<config_load_session_override_attr
>();
263 if (!attr
->override_attr
) {
264 ret
= -LTTNG_ERR_NOMEM
;
269 if (attr
->override_attr
->path_url
) {
271 * Setting a ctrl override after a path override makes no sense.
273 ret
= -LTTNG_ERR_INVALID
;
278 * FIXME: uri_parse should be able to take as parameter the protocol
279 * type to validate "url". For now only check the parsing goes through;
280 * it will fail later on.
282 ret_size
= uri_parse(url
, &uri
);
284 ret
= -LTTNG_ERR_INVALID
;
288 if (uri
[0].port
== 0) {
289 uri
[0].port
= DEFAULT_NETWORK_CONTROL_PORT
;
292 url_str
= calloc
<char>(PATH_MAX
);
294 /* FIXME: return valid error */
295 ret
= -LTTNG_ERR_NOMEM
;
299 ret
= uri_to_str_url(&uri
[0], url_str
, PATH_MAX
);
301 ret
= -LTTNG_ERR_INVALID
;
306 raw_str
= lttng_strndup(url
, PATH_MAX
);
308 ret
= -LTTNG_ERR_NOMEM
;
312 /* Squash old value if any */
313 free(attr
->override_attr
->ctrl_url
);
314 free(attr
->raw_override_ctrl_url
);
316 /* Populate the object */
317 attr
->override_attr
->ctrl_url
= url_str
;
318 attr
->raw_override_ctrl_url
= raw_str
;
320 /* Ownership passed to attr. */
331 int lttng_load_session_attr_set_override_data_url(struct lttng_load_session_attr
*attr
,
336 struct lttng_uri
*uri
= nullptr;
337 char *url_str
= nullptr;
338 char *raw_str
= nullptr;
341 ret
= -LTTNG_ERR_INVALID
;
345 if (!attr
->override_attr
) {
346 attr
->override_attr
= zmalloc
<config_load_session_override_attr
>();
347 if (!attr
->override_attr
) {
348 ret
= -LTTNG_ERR_NOMEM
;
353 if (attr
->override_attr
->path_url
) {
355 * Setting a data override after a path override makes no sense.
357 ret
= -LTTNG_ERR_INVALID
;
362 * FIXME: uri_parse should be able to take as parameter the protocol
363 * type to validate "url". For now only check the parsing goes through;
364 * it will fail later on.
366 ret_size
= uri_parse(url
, &uri
);
368 ret
= -LTTNG_ERR_INVALID
;
372 if (uri
[0].port
== 0) {
373 uri
[0].port
= DEFAULT_NETWORK_DATA_PORT
;
376 url_str
= calloc
<char>(PATH_MAX
);
378 ret
= -LTTNG_ERR_NOMEM
;
382 ret
= uri_to_str_url(&uri
[0], url_str
, PATH_MAX
);
384 ret
= -LTTNG_ERR_INVALID
;
389 raw_str
= lttng_strndup(url
, PATH_MAX
);
391 ret
= -LTTNG_ERR_NOMEM
;
395 /* Squash old value if any */
396 free(attr
->override_attr
->data_url
);
397 free(attr
->raw_override_data_url
);
399 /* Populate the object */
400 attr
->override_attr
->data_url
= url_str
;
401 attr
->raw_override_data_url
= raw_str
;
403 /* Ownership passed to attr. */
413 int lttng_load_session_attr_set_override_url(struct lttng_load_session_attr
*attr
, const char *url
)
417 struct lttng_uri
*uri
= nullptr;
418 char *raw_url_str
= nullptr;
419 char *raw_path_str
= nullptr;
420 char *path_str
= nullptr;
421 char *raw_ctrl_str
= nullptr;
422 char *ctrl_str
= nullptr;
423 char *raw_data_str
= nullptr;
424 char *data_str
= nullptr;
425 char buffer
[PATH_MAX
];
427 if (!attr
|| !url
|| strlen(url
) >= PATH_MAX
) {
428 ret
= -LTTNG_ERR_INVALID
;
432 if (!attr
->override_attr
) {
433 attr
->override_attr
= zmalloc
<config_load_session_override_attr
>();
434 if (!attr
->override_attr
) {
435 ret
= -LTTNG_ERR_NOMEM
;
441 * FIXME: uri_parse should be able to take as parameter the protocol
442 * type to validate "url". For now only check the parsing goes through;
443 * it will fail later on.
445 ret_size
= uri_parse_str_urls(url
, nullptr, &uri
);
446 if (ret_size
< 0 || ret_size
> 2) {
447 /* Unexpected URL format. */
448 ret
= -LTTNG_ERR_INVALID
;
452 raw_url_str
= lttng_strndup(url
, PATH_MAX
);
454 ret
= -LTTNG_ERR_NOMEM
;
458 /* Get path | ctrl && data string URL. */
459 ret
= uri_to_str_url(&uri
[0], buffer
, sizeof(buffer
));
460 if (ret
< 0 || ret
>= PATH_MAX
) {
461 ret
= -LTTNG_ERR_INVALID
;
466 switch (uri
[0].dtype
) {
468 raw_path_str
= lttng_strndup(buffer
, PATH_MAX
);
470 ret
= -LTTNG_ERR_NOMEM
;
474 path_str
= lttng_strndup(uri
[0].dst
.path
, PATH_MAX
);
476 ret
= -LTTNG_ERR_NOMEM
;
483 ret
= -LTTNG_ERR_INVALID
;
487 raw_ctrl_str
= lttng_strndup(buffer
, PATH_MAX
);
489 ret
= -LTTNG_ERR_NOMEM
;
493 ctrl_str
= lttng_strndup(raw_ctrl_str
, PATH_MAX
);
495 ret
= -LTTNG_ERR_NOMEM
;
499 /* Get the data uri. */
500 ret
= uri_to_str_url(&uri
[1], buffer
, sizeof(buffer
));
502 ret
= -LTTNG_ERR_INVALID
;
507 raw_data_str
= lttng_strndup(buffer
, PATH_MAX
);
509 ret
= -LTTNG_ERR_NOMEM
;
513 data_str
= lttng_strndup(raw_data_str
, PATH_MAX
);
515 ret
= -LTTNG_ERR_NOMEM
;
521 ret
= -LTTNG_ERR_INVALID
;
525 reset_load_session_attr_urls(attr
);
527 attr
->override_attr
->path_url
= path_str
;
528 attr
->override_attr
->ctrl_url
= ctrl_str
;
529 attr
->override_attr
->data_url
= data_str
;
531 attr
->raw_override_url
= raw_url_str
;
532 attr
->raw_override_path_url
= raw_path_str
;
533 attr
->raw_override_ctrl_url
= raw_ctrl_str
;
534 attr
->raw_override_data_url
= raw_data_str
;
536 /* Pass data ownership to attr. */
537 raw_url_str
= nullptr;
538 raw_path_str
= nullptr;
540 raw_ctrl_str
= nullptr;
542 raw_data_str
= nullptr;
557 int lttng_load_session_attr_set_override_session_name(struct lttng_load_session_attr
*attr
,
558 const char *session_name
)
563 if (!attr
|| !session_name
) {
564 ret
= -LTTNG_ERR_INVALID
;
568 if (!attr
->override_attr
) {
569 attr
->override_attr
= zmalloc
<config_load_session_override_attr
>();
570 if (!attr
->override_attr
) {
571 ret
= -LTTNG_ERR_NOMEM
;
576 len
= strlen(session_name
);
577 if (len
>= LTTNG_NAME_MAX
) {
578 ret
= -LTTNG_ERR_INVALID
;
582 attr
->override_attr
->session_name
= lttng_strndup(session_name
, len
);
583 if (!attr
->override_attr
->session_name
) {
584 ret
= -LTTNG_ERR_NOMEM
;
591 int lttng_load_session(struct lttng_load_session_attr
*attr
)
594 const char *url
, *session_name
;
597 ret
= -LTTNG_ERR_INVALID
;
601 ret
= validate_attr(attr
);
606 url
= attr
->input_url
[0] != '\0' ? attr
->input_url
: nullptr;
607 session_name
= attr
->session_name
[0] != '\0' ? attr
->session_name
: nullptr;
609 ret
= config_load_session(url
, session_name
, attr
->overwrite
, 0, attr
->override_attr
);