2 * Copyright (C) 2014 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
12 #include <lttng/lttng-error.h>
13 #include <lttng/load.h>
14 #include <lttng/load-internal.h>
15 #include <common/sessiond-comm/sessiond-comm.h>
16 #include <common/config/session-config.h>
17 #include <common/uri.h>
18 #include <common/macros.h>
19 #include <common/compat/string.h>
21 #include "lttng-ctl-helper.h"
23 struct lttng_load_session_attr
*lttng_load_session_attr_create(void)
25 return zmalloc(sizeof(struct lttng_load_session_attr
));
29 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
70 && attr
->session_name
[0] == '\0') {
71 ret
= -LTTNG_ERR_INVALID
;
78 const char *lttng_load_session_attr_get_session_name(
79 struct lttng_load_session_attr
*attr
)
81 const char *ret
= NULL
;
83 if (attr
&& attr
->session_name
[0]) {
84 ret
= attr
->session_name
;
90 const char *lttng_load_session_attr_get_input_url(
91 struct lttng_load_session_attr
*attr
)
93 const char *ret
= NULL
;
95 if (attr
&& attr
->input_url
[0]) {
96 ret
= attr
->input_url
;
102 int lttng_load_session_attr_get_overwrite(
103 struct lttng_load_session_attr
*attr
)
105 return attr
? attr
->overwrite
: -LTTNG_ERR_INVALID
;
108 const char *lttng_load_session_attr_get_override_ctrl_url(
109 struct lttng_load_session_attr
*attr
)
111 const char *ret
= NULL
;
113 if (!attr
|| !attr
->override_attr
) {
117 ret
= attr
->raw_override_ctrl_url
;
122 const char *lttng_load_session_attr_get_override_data_url(
123 struct lttng_load_session_attr
*attr
)
125 const char *ret
= NULL
;
127 if (!attr
|| !attr
->override_attr
) {
131 ret
= attr
->raw_override_data_url
;
136 const char *lttng_load_session_attr_get_override_url(
137 struct lttng_load_session_attr
*attr
)
139 const char *ret
= NULL
;
141 if (!attr
|| !attr
->override_attr
) {
145 if ((attr
->override_attr
->path_url
||
146 (attr
->override_attr
->ctrl_url
&&
147 attr
->override_attr
->data_url
))) {
148 ret
= attr
->raw_override_url
;
154 const char *lttng_load_session_attr_get_override_session_name(
155 struct lttng_load_session_attr
*attr
)
157 const char *ret
= NULL
;
159 if (!attr
|| !attr
->override_attr
) {
163 ret
= attr
->override_attr
->session_name
;
168 int lttng_load_session_attr_set_session_name(
169 struct lttng_load_session_attr
*attr
, const char *session_name
)
174 ret
= -LTTNG_ERR_INVALID
;
181 len
= strlen(session_name
);
182 if (len
>= LTTNG_NAME_MAX
) {
183 ret
= -LTTNG_ERR_INVALID
;
187 ret
= lttng_strncpy(attr
->session_name
, session_name
,
188 sizeof(attr
->session_name
));
190 ret
= -LTTNG_ERR_INVALID
;
194 attr
->session_name
[0] = '\0';
200 int lttng_load_session_attr_set_input_url(
201 struct lttng_load_session_attr
*attr
, const char *url
)
206 struct lttng_uri
*uris
= NULL
;
209 ret
= -LTTNG_ERR_INVALID
;
214 attr
->input_url
[0] = '\0';
220 if (len
>= PATH_MAX
) {
221 ret
= -LTTNG_ERR_INVALID
;
225 size
= uri_parse_str_urls(url
, NULL
, &uris
);
226 if (size
<= 0 || uris
[0].dtype
!= LTTNG_DST_PATH
) {
227 ret
= -LTTNG_ERR_INVALID
;
231 /* Copy string plus the NULL terminated byte. */
232 ret
= lttng_strncpy(attr
->input_url
, uris
[0].dst
.path
,
233 sizeof(attr
->input_url
));
235 ret
= -LTTNG_ERR_INVALID
;
245 int lttng_load_session_attr_set_overwrite(
246 struct lttng_load_session_attr
*attr
, int overwrite
)
251 ret
= -LTTNG_ERR_INVALID
;
255 attr
->overwrite
= !!overwrite
;
260 int lttng_load_session_attr_set_override_ctrl_url(
261 struct lttng_load_session_attr
*attr
, const char *url
)
265 struct lttng_uri
*uri
= NULL
;
266 char *url_str
= NULL
;
267 char *raw_str
= NULL
;
270 ret
= -LTTNG_ERR_INVALID
;
274 if (!attr
->override_attr
) {
275 attr
->override_attr
= zmalloc(
276 sizeof(struct config_load_session_override_attr
));
277 if (!attr
->override_attr
) {
278 ret
= -LTTNG_ERR_NOMEM
;
283 if (attr
->override_attr
->path_url
) {
285 * Setting a ctrl override after a path override makes no sense.
287 ret
= -LTTNG_ERR_INVALID
;
292 * FIXME: uri_parse should be able to take as parameter the protocol
293 * type to validate "url". For now only check the parsing goes through;
294 * it will fail later on.
296 ret_size
= uri_parse(url
, &uri
);
298 ret
= -LTTNG_ERR_INVALID
;
302 if (uri
[0].port
== 0) {
303 uri
[0].port
= DEFAULT_NETWORK_CONTROL_PORT
;
306 url_str
= zmalloc(PATH_MAX
);
308 /* FIXME: return valid error */
309 ret
= -LTTNG_ERR_NOMEM
;
313 ret
= uri_to_str_url(&uri
[0], url_str
, PATH_MAX
);
315 ret
= -LTTNG_ERR_INVALID
;
320 raw_str
= lttng_strndup(url
, PATH_MAX
);
322 ret
= -LTTNG_ERR_NOMEM
;
326 /* Squash old value if any */
327 free(attr
->override_attr
->ctrl_url
);
328 free(attr
->raw_override_ctrl_url
);
330 /* Populate the object */
331 attr
->override_attr
->ctrl_url
= url_str
;
332 attr
->raw_override_ctrl_url
= raw_str
;
334 /* Ownership passed to attr. */
345 int lttng_load_session_attr_set_override_data_url(
346 struct lttng_load_session_attr
*attr
, const char *url
)
350 struct lttng_uri
*uri
= NULL
;
351 char *url_str
= NULL
;
352 char *raw_str
= NULL
;
355 ret
= -LTTNG_ERR_INVALID
;
359 if (!attr
->override_attr
) {
360 attr
->override_attr
= zmalloc(
361 sizeof(struct config_load_session_override_attr
));
362 if (!attr
->override_attr
) {
363 ret
= -LTTNG_ERR_NOMEM
;
368 if (attr
->override_attr
->path_url
) {
370 * Setting a data override after a path override makes no sense.
372 ret
= -LTTNG_ERR_INVALID
;
377 * FIXME: uri_parse should be able to take as parameter the protocol
378 * type to validate "url". For now only check the parsing goes through;
379 * it will fail later on.
381 ret_size
= uri_parse(url
, &uri
);
383 ret
= -LTTNG_ERR_INVALID
;
387 if (uri
[0].port
== 0) {
388 uri
[0].port
= DEFAULT_NETWORK_DATA_PORT
;
391 url_str
= zmalloc(PATH_MAX
);
393 ret
= -LTTNG_ERR_NOMEM
;
397 ret
= uri_to_str_url(&uri
[0], url_str
, PATH_MAX
);
399 ret
= -LTTNG_ERR_INVALID
;
404 raw_str
= lttng_strndup(url
, PATH_MAX
);
406 ret
= -LTTNG_ERR_NOMEM
;
410 /* Squash old value if any */
411 free(attr
->override_attr
->data_url
);
412 free(attr
->raw_override_data_url
);
414 /* Populate the object */
415 attr
->override_attr
->data_url
= url_str
;
416 attr
->raw_override_data_url
= raw_str
;
418 /* Ownership passed to attr. */
428 int lttng_load_session_attr_set_override_url(
429 struct lttng_load_session_attr
*attr
, const char *url
)
433 struct lttng_uri
*uri
= NULL
;
434 char *raw_url_str
= NULL
;
435 char *raw_path_str
= NULL
;
436 char *path_str
= NULL
;
437 char *raw_ctrl_str
= NULL
;
438 char *ctrl_str
= NULL
;
439 char *raw_data_str
= NULL
;
440 char *data_str
= NULL
;
441 char buffer
[PATH_MAX
];
443 if (!attr
|| !url
|| strlen(url
) >= PATH_MAX
) {
444 ret
= -LTTNG_ERR_INVALID
;
448 if (!attr
->override_attr
) {
449 attr
->override_attr
= zmalloc(
450 sizeof(struct config_load_session_override_attr
));
451 if (!attr
->override_attr
) {
452 ret
= -LTTNG_ERR_NOMEM
;
458 * FIXME: uri_parse should be able to take as parameter the protocol
459 * type to validate "url". For now only check the parsing goes through;
460 * it will fail later on.
462 ret_size
= uri_parse_str_urls(url
, NULL
, &uri
);
463 if (ret_size
< 0 || ret_size
> 2) {
464 /* Unexpected URL format. */
465 ret
= -LTTNG_ERR_INVALID
;
469 raw_url_str
= lttng_strndup(url
, PATH_MAX
);
471 ret
= -LTTNG_ERR_NOMEM
;
475 /* Get path | ctrl && data string URL. */
476 ret
= uri_to_str_url(&uri
[0], buffer
, sizeof(buffer
));
477 if (ret
< 0 || ret
>= PATH_MAX
) {
478 ret
= -LTTNG_ERR_INVALID
;
483 switch (uri
[0].dtype
) {
485 raw_path_str
= lttng_strndup(buffer
, PATH_MAX
);
487 ret
= -LTTNG_ERR_NOMEM
;
491 path_str
= lttng_strndup(raw_path_str
, PATH_MAX
);
493 ret
= -LTTNG_ERR_NOMEM
;
500 ret
= -LTTNG_ERR_INVALID
;
504 raw_ctrl_str
= lttng_strndup(buffer
, PATH_MAX
);
506 ret
= -LTTNG_ERR_NOMEM
;
510 ctrl_str
= lttng_strndup(raw_ctrl_str
, PATH_MAX
);
512 ret
= -LTTNG_ERR_NOMEM
;
516 /* Get the data uri. */
517 ret
= uri_to_str_url(&uri
[1], buffer
, sizeof(buffer
));
519 ret
= -LTTNG_ERR_INVALID
;
524 raw_data_str
= lttng_strndup(buffer
, PATH_MAX
);
526 ret
= -LTTNG_ERR_NOMEM
;
530 data_str
= lttng_strndup(raw_data_str
, PATH_MAX
);
532 ret
= -LTTNG_ERR_NOMEM
;
538 ret
= -LTTNG_ERR_INVALID
;
542 reset_load_session_attr_urls(attr
);
544 attr
->override_attr
->path_url
= path_str
;
545 attr
->override_attr
->ctrl_url
= ctrl_str
;
546 attr
->override_attr
->data_url
= data_str
;
548 attr
->raw_override_url
= raw_url_str
;
549 attr
->raw_override_path_url
= raw_path_str
;
550 attr
->raw_override_ctrl_url
= raw_ctrl_str
;
551 attr
->raw_override_data_url
= raw_data_str
;
553 /* Pass data ownership to attr. */
574 int lttng_load_session_attr_set_override_session_name(
575 struct lttng_load_session_attr
*attr
, const char *session_name
)
580 if (!attr
||!session_name
) {
581 ret
= -LTTNG_ERR_INVALID
;
585 if (!attr
->override_attr
) {
586 attr
->override_attr
= zmalloc(
587 sizeof(struct config_load_session_override_attr
));
588 if (!attr
->override_attr
) {
589 ret
= -LTTNG_ERR_NOMEM
;
594 len
= strlen(session_name
);
595 if (len
>= LTTNG_NAME_MAX
) {
596 ret
= -LTTNG_ERR_INVALID
;
600 attr
->override_attr
->session_name
= lttng_strndup(session_name
,
602 if (!attr
->override_attr
->session_name
) {
603 ret
= -LTTNG_ERR_NOMEM
;
610 int lttng_load_session(struct lttng_load_session_attr
*attr
)
613 const char *url
, *session_name
;
616 ret
= -LTTNG_ERR_INVALID
;
620 ret
= validate_attr(attr
);
625 url
= attr
->input_url
[0] != '\0' ? attr
->input_url
: NULL
;
626 session_name
= attr
->session_name
[0] != '\0' ?
627 attr
->session_name
: NULL
;
629 ret
= config_load_session(url
, session_name
, attr
->overwrite
, 0,
630 attr
->override_attr
);