2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
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>
25 #include <ust-helper.h>
26 #include <lttng/ust-error.h>
27 #include <ust-dynamic-type.h>
28 #include <usterr-signal-safe.h>
30 #include "../liblttng-ust/ust-events-internal.h"
31 #include "../liblttng-ust/compat.h"
33 #define USTCOMM_CODE_OFFSET(code) \
34 (code == LTTNG_UST_OK ? 0 : (code - LTTNG_UST_ERR + 1))
36 #define USTCOMM_MAX_SEND_FDS 4
39 ssize_t
count_fields_recursive(size_t nr_fields
,
40 struct lttng_ust_event_field
**lttng_fields
);
42 int serialize_one_field(struct lttng_ust_session
*session
,
43 struct ustctl_field
*fields
, size_t *iter_output
,
44 struct lttng_ust_event_field
*lf
);
46 int serialize_fields(struct lttng_ust_session
*session
,
47 struct ustctl_field
*ustctl_fields
,
48 size_t *iter_output
, size_t nr_lttng_fields
,
49 struct lttng_ust_event_field
**lttng_fields
);
52 * Human readable error message.
54 static const char *ustcomm_readable_code
[] = {
55 [ USTCOMM_CODE_OFFSET(LTTNG_UST_OK
) ] = "Success",
56 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR
) ] = "Unknown error",
57 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOENT
) ] = "No entry",
58 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXIST
) ] = "Object already exists",
59 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL
) ] = "Invalid argument",
60 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PERM
) ] = "Permission denied",
61 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOSYS
) ] = "Not implemented",
62 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXITING
) ] = "Process is exiting",
64 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_MAGIC
) ] = "Invalid magic number",
65 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_SOCKET_TYPE
) ] = "Invalid socket type",
66 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_UNSUP_MAJOR
) ] = "Unsupported major version",
67 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PEERCRED
) ] = "Cannot get unix socket peer credentials",
68 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PEERCRED_PID
) ] = "Peer credentials PID is invalid. Socket appears to belong to a distinct, non-nested pid namespace.",
73 * @code: must be a negative value of enum lttng_ust_error_code (or 0).
75 * Returns a ptr to a string representing a human readable error code from the
76 * ustcomm_return_code enum.
78 const char *lttng_ust_strerror(int code
)
82 if (code
< LTTNG_UST_OK
|| code
>= LTTNG_UST_ERR_NR
)
85 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
89 * ustcomm_connect_unix_sock
91 * Connect to unix socket using the path name.
93 * Caller handles FD tracker.
95 int ustcomm_connect_unix_sock(const char *pathname
, long timeout
)
97 struct sockaddr_un sun
;
101 * libust threads require the close-on-exec flag for all
102 * resources so it does not leak file descriptors upon exec.
103 * SOCK_CLOEXEC is not used since it is linux specific.
105 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
112 /* Give at least 10ms. */
115 ret
= ustcomm_setsockopt_snd_timeout(fd
, timeout
);
117 WARN("Error setting connect socket send timeout");
120 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
127 memset(&sun
, 0, sizeof(sun
));
128 sun
.sun_family
= AF_UNIX
;
129 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
130 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
132 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
135 * Don't print message on connect ENOENT error, because
136 * connect is used in normal execution to detect if
137 * sessiond is alive. ENOENT is when the unix socket
138 * file does not exist, and ECONNREFUSED is when the
139 * file exists but no sessiond is listening.
141 if (errno
!= ECONNREFUSED
&& errno
!= ECONNRESET
142 && errno
!= ENOENT
&& errno
!= EACCES
)
145 if (ret
== -ECONNREFUSED
|| ret
== -ECONNRESET
)
157 closeret
= close(fd
);
166 * ustcomm_accept_unix_sock
168 * Do an accept(2) on the sock and return the
169 * new file descriptor. The socket MUST be bind(2) before.
171 int ustcomm_accept_unix_sock(int sock
)
174 struct sockaddr_un sun
;
178 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
180 if (errno
!= ECONNABORTED
)
183 if (new_fd
== -ECONNABORTED
)
190 * ustcomm_create_unix_sock
192 * Creates a AF_UNIX local socket using pathname
193 * bind the socket upon creation and return the fd.
195 int ustcomm_create_unix_sock(const char *pathname
)
197 struct sockaddr_un sun
;
200 /* Create server socket */
201 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
207 memset(&sun
, 0, sizeof(sun
));
208 sun
.sun_family
= AF_UNIX
;
209 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
210 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
212 /* Unlink the old file if present */
213 (void) unlink(pathname
);
214 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
227 closeret
= close(fd
);
237 * ustcomm_listen_unix_sock
239 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
241 int ustcomm_listen_unix_sock(int sock
)
245 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
255 * ustcomm_close_unix_sock
257 * Shutdown cleanly a unix socket.
259 * Handles fd tracker internally.
261 int ustcomm_close_unix_sock(int sock
)
265 lttng_ust_lock_fd_tracker();
268 lttng_ust_delete_fd_from_tracker(sock
);
273 lttng_ust_unlock_fd_tracker();
279 * ustcomm_recv_unix_sock
281 * Receive data of size len in put that data into
282 * the buf param. Using recvmsg API.
283 * Return the size of received data.
284 * Return 0 on orderly shutdown.
286 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
293 memset(&msg
, 0, sizeof(msg
));
295 iov
[0].iov_base
= buf
;
296 iov
[0].iov_len
= len
;
301 len_last
= iov
[0].iov_len
;
302 ret
= recvmsg(sock
, &msg
, 0);
304 iov
[0].iov_base
+= ret
;
305 iov
[0].iov_len
-= ret
;
306 assert(ret
<= len_last
);
308 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
313 if (errno
!= EPIPE
&& errno
!= ECONNRESET
&& errno
!= ECONNREFUSED
)
316 if (ret
== -ECONNRESET
|| ret
== -ECONNREFUSED
)
319 shutret
= shutdown(sock
, SHUT_RDWR
);
321 ERR("Socket shutdown error");
322 } else if (ret
> 0) {
325 /* ret = 0 means an orderly shutdown. */
331 * ustcomm_send_unix_sock
333 * Send buf data of size len. Using sendmsg API.
334 * Return the size of sent data.
336 ssize_t
ustcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
342 memset(&msg
, 0, sizeof(msg
));
344 iov
[0].iov_base
= (void *) buf
;
345 iov
[0].iov_len
= len
;
350 * Using the MSG_NOSIGNAL when sending data from sessiond to
351 * libust, so libust does not receive an unhandled SIGPIPE or
352 * SIGURG. The sessiond receiver side can be made more resilient
353 * by ignoring SIGPIPE, but we don't have this luxury on the
357 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
358 } while (ret
< 0 && errno
== EINTR
);
363 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
366 if (ret
== -ECONNRESET
)
369 shutret
= shutdown(sock
, SHUT_RDWR
);
371 ERR("Socket shutdown error");
378 * Send a message accompanied by fd(s) over a unix socket.
380 * Returns the size of data sent, or negative error value.
382 ssize_t
ustcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
385 struct cmsghdr
*cmptr
;
388 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
389 char tmp
[CMSG_SPACE(sizeof_fds
)];
392 memset(&msg
, 0, sizeof(msg
));
393 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
395 if (nb_fd
> USTCOMM_MAX_SEND_FDS
)
398 msg
.msg_control
= (caddr_t
)tmp
;
399 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
401 cmptr
= CMSG_FIRSTHDR(&msg
);
404 cmptr
->cmsg_level
= SOL_SOCKET
;
405 cmptr
->cmsg_type
= SCM_RIGHTS
;
406 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
407 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
408 /* Sum of the length of all control messages in the buffer: */
409 msg
.msg_controllen
= cmptr
->cmsg_len
;
411 iov
[0].iov_base
= &dummy
;
417 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
418 } while (ret
< 0 && errno
== EINTR
);
421 * We consider EPIPE and ECONNRESET as expected.
423 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
427 if (ret
== -ECONNRESET
)
434 * Recv a message accompanied by fd(s) from a unix socket.
436 * Expect at most "nb_fd" file descriptors. Returns the number of fd
437 * actually received in nb_fd.
438 * Returns -EPIPE on orderly shutdown.
440 ssize_t
ustcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
444 struct cmsghdr
*cmsg
;
445 size_t sizeof_fds
= nb_fd
* sizeof(int);
446 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
451 memset(&msg
, 0, sizeof(msg
));
453 /* Prepare to receive the structures */
454 iov
[0].iov_base
= &dummy
;
458 msg
.msg_control
= recv_fd
;
459 msg
.msg_controllen
= sizeof(recv_fd
);
462 ret
= recvmsg(sock
, &msg
, 0);
463 } while (ret
< 0 && errno
== EINTR
);
465 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
466 PERROR("recvmsg fds");
469 if (ret
== -ECONNRESET
)
474 /* orderly shutdown */
479 ERR("Error: Received %zd bytes, expected %d\n",
483 if (msg
.msg_flags
& MSG_CTRUNC
) {
484 ERR("Error: Control message truncated.\n");
488 cmsg
= CMSG_FIRSTHDR(&msg
);
490 ERR("Error: Invalid control message header\n");
494 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
495 ERR("Didn't received any fd\n");
499 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
500 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
501 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
506 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
509 for (i
= 0; i
< nb_fd
; i
++) {
510 ret
= fcntl(fds
[i
], F_SETFD
, FD_CLOEXEC
);
512 PERROR("fcntl failed to set FD_CLOEXEC on fd %d",
522 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
526 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
534 ERR("incorrect message size: %zd\n", len
);
541 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
542 uint32_t expected_handle
, uint32_t expected_cmd
)
546 memset(lur
, 0, sizeof(*lur
));
547 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
549 case 0: /* orderly shutdown */
555 if (lur
->handle
!= expected_handle
) {
556 ERR("Unexpected result message handle: "
557 "expected: %u vs received: %u\n",
558 expected_handle
, lur
->handle
);
561 if (lur
->cmd
!= expected_cmd
) {
562 ERR("Unexpected result message command "
563 "expected: %u vs received: %u\n",
564 expected_cmd
, lur
->cmd
);
570 return lur
->ret_code
;
575 ERR("incorrect message size: %zd\n", len
);
581 int ustcomm_send_app_cmd(int sock
,
582 struct ustcomm_ust_msg
*lum
,
583 struct ustcomm_ust_reply
*lur
)
587 ret
= ustcomm_send_app_msg(sock
, lum
);
590 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
597 * chan_data is allocated internally if this function returns the
600 ssize_t
ustcomm_recv_channel_from_sessiond(int sock
,
601 void **_chan_data
, uint64_t var_len
,
608 if (var_len
> LTTNG_UST_ABI_CHANNEL_DATA_MAX_LEN
) {
612 /* Receive variable length data */
613 chan_data
= zmalloc(var_len
);
618 len
= ustcomm_recv_unix_sock(sock
, chan_data
, var_len
);
619 if (len
!= var_len
) {
623 lttng_ust_lock_fd_tracker();
624 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
626 lttng_ust_unlock_fd_tracker();
636 ret
= lttng_ust_add_fd_to_tracker(wakeup_fd
);
638 ret
= close(wakeup_fd
);
640 PERROR("close on wakeup_fd");
643 lttng_ust_unlock_fd_tracker();
648 lttng_ust_unlock_fd_tracker();
650 *_chan_data
= chan_data
;
660 ssize_t
ustcomm_recv_event_notifier_notif_fd_from_sessiond(int sock
,
661 int *_event_notifier_notif_fd
)
664 int event_notifier_notif_fd
, ret
;
666 /* Receive event_notifier notification fd */
667 lttng_ust_lock_fd_tracker();
668 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &event_notifier_notif_fd
, 1);
670 lttng_ust_unlock_fd_tracker();
680 ret
= lttng_ust_add_fd_to_tracker(event_notifier_notif_fd
);
682 ret
= close(event_notifier_notif_fd
);
684 PERROR("close on event_notifier notif fd");
687 lttng_ust_unlock_fd_tracker();
691 *_event_notifier_notif_fd
= ret
;
692 lttng_ust_unlock_fd_tracker();
700 int ustcomm_recv_stream_from_sessiond(int sock
,
701 uint64_t *memory_map_size
,
702 int *shm_fd
, int *wakeup_fd
)
708 /* recv shm fd and wakeup fd */
709 lttng_ust_lock_fd_tracker();
710 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
712 lttng_ust_unlock_fd_tracker();
722 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
726 PERROR("close on received shm_fd");
729 lttng_ust_unlock_fd_tracker();
734 ret
= lttng_ust_add_fd_to_tracker(fds
[1]);
736 ret
= close(*shm_fd
);
738 PERROR("close on shm_fd");
743 PERROR("close on received wakeup_fd");
746 lttng_ust_unlock_fd_tracker();
750 lttng_ust_unlock_fd_tracker();
757 ssize_t
ustcomm_recv_counter_from_sessiond(int sock
,
758 void **_counter_data
, uint64_t var_len
)
763 if (var_len
> LTTNG_UST_ABI_COUNTER_DATA_MAX_LEN
) {
767 /* Receive variable length data */
768 counter_data
= zmalloc(var_len
);
773 len
= ustcomm_recv_unix_sock(sock
, counter_data
, var_len
);
774 if (len
!= var_len
) {
777 *_counter_data
= counter_data
;
787 int ustcomm_recv_counter_shm_from_sessiond(int sock
,
795 lttng_ust_lock_fd_tracker();
796 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 1);
798 lttng_ust_unlock_fd_tracker();
808 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
812 PERROR("close on received shm_fd");
815 lttng_ust_unlock_fd_tracker();
819 lttng_ust_unlock_fd_tracker();
827 * Returns 0 on success, negative error value on error.
829 int ustcomm_send_reg_msg(int sock
,
830 enum ustctl_socket_type type
,
831 uint32_t bits_per_long
,
832 uint32_t uint8_t_alignment
,
833 uint32_t uint16_t_alignment
,
834 uint32_t uint32_t_alignment
,
835 uint32_t uint64_t_alignment
,
836 uint32_t long_alignment
)
839 struct ustctl_reg_msg reg_msg
;
841 reg_msg
.magic
= LTTNG_UST_ABI_COMM_MAGIC
;
842 reg_msg
.major
= LTTNG_UST_ABI_MAJOR_VERSION
;
843 reg_msg
.minor
= LTTNG_UST_ABI_MINOR_VERSION
;
844 reg_msg
.pid
= getpid();
845 reg_msg
.ppid
= getppid();
846 reg_msg
.uid
= getuid();
847 reg_msg
.gid
= getgid();
848 reg_msg
.bits_per_long
= bits_per_long
;
849 reg_msg
.uint8_t_alignment
= uint8_t_alignment
;
850 reg_msg
.uint16_t_alignment
= uint16_t_alignment
;
851 reg_msg
.uint32_t_alignment
= uint32_t_alignment
;
852 reg_msg
.uint64_t_alignment
= uint64_t_alignment
;
853 reg_msg
.long_alignment
= long_alignment
;
854 reg_msg
.socket_type
= type
;
855 lttng_pthread_getname_np(reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
856 memset(reg_msg
.padding
, 0, sizeof(reg_msg
.padding
));
858 len
= ustcomm_send_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
859 if (len
> 0 && len
!= sizeof(reg_msg
))
867 ssize_t
count_one_type(struct lttng_ust_type_common
*lt
)
870 case lttng_ust_type_integer
:
871 case lttng_ust_type_float
:
872 case lttng_ust_type_string
:
874 case lttng_ust_type_enum
:
875 return count_one_type(lttng_ust_get_type_enum(lt
)->container_type
) + 1;
876 case lttng_ust_type_array
:
877 return count_one_type(lttng_ust_get_type_array(lt
)->elem_type
) + 1;
878 case lttng_ust_type_sequence
:
879 return count_one_type(lttng_ust_get_type_sequence(lt
)->elem_type
) + 1;
880 case lttng_ust_type_struct
:
881 return count_fields_recursive(lttng_ust_get_type_struct(lt
)->nr_fields
,
882 lttng_ust_get_type_struct(lt
)->fields
) + 1;
884 case lttng_ust_type_dynamic
:
886 struct lttng_ust_event_field
**choices
;
890 ret
= lttng_ust_dynamic_type_choices(&nr_choices
,
895 * Two fields for enum, one field for variant, and
896 * one field per choice.
898 return count_fields_recursive(nr_choices
, choices
) + 3;
908 ssize_t
count_fields_recursive(size_t nr_fields
,
909 struct lttng_ust_event_field
**lttng_fields
)
912 ssize_t ret
, count
= 0;
914 for (i
= 0; i
< nr_fields
; i
++) {
915 const struct lttng_ust_event_field
*lf
;
917 lf
= lttng_fields
[i
];
918 /* skip 'nowrite' fields */
921 ret
= count_one_type(lf
->type
);
923 return ret
; /* error */
930 ssize_t
count_ctx_fields_recursive(size_t nr_fields
,
931 struct lttng_ust_ctx_field
**lttng_fields
)
934 ssize_t ret
, count
= 0;
936 for (i
= 0; i
< nr_fields
; i
++) {
937 const struct lttng_ust_event_field
*lf
;
939 lf
= lttng_fields
[i
]->event_field
;
940 /* skip 'nowrite' fields */
943 ret
= count_one_type(lf
->type
);
945 return ret
; /* error */
952 int serialize_string_encoding(int32_t *ue
,
953 enum lttng_ust_string_encoding le
)
956 case lttng_ust_string_encoding_none
:
957 *ue
= ustctl_encode_none
;
959 case lttng_ust_string_encoding_UTF8
:
960 *ue
= ustctl_encode_UTF8
;
962 case lttng_ust_string_encoding_ASCII
:
963 *ue
= ustctl_encode_ASCII
;
972 int serialize_integer_type(struct ustctl_integer_type
*uit
,
973 const struct lttng_ust_type_integer
*lit
,
974 enum lttng_ust_string_encoding lencoding
)
978 uit
->size
= lit
->size
;
979 uit
->signedness
= lit
->signedness
;
980 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
981 uit
->base
= lit
->base
;
982 if (serialize_string_encoding(&encoding
, lencoding
))
984 uit
->encoding
= encoding
;
985 uit
->alignment
= lit
->alignment
;
990 int serialize_dynamic_type(struct lttng_ust_session
*session
,
991 struct ustctl_field
*fields
, size_t *iter_output
,
992 const char *field_name
)
994 struct lttng_ust_event_field
**choices
;
995 char tag_field_name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
996 struct lttng_ust_type_common
*tag_type
;
997 struct lttng_ust_event_field
*tag_field_generic
;
998 struct lttng_ust_event_field tag_field
= {
999 .name
= tag_field_name
,
1002 struct ustctl_field
*uf
;
1003 size_t nr_choices
, i
;
1006 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
1007 tag_type
= tag_field_generic
->type
;
1009 /* Serialize enum field. */
1010 strncpy(tag_field_name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1011 tag_field_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1012 strncat(tag_field_name
,
1014 LTTNG_UST_ABI_SYM_NAME_LEN
- strlen(tag_field_name
) - 1);
1015 tag_field
.type
= tag_type
;
1016 ret
= serialize_one_field(session
, fields
, iter_output
,
1021 /* Serialize variant field. */
1022 uf
= &fields
[*iter_output
];
1023 ret
= lttng_ust_dynamic_type_choices(&nr_choices
, &choices
);
1027 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1028 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1029 uf
->type
.atype
= ustctl_atype_variant
;
1030 uf
->type
.u
.variant_nestable
.nr_choices
= nr_choices
;
1031 strncpy(uf
->type
.u
.variant_nestable
.tag_name
,
1033 LTTNG_UST_ABI_SYM_NAME_LEN
);
1034 uf
->type
.u
.variant_nestable
.tag_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1035 uf
->type
.u
.variant_nestable
.alignment
= 0;
1038 /* Serialize choice fields after variant. */
1039 for (i
= 0; i
< nr_choices
; i
++) {
1040 ret
= serialize_one_field(session
, fields
,
1041 iter_output
, choices
[i
]);
1049 int serialize_one_type(struct lttng_ust_session
*session
,
1050 struct ustctl_field
*fields
, size_t *iter_output
,
1051 const char *field_name
, struct lttng_ust_type_common
*lt
,
1052 enum lttng_ust_string_encoding parent_encoding
)
1057 * Serializing a type (rather than a field) generates a ustctl_field
1058 * entry with 0-length name.
1062 case lttng_ust_type_integer
:
1064 struct ustctl_field
*uf
= &fields
[*iter_output
];
1065 struct ustctl_type
*ut
= &uf
->type
;
1068 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1069 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1073 ret
= serialize_integer_type(&ut
->u
.integer
, lttng_ust_get_type_integer(lt
),
1077 ut
->atype
= ustctl_atype_integer
;
1081 case lttng_ust_type_float
:
1083 struct ustctl_field
*uf
= &fields
[*iter_output
];
1084 struct ustctl_type
*ut
= &uf
->type
;
1085 struct ustctl_float_type
*uft
;
1086 struct lttng_ust_type_float
*lft
;
1089 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1090 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1094 uft
= &ut
->u
._float
;
1095 lft
= lttng_ust_get_type_float(lt
);
1096 uft
->exp_dig
= lft
->exp_dig
;
1097 uft
->mant_dig
= lft
->mant_dig
;
1098 uft
->alignment
= lft
->alignment
;
1099 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
1100 ut
->atype
= ustctl_atype_float
;
1104 case lttng_ust_type_string
:
1106 struct ustctl_field
*uf
= &fields
[*iter_output
];
1107 struct ustctl_type
*ut
= &uf
->type
;
1111 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1112 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1116 ret
= serialize_string_encoding(&encoding
, lttng_ust_get_type_string(lt
)->encoding
);
1119 ut
->u
.string
.encoding
= encoding
;
1120 ut
->atype
= ustctl_atype_string
;
1124 case lttng_ust_type_array
:
1126 struct ustctl_field
*uf
= &fields
[*iter_output
];
1127 struct ustctl_type
*ut
= &uf
->type
;
1130 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1131 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1135 ut
->atype
= ustctl_atype_array_nestable
;
1136 ut
->u
.array_nestable
.length
= lttng_ust_get_type_array(lt
)->length
;
1137 ut
->u
.array_nestable
.alignment
= lttng_ust_get_type_array(lt
)->alignment
;
1140 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1141 lttng_ust_get_type_array(lt
)->elem_type
,
1142 lttng_ust_get_type_array(lt
)->encoding
);
1147 case lttng_ust_type_sequence
:
1149 struct ustctl_field
*uf
= &fields
[*iter_output
];
1150 struct ustctl_type
*ut
= &uf
->type
;
1153 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1154 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1158 ut
->atype
= ustctl_atype_sequence_nestable
;
1159 strncpy(ut
->u
.sequence_nestable
.length_name
,
1160 lttng_ust_get_type_sequence(lt
)->length_name
,
1161 LTTNG_UST_ABI_SYM_NAME_LEN
);
1162 ut
->u
.sequence_nestable
.length_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1163 ut
->u
.sequence_nestable
.alignment
= lttng_ust_get_type_sequence(lt
)->alignment
;
1166 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1167 lttng_ust_get_type_sequence(lt
)->elem_type
,
1168 lttng_ust_get_type_sequence(lt
)->encoding
);
1173 case lttng_ust_type_dynamic
:
1175 ret
= serialize_dynamic_type(session
, fields
, iter_output
,
1181 case lttng_ust_type_struct
:
1183 struct ustctl_field
*uf
= &fields
[*iter_output
];
1186 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1187 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1191 uf
->type
.atype
= ustctl_atype_struct_nestable
;
1192 uf
->type
.u
.struct_nestable
.nr_fields
= lttng_ust_get_type_struct(lt
)->nr_fields
;
1193 uf
->type
.u
.struct_nestable
.alignment
= lttng_ust_get_type_struct(lt
)->alignment
;
1196 ret
= serialize_fields(session
, fields
, iter_output
,
1197 lttng_ust_get_type_struct(lt
)->nr_fields
,
1198 lttng_ust_get_type_struct(lt
)->fields
);
1203 case lttng_ust_type_enum
:
1205 struct ustctl_field
*uf
= &fields
[*iter_output
];
1206 struct ustctl_type
*ut
= &uf
->type
;
1209 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1210 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1214 strncpy(ut
->u
.enum_nestable
.name
, lttng_ust_get_type_enum(lt
)->desc
->name
,
1215 LTTNG_UST_ABI_SYM_NAME_LEN
);
1216 ut
->u
.enum_nestable
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1217 ut
->atype
= ustctl_atype_enum_nestable
;
1220 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1221 lttng_ust_get_type_enum(lt
)->container_type
,
1222 lttng_ust_string_encoding_none
);
1226 const struct lttng_enum
*_enum
;
1228 _enum
= lttng_ust_enum_get_from_desc(session
, lttng_ust_get_type_enum(lt
)->desc
);
1231 ut
->u
.enum_nestable
.id
= _enum
->id
;
1233 ut
->u
.enum_nestable
.id
= -1ULL;
1244 int serialize_one_field(struct lttng_ust_session
*session
,
1245 struct ustctl_field
*fields
, size_t *iter_output
,
1246 struct lttng_ust_event_field
*lf
)
1248 /* skip 'nowrite' fields */
1252 return serialize_one_type(session
, fields
, iter_output
, lf
->name
, lf
->type
, lttng_ust_string_encoding_none
);
1256 int serialize_fields(struct lttng_ust_session
*session
,
1257 struct ustctl_field
*ustctl_fields
,
1258 size_t *iter_output
, size_t nr_lttng_fields
,
1259 struct lttng_ust_event_field
**lttng_fields
)
1264 for (i
= 0; i
< nr_lttng_fields
; i
++) {
1265 ret
= serialize_one_field(session
, ustctl_fields
,
1266 iter_output
, lttng_fields
[i
]);
1274 int alloc_serialize_fields(struct lttng_ust_session
*session
,
1275 size_t *_nr_write_fields
,
1276 struct ustctl_field
**ustctl_fields
,
1278 struct lttng_ust_event_field
**lttng_fields
)
1280 struct ustctl_field
*fields
;
1282 size_t iter_output
= 0;
1283 ssize_t nr_write_fields
;
1285 nr_write_fields
= count_fields_recursive(nr_fields
, lttng_fields
);
1286 if (nr_write_fields
< 0) {
1287 return (int) nr_write_fields
;
1290 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1294 ret
= serialize_fields(session
, fields
, &iter_output
, nr_fields
,
1299 *_nr_write_fields
= nr_write_fields
;
1300 *ustctl_fields
= fields
;
1309 int serialize_entries(struct ustctl_enum_entry
**_entries
,
1311 struct lttng_ust_enum_entry
**lttng_entries
)
1313 struct ustctl_enum_entry
*entries
;
1316 /* Serialize the entries */
1317 entries
= zmalloc(nr_entries
* sizeof(*entries
));
1320 for (i
= 0; i
< nr_entries
; i
++) {
1321 struct ustctl_enum_entry
*uentry
;
1322 const struct lttng_ust_enum_entry
*lentry
;
1324 uentry
= &entries
[i
];
1325 lentry
= lttng_entries
[i
];
1327 uentry
->start
.value
= lentry
->start
.value
;
1328 uentry
->start
.signedness
= lentry
->start
.signedness
;
1329 uentry
->end
.value
= lentry
->end
.value
;
1330 uentry
->end
.signedness
= lentry
->end
.signedness
;
1331 strncpy(uentry
->string
, lentry
->string
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1332 uentry
->string
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1334 if (lentry
->options
& LTTNG_UST_ENUM_ENTRY_OPTION_IS_AUTO
) {
1335 uentry
->u
.extra
.options
|=
1336 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO
;
1339 *_entries
= entries
;
1344 int serialize_ctx_fields(struct lttng_ust_session
*session
,
1345 size_t *_nr_write_fields
,
1346 struct ustctl_field
**ustctl_fields
,
1348 struct lttng_ust_ctx_field
**lttng_fields
)
1350 struct ustctl_field
*fields
;
1352 size_t i
, iter_output
= 0;
1353 ssize_t nr_write_fields
;
1355 nr_write_fields
= count_ctx_fields_recursive(nr_fields
,
1357 if (nr_write_fields
< 0) {
1358 return (int) nr_write_fields
;
1361 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1365 for (i
= 0; i
< nr_fields
; i
++) {
1366 ret
= serialize_one_field(session
, fields
, &iter_output
,
1367 lttng_fields
[i
]->event_field
);
1372 *_nr_write_fields
= nr_write_fields
;
1373 *ustctl_fields
= fields
;
1382 * Returns 0 on success, negative error value on error.
1384 int ustcomm_register_event(int sock
,
1385 struct lttng_ust_session
*session
,
1386 int session_objd
, /* session descriptor */
1387 int channel_objd
, /* channel descriptor */
1388 const char *event_name
, /* event name (input) */
1390 const char *signature
, /* event signature (input) */
1391 size_t nr_fields
, /* fields */
1392 struct lttng_ust_event_field
**lttng_fields
,
1393 const char *model_emf_uri
,
1394 uint32_t *id
) /* event id (output) */
1398 struct ustcomm_notify_hdr header
;
1399 struct ustcomm_notify_event_msg m
;
1402 struct ustcomm_notify_hdr header
;
1403 struct ustcomm_notify_event_reply r
;
1405 size_t signature_len
, fields_len
, model_emf_uri_len
;
1406 struct ustctl_field
*fields
= NULL
;
1407 size_t nr_write_fields
= 0;
1410 memset(&msg
, 0, sizeof(msg
));
1411 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1412 msg
.m
.session_objd
= session_objd
;
1413 msg
.m
.channel_objd
= channel_objd
;
1414 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1415 msg
.m
.event_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1416 msg
.m
.loglevel
= loglevel
;
1417 signature_len
= strlen(signature
) + 1;
1418 msg
.m
.signature_len
= signature_len
;
1420 /* Calculate fields len, serialize fields. */
1421 if (nr_fields
> 0) {
1422 ret
= alloc_serialize_fields(session
, &nr_write_fields
, &fields
,
1423 nr_fields
, lttng_fields
);
1428 fields_len
= sizeof(*fields
) * nr_write_fields
;
1429 msg
.m
.fields_len
= fields_len
;
1430 if (model_emf_uri
) {
1431 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
1433 model_emf_uri_len
= 0;
1435 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
1437 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1438 if (len
> 0 && len
!= sizeof(msg
)) {
1447 /* send signature */
1448 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
1449 if (len
> 0 && len
!= signature_len
) {
1459 if (fields_len
> 0) {
1460 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1461 if (len
> 0 && len
!= fields_len
) {
1472 if (model_emf_uri_len
) {
1473 /* send model_emf_uri */
1474 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
1476 if (len
> 0 && len
!= model_emf_uri_len
) {
1485 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1487 case 0: /* orderly shutdown */
1490 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1491 ERR("Unexpected result message command "
1492 "expected: %u vs received: %u\n",
1493 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1496 if (reply
.r
.ret_code
> 0)
1498 if (reply
.r
.ret_code
< 0)
1499 return reply
.r
.ret_code
;
1500 *id
= reply
.r
.event_id
;
1501 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1502 event_name
, reply
.r
.ret_code
, reply
.r
.event_id
);
1506 /* Transport level error */
1507 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1511 ERR("incorrect message size: %zd\n", len
);
1517 /* Error path only. */
1524 * Returns 0 on success, negative error value on error.
1525 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1527 int ustcomm_register_enum(int sock
,
1528 int session_objd
, /* session descriptor */
1529 const char *enum_name
, /* enum name (input) */
1530 size_t nr_entries
, /* entries */
1531 struct lttng_ust_enum_entry
**lttng_entries
,
1536 struct ustcomm_notify_hdr header
;
1537 struct ustcomm_notify_enum_msg m
;
1540 struct ustcomm_notify_hdr header
;
1541 struct ustcomm_notify_enum_reply r
;
1544 struct ustctl_enum_entry
*entries
= NULL
;
1547 memset(&msg
, 0, sizeof(msg
));
1548 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1549 msg
.m
.session_objd
= session_objd
;
1550 strncpy(msg
.m
.enum_name
, enum_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1551 msg
.m
.enum_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1553 /* Calculate entries len, serialize entries. */
1554 if (nr_entries
> 0) {
1555 ret
= serialize_entries(&entries
,
1556 nr_entries
, lttng_entries
);
1561 entries_len
= sizeof(*entries
) * nr_entries
;
1562 msg
.m
.entries_len
= entries_len
;
1564 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1565 if (len
> 0 && len
!= sizeof(msg
)) {
1575 if (entries_len
> 0) {
1576 len
= ustcomm_send_unix_sock(sock
, entries
, entries_len
);
1577 if (len
> 0 && len
!= entries_len
) {
1590 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1592 case 0: /* orderly shutdown */
1595 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1596 ERR("Unexpected result message command "
1597 "expected: %u vs received: %u\n",
1598 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1601 if (reply
.r
.ret_code
> 0)
1603 if (reply
.r
.ret_code
< 0)
1604 return reply
.r
.ret_code
;
1605 *id
= reply
.r
.enum_id
;
1606 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1607 enum_name
, reply
.r
.ret_code
);
1611 /* Transport level error */
1612 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1616 ERR("incorrect message size: %zd\n", len
);
1628 * Returns 0 on success, negative error value on error.
1629 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1631 int ustcomm_register_channel(int sock
,
1632 struct lttng_ust_session
*session
,
1633 int session_objd
, /* session descriptor */
1634 int channel_objd
, /* channel descriptor */
1635 size_t nr_ctx_fields
,
1636 struct lttng_ust_ctx_field
**ctx_fields
,
1637 uint32_t *chan_id
, /* channel id (output) */
1638 int *header_type
) /* header type (output) */
1642 struct ustcomm_notify_hdr header
;
1643 struct ustcomm_notify_channel_msg m
;
1646 struct ustcomm_notify_hdr header
;
1647 struct ustcomm_notify_channel_reply r
;
1650 struct ustctl_field
*fields
= NULL
;
1652 size_t nr_write_fields
= 0;
1654 memset(&msg
, 0, sizeof(msg
));
1655 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1656 msg
.m
.session_objd
= session_objd
;
1657 msg
.m
.channel_objd
= channel_objd
;
1659 /* Calculate fields len, serialize fields. */
1660 if (nr_ctx_fields
> 0) {
1661 ret
= serialize_ctx_fields(session
, &nr_write_fields
, &fields
,
1662 nr_ctx_fields
, ctx_fields
);
1667 fields_len
= sizeof(*fields
) * nr_write_fields
;
1668 msg
.m
.ctx_fields_len
= fields_len
;
1669 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1670 if (len
> 0 && len
!= sizeof(msg
)) {
1680 if (fields_len
> 0) {
1681 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1683 if (len
> 0 && len
!= fields_len
) {
1693 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1695 case 0: /* orderly shutdown */
1698 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1699 ERR("Unexpected result message command "
1700 "expected: %u vs received: %u\n",
1701 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1704 if (reply
.r
.ret_code
> 0)
1706 if (reply
.r
.ret_code
< 0)
1707 return reply
.r
.ret_code
;
1708 *chan_id
= reply
.r
.chan_id
;
1709 switch (reply
.r
.header_type
) {
1712 *header_type
= reply
.r
.header_type
;
1715 ERR("Unexpected channel header type %u\n",
1716 reply
.r
.header_type
);
1719 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1720 reply
.r
.chan_id
, reply
.r
.header_type
);
1724 /* Transport level error */
1725 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1729 ERR("incorrect message size: %zd\n", len
);
1736 * Set socket reciving timeout.
1738 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1743 tv
.tv_sec
= msec
/ 1000;
1744 tv
.tv_usec
= (msec
* 1000 % 1000000);
1746 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1748 PERROR("setsockopt SO_RCVTIMEO");
1756 * Set socket sending timeout.
1758 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1763 tv
.tv_sec
= msec
/ 1000;
1764 tv
.tv_usec
= (msec
* 1000) % 1000000;
1766 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1768 PERROR("setsockopt SO_SNDTIMEO");