lttng-ctl: locate extended event attributes at reception
[lttng-tools.git] / src / lib / lttng-ctl / load.c
CommitLineData
9245bd0e
DG
1/*
2 * Copyright (C) 2014 - David Goulet <dgoulet@efficios.com>
3 *
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.
7 *
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
11 * for more details.
12 *
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
16 */
17
6c1c0768 18#define _LGPL_SOURCE
9245bd0e
DG
19#include <assert.h>
20#include <string.h>
a2a75fa4 21#include <limits.h>
9245bd0e
DG
22
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>
f40ef1d5 27#include <common/config/session-config.h>
a2a75fa4
JR
28#include <common/uri.h>
29#include <common/macros.h>
30#include <common/compat/string.h>
9245bd0e
DG
31
32#include "lttng-ctl-helper.h"
33
34struct lttng_load_session_attr *lttng_load_session_attr_create(void)
35{
36 return zmalloc(sizeof(struct lttng_load_session_attr));
37}
38
a2a75fa4
JR
39static
40void reset_load_session_attr_urls(struct lttng_load_session_attr *attr)
41{
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);
1b08cbce
JR
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);
2aaf5fc7 50 free(attr->override_attr->session_name);
1b08cbce 51 }
a2a75fa4
JR
52}
53
9245bd0e
DG
54void lttng_load_session_attr_destroy(struct lttng_load_session_attr *attr)
55{
56 if (attr) {
a2a75fa4 57 reset_load_session_attr_urls(attr);
1b08cbce 58 free(attr->override_attr);
9245bd0e
DG
59 free(attr);
60 }
61}
62
2aaf5fc7
JR
63static int validate_attr(const struct lttng_load_session_attr *attr)
64{
65 int ret = 0;
66
67 if (!attr) {
68 ret = -LTTNG_ERR_INVALID;
69 goto end;
70 }
71
72 if (!attr->override_attr) {
73 goto end;
74 }
75
76 /*
77 * Refuse override name if the objective is to load multiple session
78 * since this operation is ambiguous while loading multiple session.
79 */
80 if (attr->override_attr->session_name
81 && attr->session_name[0] == '\0') {
82 ret = -LTTNG_ERR_INVALID;
83 goto end;
84 }
85end:
86 return ret;
87}
88
9245bd0e
DG
89const char *lttng_load_session_attr_get_session_name(
90 struct lttng_load_session_attr *attr)
91{
92 const char *ret = NULL;
93
94 if (attr && attr->session_name[0]) {
95 ret = attr->session_name;
96 }
97
98 return ret;
99}
100
101const char *lttng_load_session_attr_get_input_url(
102 struct lttng_load_session_attr *attr)
103{
104 const char *ret = NULL;
105
106 if (attr && attr->input_url[0]) {
107 ret = attr->input_url;
108 }
109
110 return ret;
111}
112
113int lttng_load_session_attr_get_overwrite(
114 struct lttng_load_session_attr *attr)
115{
116 return attr ? attr->overwrite : -LTTNG_ERR_INVALID;
117}
118
a2a75fa4
JR
119const char *lttng_load_session_attr_get_override_ctrl_url(
120 struct lttng_load_session_attr *attr)
121{
122 const char *ret = NULL;
123
49c360e8
JR
124 if (!attr || !attr->override_attr) {
125 goto end;
a2a75fa4
JR
126 }
127
49c360e8
JR
128 ret = attr->raw_override_ctrl_url;
129end:
a2a75fa4
JR
130 return ret;
131}
132
133const char *lttng_load_session_attr_get_override_data_url(
134 struct lttng_load_session_attr *attr)
135{
136 const char *ret = NULL;
137
49c360e8
JR
138 if (!attr || !attr->override_attr) {
139 goto end;
a2a75fa4
JR
140 }
141
49c360e8
JR
142 ret = attr->raw_override_data_url;
143end:
a2a75fa4
JR
144 return ret;
145}
146
147const char *lttng_load_session_attr_get_override_url(
148 struct lttng_load_session_attr *attr)
149{
150 const char *ret = NULL;
151
49c360e8
JR
152 if (!attr || !attr->override_attr) {
153 goto end;
154 }
155
156 if ((attr->override_attr->path_url ||
1b08cbce
JR
157 (attr->override_attr->ctrl_url &&
158 attr->override_attr->data_url))) {
a2a75fa4
JR
159 ret = attr->raw_override_url;
160 }
49c360e8 161end:
a2a75fa4
JR
162 return ret;
163}
164
2aaf5fc7
JR
165const char *lttng_load_session_attr_get_override_session_name(
166 struct lttng_load_session_attr *attr)
167{
168 const char *ret = NULL;
169
170 if (!attr || !attr->override_attr) {
171 goto end;
172 }
173
174 ret = attr->override_attr->session_name;
175end:
176 return ret;
177}
178
9245bd0e
DG
179int lttng_load_session_attr_set_session_name(
180 struct lttng_load_session_attr *attr, const char *session_name)
181{
182 int ret = 0;
183
184 if (!attr) {
185 ret = -LTTNG_ERR_INVALID;
186 goto error;
187 }
188
189 if (session_name) {
190 size_t len;
191
192 len = strlen(session_name);
36d2e35d 193 if (len >= LTTNG_NAME_MAX) {
9245bd0e
DG
194 ret = -LTTNG_ERR_INVALID;
195 goto error;
196 }
197
3716477e
JG
198 ret = lttng_strncpy(attr->session_name, session_name,
199 sizeof(attr->session_name));
200 if (ret) {
201 ret = -LTTNG_ERR_INVALID;
202 goto error;
203 }
9245bd0e
DG
204 } else {
205 attr->session_name[0] = '\0';
206 }
207error:
208 return ret;
209}
210
211int lttng_load_session_attr_set_input_url(
212 struct lttng_load_session_attr *attr, const char *url)
213{
214 int ret = 0;
584e475e
JR
215 size_t len;
216 ssize_t size;
9245bd0e
DG
217 struct lttng_uri *uris = NULL;
218
219 if (!attr) {
220 ret = -LTTNG_ERR_INVALID;
221 goto error;
222 }
223
224 if (!url) {
225 attr->input_url[0] = '\0';
226 ret = 0;
227 goto end;
228 }
229
230 len = strlen(url);
231 if (len >= PATH_MAX) {
232 ret = -LTTNG_ERR_INVALID;
233 goto error;
234 }
235
236 size = uri_parse_str_urls(url, NULL, &uris);
237 if (size <= 0 || uris[0].dtype != LTTNG_DST_PATH) {
238 ret = -LTTNG_ERR_INVALID;
239 goto error;
240 }
241
242 /* Copy string plus the NULL terminated byte. */
243 lttng_ctl_copy_string(attr->input_url, uris[0].dst.path,
244 sizeof(attr->input_url));
245
246end:
247error:
248 free(uris);
249 return ret;
250}
251
252int lttng_load_session_attr_set_overwrite(
253 struct lttng_load_session_attr *attr, int overwrite)
254{
255 int ret = 0;
256
257 if (!attr) {
258 ret = -LTTNG_ERR_INVALID;
259 goto end;
260 }
261
262 attr->overwrite = !!overwrite;
263end:
264 return ret;
265}
266
a2a75fa4
JR
267int lttng_load_session_attr_set_override_ctrl_url(
268 struct lttng_load_session_attr *attr, const char *url)
269{
270 int ret = 0;
271 ssize_t ret_size;
272 struct lttng_uri *uri = NULL;
273 char *url_str = NULL;
274 char *raw_str = NULL;
275
276 if (!attr) {
277 ret = -LTTNG_ERR_INVALID;
278 goto end;
279 }
280
1b08cbce
JR
281 if (!attr->override_attr) {
282 attr->override_attr = zmalloc(
283 sizeof(struct config_load_session_override_attr));
284 if (!attr->override_attr) {
285 ret = -LTTNG_ERR_NOMEM;
286 goto end;
287 }
288 }
289
290 if (attr->override_attr->path_url) {
a2a75fa4 291 /*
1b08cbce
JR
292 * Setting a ctrl override after a path override makes no sense.
293 */
a2a75fa4
JR
294 ret = -LTTNG_ERR_INVALID;
295 goto end;
296 }
297
298 /*
299 * FIXME: uri_parse should be able to take as parameter the protocol
300 * type to validate "url". For now only check the parsing goes through;
301 * it will fail later on.
302 */
303 ret_size = uri_parse(url, &uri);
304 if (ret_size < 0) {
305 ret = -LTTNG_ERR_INVALID;
306 goto end;
307 }
308
309 if (uri[0].port == 0) {
310 uri[0].port = DEFAULT_NETWORK_CONTROL_PORT;
311 }
312
313 url_str = zmalloc(PATH_MAX);
314 if (!url_str) {
315 /* FIXME: return valid error */
316 ret = -LTTNG_ERR_NOMEM;
317 goto end;
318 }
319
320 ret = uri_to_str_url(&uri[0], url_str, PATH_MAX);
321 if (ret < 0) {
322 ret = -LTTNG_ERR_INVALID;
323 goto end;
324 }
325 ret = 0;
326
327 raw_str = lttng_strndup(url, PATH_MAX);
328 if (!raw_str) {
329 ret = -LTTNG_ERR_NOMEM;
330 goto end;
331 }
332
333 /* Squash old value if any */
1b08cbce 334 free(attr->override_attr->ctrl_url);
a2a75fa4
JR
335 free(attr->raw_override_ctrl_url);
336
337 /* Populate the object */
1b08cbce 338 attr->override_attr->ctrl_url = url_str;
a2a75fa4
JR
339 attr->raw_override_ctrl_url = raw_str;
340
341 /* Ownership passed to attr. */
342 url_str = NULL;
343 raw_str = NULL;
344
345end:
346 free(raw_str);
347 free(url_str);
348 free(uri);
349 return ret;
350}
351
352int lttng_load_session_attr_set_override_data_url(
353 struct lttng_load_session_attr *attr, const char *url)
354{
355 int ret = 0;
356 ssize_t ret_size;
357 struct lttng_uri *uri = NULL;
358 char *url_str = NULL;
359 char *raw_str = NULL;
360
361 if (!attr) {
362 ret = -LTTNG_ERR_INVALID;
363 goto end;
364 }
365
1b08cbce
JR
366 if (!attr->override_attr) {
367 attr->override_attr = zmalloc(
368 sizeof(struct config_load_session_override_attr));
369 if (!attr->override_attr) {
370 ret = -LTTNG_ERR_NOMEM;
371 goto end;
372 }
373 }
374
375 if (attr->override_attr->path_url) {
376 /*
377 * Setting a data override after a path override makes no sense.
378 */
a2a75fa4
JR
379 ret = -LTTNG_ERR_INVALID;
380 goto end;
381 }
382
383 /*
384 * FIXME: uri_parse should be able to take as parameter the protocol
385 * type to validate "url". For now only check the parsing goes through;
386 * it will fail later on.
387 */
388 ret_size = uri_parse(url, &uri);
389 if (ret_size < 0) {
390 ret = -LTTNG_ERR_INVALID;
391 goto end;
392 }
393
394 if (uri[0].port == 0) {
395 uri[0].port = DEFAULT_NETWORK_DATA_PORT;
396 }
397
398 url_str = zmalloc(PATH_MAX);
399 if (!url_str) {
400 ret = -LTTNG_ERR_NOMEM;
401 goto end;
402 }
403
404 ret = uri_to_str_url(&uri[0], url_str, PATH_MAX);
405 if (ret < 0) {
406 ret = -LTTNG_ERR_INVALID;
407 goto end;
408 }
409 ret = 0;
410
411 raw_str = lttng_strndup(url, PATH_MAX);
412 if (!raw_str) {
413 ret = -LTTNG_ERR_NOMEM;
414 goto end;
415 }
416
417 /* Squash old value if any */
1b08cbce 418 free(attr->override_attr->data_url);
a2a75fa4
JR
419 free(attr->raw_override_data_url);
420
421 /* Populate the object */
1b08cbce 422 attr->override_attr->data_url = url_str;
a2a75fa4
JR
423 attr->raw_override_data_url = raw_str;
424
425 /* Ownership passed to attr. */
426 url_str = NULL;
427 raw_str = NULL;
428end:
429 free(raw_str);
430 free(url_str);
431 free(uri);
432 return ret;
433}
434
435int lttng_load_session_attr_set_override_url(
436 struct lttng_load_session_attr *attr, const char *url)
437{
438 int ret = 0;
439 ssize_t ret_size;
440 struct lttng_uri *uri = NULL;
441 char *raw_url_str = NULL;
442 char *raw_path_str = NULL;
443 char *path_str = NULL;
444 char *raw_ctrl_str = NULL;
445 char *ctrl_str = NULL;
446 char *raw_data_str = NULL;
447 char *data_str = NULL;
448 char buffer[PATH_MAX];
449
450 if (!attr || !url || strlen(url) >= PATH_MAX) {
451 ret = -LTTNG_ERR_INVALID;
452 goto end;
453 }
454
1b08cbce
JR
455 if (!attr->override_attr) {
456 attr->override_attr = zmalloc(
457 sizeof(struct config_load_session_override_attr));
458 if (!attr->override_attr) {
459 ret = -LTTNG_ERR_NOMEM;
460 goto end;
461 }
462 }
463
a2a75fa4
JR
464 /*
465 * FIXME: uri_parse should be able to take as parameter the protocol
466 * type to validate "url". For now only check the parsing goes through;
467 * it will fail later on.
468 */
469 ret_size = uri_parse_str_urls(url, NULL, &uri);
470 if (ret_size < 0 || ret_size > 2) {
471 /* Unexpected URL format. */
472 ret = -LTTNG_ERR_INVALID;
473 goto end;
474 }
475
476 raw_url_str = lttng_strndup(url, PATH_MAX);
477 if (!raw_url_str) {
478 ret = -LTTNG_ERR_NOMEM;
479 goto end;
480 }
481
482 /* Get path | ctrl && data string URL. */
483 ret = uri_to_str_url(&uri[0], buffer, sizeof(buffer));
484 if (ret < 0 || ret >= PATH_MAX) {
485 ret = -LTTNG_ERR_INVALID;
486 goto end;
487 }
488 ret = 0;
489
490 switch (uri[0].dtype) {
491 case LTTNG_DST_PATH:
492 raw_path_str = lttng_strndup(buffer, PATH_MAX);
493 if (!raw_path_str) {
494 ret = -LTTNG_ERR_NOMEM;
495 goto end;
496 }
497
498 path_str = lttng_strndup(raw_path_str, PATH_MAX);
499 if (!path_str) {
500 ret = -LTTNG_ERR_NOMEM;
501 goto end;
502 }
503 break;
504 case LTTNG_DST_IPV4:
505 case LTTNG_DST_IPV6:
506 if (ret_size != 2) {
507 ret = -LTTNG_ERR_INVALID;
508 goto end;
509 }
510
511 raw_ctrl_str = lttng_strndup(buffer, PATH_MAX);
512 if (!raw_ctrl_str) {
513 ret = -LTTNG_ERR_NOMEM;
514 goto end;
515 }
516
517 ctrl_str = lttng_strndup(raw_ctrl_str, PATH_MAX);
518 if (!ctrl_str) {
519 ret = -LTTNG_ERR_NOMEM;
520 goto end;
521 }
522
523 /* Get the data uri. */
524 ret = uri_to_str_url(&uri[1], buffer, sizeof(buffer));
525 if (ret < 0) {
526 ret = -LTTNG_ERR_INVALID;
527 goto end;
528 }
529 ret = 0;
530
531 raw_data_str = lttng_strndup(buffer, PATH_MAX);
532 if (!raw_data_str) {
533 ret = -LTTNG_ERR_NOMEM;
534 goto end;
535 }
536
537 data_str = lttng_strndup(raw_data_str, PATH_MAX);
538 if (!data_str) {
539 ret = -LTTNG_ERR_NOMEM;
540 goto end;
541 }
542
543 break;
544 default:
545 ret = -LTTNG_ERR_INVALID;
546 goto end;
547 }
548
549 reset_load_session_attr_urls(attr);
550
1b08cbce
JR
551 attr->override_attr->path_url = path_str;
552 attr->override_attr->ctrl_url = ctrl_str;
553 attr->override_attr->data_url = data_str;
a2a75fa4
JR
554
555 attr->raw_override_url = raw_url_str;
556 attr->raw_override_path_url = raw_path_str;
557 attr->raw_override_ctrl_url = raw_ctrl_str;
558 attr->raw_override_data_url = raw_data_str;
559
560 /* Pass data ownership to attr. */
561 raw_url_str = NULL;
562 raw_path_str = NULL;
563 path_str = NULL;
564 raw_ctrl_str = NULL;
565 ctrl_str = NULL;
566 raw_data_str = NULL;
567 data_str = NULL;
568
569end:
570 free(raw_path_str);
571 free(path_str);
572 free(raw_ctrl_str);
573 free(ctrl_str);
574 free(raw_data_str);
575 free(data_str);
576 free(raw_url_str);
577 free(uri);
578 return ret;
579}
0d4bab5e 580
2aaf5fc7
JR
581int lttng_load_session_attr_set_override_session_name(
582 struct lttng_load_session_attr *attr, const char *session_name)
583{
584 int ret = 0;
585 size_t len;
586
587 if (!attr ||!session_name) {
588 ret = -LTTNG_ERR_INVALID;
589 goto end;
590 }
591
592 if (!attr->override_attr) {
593 attr->override_attr = zmalloc(
594 sizeof(struct config_load_session_override_attr));
595 if (!attr->override_attr) {
596 ret = -LTTNG_ERR_NOMEM;
597 goto end;
598 }
599 }
600
601 len = strlen(session_name);
602 if (len >= LTTNG_NAME_MAX) {
603 ret = -LTTNG_ERR_INVALID;
604 goto end;
605 }
606
607 attr->override_attr->session_name = lttng_strndup(session_name,
608 len);
609 if (!attr->override_attr->session_name) {
610 ret = -LTTNG_ERR_NOMEM;
611 goto end;
612 }
613end:
614 return ret;
615}
616
9245bd0e
DG
617int lttng_load_session(struct lttng_load_session_attr *attr)
618{
619 int ret;
18e29540 620 const char *url, *session_name;
9245bd0e
DG
621
622 if (!attr) {
623 ret = -LTTNG_ERR_INVALID;
624 goto end;
625 }
626
2aaf5fc7
JR
627 ret = validate_attr(attr);
628 if (ret) {
629 goto end;
630 }
631
18e29540
JG
632 url = attr->input_url[0] != '\0' ? attr->input_url : NULL;
633 session_name = attr->session_name[0] != '\0' ?
634 attr->session_name : NULL;
635
1b08cbce
JR
636 ret = config_load_session(url, session_name, attr->overwrite, 0,
637 attr->override_attr);
9245bd0e
DG
638
639end:
640 return ret;
641}
This page took 0.056327 seconds and 4 git commands to generate.