2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; only
8 * version 2.1 of the License.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <sys/socket.h>
27 #include <sys/types.h>
34 #include <lttng/ust-ctl.h>
38 #include <lttng/ust-error.h>
39 #include <lttng/ust-events.h>
40 #include <lttng/ust-dynamic-type.h>
41 #include <usterr-signal-safe.h>
43 #include "../liblttng-ust/compat.h"
45 #define USTCOMM_CODE_OFFSET(code) \
46 (code == LTTNG_UST_OK ? 0 : (code - LTTNG_UST_ERR + 1))
48 #define USTCOMM_MAX_SEND_FDS 4
51 ssize_t
count_fields_recursive(size_t nr_fields
,
52 const struct lttng_event_field
*lttng_fields
);
54 int serialize_one_field(struct lttng_session
*session
,
55 struct ustctl_field
*fields
, size_t *iter_output
,
56 const struct lttng_event_field
*lf
);
58 int serialize_fields(struct lttng_session
*session
,
59 struct ustctl_field
*ustctl_fields
,
60 size_t *iter_output
, size_t nr_lttng_fields
,
61 const struct lttng_event_field
*lttng_fields
);
64 * Human readable error message.
66 static const char *ustcomm_readable_code
[] = {
67 [ USTCOMM_CODE_OFFSET(LTTNG_UST_OK
) ] = "Success",
68 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR
) ] = "Unknown error",
69 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOENT
) ] = "No entry",
70 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXIST
) ] = "Object already exists",
71 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL
) ] = "Invalid argument",
72 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PERM
) ] = "Permission denied",
73 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOSYS
) ] = "Not implemented",
74 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXITING
) ] = "Process is exiting",
76 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_MAGIC
) ] = "Invalid magic number",
77 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_SOCKET_TYPE
) ] = "Invalid socket type",
78 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_UNSUP_MAJOR
) ] = "Unsupported major version",
84 * Receives positive error value.
85 * Return ptr to string representing a human readable
86 * error code from the ustcomm_return_code enum.
88 const char *lttng_ust_strerror(int code
)
90 if (code
== LTTNG_UST_OK
)
91 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
92 if (code
< LTTNG_UST_ERR
)
93 return strerror(code
);
94 if (code
>= LTTNG_UST_ERR_NR
)
96 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
100 * ustcomm_connect_unix_sock
102 * Connect to unix socket using the path name.
104 * Caller handles FD tracker.
106 int ustcomm_connect_unix_sock(const char *pathname
, long timeout
)
108 struct sockaddr_un sun
;
112 * libust threads require the close-on-exec flag for all
113 * resources so it does not leak file descriptors upon exec.
114 * SOCK_CLOEXEC is not used since it is linux specific.
116 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
123 /* Give at least 10ms. */
126 ret
= ustcomm_setsockopt_snd_timeout(fd
, timeout
);
128 WARN("Error setting connect socket send timeout");
131 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
138 memset(&sun
, 0, sizeof(sun
));
139 sun
.sun_family
= AF_UNIX
;
140 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
141 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
143 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
146 * Don't print message on connect ENOENT error, because
147 * connect is used in normal execution to detect if
148 * sessiond is alive. ENOENT is when the unix socket
149 * file does not exist, and ECONNREFUSED is when the
150 * file exists but no sessiond is listening.
152 if (errno
!= ECONNREFUSED
&& errno
!= ECONNRESET
153 && errno
!= ENOENT
&& errno
!= EACCES
)
156 if (ret
== -ECONNREFUSED
|| ret
== -ECONNRESET
)
168 closeret
= close(fd
);
177 * ustcomm_accept_unix_sock
179 * Do an accept(2) on the sock and return the
180 * new file descriptor. The socket MUST be bind(2) before.
182 int ustcomm_accept_unix_sock(int sock
)
185 struct sockaddr_un sun
;
189 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
191 if (errno
!= ECONNABORTED
)
194 if (new_fd
== -ECONNABORTED
)
201 * ustcomm_create_unix_sock
203 * Creates a AF_UNIX local socket using pathname
204 * bind the socket upon creation and return the fd.
206 int ustcomm_create_unix_sock(const char *pathname
)
208 struct sockaddr_un sun
;
211 /* Create server socket */
212 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
218 memset(&sun
, 0, sizeof(sun
));
219 sun
.sun_family
= AF_UNIX
;
220 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
221 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
223 /* Unlink the old file if present */
224 (void) unlink(pathname
);
225 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
238 closeret
= close(fd
);
248 * ustcomm_listen_unix_sock
250 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
252 int ustcomm_listen_unix_sock(int sock
)
256 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
266 * ustcomm_close_unix_sock
268 * Shutdown cleanly a unix socket.
270 * Handles fd tracker internally.
272 int ustcomm_close_unix_sock(int sock
)
276 lttng_ust_lock_fd_tracker();
279 lttng_ust_delete_fd_from_tracker(sock
);
284 lttng_ust_unlock_fd_tracker();
290 * ustcomm_recv_unix_sock
292 * Receive data of size len in put that data into
293 * the buf param. Using recvmsg API.
294 * Return the size of received data.
295 * Return 0 on orderly shutdown.
297 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
304 memset(&msg
, 0, sizeof(msg
));
306 iov
[0].iov_base
= buf
;
307 iov
[0].iov_len
= len
;
312 len_last
= iov
[0].iov_len
;
313 ret
= recvmsg(sock
, &msg
, 0);
315 iov
[0].iov_base
+= ret
;
316 iov
[0].iov_len
-= ret
;
317 assert(ret
<= len_last
);
319 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
324 if (errno
!= EPIPE
&& errno
!= ECONNRESET
&& errno
!= ECONNREFUSED
)
327 if (ret
== -ECONNRESET
|| ret
== -ECONNREFUSED
)
330 shutret
= shutdown(sock
, SHUT_RDWR
);
332 ERR("Socket shutdown error");
333 } else if (ret
> 0) {
336 /* ret = 0 means an orderly shutdown. */
342 * ustcomm_send_unix_sock
344 * Send buf data of size len. Using sendmsg API.
345 * Return the size of sent data.
347 ssize_t
ustcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
353 memset(&msg
, 0, sizeof(msg
));
355 iov
[0].iov_base
= (void *) buf
;
356 iov
[0].iov_len
= len
;
361 * Using the MSG_NOSIGNAL when sending data from sessiond to
362 * libust, so libust does not receive an unhandled SIGPIPE or
363 * SIGURG. The sessiond receiver side can be made more resilient
364 * by ignoring SIGPIPE, but we don't have this luxury on the
368 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
369 } while (ret
< 0 && errno
== EINTR
);
374 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
377 if (ret
== -ECONNRESET
)
380 shutret
= shutdown(sock
, SHUT_RDWR
);
382 ERR("Socket shutdown error");
389 * Send a message accompanied by fd(s) over a unix socket.
391 * Returns the size of data sent, or negative error value.
393 ssize_t
ustcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
396 struct cmsghdr
*cmptr
;
399 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
400 char tmp
[CMSG_SPACE(sizeof_fds
)];
403 memset(&msg
, 0, sizeof(msg
));
404 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
406 if (nb_fd
> USTCOMM_MAX_SEND_FDS
)
409 msg
.msg_control
= (caddr_t
)tmp
;
410 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
412 cmptr
= CMSG_FIRSTHDR(&msg
);
415 cmptr
->cmsg_level
= SOL_SOCKET
;
416 cmptr
->cmsg_type
= SCM_RIGHTS
;
417 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
418 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
419 /* Sum of the length of all control messages in the buffer: */
420 msg
.msg_controllen
= cmptr
->cmsg_len
;
422 iov
[0].iov_base
= &dummy
;
428 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
429 } while (ret
< 0 && errno
== EINTR
);
432 * We consider EPIPE and ECONNRESET as expected.
434 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
438 if (ret
== -ECONNRESET
)
445 * Recv a message accompanied by fd(s) from a unix socket.
447 * Expect at most "nb_fd" file descriptors. Returns the number of fd
448 * actually received in nb_fd.
449 * Returns -EPIPE on orderly shutdown.
451 ssize_t
ustcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
455 struct cmsghdr
*cmsg
;
456 size_t sizeof_fds
= nb_fd
* sizeof(int);
457 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
462 memset(&msg
, 0, sizeof(msg
));
464 /* Prepare to receive the structures */
465 iov
[0].iov_base
= &dummy
;
469 msg
.msg_control
= recv_fd
;
470 msg
.msg_controllen
= sizeof(recv_fd
);
473 ret
= recvmsg(sock
, &msg
, 0);
474 } while (ret
< 0 && errno
== EINTR
);
476 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
477 PERROR("recvmsg fds");
480 if (ret
== -ECONNRESET
)
485 /* orderly shutdown */
490 ERR("Error: Received %zd bytes, expected %d\n",
494 if (msg
.msg_flags
& MSG_CTRUNC
) {
495 ERR("Error: Control message truncated.\n");
499 cmsg
= CMSG_FIRSTHDR(&msg
);
501 ERR("Error: Invalid control message header\n");
505 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
506 ERR("Didn't received any fd\n");
510 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
511 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
512 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
517 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
520 for (i
= 0; i
< nb_fd
; i
++) {
521 ret
= fcntl(fds
[i
], F_SETFD
, FD_CLOEXEC
);
523 PERROR("fcntl failed to set FD_CLOEXEC on fd %d",
533 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
537 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
545 ERR("incorrect message size: %zd\n", len
);
552 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
553 uint32_t expected_handle
, uint32_t expected_cmd
)
557 memset(lur
, 0, sizeof(*lur
));
558 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
560 case 0: /* orderly shutdown */
566 if (lur
->handle
!= expected_handle
) {
567 ERR("Unexpected result message handle: "
568 "expected: %u vs received: %u\n",
569 expected_handle
, lur
->handle
);
572 if (lur
->cmd
!= expected_cmd
) {
573 ERR("Unexpected result message command "
574 "expected: %u vs received: %u\n",
575 expected_cmd
, lur
->cmd
);
581 return lur
->ret_code
;
586 ERR("incorrect message size: %zd\n", len
);
592 int ustcomm_send_app_cmd(int sock
,
593 struct ustcomm_ust_msg
*lum
,
594 struct ustcomm_ust_reply
*lur
)
598 ret
= ustcomm_send_app_msg(sock
, lum
);
601 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
608 * chan_data is allocated internally if this function returns the
611 ssize_t
ustcomm_recv_channel_from_sessiond(int sock
,
612 void **_chan_data
, uint64_t var_len
,
619 if (var_len
> LTTNG_UST_CHANNEL_DATA_MAX_LEN
) {
623 /* Receive variable length data */
624 chan_data
= zmalloc(var_len
);
629 len
= ustcomm_recv_unix_sock(sock
, chan_data
, var_len
);
630 if (len
!= var_len
) {
634 lttng_ust_lock_fd_tracker();
635 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
637 lttng_ust_unlock_fd_tracker();
647 ret
= lttng_ust_add_fd_to_tracker(wakeup_fd
);
649 ret
= close(wakeup_fd
);
651 PERROR("close on wakeup_fd");
654 lttng_ust_unlock_fd_tracker();
659 lttng_ust_unlock_fd_tracker();
661 *_chan_data
= chan_data
;
671 ssize_t
ustcomm_recv_event_notifier_notif_fd_from_sessiond(int sock
,
672 int *_event_notifier_notif_fd
)
675 int event_notifier_notif_fd
, ret
;
677 /* Receive event_notifier notification fd */
678 lttng_ust_lock_fd_tracker();
679 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &event_notifier_notif_fd
, 1);
681 lttng_ust_unlock_fd_tracker();
691 ret
= lttng_ust_add_fd_to_tracker(event_notifier_notif_fd
);
693 ret
= close(event_notifier_notif_fd
);
695 PERROR("close on event_notifier notif fd");
698 lttng_ust_unlock_fd_tracker();
702 *_event_notifier_notif_fd
= ret
;
703 lttng_ust_unlock_fd_tracker();
711 int ustcomm_recv_stream_from_sessiond(int sock
,
712 uint64_t *memory_map_size
,
713 int *shm_fd
, int *wakeup_fd
)
719 /* recv shm fd and wakeup fd */
720 lttng_ust_lock_fd_tracker();
721 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
723 lttng_ust_unlock_fd_tracker();
733 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
737 PERROR("close on received shm_fd");
740 lttng_ust_unlock_fd_tracker();
745 ret
= lttng_ust_add_fd_to_tracker(fds
[1]);
747 ret
= close(*shm_fd
);
749 PERROR("close on shm_fd");
754 PERROR("close on received wakeup_fd");
757 lttng_ust_unlock_fd_tracker();
761 lttng_ust_unlock_fd_tracker();
768 ssize_t
ustcomm_recv_counter_from_sessiond(int sock
,
769 void **_counter_data
, uint64_t var_len
)
774 if (var_len
> LTTNG_UST_COUNTER_DATA_MAX_LEN
) {
778 /* Receive variable length data */
779 counter_data
= zmalloc(var_len
);
784 len
= ustcomm_recv_unix_sock(sock
, counter_data
, var_len
);
785 if (len
!= var_len
) {
788 *_counter_data
= counter_data
;
798 int ustcomm_recv_counter_shm_from_sessiond(int sock
,
806 lttng_ust_lock_fd_tracker();
807 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 1);
809 lttng_ust_unlock_fd_tracker();
819 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
823 PERROR("close on received shm_fd");
826 lttng_ust_unlock_fd_tracker();
830 lttng_ust_unlock_fd_tracker();
838 * Returns 0 on success, negative error value on error.
840 int ustcomm_send_reg_msg(int sock
,
841 enum ustctl_socket_type type
,
842 uint32_t bits_per_long
,
843 uint32_t uint8_t_alignment
,
844 uint32_t uint16_t_alignment
,
845 uint32_t uint32_t_alignment
,
846 uint32_t uint64_t_alignment
,
847 uint32_t long_alignment
)
850 struct ustctl_reg_msg reg_msg
;
852 reg_msg
.magic
= LTTNG_UST_COMM_MAGIC
;
853 reg_msg
.major
= LTTNG_UST_ABI_MAJOR_VERSION
;
854 reg_msg
.minor
= LTTNG_UST_ABI_MINOR_VERSION
;
855 reg_msg
.pid
= getpid();
856 reg_msg
.ppid
= getppid();
857 reg_msg
.uid
= getuid();
858 reg_msg
.gid
= getgid();
859 reg_msg
.bits_per_long
= bits_per_long
;
860 reg_msg
.uint8_t_alignment
= uint8_t_alignment
;
861 reg_msg
.uint16_t_alignment
= uint16_t_alignment
;
862 reg_msg
.uint32_t_alignment
= uint32_t_alignment
;
863 reg_msg
.uint64_t_alignment
= uint64_t_alignment
;
864 reg_msg
.long_alignment
= long_alignment
;
865 reg_msg
.socket_type
= type
;
866 lttng_pthread_getname_np(reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
867 memset(reg_msg
.padding
, 0, sizeof(reg_msg
.padding
));
869 len
= ustcomm_send_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
870 if (len
> 0 && len
!= sizeof(reg_msg
))
878 ssize_t
count_one_type(const struct lttng_type
*lt
)
889 return count_fields_recursive(lt
->u
.legacy
._struct
.nr_fields
,
890 lt
->u
.legacy
._struct
.fields
) + 1;
891 case atype_enum_nestable
:
892 return count_one_type(lt
->u
.enum_nestable
.container_type
) + 1;
893 case atype_array_nestable
:
894 return count_one_type(lt
->u
.array_nestable
.elem_type
) + 1;
895 case atype_sequence_nestable
:
896 return count_one_type(lt
->u
.sequence_nestable
.elem_type
) + 1;
897 case atype_struct_nestable
:
898 return count_fields_recursive(lt
->u
.struct_nestable
.nr_fields
,
899 lt
->u
.struct_nestable
.fields
) + 1;
903 const struct lttng_event_field
*choices
;
907 ret
= lttng_ust_dynamic_type_choices(&nr_choices
,
912 * Two fields for enum, one field for variant, and
913 * one field per choice.
915 return count_fields_recursive(nr_choices
, choices
) + 3;
925 ssize_t
count_fields_recursive(size_t nr_fields
,
926 const struct lttng_event_field
*lttng_fields
)
929 ssize_t ret
, count
= 0;
931 for (i
= 0; i
< nr_fields
; i
++) {
932 const struct lttng_event_field
*lf
;
934 lf
= <tng_fields
[i
];
935 /* skip 'nowrite' fields */
938 ret
= count_one_type(&lf
->type
);
940 return ret
; /* error */
947 ssize_t
count_ctx_fields_recursive(size_t nr_fields
,
948 const struct lttng_ctx_field
*lttng_fields
)
951 ssize_t ret
, count
= 0;
953 for (i
= 0; i
< nr_fields
; i
++) {
954 const struct lttng_event_field
*lf
;
956 lf
= <tng_fields
[i
].event_field
;
957 /* skip 'nowrite' fields */
960 ret
= count_one_type(&lf
->type
);
962 return ret
; /* error */
969 int serialize_string_encoding(int32_t *ue
,
970 enum lttng_string_encodings le
)
973 case lttng_encode_none
:
974 *ue
= ustctl_encode_none
;
976 case lttng_encode_UTF8
:
977 *ue
= ustctl_encode_UTF8
;
979 case lttng_encode_ASCII
:
980 *ue
= ustctl_encode_ASCII
;
989 int serialize_integer_type(struct ustctl_integer_type
*uit
,
990 const struct lttng_integer_type
*lit
)
994 uit
->size
= lit
->size
;
995 uit
->signedness
= lit
->signedness
;
996 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
997 uit
->base
= lit
->base
;
998 if (serialize_string_encoding(&encoding
, lit
->encoding
))
1000 uit
->encoding
= encoding
;
1001 uit
->alignment
= lit
->alignment
;
1006 int serialize_basic_type(struct lttng_session
*session
,
1007 enum ustctl_abstract_types
*uatype
,
1008 enum lttng_abstract_types atype
,
1009 union _ustctl_basic_type
*ubt
,
1010 const union _lttng_basic_type
*lbt
)
1015 if (serialize_integer_type(&ubt
->integer
, &lbt
->integer
))
1017 *uatype
= ustctl_atype_integer
;
1024 if (serialize_string_encoding(&encoding
, lbt
->string
.encoding
))
1026 ubt
->string
.encoding
= encoding
;
1027 *uatype
= ustctl_atype_string
;
1032 struct ustctl_float_type
*uft
;
1033 const struct lttng_float_type
*lft
;
1037 uft
->exp_dig
= lft
->exp_dig
;
1038 uft
->mant_dig
= lft
->mant_dig
;
1039 uft
->alignment
= lft
->alignment
;
1040 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
1041 *uatype
= ustctl_atype_float
;
1046 strncpy(ubt
->enumeration
.name
, lbt
->enumeration
.desc
->name
,
1047 LTTNG_UST_SYM_NAME_LEN
);
1048 ubt
->enumeration
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1049 if (serialize_integer_type(&ubt
->enumeration
.container_type
,
1050 &lbt
->enumeration
.container_type
))
1053 const struct lttng_enum
*_enum
;
1055 _enum
= lttng_ust_enum_get_from_desc(session
, lbt
->enumeration
.desc
);
1058 ubt
->enumeration
.id
= _enum
->id
;
1060 ubt
->enumeration
.id
= -1ULL;
1062 *uatype
= ustctl_atype_enum
;
1066 case atype_array_nestable
:
1067 case atype_sequence
:
1068 case atype_sequence_nestable
:
1069 case atype_enum_nestable
:
1077 int serialize_dynamic_type(struct lttng_session
*session
,
1078 struct ustctl_field
*fields
, size_t *iter_output
,
1079 const char *field_name
)
1081 const struct lttng_event_field
*choices
;
1082 char tag_field_name
[LTTNG_UST_SYM_NAME_LEN
];
1083 const struct lttng_type
*tag_type
;
1084 const struct lttng_event_field
*tag_field_generic
;
1085 struct lttng_event_field tag_field
= {
1086 .name
= tag_field_name
,
1089 struct ustctl_field
*uf
;
1090 size_t nr_choices
, i
;
1093 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
1094 tag_type
= &tag_field_generic
->type
;
1096 /* Serialize enum field. */
1097 strncpy(tag_field_name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1098 tag_field_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1099 strncat(tag_field_name
,
1101 LTTNG_UST_SYM_NAME_LEN
- strlen(tag_field_name
) - 1);
1102 tag_field
.type
= *tag_type
;
1103 ret
= serialize_one_field(session
, fields
, iter_output
,
1108 /* Serialize variant field. */
1109 uf
= &fields
[*iter_output
];
1110 ret
= lttng_ust_dynamic_type_choices(&nr_choices
, &choices
);
1114 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1115 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1116 uf
->type
.atype
= ustctl_atype_variant
;
1117 uf
->type
.u
.variant_nestable
.nr_choices
= nr_choices
;
1118 strncpy(uf
->type
.u
.variant_nestable
.tag_name
,
1120 LTTNG_UST_SYM_NAME_LEN
);
1121 uf
->type
.u
.variant_nestable
.tag_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1122 uf
->type
.u
.variant_nestable
.alignment
= 0;
1125 /* Serialize choice fields after variant. */
1126 for (i
= 0; i
< nr_choices
; i
++) {
1127 ret
= serialize_one_field(session
, fields
,
1128 iter_output
, &choices
[i
]);
1136 int serialize_one_type(struct lttng_session
*session
,
1137 struct ustctl_field
*fields
, size_t *iter_output
,
1138 const char *field_name
, const struct lttng_type
*lt
)
1143 * Serializing a type (rather than a field) generates a ustctl_field
1144 * entry with 0-length name.
1147 switch (lt
->atype
) {
1153 struct ustctl_field
*uf
= &fields
[*iter_output
];
1154 struct ustctl_type
*ut
= &uf
->type
;
1155 enum ustctl_abstract_types atype
;
1158 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1159 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1163 ret
= serialize_basic_type(session
, &atype
, lt
->atype
,
1164 &ut
->u
.legacy
.basic
, <
->u
.legacy
.basic
);
1173 struct ustctl_field
*uf
= &fields
[*iter_output
];
1174 struct ustctl_type
*ut
= &uf
->type
;
1175 struct ustctl_basic_type
*ubt
;
1176 const struct lttng_basic_type
*lbt
;
1177 enum ustctl_abstract_types atype
;
1180 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1181 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1185 ut
->atype
= ustctl_atype_array
;
1186 ubt
= &ut
->u
.legacy
.array
.elem_type
;
1187 lbt
= <
->u
.legacy
.array
.elem_type
;
1188 ut
->u
.legacy
.array
.length
= lt
->u
.legacy
.array
.length
;
1189 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1190 &ubt
->u
.basic
, &lbt
->u
.basic
);
1197 case atype_array_nestable
:
1199 struct ustctl_field
*uf
= &fields
[*iter_output
];
1200 struct ustctl_type
*ut
= &uf
->type
;
1203 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1204 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1208 ut
->atype
= ustctl_atype_array_nestable
;
1209 ut
->u
.array_nestable
.length
= lt
->u
.array_nestable
.length
;
1210 ut
->u
.array_nestable
.alignment
= lt
->u
.array_nestable
.alignment
;
1213 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1214 lt
->u
.array_nestable
.elem_type
);
1219 case atype_sequence
:
1221 struct ustctl_field
*uf
= &fields
[*iter_output
];
1222 struct ustctl_type
*ut
= &uf
->type
;
1223 struct ustctl_basic_type
*ubt
;
1224 const struct lttng_basic_type
*lbt
;
1225 enum ustctl_abstract_types atype
;
1228 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1229 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1233 uf
->type
.atype
= ustctl_atype_sequence
;
1234 ubt
= &ut
->u
.legacy
.sequence
.length_type
;
1235 lbt
= <
->u
.legacy
.sequence
.length_type
;
1236 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1237 &ubt
->u
.basic
, &lbt
->u
.basic
);
1241 ubt
= &ut
->u
.legacy
.sequence
.elem_type
;
1242 lbt
= <
->u
.legacy
.sequence
.elem_type
;
1243 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1244 &ubt
->u
.basic
, &lbt
->u
.basic
);
1251 case atype_sequence_nestable
:
1253 struct ustctl_field
*uf
= &fields
[*iter_output
];
1254 struct ustctl_type
*ut
= &uf
->type
;
1257 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1258 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1262 ut
->atype
= ustctl_atype_sequence_nestable
;
1263 strncpy(ut
->u
.sequence_nestable
.length_name
,
1264 lt
->u
.sequence_nestable
.length_name
,
1265 LTTNG_UST_SYM_NAME_LEN
);
1266 ut
->u
.sequence_nestable
.length_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1267 ut
->u
.sequence_nestable
.alignment
= lt
->u
.sequence_nestable
.alignment
;
1270 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1271 lt
->u
.sequence_nestable
.elem_type
);
1278 ret
= serialize_dynamic_type(session
, fields
, iter_output
,
1286 struct ustctl_field
*uf
= &fields
[*iter_output
];
1289 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1290 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1294 uf
->type
.atype
= ustctl_atype_struct
;
1295 uf
->type
.u
.legacy
._struct
.nr_fields
= lt
->u
.legacy
._struct
.nr_fields
;
1298 ret
= serialize_fields(session
, fields
, iter_output
,
1299 lt
->u
.legacy
._struct
.nr_fields
,
1300 lt
->u
.legacy
._struct
.fields
);
1305 case atype_struct_nestable
:
1307 struct ustctl_field
*uf
= &fields
[*iter_output
];
1310 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1311 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1315 uf
->type
.atype
= ustctl_atype_struct_nestable
;
1316 uf
->type
.u
.struct_nestable
.nr_fields
= lt
->u
.struct_nestable
.nr_fields
;
1317 uf
->type
.u
.struct_nestable
.alignment
= lt
->u
.struct_nestable
.alignment
;
1320 ret
= serialize_fields(session
, fields
, iter_output
,
1321 lt
->u
.struct_nestable
.nr_fields
,
1322 lt
->u
.struct_nestable
.fields
);
1327 case atype_enum_nestable
:
1329 struct ustctl_field
*uf
= &fields
[*iter_output
];
1330 struct ustctl_type
*ut
= &uf
->type
;
1333 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1334 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1338 strncpy(ut
->u
.enum_nestable
.name
, lt
->u
.enum_nestable
.desc
->name
,
1339 LTTNG_UST_SYM_NAME_LEN
);
1340 ut
->u
.enum_nestable
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1341 ut
->atype
= ustctl_atype_enum_nestable
;
1344 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1345 lt
->u
.enum_nestable
.container_type
);
1349 const struct lttng_enum
*_enum
;
1351 _enum
= lttng_ust_enum_get_from_desc(session
, lt
->u
.enum_nestable
.desc
);
1354 ut
->u
.enum_nestable
.id
= _enum
->id
;
1356 ut
->u
.enum_nestable
.id
= -1ULL;
1367 int serialize_one_field(struct lttng_session
*session
,
1368 struct ustctl_field
*fields
, size_t *iter_output
,
1369 const struct lttng_event_field
*lf
)
1371 /* skip 'nowrite' fields */
1375 return serialize_one_type(session
, fields
, iter_output
, lf
->name
, &lf
->type
);
1379 int serialize_fields(struct lttng_session
*session
,
1380 struct ustctl_field
*ustctl_fields
,
1381 size_t *iter_output
, size_t nr_lttng_fields
,
1382 const struct lttng_event_field
*lttng_fields
)
1387 for (i
= 0; i
< nr_lttng_fields
; i
++) {
1388 ret
= serialize_one_field(session
, ustctl_fields
,
1389 iter_output
, <tng_fields
[i
]);
1397 int alloc_serialize_fields(struct lttng_session
*session
,
1398 size_t *_nr_write_fields
,
1399 struct ustctl_field
**ustctl_fields
,
1401 const struct lttng_event_field
*lttng_fields
)
1403 struct ustctl_field
*fields
;
1405 size_t iter_output
= 0;
1406 ssize_t nr_write_fields
;
1408 nr_write_fields
= count_fields_recursive(nr_fields
, lttng_fields
);
1409 if (nr_write_fields
< 0) {
1410 return (int) nr_write_fields
;
1413 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1417 ret
= serialize_fields(session
, fields
, &iter_output
, nr_fields
,
1422 *_nr_write_fields
= nr_write_fields
;
1423 *ustctl_fields
= fields
;
1432 int serialize_entries(struct ustctl_enum_entry
**_entries
,
1434 const struct lttng_enum_entry
*lttng_entries
)
1436 struct ustctl_enum_entry
*entries
;
1439 /* Serialize the entries */
1440 entries
= zmalloc(nr_entries
* sizeof(*entries
));
1443 for (i
= 0; i
< nr_entries
; i
++) {
1444 struct ustctl_enum_entry
*uentry
;
1445 const struct lttng_enum_entry
*lentry
;
1447 uentry
= &entries
[i
];
1448 lentry
= <tng_entries
[i
];
1450 uentry
->start
.value
= lentry
->start
.value
;
1451 uentry
->start
.signedness
= lentry
->start
.signedness
;
1452 uentry
->end
.value
= lentry
->end
.value
;
1453 uentry
->end
.signedness
= lentry
->end
.signedness
;
1454 strncpy(uentry
->string
, lentry
->string
, LTTNG_UST_SYM_NAME_LEN
);
1455 uentry
->string
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1457 if (lentry
->u
.extra
.options
& LTTNG_ENUM_ENTRY_OPTION_IS_AUTO
) {
1458 uentry
->u
.extra
.options
|=
1459 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO
;
1462 *_entries
= entries
;
1467 int serialize_ctx_fields(struct lttng_session
*session
,
1468 size_t *_nr_write_fields
,
1469 struct ustctl_field
**ustctl_fields
,
1471 const struct lttng_ctx_field
*lttng_fields
)
1473 struct ustctl_field
*fields
;
1475 size_t i
, iter_output
= 0;
1476 ssize_t nr_write_fields
;
1478 nr_write_fields
= count_ctx_fields_recursive(nr_fields
,
1480 if (nr_write_fields
< 0) {
1481 return (int) nr_write_fields
;
1484 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1488 for (i
= 0; i
< nr_fields
; i
++) {
1489 ret
= serialize_one_field(session
, fields
, &iter_output
,
1490 <tng_fields
[i
].event_field
);
1495 *_nr_write_fields
= nr_write_fields
;
1496 *ustctl_fields
= fields
;
1505 * Returns 0 on success, negative error value on error.
1507 int ustcomm_register_event(int sock
,
1508 struct lttng_session
*session
,
1509 int session_objd
, /* session descriptor */
1510 int channel_objd
, /* channel descriptor */
1511 const char *event_name
, /* event name (input) */
1513 const char *signature
, /* event signature (input) */
1514 size_t nr_fields
, /* fields */
1515 const struct lttng_event_field
*lttng_fields
,
1516 const char *model_emf_uri
,
1517 uint32_t *id
) /* event id (output) */
1521 struct ustcomm_notify_hdr header
;
1522 struct ustcomm_notify_event_msg m
;
1525 struct ustcomm_notify_hdr header
;
1526 struct ustcomm_notify_event_reply r
;
1528 size_t signature_len
, fields_len
, model_emf_uri_len
;
1529 struct ustctl_field
*fields
= NULL
;
1530 size_t nr_write_fields
= 0;
1533 memset(&msg
, 0, sizeof(msg
));
1534 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1535 msg
.m
.session_objd
= session_objd
;
1536 msg
.m
.channel_objd
= channel_objd
;
1537 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_SYM_NAME_LEN
);
1538 msg
.m
.event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1539 msg
.m
.loglevel
= loglevel
;
1540 signature_len
= strlen(signature
) + 1;
1541 msg
.m
.signature_len
= signature_len
;
1543 /* Calculate fields len, serialize fields. */
1544 if (nr_fields
> 0) {
1545 ret
= alloc_serialize_fields(session
, &nr_write_fields
, &fields
,
1546 nr_fields
, lttng_fields
);
1551 fields_len
= sizeof(*fields
) * nr_write_fields
;
1552 msg
.m
.fields_len
= fields_len
;
1553 if (model_emf_uri
) {
1554 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
1556 model_emf_uri_len
= 0;
1558 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
1560 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1561 if (len
> 0 && len
!= sizeof(msg
)) {
1570 /* send signature */
1571 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
1572 if (len
> 0 && len
!= signature_len
) {
1582 if (fields_len
> 0) {
1583 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1584 if (len
> 0 && len
!= fields_len
) {
1595 if (model_emf_uri_len
) {
1596 /* send model_emf_uri */
1597 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
1599 if (len
> 0 && len
!= model_emf_uri_len
) {
1608 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1610 case 0: /* orderly shutdown */
1613 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1614 ERR("Unexpected result message command "
1615 "expected: %u vs received: %u\n",
1616 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1619 if (reply
.r
.ret_code
> 0)
1621 if (reply
.r
.ret_code
< 0)
1622 return reply
.r
.ret_code
;
1623 *id
= reply
.r
.event_id
;
1624 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1625 event_name
, reply
.r
.ret_code
, reply
.r
.event_id
);
1629 /* Transport level error */
1630 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1634 ERR("incorrect message size: %zd\n", len
);
1640 /* Error path only. */
1647 * Returns 0 on success, negative error value on error.
1648 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1650 int ustcomm_register_enum(int sock
,
1651 int session_objd
, /* session descriptor */
1652 const char *enum_name
, /* enum name (input) */
1653 size_t nr_entries
, /* entries */
1654 const struct lttng_enum_entry
*lttng_entries
,
1659 struct ustcomm_notify_hdr header
;
1660 struct ustcomm_notify_enum_msg m
;
1663 struct ustcomm_notify_hdr header
;
1664 struct ustcomm_notify_enum_reply r
;
1667 struct ustctl_enum_entry
*entries
= NULL
;
1670 memset(&msg
, 0, sizeof(msg
));
1671 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1672 msg
.m
.session_objd
= session_objd
;
1673 strncpy(msg
.m
.enum_name
, enum_name
, LTTNG_UST_SYM_NAME_LEN
);
1674 msg
.m
.enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1676 /* Calculate entries len, serialize entries. */
1677 if (nr_entries
> 0) {
1678 ret
= serialize_entries(&entries
,
1679 nr_entries
, lttng_entries
);
1684 entries_len
= sizeof(*entries
) * nr_entries
;
1685 msg
.m
.entries_len
= entries_len
;
1687 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1688 if (len
> 0 && len
!= sizeof(msg
)) {
1698 if (entries_len
> 0) {
1699 len
= ustcomm_send_unix_sock(sock
, entries
, entries_len
);
1700 if (len
> 0 && len
!= entries_len
) {
1713 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1715 case 0: /* orderly shutdown */
1718 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1719 ERR("Unexpected result message command "
1720 "expected: %u vs received: %u\n",
1721 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1724 if (reply
.r
.ret_code
> 0)
1726 if (reply
.r
.ret_code
< 0)
1727 return reply
.r
.ret_code
;
1728 *id
= reply
.r
.enum_id
;
1729 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1730 enum_name
, reply
.r
.ret_code
);
1734 /* Transport level error */
1735 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1739 ERR("incorrect message size: %zd\n", len
);
1751 * Returns 0 on success, negative error value on error.
1752 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1754 int ustcomm_register_channel(int sock
,
1755 struct lttng_session
*session
,
1756 int session_objd
, /* session descriptor */
1757 int channel_objd
, /* channel descriptor */
1758 size_t nr_ctx_fields
,
1759 const struct lttng_ctx_field
*ctx_fields
,
1760 uint32_t *chan_id
, /* channel id (output) */
1761 int *header_type
) /* header type (output) */
1765 struct ustcomm_notify_hdr header
;
1766 struct ustcomm_notify_channel_msg m
;
1769 struct ustcomm_notify_hdr header
;
1770 struct ustcomm_notify_channel_reply r
;
1773 struct ustctl_field
*fields
= NULL
;
1775 size_t nr_write_fields
= 0;
1777 memset(&msg
, 0, sizeof(msg
));
1778 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1779 msg
.m
.session_objd
= session_objd
;
1780 msg
.m
.channel_objd
= channel_objd
;
1782 /* Calculate fields len, serialize fields. */
1783 if (nr_ctx_fields
> 0) {
1784 ret
= serialize_ctx_fields(session
, &nr_write_fields
, &fields
,
1785 nr_ctx_fields
, ctx_fields
);
1790 fields_len
= sizeof(*fields
) * nr_write_fields
;
1791 msg
.m
.ctx_fields_len
= fields_len
;
1792 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1793 if (len
> 0 && len
!= sizeof(msg
)) {
1803 if (fields_len
> 0) {
1804 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1806 if (len
> 0 && len
!= fields_len
) {
1816 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1818 case 0: /* orderly shutdown */
1821 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1822 ERR("Unexpected result message command "
1823 "expected: %u vs received: %u\n",
1824 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1827 if (reply
.r
.ret_code
> 0)
1829 if (reply
.r
.ret_code
< 0)
1830 return reply
.r
.ret_code
;
1831 *chan_id
= reply
.r
.chan_id
;
1832 switch (reply
.r
.header_type
) {
1835 *header_type
= reply
.r
.header_type
;
1838 ERR("Unexpected channel header type %u\n",
1839 reply
.r
.header_type
);
1842 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1843 reply
.r
.chan_id
, reply
.r
.header_type
);
1847 /* Transport level error */
1848 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1852 ERR("incorrect message size: %zd\n", len
);
1859 * Set socket reciving timeout.
1861 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1866 tv
.tv_sec
= msec
/ 1000;
1867 tv
.tv_usec
= (msec
* 1000 % 1000000);
1869 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1871 PERROR("setsockopt SO_RCVTIMEO");
1879 * Set socket sending timeout.
1881 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1886 tv
.tv_sec
= msec
/ 1000;
1887 tv
.tv_usec
= (msec
* 1000) % 1000000;
1889 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1891 PERROR("setsockopt SO_SNDTIMEO");