2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2011 EfficiOS Inc.
5 * Copyright (C) 2011-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 #include <sys/socket.h>
15 #include <sys/types.h>
22 #include <lttng/ust-ctl.h>
23 #include "common/ustcomm.h"
24 #include "common/ust-fd.h"
25 #include "common/macros.h"
26 #include "common/dynamic-type.h"
27 #include "common/logging.h"
29 #include "common/events.h"
30 #include "common/compat/pthread.h"
32 #define USTCOMM_MAX_SEND_FDS 4
35 ssize_t
count_fields_recursive(size_t nr_fields
,
36 const struct lttng_ust_event_field
* const *lttng_fields
);
38 int serialize_one_field(struct lttng_ust_session
*session
,
39 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
40 const struct lttng_ust_event_field
*lf
,
41 const char **prev_field_name
);
43 int serialize_fields(struct lttng_ust_session
*session
,
44 struct lttng_ust_ctl_field
*lttng_ust_ctl_fields
,
45 size_t *iter_output
, size_t nr_lttng_fields
,
46 const struct lttng_ust_event_field
* const *lttng_fields
);
49 * ustcomm_connect_unix_sock
51 * Connect to unix socket using the path name.
53 * Caller handles FD tracker.
55 int ustcomm_connect_unix_sock(const char *pathname
, long timeout
)
57 struct sockaddr_un sun
;
61 * libust threads require the close-on-exec flag for all
62 * resources so it does not leak file descriptors upon exec.
64 fd
= socket(PF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0);
71 /* Give at least 10ms. */
74 ret
= ustcomm_setsockopt_snd_timeout(fd
, timeout
);
76 WARN("Error setting connect socket send timeout");
80 memset(&sun
, 0, sizeof(sun
));
81 sun
.sun_family
= AF_UNIX
;
82 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
83 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
85 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
88 * Don't print message on connect ENOENT error, because
89 * connect is used in normal execution to detect if
90 * sessiond is alive. ENOENT is when the unix socket
91 * file does not exist, and ECONNREFUSED is when the
92 * file exists but no sessiond is listening.
94 if (errno
!= ECONNREFUSED
&& errno
!= ECONNRESET
95 && errno
!= ENOENT
&& errno
!= EACCES
)
98 if (ret
== -ECONNREFUSED
|| ret
== -ECONNRESET
)
109 closeret
= close(fd
);
118 * ustcomm_accept_unix_sock
120 * Do an accept(2) on the sock and return the
121 * new file descriptor. The socket MUST be bind(2) before.
123 int ustcomm_accept_unix_sock(int sock
)
126 struct sockaddr_un sun
;
130 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
132 if (errno
!= ECONNABORTED
)
135 if (new_fd
== -ECONNABORTED
)
142 * ustcomm_create_unix_sock
144 * Creates a AF_UNIX local socket using pathname
145 * bind the socket upon creation and return the fd.
147 int ustcomm_create_unix_sock(const char *pathname
)
149 struct sockaddr_un sun
;
152 /* Create server socket */
153 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
159 memset(&sun
, 0, sizeof(sun
));
160 sun
.sun_family
= AF_UNIX
;
161 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
162 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
164 /* Unlink the old file if present */
165 (void) unlink(pathname
);
166 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
179 closeret
= close(fd
);
189 * ustcomm_listen_unix_sock
191 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
193 int ustcomm_listen_unix_sock(int sock
)
197 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
207 * ustcomm_close_unix_sock
211 * Handles fd tracker internally.
213 int ustcomm_close_unix_sock(int sock
)
217 lttng_ust_lock_fd_tracker();
220 lttng_ust_delete_fd_from_tracker(sock
);
225 lttng_ust_unlock_fd_tracker();
231 * ustcomm_shutdown_unix_sock
233 * Shutdown unix socket. Keeps the file descriptor open, but shutdown
236 int ustcomm_shutdown_unix_sock(int sock
)
240 ret
= shutdown(sock
, SHUT_RDWR
);
242 PERROR("Socket shutdown error");
249 * ustcomm_recv_unix_sock
251 * Receive data of size len in put that data into
252 * the buf param. Using recvmsg API.
253 * Return the size of received data.
254 * Return 0 on orderly shutdown.
256 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
263 memset(&msg
, 0, sizeof(msg
));
265 iov
[0].iov_base
= buf
;
266 iov
[0].iov_len
= len
;
271 len_last
= iov
[0].iov_len
;
272 ret
= recvmsg(sock
, &msg
, 0);
274 iov
[0].iov_base
+= ret
;
275 iov
[0].iov_len
-= ret
;
276 assert(ret
<= len_last
);
278 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
281 if (errno
!= EPIPE
&& errno
!= ECONNRESET
&& errno
!= ECONNREFUSED
)
284 if (ret
== -ECONNRESET
|| ret
== -ECONNREFUSED
)
287 (void) ustcomm_shutdown_unix_sock(sock
);
288 } else if (ret
> 0) {
291 /* ret = 0 means an orderly shutdown. */
297 * ustcomm_send_unix_sock
299 * Send buf data of size len. Using sendmsg API.
300 * Return the size of sent data.
302 ssize_t
ustcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
308 memset(&msg
, 0, sizeof(msg
));
310 iov
[0].iov_base
= (void *) buf
;
311 iov
[0].iov_len
= len
;
316 * Using the MSG_NOSIGNAL when sending data from sessiond to
317 * libust, so libust does not receive an unhandled SIGPIPE or
318 * SIGURG. The sessiond receiver side can be made more resilient
319 * by ignoring SIGPIPE, but we don't have this luxury on the
323 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
324 } while (ret
< 0 && errno
== EINTR
);
327 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
330 if (ret
== -ECONNRESET
)
333 (void) ustcomm_shutdown_unix_sock(sock
);
340 * Send a message accompanied by fd(s) over a unix socket.
342 * Returns the size of data sent, or negative error value.
344 ssize_t
ustcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
347 struct cmsghdr
*cmptr
;
350 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
351 char tmp
[CMSG_SPACE(sizeof_fds
)];
354 memset(&msg
, 0, sizeof(msg
));
355 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
357 if (nb_fd
> USTCOMM_MAX_SEND_FDS
)
360 msg
.msg_control
= (caddr_t
)tmp
;
361 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
363 cmptr
= CMSG_FIRSTHDR(&msg
);
366 cmptr
->cmsg_level
= SOL_SOCKET
;
367 cmptr
->cmsg_type
= SCM_RIGHTS
;
368 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
369 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
370 /* Sum of the length of all control messages in the buffer: */
371 msg
.msg_controllen
= cmptr
->cmsg_len
;
373 iov
[0].iov_base
= &dummy
;
379 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
380 } while (ret
< 0 && errno
== EINTR
);
383 * We consider EPIPE and ECONNRESET as expected.
385 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
389 if (ret
== -ECONNRESET
)
396 * Recv a message accompanied by fd(s) from a unix socket.
398 * Expect at most "nb_fd" file descriptors. Returns the number of fd
399 * actually received in nb_fd.
400 * Returns -EPIPE on orderly shutdown.
402 ssize_t
ustcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
406 struct cmsghdr
*cmsg
;
407 size_t sizeof_fds
= nb_fd
* sizeof(int);
408 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
412 memset(&msg
, 0, sizeof(msg
));
414 /* Prepare to receive the structures */
415 iov
[0].iov_base
= &dummy
;
419 msg
.msg_control
= recv_fd
;
420 msg
.msg_controllen
= sizeof(recv_fd
);
423 ret
= recvmsg(sock
, &msg
, MSG_CMSG_CLOEXEC
);
424 } while (ret
< 0 && errno
== EINTR
);
426 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
427 PERROR("recvmsg fds");
430 if (ret
== -ECONNRESET
)
435 /* orderly shutdown */
440 ERR("Error: Received %zd bytes, expected %d\n",
444 if (msg
.msg_flags
& MSG_CTRUNC
) {
445 ERR("Error: Control message truncated.\n");
449 cmsg
= CMSG_FIRSTHDR(&msg
);
451 ERR("Error: Invalid control message header\n");
455 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
456 ERR("Didn't received any fd\n");
460 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
461 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
462 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
467 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
474 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
478 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
486 ERR("incorrect message size: %zd\n", len
);
493 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
494 uint32_t expected_handle
, uint32_t expected_cmd
)
498 memset(lur
, 0, sizeof(*lur
));
499 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
501 case 0: /* orderly shutdown */
507 if (lur
->handle
!= expected_handle
) {
508 ERR("Unexpected result message handle: "
509 "expected: %u vs received: %u\n",
510 expected_handle
, lur
->handle
);
513 if (lur
->cmd
!= expected_cmd
) {
514 ERR("Unexpected result message command "
515 "expected: %u vs received: %u\n",
516 expected_cmd
, lur
->cmd
);
522 return lur
->ret_code
;
527 ERR("incorrect message size: %zd\n", len
);
533 int ustcomm_send_app_cmd(int sock
,
534 struct ustcomm_ust_msg
*lum
,
535 struct ustcomm_ust_reply
*lur
)
539 ret
= ustcomm_send_app_msg(sock
, lum
);
542 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
549 * chan_data is allocated internally if this function returns the
552 ssize_t
ustcomm_recv_channel_from_sessiond(int sock
,
553 void **_chan_data
, uint64_t var_len
,
560 if (var_len
> LTTNG_UST_ABI_CHANNEL_DATA_MAX_LEN
) {
564 /* Receive variable length data */
565 chan_data
= zmalloc(var_len
);
570 len
= ustcomm_recv_unix_sock(sock
, chan_data
, var_len
);
571 if (len
!= var_len
) {
575 lttng_ust_lock_fd_tracker();
576 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
578 lttng_ust_unlock_fd_tracker();
588 ret
= lttng_ust_add_fd_to_tracker(wakeup_fd
);
590 ret
= close(wakeup_fd
);
592 PERROR("close on wakeup_fd");
595 lttng_ust_unlock_fd_tracker();
600 lttng_ust_unlock_fd_tracker();
602 *_chan_data
= chan_data
;
612 ssize_t
ustcomm_recv_event_notifier_notif_fd_from_sessiond(int sock
,
613 int *_event_notifier_notif_fd
)
616 int event_notifier_notif_fd
, ret
;
618 /* Receive event_notifier notification fd */
619 lttng_ust_lock_fd_tracker();
620 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &event_notifier_notif_fd
, 1);
622 lttng_ust_unlock_fd_tracker();
632 ret
= lttng_ust_add_fd_to_tracker(event_notifier_notif_fd
);
634 ret
= close(event_notifier_notif_fd
);
636 PERROR("close on event_notifier notif fd");
639 lttng_ust_unlock_fd_tracker();
643 *_event_notifier_notif_fd
= ret
;
644 lttng_ust_unlock_fd_tracker();
652 int ustcomm_recv_stream_from_sessiond(int sock
,
653 uint64_t *memory_map_size
__attribute__((unused
)),
654 int *shm_fd
, int *wakeup_fd
)
660 /* recv shm fd and wakeup fd */
661 lttng_ust_lock_fd_tracker();
662 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
664 lttng_ust_unlock_fd_tracker();
674 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
678 PERROR("close on received shm_fd");
681 lttng_ust_unlock_fd_tracker();
686 ret
= lttng_ust_add_fd_to_tracker(fds
[1]);
688 ret
= close(*shm_fd
);
690 PERROR("close on shm_fd");
695 PERROR("close on received wakeup_fd");
698 lttng_ust_unlock_fd_tracker();
702 lttng_ust_unlock_fd_tracker();
709 ssize_t
ustcomm_recv_counter_from_sessiond(int sock
,
710 void **_counter_data
, uint64_t var_len
)
715 if (var_len
> LTTNG_UST_ABI_COUNTER_DATA_MAX_LEN
) {
719 /* Receive variable length data */
720 counter_data
= zmalloc(var_len
);
725 len
= ustcomm_recv_unix_sock(sock
, counter_data
, var_len
);
726 if (len
!= var_len
) {
729 *_counter_data
= counter_data
;
739 int ustcomm_recv_counter_shm_from_sessiond(int sock
,
747 lttng_ust_lock_fd_tracker();
748 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 1);
750 lttng_ust_unlock_fd_tracker();
760 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
764 PERROR("close on received shm_fd");
767 lttng_ust_unlock_fd_tracker();
771 lttng_ust_unlock_fd_tracker();
779 * Returns 0 on success, negative error value on error.
781 int ustcomm_send_reg_msg(int sock
,
782 enum lttng_ust_ctl_socket_type type
,
783 uint32_t bits_per_long
,
784 uint32_t uint8_t_alignment
,
785 uint32_t uint16_t_alignment
,
786 uint32_t uint32_t_alignment
,
787 uint32_t uint64_t_alignment
,
788 uint32_t long_alignment
,
789 const char *procname
)
792 struct lttng_ust_ctl_reg_msg reg_msg
;
794 reg_msg
.magic
= LTTNG_UST_ABI_COMM_MAGIC
;
795 reg_msg
.major
= LTTNG_UST_ABI_MAJOR_VERSION
;
796 reg_msg
.minor
= LTTNG_UST_ABI_MINOR_VERSION
;
797 reg_msg
.pid
= getpid();
798 reg_msg
.ppid
= getppid();
799 reg_msg
.uid
= getuid();
800 reg_msg
.gid
= getgid();
801 reg_msg
.bits_per_long
= bits_per_long
;
802 reg_msg
.uint8_t_alignment
= uint8_t_alignment
;
803 reg_msg
.uint16_t_alignment
= uint16_t_alignment
;
804 reg_msg
.uint32_t_alignment
= uint32_t_alignment
;
805 reg_msg
.uint64_t_alignment
= uint64_t_alignment
;
806 reg_msg
.long_alignment
= long_alignment
;
807 reg_msg
.socket_type
= type
;
808 memset(reg_msg
.name
, 0, sizeof(reg_msg
.name
));
809 strncpy(reg_msg
.name
, procname
, sizeof(reg_msg
.name
) - 1);
810 memset(reg_msg
.padding
, 0, sizeof(reg_msg
.padding
));
812 len
= ustcomm_send_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
813 if (len
> 0 && len
!= sizeof(reg_msg
))
821 ssize_t
count_one_type(const struct lttng_ust_type_common
*lt
)
824 case lttng_ust_type_integer
:
825 case lttng_ust_type_float
:
826 case lttng_ust_type_string
:
828 case lttng_ust_type_enum
:
829 return count_one_type(lttng_ust_get_type_enum(lt
)->container_type
) + 1;
830 case lttng_ust_type_array
:
831 return count_one_type(lttng_ust_get_type_array(lt
)->elem_type
) + 1;
832 case lttng_ust_type_sequence
:
833 return count_one_type(lttng_ust_get_type_sequence(lt
)->elem_type
) + 1;
834 case lttng_ust_type_struct
:
835 return count_fields_recursive(lttng_ust_get_type_struct(lt
)->nr_fields
,
836 lttng_ust_get_type_struct(lt
)->fields
) + 1;
838 case lttng_ust_type_dynamic
:
840 const struct lttng_ust_event_field
* const *choices
;
844 ret
= lttng_ust_dynamic_type_choices(&nr_choices
,
849 * Two fields for enum, one field for variant, and
850 * one field per choice.
852 return count_fields_recursive(nr_choices
, choices
) + 3;
862 ssize_t
count_fields_recursive(size_t nr_fields
,
863 const struct lttng_ust_event_field
* const *lttng_fields
)
866 ssize_t ret
, count
= 0;
868 for (i
= 0; i
< nr_fields
; i
++) {
869 const struct lttng_ust_event_field
*lf
;
871 lf
= lttng_fields
[i
];
872 /* skip 'nowrite' fields */
875 ret
= count_one_type(lf
->type
);
877 return ret
; /* error */
884 ssize_t
count_ctx_fields_recursive(size_t nr_fields
,
885 struct lttng_ust_ctx_field
*lttng_fields
)
888 ssize_t ret
, count
= 0;
890 for (i
= 0; i
< nr_fields
; i
++) {
891 const struct lttng_ust_event_field
*lf
;
893 lf
= lttng_fields
[i
].event_field
;
894 /* skip 'nowrite' fields */
897 ret
= count_one_type(lf
->type
);
899 return ret
; /* error */
906 int serialize_string_encoding(int32_t *ue
,
907 enum lttng_ust_string_encoding le
)
910 case lttng_ust_string_encoding_none
:
911 *ue
= lttng_ust_ctl_encode_none
;
913 case lttng_ust_string_encoding_UTF8
:
914 *ue
= lttng_ust_ctl_encode_UTF8
;
916 case lttng_ust_string_encoding_ASCII
:
917 *ue
= lttng_ust_ctl_encode_ASCII
;
926 int serialize_integer_type(struct lttng_ust_ctl_integer_type
*uit
,
927 const struct lttng_ust_type_integer
*lit
,
928 enum lttng_ust_string_encoding lencoding
)
932 uit
->size
= lit
->size
;
933 uit
->signedness
= lit
->signedness
;
934 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
935 uit
->base
= lit
->base
;
936 if (serialize_string_encoding(&encoding
, lencoding
))
938 uit
->encoding
= encoding
;
939 uit
->alignment
= lit
->alignment
;
944 int serialize_dynamic_type(struct lttng_ust_session
*session
,
945 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
946 const char *field_name
)
948 const struct lttng_ust_event_field
* const *choices
;
949 char tag_field_name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
950 const struct lttng_ust_type_common
*tag_type
;
951 const struct lttng_ust_event_field
*tag_field_generic
;
952 struct lttng_ust_event_field tag_field
= {
953 .name
= tag_field_name
,
956 struct lttng_ust_ctl_field
*uf
;
957 size_t nr_choices
, i
;
960 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
961 tag_type
= tag_field_generic
->type
;
963 /* Serialize enum field. */
964 strncpy(tag_field_name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
965 tag_field_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
966 strncat(tag_field_name
,
968 LTTNG_UST_ABI_SYM_NAME_LEN
- strlen(tag_field_name
) - 1);
969 tag_field
.type
= tag_type
;
970 ret
= serialize_one_field(session
, fields
, iter_output
,
975 /* Serialize variant field. */
976 uf
= &fields
[*iter_output
];
977 ret
= lttng_ust_dynamic_type_choices(&nr_choices
, &choices
);
981 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
982 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
983 uf
->type
.atype
= lttng_ust_ctl_atype_variant_nestable
;
984 uf
->type
.u
.variant_nestable
.nr_choices
= nr_choices
;
985 strncpy(uf
->type
.u
.variant_nestable
.tag_name
,
987 LTTNG_UST_ABI_SYM_NAME_LEN
);
988 uf
->type
.u
.variant_nestable
.tag_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
989 uf
->type
.u
.variant_nestable
.alignment
= 0;
992 /* Serialize choice fields after variant. */
993 for (i
= 0; i
< nr_choices
; i
++) {
994 ret
= serialize_one_field(session
, fields
,
995 iter_output
, choices
[i
], NULL
);
1003 int serialize_one_type(struct lttng_ust_session
*session
,
1004 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
1005 const char *field_name
, const struct lttng_ust_type_common
*lt
,
1006 enum lttng_ust_string_encoding parent_encoding
,
1007 const char *prev_field_name
)
1012 * Serializing a type (rather than a field) generates a lttng_ust_ctl_field
1013 * entry with 0-length name.
1017 case lttng_ust_type_integer
:
1019 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1020 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1023 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1024 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1028 ret
= serialize_integer_type(&ut
->u
.integer
, lttng_ust_get_type_integer(lt
),
1032 ut
->atype
= lttng_ust_ctl_atype_integer
;
1036 case lttng_ust_type_float
:
1038 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1039 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1040 struct lttng_ust_ctl_float_type
*uft
;
1041 const struct lttng_ust_type_float
*lft
;
1044 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1045 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1049 uft
= &ut
->u
._float
;
1050 lft
= lttng_ust_get_type_float(lt
);
1051 uft
->exp_dig
= lft
->exp_dig
;
1052 uft
->mant_dig
= lft
->mant_dig
;
1053 uft
->alignment
= lft
->alignment
;
1054 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
1055 ut
->atype
= lttng_ust_ctl_atype_float
;
1059 case lttng_ust_type_string
:
1061 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1062 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1066 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1067 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1071 ret
= serialize_string_encoding(&encoding
, lttng_ust_get_type_string(lt
)->encoding
);
1074 ut
->u
.string
.encoding
= encoding
;
1075 ut
->atype
= lttng_ust_ctl_atype_string
;
1079 case lttng_ust_type_array
:
1081 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1082 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1085 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1086 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1090 ut
->atype
= lttng_ust_ctl_atype_array_nestable
;
1091 ut
->u
.array_nestable
.length
= lttng_ust_get_type_array(lt
)->length
;
1092 ut
->u
.array_nestable
.alignment
= lttng_ust_get_type_array(lt
)->alignment
;
1095 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1096 lttng_ust_get_type_array(lt
)->elem_type
,
1097 lttng_ust_get_type_array(lt
)->encoding
, NULL
);
1102 case lttng_ust_type_sequence
:
1104 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1105 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1106 const char *length_name
= lttng_ust_get_type_sequence(lt
)->length_name
;
1109 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1110 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1114 ut
->atype
= lttng_ust_ctl_atype_sequence_nestable
;
1116 * If length_name field is NULL, use the previous field
1120 length_name
= prev_field_name
;
1123 strncpy(ut
->u
.sequence_nestable
.length_name
,
1124 length_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1125 ut
->u
.sequence_nestable
.length_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1126 ut
->u
.sequence_nestable
.alignment
= lttng_ust_get_type_sequence(lt
)->alignment
;
1129 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1130 lttng_ust_get_type_sequence(lt
)->elem_type
,
1131 lttng_ust_get_type_sequence(lt
)->encoding
, NULL
);
1136 case lttng_ust_type_dynamic
:
1138 ret
= serialize_dynamic_type(session
, fields
, iter_output
,
1144 case lttng_ust_type_struct
:
1146 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1149 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1150 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1154 uf
->type
.atype
= lttng_ust_ctl_atype_struct_nestable
;
1155 uf
->type
.u
.struct_nestable
.nr_fields
= lttng_ust_get_type_struct(lt
)->nr_fields
;
1156 uf
->type
.u
.struct_nestable
.alignment
= lttng_ust_get_type_struct(lt
)->alignment
;
1159 ret
= serialize_fields(session
, fields
, iter_output
,
1160 lttng_ust_get_type_struct(lt
)->nr_fields
,
1161 lttng_ust_get_type_struct(lt
)->fields
);
1166 case lttng_ust_type_enum
:
1168 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1169 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1172 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1173 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1177 strncpy(ut
->u
.enum_nestable
.name
, lttng_ust_get_type_enum(lt
)->desc
->name
,
1178 LTTNG_UST_ABI_SYM_NAME_LEN
);
1179 ut
->u
.enum_nestable
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1180 ut
->atype
= lttng_ust_ctl_atype_enum_nestable
;
1183 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1184 lttng_ust_get_type_enum(lt
)->container_type
,
1185 lttng_ust_string_encoding_none
, NULL
);
1189 const struct lttng_enum
*_enum
;
1191 _enum
= lttng_ust_enum_get_from_desc(session
, lttng_ust_get_type_enum(lt
)->desc
);
1194 ut
->u
.enum_nestable
.id
= _enum
->id
;
1196 ut
->u
.enum_nestable
.id
= -1ULL;
1207 int serialize_one_field(struct lttng_ust_session
*session
,
1208 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
1209 const struct lttng_ust_event_field
*lf
,
1210 const char **prev_field_name_p
)
1212 const char *prev_field_name
= NULL
;
1215 /* skip 'nowrite' fields */
1219 if (prev_field_name_p
)
1220 prev_field_name
= *prev_field_name_p
;
1221 ret
= serialize_one_type(session
, fields
, iter_output
, lf
->name
, lf
->type
,
1222 lttng_ust_string_encoding_none
, prev_field_name
);
1223 if (prev_field_name_p
)
1224 *prev_field_name_p
= lf
->name
;
1229 int serialize_fields(struct lttng_ust_session
*session
,
1230 struct lttng_ust_ctl_field
*lttng_ust_ctl_fields
,
1231 size_t *iter_output
, size_t nr_lttng_fields
,
1232 const struct lttng_ust_event_field
* const *lttng_fields
)
1234 const char *prev_field_name
= NULL
;
1238 for (i
= 0; i
< nr_lttng_fields
; i
++) {
1239 ret
= serialize_one_field(session
, lttng_ust_ctl_fields
,
1240 iter_output
, lttng_fields
[i
],
1249 int alloc_serialize_fields(struct lttng_ust_session
*session
,
1250 size_t *_nr_write_fields
,
1251 struct lttng_ust_ctl_field
**lttng_ust_ctl_fields
,
1253 const struct lttng_ust_event_field
* const *lttng_fields
)
1255 struct lttng_ust_ctl_field
*fields
;
1257 size_t iter_output
= 0;
1258 ssize_t nr_write_fields
;
1260 nr_write_fields
= count_fields_recursive(nr_fields
, lttng_fields
);
1261 if (nr_write_fields
< 0) {
1262 return (int) nr_write_fields
;
1265 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1269 ret
= serialize_fields(session
, fields
, &iter_output
, nr_fields
,
1274 *_nr_write_fields
= nr_write_fields
;
1275 *lttng_ust_ctl_fields
= fields
;
1284 int serialize_entries(struct lttng_ust_ctl_enum_entry
**_entries
,
1286 const struct lttng_ust_enum_entry
* const *lttng_entries
)
1288 struct lttng_ust_ctl_enum_entry
*entries
;
1291 /* Serialize the entries */
1292 entries
= zmalloc(nr_entries
* sizeof(*entries
));
1295 for (i
= 0; i
< nr_entries
; i
++) {
1296 struct lttng_ust_ctl_enum_entry
*uentry
;
1297 const struct lttng_ust_enum_entry
*lentry
;
1299 uentry
= &entries
[i
];
1300 lentry
= lttng_entries
[i
];
1302 uentry
->start
.value
= lentry
->start
.value
;
1303 uentry
->start
.signedness
= lentry
->start
.signedness
;
1304 uentry
->end
.value
= lentry
->end
.value
;
1305 uentry
->end
.signedness
= lentry
->end
.signedness
;
1306 strncpy(uentry
->string
, lentry
->string
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1307 uentry
->string
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1309 if (lentry
->options
& LTTNG_UST_ENUM_ENTRY_OPTION_IS_AUTO
) {
1310 uentry
->u
.extra
.options
|=
1311 LTTNG_UST_CTL_UST_ENUM_ENTRY_OPTION_IS_AUTO
;
1314 *_entries
= entries
;
1319 int serialize_ctx_fields(struct lttng_ust_session
*session
,
1320 size_t *_nr_write_fields
,
1321 struct lttng_ust_ctl_field
**lttng_ust_ctl_fields
,
1323 struct lttng_ust_ctx_field
*lttng_fields
)
1325 struct lttng_ust_ctl_field
*fields
;
1326 const char *prev_field_name
= NULL
;
1327 size_t i
, iter_output
= 0;
1328 ssize_t nr_write_fields
;
1331 nr_write_fields
= count_ctx_fields_recursive(nr_fields
,
1333 if (nr_write_fields
< 0) {
1334 return (int) nr_write_fields
;
1337 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1341 for (i
= 0; i
< nr_fields
; i
++) {
1342 ret
= serialize_one_field(session
, fields
, &iter_output
,
1343 lttng_fields
[i
].event_field
, &prev_field_name
);
1348 *_nr_write_fields
= nr_write_fields
;
1349 *lttng_ust_ctl_fields
= fields
;
1358 * Returns 0 on success, negative error value on error.
1360 int ustcomm_register_event(int sock
,
1361 struct lttng_ust_session
*session
,
1362 int session_objd
, /* session descriptor */
1363 int channel_objd
, /* channel descriptor */
1364 const char *event_name
, /* event name (input) */
1366 const char *signature
, /* event signature (input) */
1367 size_t nr_fields
, /* fields */
1368 const struct lttng_ust_event_field
* const *lttng_fields
,
1369 const char *model_emf_uri
,
1370 uint32_t *id
) /* event id (output) */
1374 struct ustcomm_notify_hdr header
;
1375 struct ustcomm_notify_event_msg m
;
1378 struct ustcomm_notify_hdr header
;
1379 struct ustcomm_notify_event_reply r
;
1381 size_t signature_len
, fields_len
, model_emf_uri_len
;
1382 struct lttng_ust_ctl_field
*fields
= NULL
;
1383 size_t nr_write_fields
= 0;
1386 memset(&msg
, 0, sizeof(msg
));
1387 msg
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_EVENT
;
1388 msg
.m
.session_objd
= session_objd
;
1389 msg
.m
.channel_objd
= channel_objd
;
1390 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1391 msg
.m
.event_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1392 msg
.m
.loglevel
= loglevel
;
1393 signature_len
= strlen(signature
) + 1;
1394 msg
.m
.signature_len
= signature_len
;
1396 /* Calculate fields len, serialize fields. */
1397 if (nr_fields
> 0) {
1398 ret
= alloc_serialize_fields(session
, &nr_write_fields
, &fields
,
1399 nr_fields
, lttng_fields
);
1404 fields_len
= sizeof(*fields
) * nr_write_fields
;
1405 msg
.m
.fields_len
= fields_len
;
1406 if (model_emf_uri
) {
1407 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
1409 model_emf_uri_len
= 0;
1411 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
1413 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1414 if (len
> 0 && len
!= sizeof(msg
)) {
1423 /* send signature */
1424 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
1425 if (len
> 0 && len
!= signature_len
) {
1435 if (fields_len
> 0) {
1436 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1437 if (len
> 0 && len
!= fields_len
) {
1448 if (model_emf_uri_len
) {
1449 /* send model_emf_uri */
1450 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
1452 if (len
> 0 && len
!= model_emf_uri_len
) {
1461 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1463 case 0: /* orderly shutdown */
1466 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1467 ERR("Unexpected result message command "
1468 "expected: %u vs received: %u\n",
1469 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1472 if (reply
.r
.ret_code
> 0)
1474 if (reply
.r
.ret_code
< 0)
1475 return reply
.r
.ret_code
;
1476 *id
= reply
.r
.event_id
;
1477 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1478 event_name
, reply
.r
.ret_code
, reply
.r
.event_id
);
1482 /* Transport level error */
1483 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1487 ERR("incorrect message size: %zd\n", len
);
1493 /* Error path only. */
1500 * Returns 0 on success, negative error value on error.
1501 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1503 int ustcomm_register_enum(int sock
,
1504 int session_objd
, /* session descriptor */
1505 const char *enum_name
, /* enum name (input) */
1506 size_t nr_entries
, /* entries */
1507 const struct lttng_ust_enum_entry
* const *lttng_entries
,
1512 struct ustcomm_notify_hdr header
;
1513 struct ustcomm_notify_enum_msg m
;
1516 struct ustcomm_notify_hdr header
;
1517 struct ustcomm_notify_enum_reply r
;
1520 struct lttng_ust_ctl_enum_entry
*entries
= NULL
;
1523 memset(&msg
, 0, sizeof(msg
));
1524 msg
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_ENUM
;
1525 msg
.m
.session_objd
= session_objd
;
1526 strncpy(msg
.m
.enum_name
, enum_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1527 msg
.m
.enum_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1529 /* Calculate entries len, serialize entries. */
1530 if (nr_entries
> 0) {
1531 ret
= serialize_entries(&entries
,
1532 nr_entries
, lttng_entries
);
1537 entries_len
= sizeof(*entries
) * nr_entries
;
1538 msg
.m
.entries_len
= entries_len
;
1540 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1541 if (len
> 0 && len
!= sizeof(msg
)) {
1551 if (entries_len
> 0) {
1552 len
= ustcomm_send_unix_sock(sock
, entries
, entries_len
);
1553 if (len
> 0 && len
!= entries_len
) {
1566 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1568 case 0: /* orderly shutdown */
1571 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1572 ERR("Unexpected result message command "
1573 "expected: %u vs received: %u\n",
1574 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1577 if (reply
.r
.ret_code
> 0)
1579 if (reply
.r
.ret_code
< 0)
1580 return reply
.r
.ret_code
;
1581 *id
= reply
.r
.enum_id
;
1582 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1583 enum_name
, reply
.r
.ret_code
);
1587 /* Transport level error */
1588 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1592 ERR("incorrect message size: %zd\n", len
);
1604 * Returns 0 on success, negative error value on error.
1605 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1607 int ustcomm_register_channel(int sock
,
1608 struct lttng_ust_session
*session
,
1609 int session_objd
, /* session descriptor */
1610 int channel_objd
, /* channel descriptor */
1611 size_t nr_ctx_fields
,
1612 struct lttng_ust_ctx_field
*ctx_fields
,
1613 uint32_t *chan_id
, /* channel id (output) */
1614 int *header_type
) /* header type (output) */
1618 struct ustcomm_notify_hdr header
;
1619 struct ustcomm_notify_channel_msg m
;
1622 struct ustcomm_notify_hdr header
;
1623 struct ustcomm_notify_channel_reply r
;
1626 struct lttng_ust_ctl_field
*fields
= NULL
;
1628 size_t nr_write_fields
= 0;
1630 memset(&msg
, 0, sizeof(msg
));
1631 msg
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
;
1632 msg
.m
.session_objd
= session_objd
;
1633 msg
.m
.channel_objd
= channel_objd
;
1635 /* Calculate fields len, serialize fields. */
1636 if (nr_ctx_fields
> 0) {
1637 ret
= serialize_ctx_fields(session
, &nr_write_fields
, &fields
,
1638 nr_ctx_fields
, ctx_fields
);
1643 fields_len
= sizeof(*fields
) * nr_write_fields
;
1644 msg
.m
.ctx_fields_len
= fields_len
;
1645 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1646 if (len
> 0 && len
!= sizeof(msg
)) {
1656 if (fields_len
> 0) {
1657 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1659 if (len
> 0 && len
!= fields_len
) {
1669 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1671 case 0: /* orderly shutdown */
1674 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1675 ERR("Unexpected result message command "
1676 "expected: %u vs received: %u\n",
1677 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1680 if (reply
.r
.ret_code
> 0)
1682 if (reply
.r
.ret_code
< 0)
1683 return reply
.r
.ret_code
;
1684 *chan_id
= reply
.r
.chan_id
;
1685 switch (reply
.r
.header_type
) {
1688 *header_type
= reply
.r
.header_type
;
1691 ERR("Unexpected channel header type %u\n",
1692 reply
.r
.header_type
);
1695 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1696 reply
.r
.chan_id
, reply
.r
.header_type
);
1700 /* Transport level error */
1701 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1705 ERR("incorrect message size: %zd\n", len
);
1712 * Set socket receiving timeout.
1714 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1719 tv
.tv_sec
= msec
/ 1000;
1720 tv
.tv_usec
= (msec
* 1000 % 1000000);
1722 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1724 PERROR("setsockopt SO_RCVTIMEO");
1732 * Set socket sending timeout.
1734 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1739 tv
.tv_sec
= msec
/ 1000;
1740 tv
.tv_usec
= (msec
* 1000) % 1000000;
1742 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1744 PERROR("setsockopt SO_SNDTIMEO");