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
);
53 void lttng_load_session_attr_destroy(struct lttng_load_session_attr
*attr
)
56 reset_load_session_attr_urls(attr
);
57 free(attr
->override_attr
);
62 const char *lttng_load_session_attr_get_session_name(
63 struct lttng_load_session_attr
*attr
)
65 const char *ret
= NULL
;
67 if (attr
&& attr
->session_name
[0]) {
68 ret
= attr
->session_name
;
74 const char *lttng_load_session_attr_get_input_url(
75 struct lttng_load_session_attr
*attr
)
77 const char *ret
= NULL
;
79 if (attr
&& attr
->input_url
[0]) {
80 ret
= attr
->input_url
;
86 int lttng_load_session_attr_get_overwrite(
87 struct lttng_load_session_attr
*attr
)
89 return attr
? attr
->overwrite
: -LTTNG_ERR_INVALID
;
92 const char *lttng_load_session_attr_get_override_ctrl_url(
93 struct lttng_load_session_attr
*attr
)
95 const char *ret
= NULL
;
97 if (attr
&& attr
->override_attr
->ctrl_url
) {
98 ret
= attr
->raw_override_ctrl_url
;
104 const char *lttng_load_session_attr_get_override_data_url(
105 struct lttng_load_session_attr
*attr
)
107 const char *ret
= NULL
;
109 if (attr
&& attr
->override_attr
->data_url
) {
110 ret
= attr
->raw_override_data_url
;
116 const char *lttng_load_session_attr_get_override_url(
117 struct lttng_load_session_attr
*attr
)
119 const char *ret
= NULL
;
121 if (attr
&& (attr
->override_attr
->path_url
||
122 (attr
->override_attr
->ctrl_url
&&
123 attr
->override_attr
->data_url
))) {
124 ret
= attr
->raw_override_url
;
130 int lttng_load_session_attr_set_session_name(
131 struct lttng_load_session_attr
*attr
, const char *session_name
)
136 ret
= -LTTNG_ERR_INVALID
;
143 len
= strlen(session_name
);
144 if (len
>= LTTNG_NAME_MAX
) {
145 ret
= -LTTNG_ERR_INVALID
;
149 strncpy(attr
->session_name
, session_name
, len
);
151 attr
->session_name
[0] = '\0';
157 int lttng_load_session_attr_set_input_url(
158 struct lttng_load_session_attr
*attr
, const char *url
)
163 struct lttng_uri
*uris
= NULL
;
166 ret
= -LTTNG_ERR_INVALID
;
171 attr
->input_url
[0] = '\0';
177 if (len
>= PATH_MAX
) {
178 ret
= -LTTNG_ERR_INVALID
;
182 size
= uri_parse_str_urls(url
, NULL
, &uris
);
183 if (size
<= 0 || uris
[0].dtype
!= LTTNG_DST_PATH
) {
184 ret
= -LTTNG_ERR_INVALID
;
188 /* Copy string plus the NULL terminated byte. */
189 lttng_ctl_copy_string(attr
->input_url
, uris
[0].dst
.path
,
190 sizeof(attr
->input_url
));
198 int lttng_load_session_attr_set_overwrite(
199 struct lttng_load_session_attr
*attr
, int overwrite
)
204 ret
= -LTTNG_ERR_INVALID
;
208 attr
->overwrite
= !!overwrite
;
213 int lttng_load_session_attr_set_override_ctrl_url(
214 struct lttng_load_session_attr
*attr
, const char *url
)
218 struct lttng_uri
*uri
= NULL
;
219 char *url_str
= NULL
;
220 char *raw_str
= NULL
;
223 ret
= -LTTNG_ERR_INVALID
;
227 if (!attr
->override_attr
) {
228 attr
->override_attr
= zmalloc(
229 sizeof(struct config_load_session_override_attr
));
230 if (!attr
->override_attr
) {
231 ret
= -LTTNG_ERR_NOMEM
;
236 if (attr
->override_attr
->path_url
) {
238 * Setting a ctrl override after a path override makes no sense.
240 ret
= -LTTNG_ERR_INVALID
;
245 * FIXME: uri_parse should be able to take as parameter the protocol
246 * type to validate "url". For now only check the parsing goes through;
247 * it will fail later on.
249 ret_size
= uri_parse(url
, &uri
);
251 ret
= -LTTNG_ERR_INVALID
;
255 if (uri
[0].port
== 0) {
256 uri
[0].port
= DEFAULT_NETWORK_CONTROL_PORT
;
259 url_str
= zmalloc(PATH_MAX
);
261 /* FIXME: return valid error */
262 ret
= -LTTNG_ERR_NOMEM
;
266 ret
= uri_to_str_url(&uri
[0], url_str
, PATH_MAX
);
268 ret
= -LTTNG_ERR_INVALID
;
273 raw_str
= lttng_strndup(url
, PATH_MAX
);
275 ret
= -LTTNG_ERR_NOMEM
;
279 /* Squash old value if any */
280 free(attr
->override_attr
->ctrl_url
);
281 free(attr
->raw_override_ctrl_url
);
283 /* Populate the object */
284 attr
->override_attr
->ctrl_url
= url_str
;
285 attr
->raw_override_ctrl_url
= raw_str
;
287 /* Ownership passed to attr. */
298 int lttng_load_session_attr_set_override_data_url(
299 struct lttng_load_session_attr
*attr
, const char *url
)
303 struct lttng_uri
*uri
= NULL
;
304 char *url_str
= NULL
;
305 char *raw_str
= NULL
;
308 ret
= -LTTNG_ERR_INVALID
;
312 if (!attr
->override_attr
) {
313 attr
->override_attr
= zmalloc(
314 sizeof(struct config_load_session_override_attr
));
315 if (!attr
->override_attr
) {
316 ret
= -LTTNG_ERR_NOMEM
;
321 if (attr
->override_attr
->path_url
) {
323 * Setting a data override after a path override makes no sense.
325 ret
= -LTTNG_ERR_INVALID
;
330 * FIXME: uri_parse should be able to take as parameter the protocol
331 * type to validate "url". For now only check the parsing goes through;
332 * it will fail later on.
334 ret_size
= uri_parse(url
, &uri
);
336 ret
= -LTTNG_ERR_INVALID
;
340 if (uri
[0].port
== 0) {
341 uri
[0].port
= DEFAULT_NETWORK_DATA_PORT
;
344 url_str
= zmalloc(PATH_MAX
);
346 ret
= -LTTNG_ERR_NOMEM
;
350 ret
= uri_to_str_url(&uri
[0], url_str
, PATH_MAX
);
352 ret
= -LTTNG_ERR_INVALID
;
357 raw_str
= lttng_strndup(url
, PATH_MAX
);
359 ret
= -LTTNG_ERR_NOMEM
;
363 /* Squash old value if any */
364 free(attr
->override_attr
->data_url
);
365 free(attr
->raw_override_data_url
);
367 /* Populate the object */
368 attr
->override_attr
->data_url
= url_str
;
369 attr
->raw_override_data_url
= raw_str
;
371 /* Ownership passed to attr. */
381 int lttng_load_session_attr_set_override_url(
382 struct lttng_load_session_attr
*attr
, const char *url
)
386 struct lttng_uri
*uri
= NULL
;
387 char *raw_url_str
= NULL
;
388 char *raw_path_str
= NULL
;
389 char *path_str
= NULL
;
390 char *raw_ctrl_str
= NULL
;
391 char *ctrl_str
= NULL
;
392 char *raw_data_str
= NULL
;
393 char *data_str
= NULL
;
394 char buffer
[PATH_MAX
];
396 if (!attr
|| !url
|| strlen(url
) >= PATH_MAX
) {
397 ret
= -LTTNG_ERR_INVALID
;
401 if (!attr
->override_attr
) {
402 attr
->override_attr
= zmalloc(
403 sizeof(struct config_load_session_override_attr
));
404 if (!attr
->override_attr
) {
405 ret
= -LTTNG_ERR_NOMEM
;
411 * FIXME: uri_parse should be able to take as parameter the protocol
412 * type to validate "url". For now only check the parsing goes through;
413 * it will fail later on.
415 ret_size
= uri_parse_str_urls(url
, NULL
, &uri
);
416 if (ret_size
< 0 || ret_size
> 2) {
417 /* Unexpected URL format. */
418 ret
= -LTTNG_ERR_INVALID
;
422 raw_url_str
= lttng_strndup(url
, PATH_MAX
);
424 ret
= -LTTNG_ERR_NOMEM
;
428 /* Get path | ctrl && data string URL. */
429 ret
= uri_to_str_url(&uri
[0], buffer
, sizeof(buffer
));
430 if (ret
< 0 || ret
>= PATH_MAX
) {
431 ret
= -LTTNG_ERR_INVALID
;
436 switch (uri
[0].dtype
) {
438 raw_path_str
= lttng_strndup(buffer
, PATH_MAX
);
440 ret
= -LTTNG_ERR_NOMEM
;
444 path_str
= lttng_strndup(raw_path_str
, PATH_MAX
);
446 ret
= -LTTNG_ERR_NOMEM
;
453 ret
= -LTTNG_ERR_INVALID
;
457 raw_ctrl_str
= lttng_strndup(buffer
, PATH_MAX
);
459 ret
= -LTTNG_ERR_NOMEM
;
463 ctrl_str
= lttng_strndup(raw_ctrl_str
, PATH_MAX
);
465 ret
= -LTTNG_ERR_NOMEM
;
469 /* Get the data uri. */
470 ret
= uri_to_str_url(&uri
[1], buffer
, sizeof(buffer
));
472 ret
= -LTTNG_ERR_INVALID
;
477 raw_data_str
= lttng_strndup(buffer
, PATH_MAX
);
479 ret
= -LTTNG_ERR_NOMEM
;
483 data_str
= lttng_strndup(raw_data_str
, PATH_MAX
);
485 ret
= -LTTNG_ERR_NOMEM
;
491 ret
= -LTTNG_ERR_INVALID
;
495 reset_load_session_attr_urls(attr
);
497 attr
->override_attr
->path_url
= path_str
;
498 attr
->override_attr
->ctrl_url
= ctrl_str
;
499 attr
->override_attr
->data_url
= data_str
;
501 attr
->raw_override_url
= raw_url_str
;
502 attr
->raw_override_path_url
= raw_path_str
;
503 attr
->raw_override_ctrl_url
= raw_ctrl_str
;
504 attr
->raw_override_data_url
= raw_data_str
;
506 /* Pass data ownership to attr. */
527 int lttng_load_session(struct lttng_load_session_attr
*attr
)
530 const char *url
, *session_name
;
533 ret
= -LTTNG_ERR_INVALID
;
537 url
= attr
->input_url
[0] != '\0' ? attr
->input_url
: NULL
;
538 session_name
= attr
->session_name
[0] != '\0' ?
539 attr
->session_name
: NULL
;
541 ret
= config_load_session(url
, session_name
, attr
->overwrite
, 0,
542 attr
->override_attr
);