2 * Copyright (C) 2014 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
13 #include <lttng/lttng-error.h>
14 #include <lttng/load.h>
15 #include <lttng/load-internal.h>
16 #include <common/sessiond-comm/sessiond-comm.h>
17 #include <common/config/session-config.h>
18 #include <common/uri.h>
19 #include <common/macros.h>
20 #include <common/compat/string.h>
22 #include "lttng-ctl-helper.h"
24 struct lttng_load_session_attr
*lttng_load_session_attr_create(void)
26 return zmalloc(sizeof(struct lttng_load_session_attr
));
30 void reset_load_session_attr_urls(struct lttng_load_session_attr
*attr
)
32 free(attr
->raw_override_url
);
33 free(attr
->raw_override_path_url
);
34 free(attr
->raw_override_ctrl_url
);
35 free(attr
->raw_override_data_url
);
36 if (attr
->override_attr
) {
37 free(attr
->override_attr
->path_url
);
38 free(attr
->override_attr
->ctrl_url
);
39 free(attr
->override_attr
->data_url
);
40 free(attr
->override_attr
->session_name
);
44 void lttng_load_session_attr_destroy(struct lttng_load_session_attr
*attr
)
47 reset_load_session_attr_urls(attr
);
48 free(attr
->override_attr
);
53 static int validate_attr(const struct lttng_load_session_attr
*attr
)
58 ret
= -LTTNG_ERR_INVALID
;
62 if (!attr
->override_attr
) {
67 * Refuse override name if the objective is to load multiple session
68 * since this operation is ambiguous while loading multiple session.
70 if (attr
->override_attr
->session_name
71 && attr
->session_name
[0] == '\0') {
72 ret
= -LTTNG_ERR_INVALID
;
79 const char *lttng_load_session_attr_get_session_name(
80 struct lttng_load_session_attr
*attr
)
82 const char *ret
= NULL
;
84 if (attr
&& attr
->session_name
[0]) {
85 ret
= attr
->session_name
;
91 const char *lttng_load_session_attr_get_input_url(
92 struct lttng_load_session_attr
*attr
)
94 const char *ret
= NULL
;
96 if (attr
&& attr
->input_url
[0]) {
97 ret
= attr
->input_url
;
103 int lttng_load_session_attr_get_overwrite(
104 struct lttng_load_session_attr
*attr
)
106 return attr
? attr
->overwrite
: -LTTNG_ERR_INVALID
;
109 const char *lttng_load_session_attr_get_override_ctrl_url(
110 struct lttng_load_session_attr
*attr
)
112 const char *ret
= NULL
;
114 if (!attr
|| !attr
->override_attr
) {
118 ret
= attr
->raw_override_ctrl_url
;
123 const char *lttng_load_session_attr_get_override_data_url(
124 struct lttng_load_session_attr
*attr
)
126 const char *ret
= NULL
;
128 if (!attr
|| !attr
->override_attr
) {
132 ret
= attr
->raw_override_data_url
;
137 const char *lttng_load_session_attr_get_override_url(
138 struct lttng_load_session_attr
*attr
)
140 const char *ret
= NULL
;
142 if (!attr
|| !attr
->override_attr
) {
146 if ((attr
->override_attr
->path_url
||
147 (attr
->override_attr
->ctrl_url
&&
148 attr
->override_attr
->data_url
))) {
149 ret
= attr
->raw_override_url
;
155 const char *lttng_load_session_attr_get_override_session_name(
156 struct lttng_load_session_attr
*attr
)
158 const char *ret
= NULL
;
160 if (!attr
|| !attr
->override_attr
) {
164 ret
= attr
->override_attr
->session_name
;
169 int lttng_load_session_attr_set_session_name(
170 struct lttng_load_session_attr
*attr
, const char *session_name
)
175 ret
= -LTTNG_ERR_INVALID
;
182 len
= strlen(session_name
);
183 if (len
>= LTTNG_NAME_MAX
) {
184 ret
= -LTTNG_ERR_INVALID
;
188 ret
= lttng_strncpy(attr
->session_name
, session_name
,
189 sizeof(attr
->session_name
));
191 ret
= -LTTNG_ERR_INVALID
;
195 attr
->session_name
[0] = '\0';
201 int lttng_load_session_attr_set_input_url(
202 struct lttng_load_session_attr
*attr
, const char *url
)
207 struct lttng_uri
*uris
= NULL
;
210 ret
= -LTTNG_ERR_INVALID
;
215 attr
->input_url
[0] = '\0';
221 if (len
>= PATH_MAX
) {
222 ret
= -LTTNG_ERR_INVALID
;
226 size
= uri_parse_str_urls(url
, NULL
, &uris
);
227 if (size
<= 0 || uris
[0].dtype
!= LTTNG_DST_PATH
) {
228 ret
= -LTTNG_ERR_INVALID
;
232 /* Copy string plus the NULL terminated byte. */
233 ret
= lttng_strncpy(attr
->input_url
, uris
[0].dst
.path
,
234 sizeof(attr
->input_url
));
236 ret
= -LTTNG_ERR_INVALID
;
246 int lttng_load_session_attr_set_overwrite(
247 struct lttng_load_session_attr
*attr
, int overwrite
)
252 ret
= -LTTNG_ERR_INVALID
;
256 attr
->overwrite
= !!overwrite
;
261 int lttng_load_session_attr_set_override_ctrl_url(
262 struct lttng_load_session_attr
*attr
, const char *url
)
266 struct lttng_uri
*uri
= NULL
;
267 char *url_str
= NULL
;
268 char *raw_str
= NULL
;
271 ret
= -LTTNG_ERR_INVALID
;
275 if (!attr
->override_attr
) {
276 attr
->override_attr
= zmalloc(
277 sizeof(struct config_load_session_override_attr
));
278 if (!attr
->override_attr
) {
279 ret
= -LTTNG_ERR_NOMEM
;
284 if (attr
->override_attr
->path_url
) {
286 * Setting a ctrl override after a path override makes no sense.
288 ret
= -LTTNG_ERR_INVALID
;
293 * FIXME: uri_parse should be able to take as parameter the protocol
294 * type to validate "url". For now only check the parsing goes through;
295 * it will fail later on.
297 ret_size
= uri_parse(url
, &uri
);
299 ret
= -LTTNG_ERR_INVALID
;
303 if (uri
[0].port
== 0) {
304 uri
[0].port
= DEFAULT_NETWORK_CONTROL_PORT
;
307 url_str
= zmalloc(PATH_MAX
);
309 /* FIXME: return valid error */
310 ret
= -LTTNG_ERR_NOMEM
;
314 ret
= uri_to_str_url(&uri
[0], url_str
, PATH_MAX
);
316 ret
= -LTTNG_ERR_INVALID
;
321 raw_str
= lttng_strndup(url
, PATH_MAX
);
323 ret
= -LTTNG_ERR_NOMEM
;
327 /* Squash old value if any */
328 free(attr
->override_attr
->ctrl_url
);
329 free(attr
->raw_override_ctrl_url
);
331 /* Populate the object */
332 attr
->override_attr
->ctrl_url
= url_str
;
333 attr
->raw_override_ctrl_url
= raw_str
;
335 /* Ownership passed to attr. */
346 int lttng_load_session_attr_set_override_data_url(
347 struct lttng_load_session_attr
*attr
, const char *url
)
351 struct lttng_uri
*uri
= NULL
;
352 char *url_str
= NULL
;
353 char *raw_str
= NULL
;
356 ret
= -LTTNG_ERR_INVALID
;
360 if (!attr
->override_attr
) {
361 attr
->override_attr
= zmalloc(
362 sizeof(struct config_load_session_override_attr
));
363 if (!attr
->override_attr
) {
364 ret
= -LTTNG_ERR_NOMEM
;
369 if (attr
->override_attr
->path_url
) {
371 * Setting a data override after a path override makes no sense.
373 ret
= -LTTNG_ERR_INVALID
;
378 * FIXME: uri_parse should be able to take as parameter the protocol
379 * type to validate "url". For now only check the parsing goes through;
380 * it will fail later on.
382 ret_size
= uri_parse(url
, &uri
);
384 ret
= -LTTNG_ERR_INVALID
;
388 if (uri
[0].port
== 0) {
389 uri
[0].port
= DEFAULT_NETWORK_DATA_PORT
;
392 url_str
= zmalloc(PATH_MAX
);
394 ret
= -LTTNG_ERR_NOMEM
;
398 ret
= uri_to_str_url(&uri
[0], url_str
, PATH_MAX
);
400 ret
= -LTTNG_ERR_INVALID
;
405 raw_str
= lttng_strndup(url
, PATH_MAX
);
407 ret
= -LTTNG_ERR_NOMEM
;
411 /* Squash old value if any */
412 free(attr
->override_attr
->data_url
);
413 free(attr
->raw_override_data_url
);
415 /* Populate the object */
416 attr
->override_attr
->data_url
= url_str
;
417 attr
->raw_override_data_url
= raw_str
;
419 /* Ownership passed to attr. */
429 int lttng_load_session_attr_set_override_url(
430 struct lttng_load_session_attr
*attr
, const char *url
)
434 struct lttng_uri
*uri
= NULL
;
435 char *raw_url_str
= NULL
;
436 char *raw_path_str
= NULL
;
437 char *path_str
= NULL
;
438 char *raw_ctrl_str
= NULL
;
439 char *ctrl_str
= NULL
;
440 char *raw_data_str
= NULL
;
441 char *data_str
= NULL
;
442 char buffer
[PATH_MAX
];
444 if (!attr
|| !url
|| strlen(url
) >= PATH_MAX
) {
445 ret
= -LTTNG_ERR_INVALID
;
449 if (!attr
->override_attr
) {
450 attr
->override_attr
= zmalloc(
451 sizeof(struct config_load_session_override_attr
));
452 if (!attr
->override_attr
) {
453 ret
= -LTTNG_ERR_NOMEM
;
459 * FIXME: uri_parse should be able to take as parameter the protocol
460 * type to validate "url". For now only check the parsing goes through;
461 * it will fail later on.
463 ret_size
= uri_parse_str_urls(url
, NULL
, &uri
);
464 if (ret_size
< 0 || ret_size
> 2) {
465 /* Unexpected URL format. */
466 ret
= -LTTNG_ERR_INVALID
;
470 raw_url_str
= lttng_strndup(url
, PATH_MAX
);
472 ret
= -LTTNG_ERR_NOMEM
;
476 /* Get path | ctrl && data string URL. */
477 ret
= uri_to_str_url(&uri
[0], buffer
, sizeof(buffer
));
478 if (ret
< 0 || ret
>= PATH_MAX
) {
479 ret
= -LTTNG_ERR_INVALID
;
484 switch (uri
[0].dtype
) {
486 raw_path_str
= lttng_strndup(buffer
, PATH_MAX
);
488 ret
= -LTTNG_ERR_NOMEM
;
492 path_str
= lttng_strndup(raw_path_str
, PATH_MAX
);
494 ret
= -LTTNG_ERR_NOMEM
;
501 ret
= -LTTNG_ERR_INVALID
;
505 raw_ctrl_str
= lttng_strndup(buffer
, PATH_MAX
);
507 ret
= -LTTNG_ERR_NOMEM
;
511 ctrl_str
= lttng_strndup(raw_ctrl_str
, PATH_MAX
);
513 ret
= -LTTNG_ERR_NOMEM
;
517 /* Get the data uri. */
518 ret
= uri_to_str_url(&uri
[1], buffer
, sizeof(buffer
));
520 ret
= -LTTNG_ERR_INVALID
;
525 raw_data_str
= lttng_strndup(buffer
, PATH_MAX
);
527 ret
= -LTTNG_ERR_NOMEM
;
531 data_str
= lttng_strndup(raw_data_str
, PATH_MAX
);
533 ret
= -LTTNG_ERR_NOMEM
;
539 ret
= -LTTNG_ERR_INVALID
;
543 reset_load_session_attr_urls(attr
);
545 attr
->override_attr
->path_url
= path_str
;
546 attr
->override_attr
->ctrl_url
= ctrl_str
;
547 attr
->override_attr
->data_url
= data_str
;
549 attr
->raw_override_url
= raw_url_str
;
550 attr
->raw_override_path_url
= raw_path_str
;
551 attr
->raw_override_ctrl_url
= raw_ctrl_str
;
552 attr
->raw_override_data_url
= raw_data_str
;
554 /* Pass data ownership to attr. */
575 int lttng_load_session_attr_set_override_session_name(
576 struct lttng_load_session_attr
*attr
, const char *session_name
)
581 if (!attr
||!session_name
) {
582 ret
= -LTTNG_ERR_INVALID
;
586 if (!attr
->override_attr
) {
587 attr
->override_attr
= zmalloc(
588 sizeof(struct config_load_session_override_attr
));
589 if (!attr
->override_attr
) {
590 ret
= -LTTNG_ERR_NOMEM
;
595 len
= strlen(session_name
);
596 if (len
>= LTTNG_NAME_MAX
) {
597 ret
= -LTTNG_ERR_INVALID
;
601 attr
->override_attr
->session_name
= lttng_strndup(session_name
,
603 if (!attr
->override_attr
->session_name
) {
604 ret
= -LTTNG_ERR_NOMEM
;
611 int lttng_load_session(struct lttng_load_session_attr
*attr
)
614 const char *url
, *session_name
;
617 ret
= -LTTNG_ERR_INVALID
;
621 ret
= validate_attr(attr
);
626 url
= attr
->input_url
[0] != '\0' ? attr
->input_url
: NULL
;
627 session_name
= attr
->session_name
[0] != '\0' ?
628 attr
->session_name
: NULL
;
630 ret
= config_load_session(url
, session_name
, attr
->overwrite
, 0,
631 attr
->override_attr
);