liblttng-ctl: use export list to define exported symbols
[lttng-tools.git] / src / common / session-descriptor.c
CommitLineData
b178f53e 1/*
ab5be9fa 2 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
b178f53e 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
b178f53e
JG
5 */
6
7#include <lttng/session-descriptor-internal.h>
8#include <common/macros.h>
9#include <common/uri.h>
10#include <common/defaults.h>
11#include <common/error.h>
12#include <time.h>
b178f53e
JG
13#include <stdio.h>
14
15struct lttng_session_descriptor_network_location {
16 struct lttng_uri *control;
17 struct lttng_uri *data;
18};
19
20struct lttng_session_descriptor {
21 enum lttng_session_descriptor_type type;
22 /*
23 * If an output type that is not OUTPUT_TYPE_NONE is specified,
24 * it means that an output of that type must be generated at
25 * session-creation time.
26 */
27 enum lttng_session_descriptor_output_type output_type;
28 char *name;
29 union {
30 struct lttng_session_descriptor_network_location network;
31 struct lttng_uri *local;
32 } output;
33};
34
35struct lttng_session_descriptor_snapshot {
36 struct lttng_session_descriptor base;
37 /*
38 * Assumes at-most one snapshot output is supported. Uses
39 * the output field of the base class.
40 */
41};
42
43struct lttng_session_descriptor_live {
44 struct lttng_session_descriptor base;
45 unsigned long long live_timer_us;
46};
47
48struct lttng_session_descriptor_comm {
49 /* enum lttng_session_descriptor_type */
50 uint8_t type;
51 /* enum lttng_session_descriptor_output_type */
52 uint8_t output_type;
53 /* Includes trailing null. */
54 uint32_t name_len;
55 /* Name follows, followed by URIs */
56 uint8_t uri_count;
57} LTTNG_PACKED;
58
59struct lttng_session_descriptor_live_comm {
60 struct lttng_session_descriptor_comm base;
61 /* Live-specific parameters. */
62 uint64_t live_timer_us;
63} LTTNG_PACKED;
64
65static
66struct lttng_uri *uri_copy(const struct lttng_uri *uri)
67{
68 struct lttng_uri *new_uri = NULL;
69
70 if (!uri) {
71 goto end;
72 }
73
74 new_uri = zmalloc(sizeof(*new_uri));
75 if (!new_uri) {
76 goto end;
77 }
78 memcpy(new_uri, uri, sizeof(*new_uri));
79end:
80 return new_uri;
81}
82
83static
84struct lttng_uri *uri_from_path(const char *path)
85{
86 struct lttng_uri *uris = NULL;
87 ssize_t uri_count;
88 char local_protocol_string[LTTNG_PATH_MAX + sizeof("file://")] =
89 "file://";
90
91 if (strlen(path) >= LTTNG_PATH_MAX) {
92 goto end;
93 }
94
95 if (path[0] != '/') {
96 /* Not an absolute path. */
97 goto end;
98 }
99
100 strncat(local_protocol_string, path, LTTNG_PATH_MAX);
101 uri_count = uri_parse(local_protocol_string, &uris);
102 if (uri_count != 1) {
103 goto error;
104 }
105 if (uris[0].dtype != LTTNG_DST_PATH) {
106 goto error;
107 }
108
109end:
110 return uris;
111error:
112 free(uris);
113 return NULL;
114}
115
116static
117void network_location_fini(
118 struct lttng_session_descriptor_network_location *location)
119{
120 free(location->control);
121 free(location->data);
122}
123
124/* Assumes ownership of control and data. */
125static
126int network_location_set_from_lttng_uris(
127 struct lttng_session_descriptor_network_location *location,
128 struct lttng_uri *control, struct lttng_uri *data)
129{
130 int ret = 0;
131
132 if (!control && !data) {
133 goto end;
134 }
135
136 if (!(control && data)) {
137 /* None or both must be set. */
138 ret = -1;
139 goto end;
140 }
141
142 if (control->stype != LTTNG_STREAM_CONTROL ||
143 data->stype != LTTNG_STREAM_DATA) {
144 ret = -1;
145 goto end;
146 }
147
148 free(location->control);
149 free(location->data);
150 location->control = control;
151 location->data = data;
152 control = NULL;
153 data = NULL;
154end:
155 free(control);
156 free(data);
157 return ret;
158}
159
160static
161int network_location_set_from_uri_strings(
162 struct lttng_session_descriptor_network_location *location,
163 const char *control, const char *data)
164{
165 int ret = 0;
166 ssize_t uri_count;
167 struct lttng_uri *parsed_uris = NULL;
168 struct lttng_uri *control_uri = NULL;
169 struct lttng_uri *data_uri = NULL;
170
171 uri_count = uri_parse_str_urls(control, data, &parsed_uris);
172 if (uri_count != 2 && uri_count != 0) {
173 ret = -1;
174 goto end;
175 }
176
177 /*
178 * uri_parse_str_urls returns a contiguous array of lttng_uris whereas
179 * session descriptors expect individually allocated lttng_uris.
180 */
181 if (uri_count == 2) {
182 control_uri = zmalloc(sizeof(*control_uri));
183 data_uri = zmalloc(sizeof(*data_uri));
184 if (!control_uri || !data_uri) {
185 ret = -1;
186 goto end;
187 }
188 memcpy(control_uri, &parsed_uris[0], sizeof(*control_uri));
189 memcpy(data_uri, &parsed_uris[1], sizeof(*data_uri));
190 }
191
192 /* Ownership of control and data uris is transferred. */
193 ret = network_location_set_from_lttng_uris(
194 location,
195 control_uri,
196 data_uri);
197 control_uri = NULL;
198 data_uri = NULL;
199end:
200 free(parsed_uris);
201 free(control_uri);
202 free(data_uri);
203 return ret;
204}
205
206struct lttng_session_descriptor *
207lttng_session_descriptor_create(const char *name)
208{
209 struct lttng_session_descriptor *descriptor;
210
211 descriptor = zmalloc(sizeof(*descriptor));
212 if (!descriptor) {
213 goto error;
214 }
215
216 descriptor->type = LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR;
217 descriptor->output_type =
218 LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE;
219 if (lttng_session_descriptor_set_session_name(descriptor, name)) {
220 goto error;
221 }
222 return descriptor;
223error:
224 lttng_session_descriptor_destroy(descriptor);
225 return NULL;
226}
227
228/* Ownership of uri is transferred. */
229static
230struct lttng_session_descriptor *
231_lttng_session_descriptor_local_create(const char *name,
232 struct lttng_uri *uri)
233{
234 struct lttng_session_descriptor *descriptor;
235
236 descriptor = lttng_session_descriptor_create(name);
237 if (!descriptor) {
238 goto error;
239 }
240 descriptor->type = LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR;
241 descriptor->output_type =
242 LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL;
243 if (uri) {
244 if (uri->dtype != LTTNG_DST_PATH) {
245 goto error;
246 }
247 descriptor->output.local = uri;
248 uri = NULL;
249 }
250 return descriptor;
251error:
252 free(uri);
253 lttng_session_descriptor_destroy(descriptor);
254 return NULL;
255}
256
257struct lttng_session_descriptor *
258lttng_session_descriptor_local_create(const char *name, const char *path)
259{
260 struct lttng_uri *uri = NULL;
261 struct lttng_session_descriptor *descriptor;
262
263 if (path) {
264 uri = uri_from_path(path);
265 if (!uri) {
266 goto error;
267 }
268 }
269 descriptor = _lttng_session_descriptor_local_create(name, uri);
270 return descriptor;
271error:
272 return NULL;
273}
274
275/* Assumes the ownership of both uris. */
276static
277struct lttng_session_descriptor *
278_lttng_session_descriptor_network_create(const char *name,
279 struct lttng_uri *control, struct lttng_uri *data)
280{
281 int ret;
282 struct lttng_session_descriptor *descriptor;
283
284 descriptor = lttng_session_descriptor_create(name);
285 if (!descriptor) {
286 goto error;
287 }
288
289 descriptor->type = LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR;
290 descriptor->output_type = LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK;
291 /* Assumes the ownership of both uris. */
292 ret = network_location_set_from_lttng_uris(&descriptor->output.network,
293 control, data);
294 control = NULL;
295 data = NULL;
296 if (ret) {
297 goto error;
298 }
299 return descriptor;
300error:
301 lttng_session_descriptor_destroy(descriptor);
302 free(control);
303 free(data);
304 return NULL;
305}
306
307struct lttng_session_descriptor *
308lttng_session_descriptor_network_create(const char *name,
309 const char *control_url, const char *data_url)
310{
311 int ret;
312 struct lttng_session_descriptor *descriptor;
313
314 descriptor = _lttng_session_descriptor_network_create(name,
315 NULL, NULL);
316 if (!descriptor) {
317 goto error;
318 }
319
320 ret = network_location_set_from_uri_strings(&descriptor->output.network,
321 control_url, data_url);
322 if (ret) {
323 goto error;
324 }
325 return descriptor;
326error:
327 lttng_session_descriptor_destroy(descriptor);
328 return NULL;
329}
330
331static
332struct lttng_session_descriptor_snapshot *
333_lttng_session_descriptor_snapshot_create(const char *name)
334{
335 struct lttng_session_descriptor_snapshot *descriptor;
336
337 descriptor = zmalloc(sizeof(*descriptor));
338 if (!descriptor) {
339 goto error;
340 }
341
342 descriptor->base.type = LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT;
343 descriptor->base.output_type =
344 LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE;
345 if (lttng_session_descriptor_set_session_name(&descriptor->base,
346 name)) {
347 goto error;
348 }
349 return descriptor;
350error:
351 lttng_session_descriptor_destroy(descriptor ? &descriptor->base : NULL);
352 return NULL;
353}
354
355/* Ownership of control and data is transferred. */
356static
357struct lttng_session_descriptor_snapshot *
358_lttng_session_descriptor_snapshot_network_create(const char *name,
359 struct lttng_uri *control, struct lttng_uri *data)
360{
361 int ret;
362 struct lttng_session_descriptor_snapshot *descriptor;
363
364 descriptor = _lttng_session_descriptor_snapshot_create(name);
365 if (!descriptor) {
366 goto error;
367 }
368
369 descriptor->base.output_type =
370 LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK;
371 /* Ownership of control and data is transferred. */
372 ret = network_location_set_from_lttng_uris(
373 &descriptor->base.output.network,
374 control, data);
375 control = NULL;
376 data = NULL;
377 if (ret) {
378 goto error;
379 }
380 return descriptor;
381error:
382 free(control);
383 free(data);
384 lttng_session_descriptor_destroy(descriptor ? &descriptor->base : NULL);
385 return NULL;
386}
387
388struct lttng_session_descriptor *
389lttng_session_descriptor_snapshot_create(const char *name)
390{
391 struct lttng_session_descriptor_snapshot *descriptor;
392
393 descriptor = _lttng_session_descriptor_snapshot_create(name);
394 return descriptor ? &descriptor->base : NULL;
395}
396
397struct lttng_session_descriptor *
398lttng_session_descriptor_snapshot_network_create(const char *name,
399 const char *control_url, const char *data_url)
400{
401 int ret;
402 struct lttng_session_descriptor_snapshot *descriptor;
403
404 descriptor = _lttng_session_descriptor_snapshot_network_create(name,
405 NULL, NULL);
406 if (!descriptor) {
407 goto error;
408 }
409
410 ret = network_location_set_from_uri_strings(
411 &descriptor->base.output.network,
412 control_url, data_url);
413 if (ret) {
414 goto error;
415 }
416 return &descriptor->base;
417error:
418 lttng_session_descriptor_destroy(descriptor ? &descriptor->base : NULL);
419 return NULL;
420}
421
422/* Ownership of uri is transferred. */
423static
424struct lttng_session_descriptor_snapshot *
425_lttng_session_descriptor_snapshot_local_create(const char *name,
426 struct lttng_uri *uri)
427{
428 struct lttng_session_descriptor_snapshot *descriptor;
429
430 descriptor = _lttng_session_descriptor_snapshot_create(name);
431 if (!descriptor) {
432 goto error;
433 }
434 descriptor->base.output_type =
435 LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL;
436 if (uri) {
437 if (uri->dtype != LTTNG_DST_PATH) {
438 goto error;
439 }
440 descriptor->base.output.local = uri;
441 uri = NULL;
442 }
443 return descriptor;
444error:
445 free(uri);
446 lttng_session_descriptor_destroy(descriptor ? &descriptor->base : NULL);
447 return NULL;
448}
449
450struct lttng_session_descriptor *
451lttng_session_descriptor_snapshot_local_create(const char *name,
452 const char *path)
453{
454 struct lttng_uri *path_uri = NULL;
455 struct lttng_session_descriptor_snapshot *descriptor;
456
457 if (path) {
458 path_uri = uri_from_path(path);
459 if (!path_uri) {
460 goto error;
461 }
462 }
463 descriptor = _lttng_session_descriptor_snapshot_local_create(name,
464 path_uri);
465 return descriptor ? &descriptor->base : NULL;
466error:
467 return NULL;
468}
469
470static
471struct lttng_session_descriptor_live *
472_lttng_session_descriptor_live_create(const char *name,
473 unsigned long long live_timer_interval_us)
474{
475 struct lttng_session_descriptor_live *descriptor = NULL;
476
477 if (live_timer_interval_us == 0) {
478 goto error;
479 }
480 descriptor = zmalloc(sizeof(*descriptor));
481 if (!descriptor) {
482 goto error;
483 }
484
485 descriptor->base.type = LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE;
486 descriptor->base.output_type =
487 LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE;
488 descriptor->live_timer_us = live_timer_interval_us;
489 if (lttng_session_descriptor_set_session_name(&descriptor->base,
490 name)) {
491 goto error;
492 }
493
494 return descriptor;
495error:
496 lttng_session_descriptor_destroy(descriptor ? &descriptor->base : NULL);
497 return NULL;
498}
499
500/* Ownership of control and data is transferred. */
501static
502struct lttng_session_descriptor_live *
503_lttng_session_descriptor_live_network_create(
504 const char *name,
505 struct lttng_uri *control, struct lttng_uri *data,
506 unsigned long long live_timer_interval_us)
507{
508 int ret;
509 struct lttng_session_descriptor_live *descriptor;
510
511 descriptor = _lttng_session_descriptor_live_create(name,
512 live_timer_interval_us);
ca258a7d
FD
513 if (!descriptor) {
514 goto error;
515 }
516
b178f53e
JG
517 descriptor->base.output_type =
518 LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK;
519
520 /* Ownerwhip of control and data is transferred. */
521 ret = network_location_set_from_lttng_uris(
522 &descriptor->base.output.network,
523 control, data);
524 control = NULL;
525 data = NULL;
526 if (ret) {
527 goto error;
528 }
529 return descriptor;
530error:
531 free(control);
532 free(data);
533 lttng_session_descriptor_destroy(descriptor ? &descriptor->base : NULL);
534 return NULL;
535}
536
537struct lttng_session_descriptor *
538lttng_session_descriptor_live_create(
539 const char *name,
540 unsigned long long live_timer_us)
541{
542 struct lttng_session_descriptor_live *descriptor;
543
544 descriptor = _lttng_session_descriptor_live_create(name, live_timer_us);
b178f53e
JG
545
546 return descriptor ? &descriptor->base : NULL;
b178f53e
JG
547}
548
549struct lttng_session_descriptor *
550lttng_session_descriptor_live_network_create(
551 const char *name,
552 const char *control_url, const char *data_url,
553 unsigned long long live_timer_us)
554{
555 int ret;
556 struct lttng_session_descriptor_live *descriptor;
557
558 descriptor = _lttng_session_descriptor_live_network_create(name,
559 NULL, NULL, live_timer_us);
560 if (!descriptor) {
561 goto error;
562 }
563
564 ret = network_location_set_from_uri_strings(
565 &descriptor->base.output.network,
566 control_url, data_url);
567 if (ret) {
568 goto error;
569 }
570 return &descriptor->base;
571error:
572 lttng_session_descriptor_destroy(descriptor ? &descriptor->base : NULL);
573 return NULL;
574}
575
576void lttng_session_descriptor_destroy(
577 struct lttng_session_descriptor *descriptor)
578{
579 if (!descriptor) {
580 return;
581 }
582
583 switch (descriptor->output_type) {
584 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
585 break;
586 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
587 free(descriptor->output.local);
588 break;
589 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
590 network_location_fini(&descriptor->output.network);
591 break;
592 default:
593 abort();
594 }
595
596 free(descriptor->name);
597 free(descriptor);
598}
599
b178f53e
JG
600ssize_t lttng_session_descriptor_create_from_buffer(
601 const struct lttng_buffer_view *payload,
602 struct lttng_session_descriptor **descriptor)
603{
604 int i;
605 ssize_t offset = 0, ret;
606 struct lttng_buffer_view current_view;
607 const char *name = NULL;
608 const struct lttng_session_descriptor_comm *base_header;
609 size_t max_expected_uri_count;
610 uint64_t live_timer_us = 0;
611 struct lttng_uri *uris[2] = {};
612 enum lttng_session_descriptor_type type;
613 enum lttng_session_descriptor_output_type output_type;
614
615 current_view = lttng_buffer_view_from_view(payload, offset,
616 sizeof(*base_header));
3e6e0df2 617 if (!lttng_buffer_view_is_valid(&current_view)) {
b178f53e
JG
618 ret = -1;
619 goto end;
620 }
621
3e6e0df2 622 base_header = (typeof(base_header)) current_view.data;
b178f53e
JG
623 switch (base_header->type) {
624 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR:
625 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
626 break;
627 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
628 {
629 const struct lttng_session_descriptor_live_comm *live_header;
630
631 current_view = lttng_buffer_view_from_view(payload, offset,
632 sizeof(*live_header));
3e6e0df2 633 if (!lttng_buffer_view_is_valid(&current_view)) {
b178f53e
JG
634 ret = -1;
635 goto end;
636 }
637
3e6e0df2 638 live_header = (typeof(live_header)) current_view.data;
b178f53e
JG
639 live_timer_us = live_header->live_timer_us;
640 break;
641 }
642 default:
643 ret = -1;
644 goto end;
645 }
646 /* type has been validated. */
647 type = base_header->type;
648
649 switch (base_header->output_type) {
650 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
651 max_expected_uri_count = 0;
652 break;
653 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
654 max_expected_uri_count = 1;
655 break;
656 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
657 max_expected_uri_count = 2;
658 break;
659 default:
660 ret = -1;
661 goto end;
662 }
663 /* output_type has been validated. */
664 output_type = base_header->output_type;
665
666 /* Skip after header. */
667 offset += current_view.size;
668 if (!base_header->name_len) {
669 goto skip_name;
670 }
671
672 /* Map the name. */
673 current_view = lttng_buffer_view_from_view(payload, offset,
674 base_header->name_len);
3e6e0df2 675 if (!lttng_buffer_view_is_valid(&current_view)) {
b178f53e
JG
676 ret = -1;
677 goto end;
678 }
679
3e6e0df2 680 name = current_view.data;
b178f53e
JG
681 if (base_header->name_len == 1 ||
682 name[base_header->name_len - 1] ||
683 strlen(name) != base_header->name_len - 1) {
684 /*
685 * Check that the name is not NULL, is NULL-terminated, and
686 * does not contain a NULL before the last byte.
687 */
688 ret = -1;
689 goto end;
690 }
691
692 /* Skip after the name. */
693 offset += base_header->name_len;
694skip_name:
695 if (base_header->uri_count > max_expected_uri_count) {
696 ret = -1;
697 goto end;
698 }
699
700 for (i = 0; i < base_header->uri_count; i++) {
701 struct lttng_uri *uri;
702
703 /* Map a URI. */
704 current_view = lttng_buffer_view_from_view(payload,
705 offset, sizeof(*uri));
3e6e0df2 706 if (!lttng_buffer_view_is_valid(&current_view)) {
b178f53e
JG
707 ret = -1;
708 goto end;
709 }
3e6e0df2
JG
710
711 uri = (typeof(uri)) current_view.data;
b178f53e
JG
712 uris[i] = zmalloc(sizeof(*uri));
713 if (!uris[i]) {
714 ret = -1;
715 goto end;
716 }
717 memcpy(uris[i], uri, sizeof(*uri));
718 offset += sizeof(*uri);
719 }
720
721 switch (type) {
722 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR:
723 switch (output_type) {
724 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
725 *descriptor = lttng_session_descriptor_create(name);
726 break;
727 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
728 *descriptor = _lttng_session_descriptor_local_create(
729 name, uris[0]);
730 break;
731 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
732 *descriptor = _lttng_session_descriptor_network_create(
733 name, uris[0], uris[1]);
734 break;
735 default:
736 /* Already checked. */
737 abort();
738 }
739 break;
740 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
741 {
742 struct lttng_session_descriptor_snapshot *snapshot;
743 switch (output_type) {
744 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
745 snapshot = _lttng_session_descriptor_snapshot_create(
746 name);
747 break;
748 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
749 snapshot = _lttng_session_descriptor_snapshot_local_create(
750 name, uris[0]);
751 break;
752 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
753 snapshot = _lttng_session_descriptor_snapshot_network_create(
754 name, uris[0], uris[1]);
755 break;
756 default:
757 /* Already checked. */
758 abort();
759 }
760 *descriptor = snapshot ? &snapshot->base : NULL;
761 break;
762 }
763 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
764 {
765 struct lttng_session_descriptor_live *live;
766
767 switch (output_type) {
768 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
769 live = _lttng_session_descriptor_live_create(
770 name, live_timer_us);
771 break;
772 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
773 live = _lttng_session_descriptor_live_network_create(
774 name, uris[0], uris[1],
775 live_timer_us);
776 break;
777 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
778 ret = -1;
779 goto end;
780 default:
781 /* Already checked. */
782 abort();
783 }
784 *descriptor = live ? &live->base : NULL;
785 break;
786 }
787 default:
788 /* Already checked. */
789 abort();
790 }
791 memset(uris, 0, sizeof(uris));
792 if (!*descriptor) {
793 ret = -1;
794 goto end;
795 }
796
797 ret = offset;
798end:
799 free(uris[0]);
800 free(uris[1]);
801 return ret;
802}
803
b178f53e
JG
804int lttng_session_descriptor_serialize(
805 const struct lttng_session_descriptor *descriptor,
806 struct lttng_dynamic_buffer *buffer)
807{
808 int ret, i;
809 /* There are, at most, two URIs to serialize. */
810 struct lttng_uri *uris[2] = {};
811 size_t uri_count = 0;
812 /* The live header is a superset of all headers. */
813 struct lttng_session_descriptor_live_comm header = {
814 .base.type = (uint8_t) descriptor->type,
815 .base.output_type = (uint8_t) descriptor->output_type,
816 .base.name_len = descriptor->name ?
817 strlen(descriptor->name) + 1 : 0,
818 };
819 const void *header_ptr = NULL;
820 size_t header_size;
821
822 switch (descriptor->output_type) {
823 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
824 break;
825 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
826 uris[0] = descriptor->output.local;
827 break;
828 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
829 uris[0] = descriptor->output.network.control;
830 uris[1] = descriptor->output.network.data;
831 break;
832 default:
833 ret = -1;
834 goto end;
835 }
836 uri_count += !!uris[0];
837 uri_count += !!uris[1];
838
839 header.base.uri_count = uri_count;
840 if (descriptor->type == LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE) {
841 const struct lttng_session_descriptor_live *live =
842 container_of(descriptor, typeof(*live),
843 base);
844
845 header.live_timer_us = live->live_timer_us;
846 header_ptr = &header;
847 header_size = sizeof(header);
848 } else {
849 header_ptr = &header.base;
850 header_size = sizeof(header.base);
851 }
852
853 ret = lttng_dynamic_buffer_append(buffer, header_ptr, header_size);
854 if (ret) {
855 goto end;
856 }
857 if (header.base.name_len) {
858 ret = lttng_dynamic_buffer_append(buffer, descriptor->name,
859 header.base.name_len);
860 if (ret) {
861 goto end;
862 }
863 }
864
865 for (i = 0; i < uri_count; i++) {
866 ret = lttng_dynamic_buffer_append(buffer, uris[i],
867 sizeof(struct lttng_uri));
868 if (ret) {
869 goto end;
870 }
871 }
872end:
873 return ret;
874}
875
b178f53e
JG
876enum lttng_session_descriptor_type
877lttng_session_descriptor_get_type(
878 const struct lttng_session_descriptor *descriptor)
879{
880 return descriptor->type;
881}
882
b178f53e
JG
883enum lttng_session_descriptor_output_type
884lttng_session_descriptor_get_output_type(
885 const struct lttng_session_descriptor *descriptor)
886{
887 return descriptor->output_type;
888}
889
b178f53e
JG
890void lttng_session_descriptor_get_local_output_uri(
891 const struct lttng_session_descriptor *descriptor,
892 struct lttng_uri *local_uri)
893{
894 memcpy(local_uri, descriptor->output.local, sizeof(*local_uri));
895}
896
b178f53e
JG
897void lttng_session_descriptor_get_network_output_uris(
898 const struct lttng_session_descriptor *descriptor,
899 struct lttng_uri *control,
900 struct lttng_uri *data)
901{
902 memcpy(control, descriptor->output.network.control, sizeof(*control));
903 memcpy(data, descriptor->output.network.data, sizeof(*data));
904}
905
b178f53e
JG
906unsigned long long
907lttng_session_descriptor_live_get_timer_interval(
908 const struct lttng_session_descriptor *descriptor)
909{
910 struct lttng_session_descriptor_live *live;
911
912 live = container_of(descriptor, typeof(*live), base);
913 return live->live_timer_us;
914}
915
916enum lttng_session_descriptor_status
917lttng_session_descriptor_get_session_name(
918 const struct lttng_session_descriptor *descriptor,
919 const char **session_name)
920{
921 enum lttng_session_descriptor_status status;
922
923 if (!descriptor || !session_name) {
924 status = LTTNG_SESSION_DESCRIPTOR_STATUS_INVALID;
925 goto end;
926 }
927
928 *session_name = descriptor->name;
929 status = descriptor->name ?
930 LTTNG_SESSION_DESCRIPTOR_STATUS_OK :
931 LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET;
932end:
933 return status;
934}
935
b178f53e
JG
936int lttng_session_descriptor_set_session_name(
937 struct lttng_session_descriptor *descriptor,
938 const char *name)
939{
940 int ret = 0;
941 char *new_name;
942
943 if (!name) {
944 goto end;
945 }
946 if (strlen(name) >= LTTNG_NAME_MAX) {
947 ret = -1;
948 goto end;
949 }
950 new_name = strdup(name);
951 if (!new_name) {
952 ret = -1;
953 goto end;
954 }
955 free(descriptor->name);
956 descriptor->name = new_name;
957end:
958 return ret;
959}
960
b178f53e
JG
961bool lttng_session_descriptor_is_output_destination_initialized(
962 const struct lttng_session_descriptor *descriptor)
963{
964 switch (descriptor->output_type) {
965 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
966 return true;
967 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
968 return descriptor->output.local;
969 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
970 return descriptor->output.network.control;
971 default:
972 abort();
973 }
974}
975
b178f53e
JG
976bool lttng_session_descriptor_has_output_directory(
977 const struct lttng_session_descriptor *descriptor)
978{
979 switch (descriptor->output_type) {
980 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
981 break;
982 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
983 if (descriptor->output.local) {
984 return *descriptor->output.local->dst.path;
985 }
986 break;
987 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
988 if (descriptor->output.network.control) {
989 return *descriptor->output.network.control->subdir;
990 }
991 break;
992 default:
993 abort();
994 }
995 return false;
996}
997
b178f53e
JG
998enum lttng_error_code lttng_session_descriptor_set_default_output(
999 struct lttng_session_descriptor *descriptor,
1000 time_t *session_creation_time,
1001 const char *absolute_home_path)
1002{
1003 enum lttng_error_code ret_code = LTTNG_OK;
1004 struct lttng_uri *uris = NULL;
1005
1006 switch (descriptor->output_type) {
1007 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
1008 goto end;
1009 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
1010 {
1011 int ret;
1012 ssize_t uri_ret;
1013 char local_uri[LTTNG_PATH_MAX];
1014 char creation_datetime_suffix[17] = {};
1015
1016 if (session_creation_time) {
1017 size_t strftime_ret;
1018 struct tm *timeinfo;
1019
1020 timeinfo = localtime(session_creation_time);
1021 if (!timeinfo) {
1022 ret_code = LTTNG_ERR_FATAL;
1023 goto end;
1024 }
1025 strftime_ret = strftime(creation_datetime_suffix,
1026 sizeof(creation_datetime_suffix),
1027 "-%Y%m%d-%H%M%S", timeinfo);
1028 if (strftime_ret == 0) {
1029 ERR("Failed to format session creation timestamp while setting default local output destination");
1030 ret_code = LTTNG_ERR_FATAL;
1031 goto end;
1032 }
1033 }
a0377dfe 1034 LTTNG_ASSERT(descriptor->name);
b178f53e
JG
1035 ret = snprintf(local_uri, sizeof(local_uri),
1036 "file://%s/%s/%s%s",
1037 absolute_home_path,
1038 DEFAULT_TRACE_DIR_NAME, descriptor->name,
1039 creation_datetime_suffix);
1040 if (ret >= sizeof(local_uri)) {
1041 ERR("Truncation occurred while setting default local output destination");
1042 ret_code = LTTNG_ERR_SET_URL;
1043 goto end;
1044 } else if (ret < 0) {
1045 PERROR("Failed to format default local output URI");
1046 ret_code = LTTNG_ERR_SET_URL;
1047 goto end;
1048 }
1049
1050 uri_ret = uri_parse(local_uri, &uris);
1051 if (uri_ret != 1) {
1052 ret_code = LTTNG_ERR_SET_URL;
1053 goto end;
1054 }
1055 free(descriptor->output.local);
1056 descriptor->output.local = &uris[0];
1057 uris = NULL;
1058 break;
1059 }
1060 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
1061 {
1062 int ret;
1063 ssize_t uri_ret;
1064 struct lttng_uri *control = NULL, *data = NULL;
1065
1066 uri_ret = uri_parse_str_urls("net://127.0.0.1", NULL, &uris);
1067 if (uri_ret != 2) {
1068 ret_code = LTTNG_ERR_SET_URL;
1069 goto end;
1070 }
1071
1072 control = uri_copy(&uris[0]);
1073 data = uri_copy(&uris[1]);
1074 if (!control || !data) {
1075 free(control);
1076 free(data);
1077 ret_code = LTTNG_ERR_SET_URL;
1078 goto end;
1079 }
1080
1081 /* Ownership of uris is transferred. */
1082 ret = network_location_set_from_lttng_uris(
1083 &descriptor->output.network,
1084 control, data);
1085 if (ret) {
1086 abort();
1087 ret_code = LTTNG_ERR_SET_URL;
1088 goto end;
1089 }
1090 break;
1091 }
1092 default:
1093 abort();
1094 }
1095end:
1096 free(uris);
1097 return ret_code;
1098}
1099
1100/*
1101 * Note that only properties that can be populated by the session daemon
1102 * (output destination and name) are assigned.
1103 */
b178f53e
JG
1104int lttng_session_descriptor_assign(
1105 struct lttng_session_descriptor *dst,
1106 const struct lttng_session_descriptor *src)
1107{
1108 int ret = 0;
1109
1110 if (dst->type != src->type) {
1111 ret = -1;
1112 goto end;
1113 }
1114 if (dst->output_type != src->output_type) {
1115 ret = -1;
1116 goto end;
1117 }
1118 ret = lttng_session_descriptor_set_session_name(dst, src->name);
1119 if (ret) {
1120 goto end;
1121 }
1122 switch (dst->output_type) {
1123 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
1124 free(dst->output.local);
1125 dst->output.local = uri_copy(src->output.local);
1126 if (!dst->output.local) {
1127 ret = -1;
1128 goto end;
1129 }
1130 break;
1131 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
1132 {
1133 struct lttng_uri *control_copy = NULL, *data_copy = NULL;
1134
1135 control_copy = uri_copy(dst->output.network.control);
1136 if (!control_copy && dst->output.network.control) {
1137 ret = -1;
1138 goto end;
1139 }
1140 data_copy = uri_copy(dst->output.network.data);
1141 if (!data_copy && dst->output.network.data) {
1142 free(control_copy);
1143 ret = -1;
1144 goto end;
1145 }
1146 ret = network_location_set_from_lttng_uris(&dst->output.network,
1147 control_copy, data_copy);
1148 break;
1149 }
1150 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
1151 goto end;
1152 }
1153end:
1154 return ret;
1155}
This page took 0.075273 seconds and 4 git commands to generate.