2 * Copyright (C) 2014 - David Goulet <dgoulet@efficios.com>
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <lttng/lttng-error.h>
24 #include <lttng/load.h>
25 #include <lttng/load-internal.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
27 #include <common/config/session-config.h>
28 #include <common/uri.h>
29 #include <common/macros.h>
30 #include <common/compat/string.h>
32 #include "lttng-ctl-helper.h"
34 struct lttng_load_session_attr
*lttng_load_session_attr_create(void)
36 return zmalloc(sizeof(struct lttng_load_session_attr
));
40 void reset_load_session_attr_urls(struct lttng_load_session_attr
*attr
)
42 free(attr
->raw_override_url
);
43 free(attr
->raw_override_path_url
);
44 free(attr
->raw_override_ctrl_url
);
45 free(attr
->raw_override_data_url
);
46 if (attr
->override_attr
) {
47 free(attr
->override_attr
->path_url
);
48 free(attr
->override_attr
->ctrl_url
);
49 free(attr
->override_attr
->data_url
);
50 free(attr
->override_attr
->session_name
);
54 void lttng_load_session_attr_destroy(struct lttng_load_session_attr
*attr
)
57 reset_load_session_attr_urls(attr
);
58 free(attr
->override_attr
);
63 static int validate_attr(const struct lttng_load_session_attr
*attr
)
68 ret
= -LTTNG_ERR_INVALID
;
72 if (!attr
->override_attr
) {
77 * Refuse override name if the objective is to load multiple session
78 * since this operation is ambiguous while loading multiple session.
80 if (attr
->override_attr
->session_name
81 && attr
->session_name
[0] == '\0') {
82 ret
= -LTTNG_ERR_INVALID
;
89 const char *lttng_load_session_attr_get_session_name(
90 struct lttng_load_session_attr
*attr
)
92 const char *ret
= NULL
;
94 if (attr
&& attr
->session_name
[0]) {
95 ret
= attr
->session_name
;
101 const char *lttng_load_session_attr_get_input_url(
102 struct lttng_load_session_attr
*attr
)
104 const char *ret
= NULL
;
106 if (attr
&& attr
->input_url
[0]) {
107 ret
= attr
->input_url
;
113 int lttng_load_session_attr_get_overwrite(
114 struct lttng_load_session_attr
*attr
)
116 return attr
? attr
->overwrite
: -LTTNG_ERR_INVALID
;
119 const char *lttng_load_session_attr_get_override_ctrl_url(
120 struct lttng_load_session_attr
*attr
)
122 const char *ret
= NULL
;
124 if (!attr
|| !attr
->override_attr
) {
128 ret
= attr
->raw_override_ctrl_url
;
133 const char *lttng_load_session_attr_get_override_data_url(
134 struct lttng_load_session_attr
*attr
)
136 const char *ret
= NULL
;
138 if (!attr
|| !attr
->override_attr
) {
142 ret
= attr
->raw_override_data_url
;
147 const char *lttng_load_session_attr_get_override_url(
148 struct lttng_load_session_attr
*attr
)
150 const char *ret
= NULL
;
152 if (!attr
|| !attr
->override_attr
) {
156 if ((attr
->override_attr
->path_url
||
157 (attr
->override_attr
->ctrl_url
&&
158 attr
->override_attr
->data_url
))) {
159 ret
= attr
->raw_override_url
;
165 const char *lttng_load_session_attr_get_override_session_name(
166 struct lttng_load_session_attr
*attr
)
168 const char *ret
= NULL
;
170 if (!attr
|| !attr
->override_attr
) {
174 ret
= attr
->override_attr
->session_name
;
179 int lttng_load_session_attr_set_session_name(
180 struct lttng_load_session_attr
*attr
, const char *session_name
)
185 ret
= -LTTNG_ERR_INVALID
;
192 len
= strlen(session_name
);
193 if (len
>= LTTNG_NAME_MAX
) {
194 ret
= -LTTNG_ERR_INVALID
;
198 strncpy(attr
->session_name
, session_name
, len
);
200 attr
->session_name
[0] = '\0';
206 int lttng_load_session_attr_set_input_url(
207 struct lttng_load_session_attr
*attr
, const char *url
)
212 struct lttng_uri
*uris
= NULL
;
215 ret
= -LTTNG_ERR_INVALID
;
220 attr
->input_url
[0] = '\0';
226 if (len
>= PATH_MAX
) {
227 ret
= -LTTNG_ERR_INVALID
;
231 size
= uri_parse_str_urls(url
, NULL
, &uris
);
232 if (size
<= 0 || uris
[0].dtype
!= LTTNG_DST_PATH
) {
233 ret
= -LTTNG_ERR_INVALID
;
237 /* Copy string plus the NULL terminated byte. */
238 lttng_ctl_copy_string(attr
->input_url
, uris
[0].dst
.path
,
239 sizeof(attr
->input_url
));
247 int lttng_load_session_attr_set_overwrite(
248 struct lttng_load_session_attr
*attr
, int overwrite
)
253 ret
= -LTTNG_ERR_INVALID
;
257 attr
->overwrite
= !!overwrite
;
262 int lttng_load_session_attr_set_override_ctrl_url(
263 struct lttng_load_session_attr
*attr
, const char *url
)
267 struct lttng_uri
*uri
= NULL
;
268 char *url_str
= NULL
;
269 char *raw_str
= NULL
;
272 ret
= -LTTNG_ERR_INVALID
;
276 if (!attr
->override_attr
) {
277 attr
->override_attr
= zmalloc(
278 sizeof(struct config_load_session_override_attr
));
279 if (!attr
->override_attr
) {
280 ret
= -LTTNG_ERR_NOMEM
;
285 if (attr
->override_attr
->path_url
) {
287 * Setting a ctrl override after a path override makes no sense.
289 ret
= -LTTNG_ERR_INVALID
;
294 * FIXME: uri_parse should be able to take as parameter the protocol
295 * type to validate "url". For now only check the parsing goes through;
296 * it will fail later on.
298 ret_size
= uri_parse(url
, &uri
);
300 ret
= -LTTNG_ERR_INVALID
;
304 if (uri
[0].port
== 0) {
305 uri
[0].port
= DEFAULT_NETWORK_CONTROL_PORT
;
308 url_str
= zmalloc(PATH_MAX
);
310 /* FIXME: return valid error */
311 ret
= -LTTNG_ERR_NOMEM
;
315 ret
= uri_to_str_url(&uri
[0], url_str
, PATH_MAX
);
317 ret
= -LTTNG_ERR_INVALID
;
322 raw_str
= lttng_strndup(url
, PATH_MAX
);
324 ret
= -LTTNG_ERR_NOMEM
;
328 /* Squash old value if any */
329 free(attr
->override_attr
->ctrl_url
);
330 free(attr
->raw_override_ctrl_url
);
332 /* Populate the object */
333 attr
->override_attr
->ctrl_url
= url_str
;
334 attr
->raw_override_ctrl_url
= raw_str
;
336 /* Ownership passed to attr. */
347 int lttng_load_session_attr_set_override_data_url(
348 struct lttng_load_session_attr
*attr
, const char *url
)
352 struct lttng_uri
*uri
= NULL
;
353 char *url_str
= NULL
;
354 char *raw_str
= NULL
;
357 ret
= -LTTNG_ERR_INVALID
;
361 if (!attr
->override_attr
) {
362 attr
->override_attr
= zmalloc(
363 sizeof(struct config_load_session_override_attr
));
364 if (!attr
->override_attr
) {
365 ret
= -LTTNG_ERR_NOMEM
;
370 if (attr
->override_attr
->path_url
) {
372 * Setting a data override after a path override makes no sense.
374 ret
= -LTTNG_ERR_INVALID
;
379 * FIXME: uri_parse should be able to take as parameter the protocol
380 * type to validate "url". For now only check the parsing goes through;
381 * it will fail later on.
383 ret_size
= uri_parse(url
, &uri
);
385 ret
= -LTTNG_ERR_INVALID
;
389 if (uri
[0].port
== 0) {
390 uri
[0].port
= DEFAULT_NETWORK_DATA_PORT
;
393 url_str
= zmalloc(PATH_MAX
);
395 ret
= -LTTNG_ERR_NOMEM
;
399 ret
= uri_to_str_url(&uri
[0], url_str
, PATH_MAX
);
401 ret
= -LTTNG_ERR_INVALID
;
406 raw_str
= lttng_strndup(url
, PATH_MAX
);
408 ret
= -LTTNG_ERR_NOMEM
;
412 /* Squash old value if any */
413 free(attr
->override_attr
->data_url
);
414 free(attr
->raw_override_data_url
);
416 /* Populate the object */
417 attr
->override_attr
->data_url
= url_str
;
418 attr
->raw_override_data_url
= raw_str
;
420 /* Ownership passed to attr. */
430 int lttng_load_session_attr_set_override_url(
431 struct lttng_load_session_attr
*attr
, const char *url
)
435 struct lttng_uri
*uri
= NULL
;
436 char *raw_url_str
= NULL
;
437 char *raw_path_str
= NULL
;
438 char *path_str
= NULL
;
439 char *raw_ctrl_str
= NULL
;
440 char *ctrl_str
= NULL
;
441 char *raw_data_str
= NULL
;
442 char *data_str
= NULL
;
443 char buffer
[PATH_MAX
];
445 if (!attr
|| !url
|| strlen(url
) >= PATH_MAX
) {
446 ret
= -LTTNG_ERR_INVALID
;
450 if (!attr
->override_attr
) {
451 attr
->override_attr
= zmalloc(
452 sizeof(struct config_load_session_override_attr
));
453 if (!attr
->override_attr
) {
454 ret
= -LTTNG_ERR_NOMEM
;
460 * FIXME: uri_parse should be able to take as parameter the protocol
461 * type to validate "url". For now only check the parsing goes through;
462 * it will fail later on.
464 ret_size
= uri_parse_str_urls(url
, NULL
, &uri
);
465 if (ret_size
< 0 || ret_size
> 2) {
466 /* Unexpected URL format. */
467 ret
= -LTTNG_ERR_INVALID
;
471 raw_url_str
= lttng_strndup(url
, PATH_MAX
);
473 ret
= -LTTNG_ERR_NOMEM
;
477 /* Get path | ctrl && data string URL. */
478 ret
= uri_to_str_url(&uri
[0], buffer
, sizeof(buffer
));
479 if (ret
< 0 || ret
>= PATH_MAX
) {
480 ret
= -LTTNG_ERR_INVALID
;
485 switch (uri
[0].dtype
) {
487 raw_path_str
= lttng_strndup(buffer
, PATH_MAX
);
489 ret
= -LTTNG_ERR_NOMEM
;
493 path_str
= lttng_strndup(raw_path_str
, PATH_MAX
);
495 ret
= -LTTNG_ERR_NOMEM
;
502 ret
= -LTTNG_ERR_INVALID
;
506 raw_ctrl_str
= lttng_strndup(buffer
, PATH_MAX
);
508 ret
= -LTTNG_ERR_NOMEM
;
512 ctrl_str
= lttng_strndup(raw_ctrl_str
, PATH_MAX
);
514 ret
= -LTTNG_ERR_NOMEM
;
518 /* Get the data uri. */
519 ret
= uri_to_str_url(&uri
[1], buffer
, sizeof(buffer
));
521 ret
= -LTTNG_ERR_INVALID
;
526 raw_data_str
= lttng_strndup(buffer
, PATH_MAX
);
528 ret
= -LTTNG_ERR_NOMEM
;
532 data_str
= lttng_strndup(raw_data_str
, PATH_MAX
);
534 ret
= -LTTNG_ERR_NOMEM
;
540 ret
= -LTTNG_ERR_INVALID
;
544 reset_load_session_attr_urls(attr
);
546 attr
->override_attr
->path_url
= path_str
;
547 attr
->override_attr
->ctrl_url
= ctrl_str
;
548 attr
->override_attr
->data_url
= data_str
;
550 attr
->raw_override_url
= raw_url_str
;
551 attr
->raw_override_path_url
= raw_path_str
;
552 attr
->raw_override_ctrl_url
= raw_ctrl_str
;
553 attr
->raw_override_data_url
= raw_data_str
;
555 /* Pass data ownership to attr. */
576 int lttng_load_session_attr_set_override_session_name(
577 struct lttng_load_session_attr
*attr
, const char *session_name
)
582 if (!attr
||!session_name
) {
583 ret
= -LTTNG_ERR_INVALID
;
587 if (!attr
->override_attr
) {
588 attr
->override_attr
= zmalloc(
589 sizeof(struct config_load_session_override_attr
));
590 if (!attr
->override_attr
) {
591 ret
= -LTTNG_ERR_NOMEM
;
596 len
= strlen(session_name
);
597 if (len
>= LTTNG_NAME_MAX
) {
598 ret
= -LTTNG_ERR_INVALID
;
602 attr
->override_attr
->session_name
= lttng_strndup(session_name
,
604 if (!attr
->override_attr
->session_name
) {
605 ret
= -LTTNG_ERR_NOMEM
;
612 int lttng_load_session(struct lttng_load_session_attr
*attr
)
615 const char *url
, *session_name
;
618 ret
= -LTTNG_ERR_INVALID
;
622 ret
= validate_attr(attr
);
627 url
= attr
->input_url
[0] != '\0' ? attr
->input_url
: NULL
;
628 session_name
= attr
->session_name
[0] != '\0' ?
629 attr
->session_name
: NULL
;
631 ret
= config_load_session(url
, session_name
, attr
->overwrite
, 0,
632 attr
->override_attr
);