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 const struct lttng_event_field
*lttng_fields
);
42 int serialize_one_field(struct lttng_session
*session
,
43 struct ustctl_field
*fields
, size_t *iter_output
,
44 const struct lttng_event_field
*lf
);
46 int serialize_fields(struct lttng_session
*session
,
47 struct ustctl_field
*ustctl_fields
,
48 size_t *iter_output
, size_t nr_lttng_fields
,
49 const struct lttng_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.",
74 * Receives positive error value.
75 * Return ptr to string representing a human readable
76 * error code from the ustcomm_return_code enum.
78 const char *lttng_ust_strerror(int code
)
80 if (code
== LTTNG_UST_OK
)
81 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
82 if (code
< LTTNG_UST_ERR
)
83 return strerror(code
);
84 if (code
>= LTTNG_UST_ERR_NR
)
86 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
90 * ustcomm_connect_unix_sock
92 * Connect to unix socket using the path name.
94 * Caller handles FD tracker.
96 int ustcomm_connect_unix_sock(const char *pathname
, long timeout
)
98 struct sockaddr_un sun
;
102 * libust threads require the close-on-exec flag for all
103 * resources so it does not leak file descriptors upon exec.
104 * SOCK_CLOEXEC is not used since it is linux specific.
106 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
113 /* Give at least 10ms. */
116 ret
= ustcomm_setsockopt_snd_timeout(fd
, timeout
);
118 WARN("Error setting connect socket send timeout");
121 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
128 memset(&sun
, 0, sizeof(sun
));
129 sun
.sun_family
= AF_UNIX
;
130 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
131 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
133 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
136 * Don't print message on connect ENOENT error, because
137 * connect is used in normal execution to detect if
138 * sessiond is alive. ENOENT is when the unix socket
139 * file does not exist, and ECONNREFUSED is when the
140 * file exists but no sessiond is listening.
142 if (errno
!= ECONNREFUSED
&& errno
!= ECONNRESET
143 && errno
!= ENOENT
&& errno
!= EACCES
)
146 if (ret
== -ECONNREFUSED
|| ret
== -ECONNRESET
)
158 closeret
= close(fd
);
167 * ustcomm_accept_unix_sock
169 * Do an accept(2) on the sock and return the
170 * new file descriptor. The socket MUST be bind(2) before.
172 int ustcomm_accept_unix_sock(int sock
)
175 struct sockaddr_un sun
;
179 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
181 if (errno
!= ECONNABORTED
)
184 if (new_fd
== -ECONNABORTED
)
191 * ustcomm_create_unix_sock
193 * Creates a AF_UNIX local socket using pathname
194 * bind the socket upon creation and return the fd.
196 int ustcomm_create_unix_sock(const char *pathname
)
198 struct sockaddr_un sun
;
201 /* Create server socket */
202 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
208 memset(&sun
, 0, sizeof(sun
));
209 sun
.sun_family
= AF_UNIX
;
210 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
211 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
213 /* Unlink the old file if present */
214 (void) unlink(pathname
);
215 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
228 closeret
= close(fd
);
238 * ustcomm_listen_unix_sock
240 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
242 int ustcomm_listen_unix_sock(int sock
)
246 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
256 * ustcomm_close_unix_sock
258 * Shutdown cleanly a unix socket.
260 * Handles fd tracker internally.
262 int ustcomm_close_unix_sock(int sock
)
266 lttng_ust_lock_fd_tracker();
269 lttng_ust_delete_fd_from_tracker(sock
);
274 lttng_ust_unlock_fd_tracker();
280 * ustcomm_recv_unix_sock
282 * Receive data of size len in put that data into
283 * the buf param. Using recvmsg API.
284 * Return the size of received data.
285 * Return 0 on orderly shutdown.
287 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
294 memset(&msg
, 0, sizeof(msg
));
296 iov
[0].iov_base
= buf
;
297 iov
[0].iov_len
= len
;
302 len_last
= iov
[0].iov_len
;
303 ret
= recvmsg(sock
, &msg
, 0);
305 iov
[0].iov_base
+= ret
;
306 iov
[0].iov_len
-= ret
;
307 assert(ret
<= len_last
);
309 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
314 if (errno
!= EPIPE
&& errno
!= ECONNRESET
&& errno
!= ECONNREFUSED
)
317 if (ret
== -ECONNRESET
|| ret
== -ECONNREFUSED
)
320 shutret
= shutdown(sock
, SHUT_RDWR
);
322 ERR("Socket shutdown error");
323 } else if (ret
> 0) {
326 /* ret = 0 means an orderly shutdown. */
332 * ustcomm_send_unix_sock
334 * Send buf data of size len. Using sendmsg API.
335 * Return the size of sent data.
337 ssize_t
ustcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
343 memset(&msg
, 0, sizeof(msg
));
345 iov
[0].iov_base
= (void *) buf
;
346 iov
[0].iov_len
= len
;
351 * Using the MSG_NOSIGNAL when sending data from sessiond to
352 * libust, so libust does not receive an unhandled SIGPIPE or
353 * SIGURG. The sessiond receiver side can be made more resilient
354 * by ignoring SIGPIPE, but we don't have this luxury on the
358 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
359 } while (ret
< 0 && errno
== EINTR
);
364 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
367 if (ret
== -ECONNRESET
)
370 shutret
= shutdown(sock
, SHUT_RDWR
);
372 ERR("Socket shutdown error");
379 * Send a message accompanied by fd(s) over a unix socket.
381 * Returns the size of data sent, or negative error value.
383 ssize_t
ustcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
386 struct cmsghdr
*cmptr
;
389 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
390 char tmp
[CMSG_SPACE(sizeof_fds
)];
393 memset(&msg
, 0, sizeof(msg
));
394 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
396 if (nb_fd
> USTCOMM_MAX_SEND_FDS
)
399 msg
.msg_control
= (caddr_t
)tmp
;
400 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
402 cmptr
= CMSG_FIRSTHDR(&msg
);
405 cmptr
->cmsg_level
= SOL_SOCKET
;
406 cmptr
->cmsg_type
= SCM_RIGHTS
;
407 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
408 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
409 /* Sum of the length of all control messages in the buffer: */
410 msg
.msg_controllen
= cmptr
->cmsg_len
;
412 iov
[0].iov_base
= &dummy
;
418 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
419 } while (ret
< 0 && errno
== EINTR
);
422 * We consider EPIPE and ECONNRESET as expected.
424 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
428 if (ret
== -ECONNRESET
)
435 * Recv a message accompanied by fd(s) from a unix socket.
437 * Expect at most "nb_fd" file descriptors. Returns the number of fd
438 * actually received in nb_fd.
439 * Returns -EPIPE on orderly shutdown.
441 ssize_t
ustcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
445 struct cmsghdr
*cmsg
;
446 size_t sizeof_fds
= nb_fd
* sizeof(int);
447 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
452 memset(&msg
, 0, sizeof(msg
));
454 /* Prepare to receive the structures */
455 iov
[0].iov_base
= &dummy
;
459 msg
.msg_control
= recv_fd
;
460 msg
.msg_controllen
= sizeof(recv_fd
);
463 ret
= recvmsg(sock
, &msg
, 0);
464 } while (ret
< 0 && errno
== EINTR
);
466 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
467 PERROR("recvmsg fds");
470 if (ret
== -ECONNRESET
)
475 /* orderly shutdown */
480 ERR("Error: Received %zd bytes, expected %d\n",
484 if (msg
.msg_flags
& MSG_CTRUNC
) {
485 ERR("Error: Control message truncated.\n");
489 cmsg
= CMSG_FIRSTHDR(&msg
);
491 ERR("Error: Invalid control message header\n");
495 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
496 ERR("Didn't received any fd\n");
500 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
501 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
502 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
507 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
510 for (i
= 0; i
< nb_fd
; i
++) {
511 ret
= fcntl(fds
[i
], F_SETFD
, FD_CLOEXEC
);
513 PERROR("fcntl failed to set FD_CLOEXEC on fd %d",
523 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
527 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
535 ERR("incorrect message size: %zd\n", len
);
542 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
543 uint32_t expected_handle
, uint32_t expected_cmd
)
547 memset(lur
, 0, sizeof(*lur
));
548 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
550 case 0: /* orderly shutdown */
556 if (lur
->handle
!= expected_handle
) {
557 ERR("Unexpected result message handle: "
558 "expected: %u vs received: %u\n",
559 expected_handle
, lur
->handle
);
562 if (lur
->cmd
!= expected_cmd
) {
563 ERR("Unexpected result message command "
564 "expected: %u vs received: %u\n",
565 expected_cmd
, lur
->cmd
);
571 return lur
->ret_code
;
576 ERR("incorrect message size: %zd\n", len
);
582 int ustcomm_send_app_cmd(int sock
,
583 struct ustcomm_ust_msg
*lum
,
584 struct ustcomm_ust_reply
*lur
)
588 ret
= ustcomm_send_app_msg(sock
, lum
);
591 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
598 * chan_data is allocated internally if this function returns the
601 ssize_t
ustcomm_recv_channel_from_sessiond(int sock
,
602 void **_chan_data
, uint64_t var_len
,
609 if (var_len
> LTTNG_UST_CHANNEL_DATA_MAX_LEN
) {
613 /* Receive variable length data */
614 chan_data
= zmalloc(var_len
);
619 len
= ustcomm_recv_unix_sock(sock
, chan_data
, var_len
);
620 if (len
!= var_len
) {
624 lttng_ust_lock_fd_tracker();
625 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
627 lttng_ust_unlock_fd_tracker();
637 ret
= lttng_ust_add_fd_to_tracker(wakeup_fd
);
639 ret
= close(wakeup_fd
);
641 PERROR("close on wakeup_fd");
644 lttng_ust_unlock_fd_tracker();
649 lttng_ust_unlock_fd_tracker();
651 *_chan_data
= chan_data
;
661 ssize_t
ustcomm_recv_event_notifier_notif_fd_from_sessiond(int sock
,
662 int *_event_notifier_notif_fd
)
665 int event_notifier_notif_fd
, ret
;
667 /* Receive event_notifier notification fd */
668 lttng_ust_lock_fd_tracker();
669 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &event_notifier_notif_fd
, 1);
671 lttng_ust_unlock_fd_tracker();
681 ret
= lttng_ust_add_fd_to_tracker(event_notifier_notif_fd
);
683 ret
= close(event_notifier_notif_fd
);
685 PERROR("close on event_notifier notif fd");
688 lttng_ust_unlock_fd_tracker();
692 *_event_notifier_notif_fd
= ret
;
693 lttng_ust_unlock_fd_tracker();
701 int ustcomm_recv_stream_from_sessiond(int sock
,
702 uint64_t *memory_map_size
,
703 int *shm_fd
, int *wakeup_fd
)
709 /* recv shm fd and wakeup fd */
710 lttng_ust_lock_fd_tracker();
711 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
713 lttng_ust_unlock_fd_tracker();
723 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
727 PERROR("close on received shm_fd");
730 lttng_ust_unlock_fd_tracker();
735 ret
= lttng_ust_add_fd_to_tracker(fds
[1]);
737 ret
= close(*shm_fd
);
739 PERROR("close on shm_fd");
744 PERROR("close on received wakeup_fd");
747 lttng_ust_unlock_fd_tracker();
751 lttng_ust_unlock_fd_tracker();
758 ssize_t
ustcomm_recv_counter_from_sessiond(int sock
,
759 void **_counter_data
, uint64_t var_len
)
764 if (var_len
> LTTNG_UST_COUNTER_DATA_MAX_LEN
) {
768 /* Receive variable length data */
769 counter_data
= zmalloc(var_len
);
774 len
= ustcomm_recv_unix_sock(sock
, counter_data
, var_len
);
775 if (len
!= var_len
) {
778 *_counter_data
= counter_data
;
788 int ustcomm_recv_counter_shm_from_sessiond(int sock
,
796 lttng_ust_lock_fd_tracker();
797 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 1);
799 lttng_ust_unlock_fd_tracker();
809 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
813 PERROR("close on received shm_fd");
816 lttng_ust_unlock_fd_tracker();
820 lttng_ust_unlock_fd_tracker();
828 * Returns 0 on success, negative error value on error.
830 int ustcomm_send_reg_msg(int sock
,
831 enum ustctl_socket_type type
,
832 uint32_t bits_per_long
,
833 uint32_t uint8_t_alignment
,
834 uint32_t uint16_t_alignment
,
835 uint32_t uint32_t_alignment
,
836 uint32_t uint64_t_alignment
,
837 uint32_t long_alignment
)
840 struct ustctl_reg_msg reg_msg
;
842 reg_msg
.magic
= LTTNG_UST_COMM_MAGIC
;
843 reg_msg
.major
= LTTNG_UST_ABI_MAJOR_VERSION
;
844 reg_msg
.minor
= LTTNG_UST_ABI_MINOR_VERSION
;
845 reg_msg
.pid
= getpid();
846 reg_msg
.ppid
= getppid();
847 reg_msg
.uid
= getuid();
848 reg_msg
.gid
= getgid();
849 reg_msg
.bits_per_long
= bits_per_long
;
850 reg_msg
.uint8_t_alignment
= uint8_t_alignment
;
851 reg_msg
.uint16_t_alignment
= uint16_t_alignment
;
852 reg_msg
.uint32_t_alignment
= uint32_t_alignment
;
853 reg_msg
.uint64_t_alignment
= uint64_t_alignment
;
854 reg_msg
.long_alignment
= long_alignment
;
855 reg_msg
.socket_type
= type
;
856 lttng_pthread_getname_np(reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
857 memset(reg_msg
.padding
, 0, sizeof(reg_msg
.padding
));
859 len
= ustcomm_send_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
860 if (len
> 0 && len
!= sizeof(reg_msg
))
868 ssize_t
count_one_type(const struct lttng_type
*lt
)
879 return count_fields_recursive(lt
->u
.legacy
._struct
.nr_fields
,
880 lt
->u
.legacy
._struct
.fields
) + 1;
881 case atype_enum_nestable
:
882 return count_one_type(lt
->u
.enum_nestable
.container_type
) + 1;
883 case atype_array_nestable
:
884 return count_one_type(lt
->u
.array_nestable
.elem_type
) + 1;
885 case atype_sequence_nestable
:
886 return count_one_type(lt
->u
.sequence_nestable
.elem_type
) + 1;
887 case atype_struct_nestable
:
888 return count_fields_recursive(lt
->u
.struct_nestable
.nr_fields
,
889 lt
->u
.struct_nestable
.fields
) + 1;
893 const struct lttng_event_field
*choices
;
897 ret
= lttng_ust_dynamic_type_choices(&nr_choices
,
902 * Two fields for enum, one field for variant, and
903 * one field per choice.
905 return count_fields_recursive(nr_choices
, choices
) + 3;
915 ssize_t
count_fields_recursive(size_t nr_fields
,
916 const struct lttng_event_field
*lttng_fields
)
919 ssize_t ret
, count
= 0;
921 for (i
= 0; i
< nr_fields
; i
++) {
922 const struct lttng_event_field
*lf
;
924 lf
= <tng_fields
[i
];
925 /* skip 'nowrite' fields */
928 ret
= count_one_type(&lf
->type
);
930 return ret
; /* error */
937 ssize_t
count_ctx_fields_recursive(size_t nr_fields
,
938 const struct lttng_ctx_field
*lttng_fields
)
941 ssize_t ret
, count
= 0;
943 for (i
= 0; i
< nr_fields
; i
++) {
944 const struct lttng_event_field
*lf
;
946 lf
= <tng_fields
[i
].event_field
;
947 /* skip 'nowrite' fields */
950 ret
= count_one_type(&lf
->type
);
952 return ret
; /* error */
959 int serialize_string_encoding(int32_t *ue
,
960 enum lttng_string_encodings le
)
963 case lttng_encode_none
:
964 *ue
= ustctl_encode_none
;
966 case lttng_encode_UTF8
:
967 *ue
= ustctl_encode_UTF8
;
969 case lttng_encode_ASCII
:
970 *ue
= ustctl_encode_ASCII
;
979 int serialize_integer_type(struct ustctl_integer_type
*uit
,
980 const struct lttng_integer_type
*lit
)
984 uit
->size
= lit
->size
;
985 uit
->signedness
= lit
->signedness
;
986 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
987 uit
->base
= lit
->base
;
988 if (serialize_string_encoding(&encoding
, lit
->encoding
))
990 uit
->encoding
= encoding
;
991 uit
->alignment
= lit
->alignment
;
996 int serialize_basic_type(struct lttng_session
*session
,
997 enum ustctl_abstract_types
*uatype
,
998 enum lttng_abstract_types atype
,
999 union _ustctl_basic_type
*ubt
,
1000 const union _lttng_basic_type
*lbt
)
1005 if (serialize_integer_type(&ubt
->integer
, &lbt
->integer
))
1007 *uatype
= ustctl_atype_integer
;
1014 if (serialize_string_encoding(&encoding
, lbt
->string
.encoding
))
1016 ubt
->string
.encoding
= encoding
;
1017 *uatype
= ustctl_atype_string
;
1022 struct ustctl_float_type
*uft
;
1023 const struct lttng_float_type
*lft
;
1027 uft
->exp_dig
= lft
->exp_dig
;
1028 uft
->mant_dig
= lft
->mant_dig
;
1029 uft
->alignment
= lft
->alignment
;
1030 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
1031 *uatype
= ustctl_atype_float
;
1036 strncpy(ubt
->enumeration
.name
, lbt
->enumeration
.desc
->name
,
1037 LTTNG_UST_SYM_NAME_LEN
);
1038 ubt
->enumeration
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1039 if (serialize_integer_type(&ubt
->enumeration
.container_type
,
1040 &lbt
->enumeration
.container_type
))
1043 const struct lttng_enum
*_enum
;
1045 _enum
= lttng_ust_enum_get_from_desc(session
, lbt
->enumeration
.desc
);
1048 ubt
->enumeration
.id
= _enum
->id
;
1050 ubt
->enumeration
.id
= -1ULL;
1052 *uatype
= ustctl_atype_enum
;
1056 case atype_array_nestable
:
1057 case atype_sequence
:
1058 case atype_sequence_nestable
:
1059 case atype_enum_nestable
:
1067 int serialize_dynamic_type(struct lttng_session
*session
,
1068 struct ustctl_field
*fields
, size_t *iter_output
,
1069 const char *field_name
)
1071 const struct lttng_event_field
*choices
;
1072 char tag_field_name
[LTTNG_UST_SYM_NAME_LEN
];
1073 const struct lttng_type
*tag_type
;
1074 const struct lttng_event_field
*tag_field_generic
;
1075 struct lttng_event_field tag_field
= {
1076 .name
= tag_field_name
,
1079 struct ustctl_field
*uf
;
1080 size_t nr_choices
, i
;
1083 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
1084 tag_type
= &tag_field_generic
->type
;
1086 /* Serialize enum field. */
1087 strncpy(tag_field_name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1088 tag_field_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1089 strncat(tag_field_name
,
1091 LTTNG_UST_SYM_NAME_LEN
- strlen(tag_field_name
) - 1);
1092 tag_field
.type
= *tag_type
;
1093 ret
= serialize_one_field(session
, fields
, iter_output
,
1098 /* Serialize variant field. */
1099 uf
= &fields
[*iter_output
];
1100 ret
= lttng_ust_dynamic_type_choices(&nr_choices
, &choices
);
1104 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1105 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1106 uf
->type
.atype
= ustctl_atype_variant
;
1107 uf
->type
.u
.variant_nestable
.nr_choices
= nr_choices
;
1108 strncpy(uf
->type
.u
.variant_nestable
.tag_name
,
1110 LTTNG_UST_SYM_NAME_LEN
);
1111 uf
->type
.u
.variant_nestable
.tag_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1112 uf
->type
.u
.variant_nestable
.alignment
= 0;
1115 /* Serialize choice fields after variant. */
1116 for (i
= 0; i
< nr_choices
; i
++) {
1117 ret
= serialize_one_field(session
, fields
,
1118 iter_output
, &choices
[i
]);
1126 int serialize_one_type(struct lttng_session
*session
,
1127 struct ustctl_field
*fields
, size_t *iter_output
,
1128 const char *field_name
, const struct lttng_type
*lt
)
1133 * Serializing a type (rather than a field) generates a ustctl_field
1134 * entry with 0-length name.
1137 switch (lt
->atype
) {
1143 struct ustctl_field
*uf
= &fields
[*iter_output
];
1144 struct ustctl_type
*ut
= &uf
->type
;
1145 enum ustctl_abstract_types atype
;
1148 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1149 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1153 ret
= serialize_basic_type(session
, &atype
, lt
->atype
,
1154 &ut
->u
.legacy
.basic
, <
->u
.legacy
.basic
);
1163 struct ustctl_field
*uf
= &fields
[*iter_output
];
1164 struct ustctl_type
*ut
= &uf
->type
;
1165 struct ustctl_basic_type
*ubt
;
1166 const struct lttng_basic_type
*lbt
;
1167 enum ustctl_abstract_types atype
;
1170 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1171 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1175 ut
->atype
= ustctl_atype_array
;
1176 ubt
= &ut
->u
.legacy
.array
.elem_type
;
1177 lbt
= <
->u
.legacy
.array
.elem_type
;
1178 ut
->u
.legacy
.array
.length
= lt
->u
.legacy
.array
.length
;
1179 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1180 &ubt
->u
.basic
, &lbt
->u
.basic
);
1187 case atype_array_nestable
:
1189 struct ustctl_field
*uf
= &fields
[*iter_output
];
1190 struct ustctl_type
*ut
= &uf
->type
;
1193 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1194 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1198 ut
->atype
= ustctl_atype_array_nestable
;
1199 ut
->u
.array_nestable
.length
= lt
->u
.array_nestable
.length
;
1200 ut
->u
.array_nestable
.alignment
= lt
->u
.array_nestable
.alignment
;
1203 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1204 lt
->u
.array_nestable
.elem_type
);
1209 case atype_sequence
:
1211 struct ustctl_field
*uf
= &fields
[*iter_output
];
1212 struct ustctl_type
*ut
= &uf
->type
;
1213 struct ustctl_basic_type
*ubt
;
1214 const struct lttng_basic_type
*lbt
;
1215 enum ustctl_abstract_types atype
;
1218 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1219 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1223 uf
->type
.atype
= ustctl_atype_sequence
;
1224 ubt
= &ut
->u
.legacy
.sequence
.length_type
;
1225 lbt
= <
->u
.legacy
.sequence
.length_type
;
1226 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1227 &ubt
->u
.basic
, &lbt
->u
.basic
);
1231 ubt
= &ut
->u
.legacy
.sequence
.elem_type
;
1232 lbt
= <
->u
.legacy
.sequence
.elem_type
;
1233 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1234 &ubt
->u
.basic
, &lbt
->u
.basic
);
1241 case atype_sequence_nestable
:
1243 struct ustctl_field
*uf
= &fields
[*iter_output
];
1244 struct ustctl_type
*ut
= &uf
->type
;
1247 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1248 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1252 ut
->atype
= ustctl_atype_sequence_nestable
;
1253 strncpy(ut
->u
.sequence_nestable
.length_name
,
1254 lt
->u
.sequence_nestable
.length_name
,
1255 LTTNG_UST_SYM_NAME_LEN
);
1256 ut
->u
.sequence_nestable
.length_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1257 ut
->u
.sequence_nestable
.alignment
= lt
->u
.sequence_nestable
.alignment
;
1260 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1261 lt
->u
.sequence_nestable
.elem_type
);
1268 ret
= serialize_dynamic_type(session
, fields
, iter_output
,
1276 struct ustctl_field
*uf
= &fields
[*iter_output
];
1279 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1280 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1284 uf
->type
.atype
= ustctl_atype_struct
;
1285 uf
->type
.u
.legacy
._struct
.nr_fields
= lt
->u
.legacy
._struct
.nr_fields
;
1288 ret
= serialize_fields(session
, fields
, iter_output
,
1289 lt
->u
.legacy
._struct
.nr_fields
,
1290 lt
->u
.legacy
._struct
.fields
);
1295 case atype_struct_nestable
:
1297 struct ustctl_field
*uf
= &fields
[*iter_output
];
1300 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1301 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1305 uf
->type
.atype
= ustctl_atype_struct_nestable
;
1306 uf
->type
.u
.struct_nestable
.nr_fields
= lt
->u
.struct_nestable
.nr_fields
;
1307 uf
->type
.u
.struct_nestable
.alignment
= lt
->u
.struct_nestable
.alignment
;
1310 ret
= serialize_fields(session
, fields
, iter_output
,
1311 lt
->u
.struct_nestable
.nr_fields
,
1312 lt
->u
.struct_nestable
.fields
);
1317 case atype_enum_nestable
:
1319 struct ustctl_field
*uf
= &fields
[*iter_output
];
1320 struct ustctl_type
*ut
= &uf
->type
;
1323 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1324 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1328 strncpy(ut
->u
.enum_nestable
.name
, lt
->u
.enum_nestable
.desc
->name
,
1329 LTTNG_UST_SYM_NAME_LEN
);
1330 ut
->u
.enum_nestable
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1331 ut
->atype
= ustctl_atype_enum_nestable
;
1334 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1335 lt
->u
.enum_nestable
.container_type
);
1339 const struct lttng_enum
*_enum
;
1341 _enum
= lttng_ust_enum_get_from_desc(session
, lt
->u
.enum_nestable
.desc
);
1344 ut
->u
.enum_nestable
.id
= _enum
->id
;
1346 ut
->u
.enum_nestable
.id
= -1ULL;
1357 int serialize_one_field(struct lttng_session
*session
,
1358 struct ustctl_field
*fields
, size_t *iter_output
,
1359 const struct lttng_event_field
*lf
)
1361 /* skip 'nowrite' fields */
1365 return serialize_one_type(session
, fields
, iter_output
, lf
->name
, &lf
->type
);
1369 int serialize_fields(struct lttng_session
*session
,
1370 struct ustctl_field
*ustctl_fields
,
1371 size_t *iter_output
, size_t nr_lttng_fields
,
1372 const struct lttng_event_field
*lttng_fields
)
1377 for (i
= 0; i
< nr_lttng_fields
; i
++) {
1378 ret
= serialize_one_field(session
, ustctl_fields
,
1379 iter_output
, <tng_fields
[i
]);
1387 int alloc_serialize_fields(struct lttng_session
*session
,
1388 size_t *_nr_write_fields
,
1389 struct ustctl_field
**ustctl_fields
,
1391 const struct lttng_event_field
*lttng_fields
)
1393 struct ustctl_field
*fields
;
1395 size_t iter_output
= 0;
1396 ssize_t nr_write_fields
;
1398 nr_write_fields
= count_fields_recursive(nr_fields
, lttng_fields
);
1399 if (nr_write_fields
< 0) {
1400 return (int) nr_write_fields
;
1403 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1407 ret
= serialize_fields(session
, fields
, &iter_output
, nr_fields
,
1412 *_nr_write_fields
= nr_write_fields
;
1413 *ustctl_fields
= fields
;
1422 int serialize_entries(struct ustctl_enum_entry
**_entries
,
1424 const struct lttng_enum_entry
*lttng_entries
)
1426 struct ustctl_enum_entry
*entries
;
1429 /* Serialize the entries */
1430 entries
= zmalloc(nr_entries
* sizeof(*entries
));
1433 for (i
= 0; i
< nr_entries
; i
++) {
1434 struct ustctl_enum_entry
*uentry
;
1435 const struct lttng_enum_entry
*lentry
;
1437 uentry
= &entries
[i
];
1438 lentry
= <tng_entries
[i
];
1440 uentry
->start
.value
= lentry
->start
.value
;
1441 uentry
->start
.signedness
= lentry
->start
.signedness
;
1442 uentry
->end
.value
= lentry
->end
.value
;
1443 uentry
->end
.signedness
= lentry
->end
.signedness
;
1444 strncpy(uentry
->string
, lentry
->string
, LTTNG_UST_SYM_NAME_LEN
);
1445 uentry
->string
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1447 if (lentry
->u
.extra
.options
& LTTNG_ENUM_ENTRY_OPTION_IS_AUTO
) {
1448 uentry
->u
.extra
.options
|=
1449 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO
;
1452 *_entries
= entries
;
1457 int serialize_ctx_fields(struct lttng_session
*session
,
1458 size_t *_nr_write_fields
,
1459 struct ustctl_field
**ustctl_fields
,
1461 const struct lttng_ctx_field
*lttng_fields
)
1463 struct ustctl_field
*fields
;
1465 size_t i
, iter_output
= 0;
1466 ssize_t nr_write_fields
;
1468 nr_write_fields
= count_ctx_fields_recursive(nr_fields
,
1470 if (nr_write_fields
< 0) {
1471 return (int) nr_write_fields
;
1474 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1478 for (i
= 0; i
< nr_fields
; i
++) {
1479 ret
= serialize_one_field(session
, fields
, &iter_output
,
1480 <tng_fields
[i
].event_field
);
1485 *_nr_write_fields
= nr_write_fields
;
1486 *ustctl_fields
= fields
;
1495 * Returns 0 on success, negative error value on error.
1497 int ustcomm_register_event(int sock
,
1498 struct lttng_session
*session
,
1499 int session_objd
, /* session descriptor */
1500 int channel_objd
, /* channel descriptor */
1501 const char *event_name
, /* event name (input) */
1503 const char *signature
, /* event signature (input) */
1504 size_t nr_fields
, /* fields */
1505 const struct lttng_event_field
*lttng_fields
,
1506 const char *model_emf_uri
,
1507 uint32_t *id
) /* event id (output) */
1511 struct ustcomm_notify_hdr header
;
1512 struct ustcomm_notify_event_msg m
;
1515 struct ustcomm_notify_hdr header
;
1516 struct ustcomm_notify_event_reply r
;
1518 size_t signature_len
, fields_len
, model_emf_uri_len
;
1519 struct ustctl_field
*fields
= NULL
;
1520 size_t nr_write_fields
= 0;
1523 memset(&msg
, 0, sizeof(msg
));
1524 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1525 msg
.m
.session_objd
= session_objd
;
1526 msg
.m
.channel_objd
= channel_objd
;
1527 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_SYM_NAME_LEN
);
1528 msg
.m
.event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1529 msg
.m
.loglevel
= loglevel
;
1530 signature_len
= strlen(signature
) + 1;
1531 msg
.m
.signature_len
= signature_len
;
1533 /* Calculate fields len, serialize fields. */
1534 if (nr_fields
> 0) {
1535 ret
= alloc_serialize_fields(session
, &nr_write_fields
, &fields
,
1536 nr_fields
, lttng_fields
);
1541 fields_len
= sizeof(*fields
) * nr_write_fields
;
1542 msg
.m
.fields_len
= fields_len
;
1543 if (model_emf_uri
) {
1544 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
1546 model_emf_uri_len
= 0;
1548 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
1550 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1551 if (len
> 0 && len
!= sizeof(msg
)) {
1560 /* send signature */
1561 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
1562 if (len
> 0 && len
!= signature_len
) {
1572 if (fields_len
> 0) {
1573 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1574 if (len
> 0 && len
!= fields_len
) {
1585 if (model_emf_uri_len
) {
1586 /* send model_emf_uri */
1587 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
1589 if (len
> 0 && len
!= model_emf_uri_len
) {
1598 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1600 case 0: /* orderly shutdown */
1603 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1604 ERR("Unexpected result message command "
1605 "expected: %u vs received: %u\n",
1606 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1609 if (reply
.r
.ret_code
> 0)
1611 if (reply
.r
.ret_code
< 0)
1612 return reply
.r
.ret_code
;
1613 *id
= reply
.r
.event_id
;
1614 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1615 event_name
, reply
.r
.ret_code
, reply
.r
.event_id
);
1619 /* Transport level error */
1620 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1624 ERR("incorrect message size: %zd\n", len
);
1630 /* Error path only. */
1637 * Returns 0 on success, negative error value on error.
1638 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1640 int ustcomm_register_enum(int sock
,
1641 int session_objd
, /* session descriptor */
1642 const char *enum_name
, /* enum name (input) */
1643 size_t nr_entries
, /* entries */
1644 const struct lttng_enum_entry
*lttng_entries
,
1649 struct ustcomm_notify_hdr header
;
1650 struct ustcomm_notify_enum_msg m
;
1653 struct ustcomm_notify_hdr header
;
1654 struct ustcomm_notify_enum_reply r
;
1657 struct ustctl_enum_entry
*entries
= NULL
;
1660 memset(&msg
, 0, sizeof(msg
));
1661 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1662 msg
.m
.session_objd
= session_objd
;
1663 strncpy(msg
.m
.enum_name
, enum_name
, LTTNG_UST_SYM_NAME_LEN
);
1664 msg
.m
.enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1666 /* Calculate entries len, serialize entries. */
1667 if (nr_entries
> 0) {
1668 ret
= serialize_entries(&entries
,
1669 nr_entries
, lttng_entries
);
1674 entries_len
= sizeof(*entries
) * nr_entries
;
1675 msg
.m
.entries_len
= entries_len
;
1677 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1678 if (len
> 0 && len
!= sizeof(msg
)) {
1688 if (entries_len
> 0) {
1689 len
= ustcomm_send_unix_sock(sock
, entries
, entries_len
);
1690 if (len
> 0 && len
!= entries_len
) {
1703 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1705 case 0: /* orderly shutdown */
1708 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1709 ERR("Unexpected result message command "
1710 "expected: %u vs received: %u\n",
1711 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1714 if (reply
.r
.ret_code
> 0)
1716 if (reply
.r
.ret_code
< 0)
1717 return reply
.r
.ret_code
;
1718 *id
= reply
.r
.enum_id
;
1719 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1720 enum_name
, reply
.r
.ret_code
);
1724 /* Transport level error */
1725 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1729 ERR("incorrect message size: %zd\n", len
);
1741 * Returns 0 on success, negative error value on error.
1742 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1744 int ustcomm_register_channel(int sock
,
1745 struct lttng_session
*session
,
1746 int session_objd
, /* session descriptor */
1747 int channel_objd
, /* channel descriptor */
1748 size_t nr_ctx_fields
,
1749 const struct lttng_ctx_field
*ctx_fields
,
1750 uint32_t *chan_id
, /* channel id (output) */
1751 int *header_type
) /* header type (output) */
1755 struct ustcomm_notify_hdr header
;
1756 struct ustcomm_notify_channel_msg m
;
1759 struct ustcomm_notify_hdr header
;
1760 struct ustcomm_notify_channel_reply r
;
1763 struct ustctl_field
*fields
= NULL
;
1765 size_t nr_write_fields
= 0;
1767 memset(&msg
, 0, sizeof(msg
));
1768 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1769 msg
.m
.session_objd
= session_objd
;
1770 msg
.m
.channel_objd
= channel_objd
;
1772 /* Calculate fields len, serialize fields. */
1773 if (nr_ctx_fields
> 0) {
1774 ret
= serialize_ctx_fields(session
, &nr_write_fields
, &fields
,
1775 nr_ctx_fields
, ctx_fields
);
1780 fields_len
= sizeof(*fields
) * nr_write_fields
;
1781 msg
.m
.ctx_fields_len
= fields_len
;
1782 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1783 if (len
> 0 && len
!= sizeof(msg
)) {
1793 if (fields_len
> 0) {
1794 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1796 if (len
> 0 && len
!= fields_len
) {
1806 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1808 case 0: /* orderly shutdown */
1811 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1812 ERR("Unexpected result message command "
1813 "expected: %u vs received: %u\n",
1814 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1817 if (reply
.r
.ret_code
> 0)
1819 if (reply
.r
.ret_code
< 0)
1820 return reply
.r
.ret_code
;
1821 *chan_id
= reply
.r
.chan_id
;
1822 switch (reply
.r
.header_type
) {
1825 *header_type
= reply
.r
.header_type
;
1828 ERR("Unexpected channel header type %u\n",
1829 reply
.r
.header_type
);
1832 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1833 reply
.r
.chan_id
, reply
.r
.header_type
);
1837 /* Transport level error */
1838 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1842 ERR("incorrect message size: %zd\n", len
);
1849 * Set socket reciving timeout.
1851 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1856 tv
.tv_sec
= msec
/ 1000;
1857 tv
.tv_usec
= (msec
* 1000 % 1000000);
1859 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1861 PERROR("setsockopt SO_RCVTIMEO");
1869 * Set socket sending timeout.
1871 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1876 tv
.tv_sec
= msec
/ 1000;
1877 tv
.tv_usec
= (msec
* 1000) % 1000000;
1879 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1881 PERROR("setsockopt SO_SNDTIMEO");