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>
23 #include "common/ustcomm.h"
24 #include "common/ust-fd.h"
25 #include "common/macros.h"
26 #include "common/dynamic-type.h"
27 #include "common/logging.h"
29 #include "common/events.h"
30 #include "common/compat/pthread.h"
32 #define USTCOMM_MAX_SEND_FDS 4
35 ssize_t
count_fields_recursive(size_t nr_fields
,
36 const struct lttng_ust_event_field
* const *lttng_fields
);
38 int serialize_one_field(struct lttng_ust_session
*session
,
39 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
40 const struct lttng_ust_event_field
*lf
);
42 int serialize_fields(struct lttng_ust_session
*session
,
43 struct lttng_ust_ctl_field
*lttng_ust_ctl_fields
,
44 size_t *iter_output
, size_t nr_lttng_fields
,
45 const struct lttng_ust_event_field
* const *lttng_fields
);
48 * ustcomm_connect_unix_sock
50 * Connect to unix socket using the path name.
52 * Caller handles FD tracker.
54 int ustcomm_connect_unix_sock(const char *pathname
, long timeout
)
56 struct sockaddr_un sun
;
60 * libust threads require the close-on-exec flag for all
61 * resources so it does not leak file descriptors upon exec.
62 * SOCK_CLOEXEC is not used since it is linux specific.
64 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
71 /* Give at least 10ms. */
74 ret
= ustcomm_setsockopt_snd_timeout(fd
, timeout
);
76 WARN("Error setting connect socket send timeout");
79 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
86 memset(&sun
, 0, sizeof(sun
));
87 sun
.sun_family
= AF_UNIX
;
88 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
89 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
91 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
94 * Don't print message on connect ENOENT error, because
95 * connect is used in normal execution to detect if
96 * sessiond is alive. ENOENT is when the unix socket
97 * file does not exist, and ECONNREFUSED is when the
98 * file exists but no sessiond is listening.
100 if (errno
!= ECONNREFUSED
&& errno
!= ECONNRESET
101 && errno
!= ENOENT
&& errno
!= EACCES
)
104 if (ret
== -ECONNREFUSED
|| ret
== -ECONNRESET
)
116 closeret
= close(fd
);
125 * ustcomm_accept_unix_sock
127 * Do an accept(2) on the sock and return the
128 * new file descriptor. The socket MUST be bind(2) before.
130 int ustcomm_accept_unix_sock(int sock
)
133 struct sockaddr_un sun
;
137 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
139 if (errno
!= ECONNABORTED
)
142 if (new_fd
== -ECONNABORTED
)
149 * ustcomm_create_unix_sock
151 * Creates a AF_UNIX local socket using pathname
152 * bind the socket upon creation and return the fd.
154 int ustcomm_create_unix_sock(const char *pathname
)
156 struct sockaddr_un sun
;
159 /* Create server socket */
160 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
166 memset(&sun
, 0, sizeof(sun
));
167 sun
.sun_family
= AF_UNIX
;
168 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
169 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
171 /* Unlink the old file if present */
172 (void) unlink(pathname
);
173 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
186 closeret
= close(fd
);
196 * ustcomm_listen_unix_sock
198 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
200 int ustcomm_listen_unix_sock(int sock
)
204 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
214 * ustcomm_close_unix_sock
216 * Shutdown cleanly a unix socket.
218 * Handles fd tracker internally.
220 int ustcomm_close_unix_sock(int sock
)
224 lttng_ust_lock_fd_tracker();
227 lttng_ust_delete_fd_from_tracker(sock
);
232 lttng_ust_unlock_fd_tracker();
238 * ustcomm_recv_unix_sock
240 * Receive data of size len in put that data into
241 * the buf param. Using recvmsg API.
242 * Return the size of received data.
243 * Return 0 on orderly shutdown.
245 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
252 memset(&msg
, 0, sizeof(msg
));
254 iov
[0].iov_base
= buf
;
255 iov
[0].iov_len
= len
;
260 len_last
= iov
[0].iov_len
;
261 ret
= recvmsg(sock
, &msg
, 0);
263 iov
[0].iov_base
+= ret
;
264 iov
[0].iov_len
-= ret
;
265 assert(ret
<= len_last
);
267 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
272 if (errno
!= EPIPE
&& errno
!= ECONNRESET
&& errno
!= ECONNREFUSED
)
275 if (ret
== -ECONNRESET
|| ret
== -ECONNREFUSED
)
278 shutret
= shutdown(sock
, SHUT_RDWR
);
280 ERR("Socket shutdown error");
281 } else if (ret
> 0) {
284 /* ret = 0 means an orderly shutdown. */
290 * ustcomm_send_unix_sock
292 * Send buf data of size len. Using sendmsg API.
293 * Return the size of sent data.
295 ssize_t
ustcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
301 memset(&msg
, 0, sizeof(msg
));
303 iov
[0].iov_base
= (void *) buf
;
304 iov
[0].iov_len
= len
;
309 * Using the MSG_NOSIGNAL when sending data from sessiond to
310 * libust, so libust does not receive an unhandled SIGPIPE or
311 * SIGURG. The sessiond receiver side can be made more resilient
312 * by ignoring SIGPIPE, but we don't have this luxury on the
316 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
317 } while (ret
< 0 && errno
== EINTR
);
322 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
325 if (ret
== -ECONNRESET
)
328 shutret
= shutdown(sock
, SHUT_RDWR
);
330 ERR("Socket shutdown error");
337 * Send a message accompanied by fd(s) over a unix socket.
339 * Returns the size of data sent, or negative error value.
341 ssize_t
ustcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
344 struct cmsghdr
*cmptr
;
347 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
348 char tmp
[CMSG_SPACE(sizeof_fds
)];
351 memset(&msg
, 0, sizeof(msg
));
352 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
354 if (nb_fd
> USTCOMM_MAX_SEND_FDS
)
357 msg
.msg_control
= (caddr_t
)tmp
;
358 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
360 cmptr
= CMSG_FIRSTHDR(&msg
);
363 cmptr
->cmsg_level
= SOL_SOCKET
;
364 cmptr
->cmsg_type
= SCM_RIGHTS
;
365 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
366 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
367 /* Sum of the length of all control messages in the buffer: */
368 msg
.msg_controllen
= cmptr
->cmsg_len
;
370 iov
[0].iov_base
= &dummy
;
376 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
377 } while (ret
< 0 && errno
== EINTR
);
380 * We consider EPIPE and ECONNRESET as expected.
382 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
386 if (ret
== -ECONNRESET
)
393 * Recv a message accompanied by fd(s) from a unix socket.
395 * Expect at most "nb_fd" file descriptors. Returns the number of fd
396 * actually received in nb_fd.
397 * Returns -EPIPE on orderly shutdown.
399 ssize_t
ustcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
403 struct cmsghdr
*cmsg
;
404 size_t sizeof_fds
= nb_fd
* sizeof(int);
405 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
410 memset(&msg
, 0, sizeof(msg
));
412 /* Prepare to receive the structures */
413 iov
[0].iov_base
= &dummy
;
417 msg
.msg_control
= recv_fd
;
418 msg
.msg_controllen
= sizeof(recv_fd
);
421 ret
= recvmsg(sock
, &msg
, 0);
422 } while (ret
< 0 && errno
== EINTR
);
424 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
425 PERROR("recvmsg fds");
428 if (ret
== -ECONNRESET
)
433 /* orderly shutdown */
438 ERR("Error: Received %zd bytes, expected %d\n",
442 if (msg
.msg_flags
& MSG_CTRUNC
) {
443 ERR("Error: Control message truncated.\n");
447 cmsg
= CMSG_FIRSTHDR(&msg
);
449 ERR("Error: Invalid control message header\n");
453 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
454 ERR("Didn't received any fd\n");
458 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
459 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
460 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
465 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
468 for (i
= 0; i
< nb_fd
; i
++) {
469 ret
= fcntl(fds
[i
], F_SETFD
, FD_CLOEXEC
);
471 PERROR("fcntl failed to set FD_CLOEXEC on fd %d",
481 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
485 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
493 ERR("incorrect message size: %zd\n", len
);
500 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
501 uint32_t expected_handle
, uint32_t expected_cmd
)
505 memset(lur
, 0, sizeof(*lur
));
506 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
508 case 0: /* orderly shutdown */
514 if (lur
->handle
!= expected_handle
) {
515 ERR("Unexpected result message handle: "
516 "expected: %u vs received: %u\n",
517 expected_handle
, lur
->handle
);
520 if (lur
->cmd
!= expected_cmd
) {
521 ERR("Unexpected result message command "
522 "expected: %u vs received: %u\n",
523 expected_cmd
, lur
->cmd
);
529 return lur
->ret_code
;
534 ERR("incorrect message size: %zd\n", len
);
540 int ustcomm_send_app_cmd(int sock
,
541 struct ustcomm_ust_msg
*lum
,
542 struct ustcomm_ust_reply
*lur
)
546 ret
= ustcomm_send_app_msg(sock
, lum
);
549 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
556 * chan_data is allocated internally if this function returns the
559 ssize_t
ustcomm_recv_channel_from_sessiond(int sock
,
560 void **_chan_data
, uint64_t var_len
,
567 if (var_len
> LTTNG_UST_ABI_CHANNEL_DATA_MAX_LEN
) {
571 /* Receive variable length data */
572 chan_data
= zmalloc(var_len
);
577 len
= ustcomm_recv_unix_sock(sock
, chan_data
, var_len
);
578 if (len
!= var_len
) {
582 lttng_ust_lock_fd_tracker();
583 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
585 lttng_ust_unlock_fd_tracker();
595 ret
= lttng_ust_add_fd_to_tracker(wakeup_fd
);
597 ret
= close(wakeup_fd
);
599 PERROR("close on wakeup_fd");
602 lttng_ust_unlock_fd_tracker();
607 lttng_ust_unlock_fd_tracker();
609 *_chan_data
= chan_data
;
619 ssize_t
ustcomm_recv_event_notifier_notif_fd_from_sessiond(int sock
,
620 int *_event_notifier_notif_fd
)
623 int event_notifier_notif_fd
, ret
;
625 /* Receive event_notifier notification fd */
626 lttng_ust_lock_fd_tracker();
627 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &event_notifier_notif_fd
, 1);
629 lttng_ust_unlock_fd_tracker();
639 ret
= lttng_ust_add_fd_to_tracker(event_notifier_notif_fd
);
641 ret
= close(event_notifier_notif_fd
);
643 PERROR("close on event_notifier notif fd");
646 lttng_ust_unlock_fd_tracker();
650 *_event_notifier_notif_fd
= ret
;
651 lttng_ust_unlock_fd_tracker();
659 int ustcomm_recv_stream_from_sessiond(int sock
,
660 uint64_t *memory_map_size
__attribute__((unused
)),
661 int *shm_fd
, int *wakeup_fd
)
667 /* recv shm fd and wakeup fd */
668 lttng_ust_lock_fd_tracker();
669 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
671 lttng_ust_unlock_fd_tracker();
681 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
685 PERROR("close on received shm_fd");
688 lttng_ust_unlock_fd_tracker();
693 ret
= lttng_ust_add_fd_to_tracker(fds
[1]);
695 ret
= close(*shm_fd
);
697 PERROR("close on shm_fd");
702 PERROR("close on received wakeup_fd");
705 lttng_ust_unlock_fd_tracker();
709 lttng_ust_unlock_fd_tracker();
716 ssize_t
ustcomm_recv_counter_from_sessiond(int sock
,
717 void **_counter_data
, uint64_t var_len
)
722 if (var_len
> LTTNG_UST_ABI_COUNTER_DATA_MAX_LEN
) {
726 /* Receive variable length data */
727 counter_data
= zmalloc(var_len
);
732 len
= ustcomm_recv_unix_sock(sock
, counter_data
, var_len
);
733 if (len
!= var_len
) {
736 *_counter_data
= counter_data
;
746 int ustcomm_recv_counter_shm_from_sessiond(int sock
,
754 lttng_ust_lock_fd_tracker();
755 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 1);
757 lttng_ust_unlock_fd_tracker();
767 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
771 PERROR("close on received shm_fd");
774 lttng_ust_unlock_fd_tracker();
778 lttng_ust_unlock_fd_tracker();
786 * Returns 0 on success, negative error value on error.
788 int ustcomm_send_reg_msg(int sock
,
789 enum lttng_ust_ctl_socket_type type
,
790 uint32_t bits_per_long
,
791 uint32_t uint8_t_alignment
,
792 uint32_t uint16_t_alignment
,
793 uint32_t uint32_t_alignment
,
794 uint32_t uint64_t_alignment
,
795 uint32_t long_alignment
)
798 struct lttng_ust_ctl_reg_msg reg_msg
;
800 reg_msg
.magic
= LTTNG_UST_ABI_COMM_MAGIC
;
801 reg_msg
.major
= LTTNG_UST_ABI_MAJOR_VERSION
;
802 reg_msg
.minor
= LTTNG_UST_ABI_MINOR_VERSION
;
803 reg_msg
.pid
= getpid();
804 reg_msg
.ppid
= getppid();
805 reg_msg
.uid
= getuid();
806 reg_msg
.gid
= getgid();
807 reg_msg
.bits_per_long
= bits_per_long
;
808 reg_msg
.uint8_t_alignment
= uint8_t_alignment
;
809 reg_msg
.uint16_t_alignment
= uint16_t_alignment
;
810 reg_msg
.uint32_t_alignment
= uint32_t_alignment
;
811 reg_msg
.uint64_t_alignment
= uint64_t_alignment
;
812 reg_msg
.long_alignment
= long_alignment
;
813 reg_msg
.socket_type
= type
;
814 lttng_pthread_getname_np(reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
815 memset(reg_msg
.padding
, 0, sizeof(reg_msg
.padding
));
817 len
= ustcomm_send_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
818 if (len
> 0 && len
!= sizeof(reg_msg
))
826 ssize_t
count_one_type(const struct lttng_ust_type_common
*lt
)
829 case lttng_ust_type_integer
:
830 case lttng_ust_type_float
:
831 case lttng_ust_type_string
:
833 case lttng_ust_type_enum
:
834 return count_one_type(lttng_ust_get_type_enum(lt
)->container_type
) + 1;
835 case lttng_ust_type_array
:
836 return count_one_type(lttng_ust_get_type_array(lt
)->elem_type
) + 1;
837 case lttng_ust_type_sequence
:
838 return count_one_type(lttng_ust_get_type_sequence(lt
)->elem_type
) + 1;
839 case lttng_ust_type_struct
:
840 return count_fields_recursive(lttng_ust_get_type_struct(lt
)->nr_fields
,
841 lttng_ust_get_type_struct(lt
)->fields
) + 1;
843 case lttng_ust_type_dynamic
:
845 const struct lttng_ust_event_field
* const *choices
;
849 ret
= lttng_ust_dynamic_type_choices(&nr_choices
,
854 * Two fields for enum, one field for variant, and
855 * one field per choice.
857 return count_fields_recursive(nr_choices
, choices
) + 3;
867 ssize_t
count_fields_recursive(size_t nr_fields
,
868 const struct lttng_ust_event_field
* const *lttng_fields
)
871 ssize_t ret
, count
= 0;
873 for (i
= 0; i
< nr_fields
; i
++) {
874 const struct lttng_ust_event_field
*lf
;
876 lf
= lttng_fields
[i
];
877 /* skip 'nowrite' fields */
880 ret
= count_one_type(lf
->type
);
882 return ret
; /* error */
889 ssize_t
count_ctx_fields_recursive(size_t nr_fields
,
890 struct lttng_ust_ctx_field
*lttng_fields
)
893 ssize_t ret
, count
= 0;
895 for (i
= 0; i
< nr_fields
; i
++) {
896 const struct lttng_ust_event_field
*lf
;
898 lf
= lttng_fields
[i
].event_field
;
899 /* skip 'nowrite' fields */
902 ret
= count_one_type(lf
->type
);
904 return ret
; /* error */
911 int serialize_string_encoding(int32_t *ue
,
912 enum lttng_ust_string_encoding le
)
915 case lttng_ust_string_encoding_none
:
916 *ue
= lttng_ust_ctl_encode_none
;
918 case lttng_ust_string_encoding_UTF8
:
919 *ue
= lttng_ust_ctl_encode_UTF8
;
921 case lttng_ust_string_encoding_ASCII
:
922 *ue
= lttng_ust_ctl_encode_ASCII
;
931 int serialize_integer_type(struct lttng_ust_ctl_integer_type
*uit
,
932 const struct lttng_ust_type_integer
*lit
,
933 enum lttng_ust_string_encoding lencoding
)
937 uit
->size
= lit
->size
;
938 uit
->signedness
= lit
->signedness
;
939 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
940 uit
->base
= lit
->base
;
941 if (serialize_string_encoding(&encoding
, lencoding
))
943 uit
->encoding
= encoding
;
944 uit
->alignment
= lit
->alignment
;
949 int serialize_dynamic_type(struct lttng_ust_session
*session
,
950 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
951 const char *field_name
)
953 const struct lttng_ust_event_field
* const *choices
;
954 char tag_field_name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
955 const struct lttng_ust_type_common
*tag_type
;
956 const struct lttng_ust_event_field
*tag_field_generic
;
957 struct lttng_ust_event_field tag_field
= {
958 .name
= tag_field_name
,
961 struct lttng_ust_ctl_field
*uf
;
962 size_t nr_choices
, i
;
965 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
966 tag_type
= tag_field_generic
->type
;
968 /* Serialize enum field. */
969 strncpy(tag_field_name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
970 tag_field_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
971 strncat(tag_field_name
,
973 LTTNG_UST_ABI_SYM_NAME_LEN
- strlen(tag_field_name
) - 1);
974 tag_field
.type
= tag_type
;
975 ret
= serialize_one_field(session
, fields
, iter_output
,
980 /* Serialize variant field. */
981 uf
= &fields
[*iter_output
];
982 ret
= lttng_ust_dynamic_type_choices(&nr_choices
, &choices
);
986 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
987 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
988 uf
->type
.atype
= lttng_ust_ctl_atype_variant
;
989 uf
->type
.u
.variant_nestable
.nr_choices
= nr_choices
;
990 strncpy(uf
->type
.u
.variant_nestable
.tag_name
,
992 LTTNG_UST_ABI_SYM_NAME_LEN
);
993 uf
->type
.u
.variant_nestable
.tag_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
994 uf
->type
.u
.variant_nestable
.alignment
= 0;
997 /* Serialize choice fields after variant. */
998 for (i
= 0; i
< nr_choices
; i
++) {
999 ret
= serialize_one_field(session
, fields
,
1000 iter_output
, choices
[i
]);
1008 int serialize_one_type(struct lttng_ust_session
*session
,
1009 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
1010 const char *field_name
, const struct lttng_ust_type_common
*lt
,
1011 enum lttng_ust_string_encoding parent_encoding
)
1016 * Serializing a type (rather than a field) generates a lttng_ust_ctl_field
1017 * entry with 0-length name.
1021 case lttng_ust_type_integer
:
1023 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1024 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1027 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1028 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1032 ret
= serialize_integer_type(&ut
->u
.integer
, lttng_ust_get_type_integer(lt
),
1036 ut
->atype
= lttng_ust_ctl_atype_integer
;
1040 case lttng_ust_type_float
:
1042 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1043 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1044 struct lttng_ust_ctl_float_type
*uft
;
1045 const struct lttng_ust_type_float
*lft
;
1048 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1049 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1053 uft
= &ut
->u
._float
;
1054 lft
= lttng_ust_get_type_float(lt
);
1055 uft
->exp_dig
= lft
->exp_dig
;
1056 uft
->mant_dig
= lft
->mant_dig
;
1057 uft
->alignment
= lft
->alignment
;
1058 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
1059 ut
->atype
= lttng_ust_ctl_atype_float
;
1063 case lttng_ust_type_string
:
1065 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1066 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1070 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1071 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1075 ret
= serialize_string_encoding(&encoding
, lttng_ust_get_type_string(lt
)->encoding
);
1078 ut
->u
.string
.encoding
= encoding
;
1079 ut
->atype
= lttng_ust_ctl_atype_string
;
1083 case lttng_ust_type_array
:
1085 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1086 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1089 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1090 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1094 ut
->atype
= lttng_ust_ctl_atype_array_nestable
;
1095 ut
->u
.array_nestable
.length
= lttng_ust_get_type_array(lt
)->length
;
1096 ut
->u
.array_nestable
.alignment
= lttng_ust_get_type_array(lt
)->alignment
;
1099 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1100 lttng_ust_get_type_array(lt
)->elem_type
,
1101 lttng_ust_get_type_array(lt
)->encoding
);
1106 case lttng_ust_type_sequence
:
1108 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1109 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1112 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1113 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1117 ut
->atype
= lttng_ust_ctl_atype_sequence_nestable
;
1118 strncpy(ut
->u
.sequence_nestable
.length_name
,
1119 lttng_ust_get_type_sequence(lt
)->length_name
,
1120 LTTNG_UST_ABI_SYM_NAME_LEN
);
1121 ut
->u
.sequence_nestable
.length_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1122 ut
->u
.sequence_nestable
.alignment
= lttng_ust_get_type_sequence(lt
)->alignment
;
1125 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1126 lttng_ust_get_type_sequence(lt
)->elem_type
,
1127 lttng_ust_get_type_sequence(lt
)->encoding
);
1132 case lttng_ust_type_dynamic
:
1134 ret
= serialize_dynamic_type(session
, fields
, iter_output
,
1140 case lttng_ust_type_struct
:
1142 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1145 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1146 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1150 uf
->type
.atype
= lttng_ust_ctl_atype_struct_nestable
;
1151 uf
->type
.u
.struct_nestable
.nr_fields
= lttng_ust_get_type_struct(lt
)->nr_fields
;
1152 uf
->type
.u
.struct_nestable
.alignment
= lttng_ust_get_type_struct(lt
)->alignment
;
1155 ret
= serialize_fields(session
, fields
, iter_output
,
1156 lttng_ust_get_type_struct(lt
)->nr_fields
,
1157 lttng_ust_get_type_struct(lt
)->fields
);
1162 case lttng_ust_type_enum
:
1164 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1165 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1168 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1169 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1173 strncpy(ut
->u
.enum_nestable
.name
, lttng_ust_get_type_enum(lt
)->desc
->name
,
1174 LTTNG_UST_ABI_SYM_NAME_LEN
);
1175 ut
->u
.enum_nestable
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1176 ut
->atype
= lttng_ust_ctl_atype_enum_nestable
;
1179 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1180 lttng_ust_get_type_enum(lt
)->container_type
,
1181 lttng_ust_string_encoding_none
);
1185 const struct lttng_enum
*_enum
;
1187 _enum
= lttng_ust_enum_get_from_desc(session
, lttng_ust_get_type_enum(lt
)->desc
);
1190 ut
->u
.enum_nestable
.id
= _enum
->id
;
1192 ut
->u
.enum_nestable
.id
= -1ULL;
1203 int serialize_one_field(struct lttng_ust_session
*session
,
1204 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
1205 const struct lttng_ust_event_field
*lf
)
1207 /* skip 'nowrite' fields */
1211 return serialize_one_type(session
, fields
, iter_output
, lf
->name
, lf
->type
, lttng_ust_string_encoding_none
);
1215 int serialize_fields(struct lttng_ust_session
*session
,
1216 struct lttng_ust_ctl_field
*lttng_ust_ctl_fields
,
1217 size_t *iter_output
, size_t nr_lttng_fields
,
1218 const struct lttng_ust_event_field
* const *lttng_fields
)
1223 for (i
= 0; i
< nr_lttng_fields
; i
++) {
1224 ret
= serialize_one_field(session
, lttng_ust_ctl_fields
,
1225 iter_output
, lttng_fields
[i
]);
1233 int alloc_serialize_fields(struct lttng_ust_session
*session
,
1234 size_t *_nr_write_fields
,
1235 struct lttng_ust_ctl_field
**lttng_ust_ctl_fields
,
1237 const struct lttng_ust_event_field
* const *lttng_fields
)
1239 struct lttng_ust_ctl_field
*fields
;
1241 size_t iter_output
= 0;
1242 ssize_t nr_write_fields
;
1244 nr_write_fields
= count_fields_recursive(nr_fields
, lttng_fields
);
1245 if (nr_write_fields
< 0) {
1246 return (int) nr_write_fields
;
1249 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1253 ret
= serialize_fields(session
, fields
, &iter_output
, nr_fields
,
1258 *_nr_write_fields
= nr_write_fields
;
1259 *lttng_ust_ctl_fields
= fields
;
1268 int serialize_entries(struct lttng_ust_ctl_enum_entry
**_entries
,
1270 const struct lttng_ust_enum_entry
* const *lttng_entries
)
1272 struct lttng_ust_ctl_enum_entry
*entries
;
1275 /* Serialize the entries */
1276 entries
= zmalloc(nr_entries
* sizeof(*entries
));
1279 for (i
= 0; i
< nr_entries
; i
++) {
1280 struct lttng_ust_ctl_enum_entry
*uentry
;
1281 const struct lttng_ust_enum_entry
*lentry
;
1283 uentry
= &entries
[i
];
1284 lentry
= lttng_entries
[i
];
1286 uentry
->start
.value
= lentry
->start
.value
;
1287 uentry
->start
.signedness
= lentry
->start
.signedness
;
1288 uentry
->end
.value
= lentry
->end
.value
;
1289 uentry
->end
.signedness
= lentry
->end
.signedness
;
1290 strncpy(uentry
->string
, lentry
->string
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1291 uentry
->string
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1293 if (lentry
->options
& LTTNG_UST_ENUM_ENTRY_OPTION_IS_AUTO
) {
1294 uentry
->u
.extra
.options
|=
1295 LTTNG_UST_CTL_UST_ENUM_ENTRY_OPTION_IS_AUTO
;
1298 *_entries
= entries
;
1303 int serialize_ctx_fields(struct lttng_ust_session
*session
,
1304 size_t *_nr_write_fields
,
1305 struct lttng_ust_ctl_field
**lttng_ust_ctl_fields
,
1307 struct lttng_ust_ctx_field
*lttng_fields
)
1309 struct lttng_ust_ctl_field
*fields
;
1311 size_t i
, iter_output
= 0;
1312 ssize_t nr_write_fields
;
1314 nr_write_fields
= count_ctx_fields_recursive(nr_fields
,
1316 if (nr_write_fields
< 0) {
1317 return (int) nr_write_fields
;
1320 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1324 for (i
= 0; i
< nr_fields
; i
++) {
1325 ret
= serialize_one_field(session
, fields
, &iter_output
,
1326 lttng_fields
[i
].event_field
);
1331 *_nr_write_fields
= nr_write_fields
;
1332 *lttng_ust_ctl_fields
= fields
;
1341 * Returns 0 on success, negative error value on error.
1343 int ustcomm_register_event(int sock
,
1344 struct lttng_ust_session
*session
,
1345 int session_objd
, /* session descriptor */
1346 int channel_objd
, /* channel descriptor */
1347 const char *event_name
, /* event name (input) */
1349 const char *signature
, /* event signature (input) */
1350 size_t nr_fields
, /* fields */
1351 const struct lttng_ust_event_field
* const *lttng_fields
,
1352 const char *model_emf_uri
,
1353 uint32_t *id
) /* event id (output) */
1357 struct ustcomm_notify_hdr header
;
1358 struct ustcomm_notify_event_msg m
;
1361 struct ustcomm_notify_hdr header
;
1362 struct ustcomm_notify_event_reply r
;
1364 size_t signature_len
, fields_len
, model_emf_uri_len
;
1365 struct lttng_ust_ctl_field
*fields
= NULL
;
1366 size_t nr_write_fields
= 0;
1369 memset(&msg
, 0, sizeof(msg
));
1370 msg
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_EVENT
;
1371 msg
.m
.session_objd
= session_objd
;
1372 msg
.m
.channel_objd
= channel_objd
;
1373 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1374 msg
.m
.event_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1375 msg
.m
.loglevel
= loglevel
;
1376 signature_len
= strlen(signature
) + 1;
1377 msg
.m
.signature_len
= signature_len
;
1379 /* Calculate fields len, serialize fields. */
1380 if (nr_fields
> 0) {
1381 ret
= alloc_serialize_fields(session
, &nr_write_fields
, &fields
,
1382 nr_fields
, lttng_fields
);
1387 fields_len
= sizeof(*fields
) * nr_write_fields
;
1388 msg
.m
.fields_len
= fields_len
;
1389 if (model_emf_uri
) {
1390 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
1392 model_emf_uri_len
= 0;
1394 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
1396 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1397 if (len
> 0 && len
!= sizeof(msg
)) {
1406 /* send signature */
1407 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
1408 if (len
> 0 && len
!= signature_len
) {
1418 if (fields_len
> 0) {
1419 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1420 if (len
> 0 && len
!= fields_len
) {
1431 if (model_emf_uri_len
) {
1432 /* send model_emf_uri */
1433 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
1435 if (len
> 0 && len
!= model_emf_uri_len
) {
1444 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1446 case 0: /* orderly shutdown */
1449 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1450 ERR("Unexpected result message command "
1451 "expected: %u vs received: %u\n",
1452 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1455 if (reply
.r
.ret_code
> 0)
1457 if (reply
.r
.ret_code
< 0)
1458 return reply
.r
.ret_code
;
1459 *id
= reply
.r
.event_id
;
1460 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1461 event_name
, reply
.r
.ret_code
, reply
.r
.event_id
);
1465 /* Transport level error */
1466 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1470 ERR("incorrect message size: %zd\n", len
);
1476 /* Error path only. */
1483 * Returns 0 on success, negative error value on error.
1484 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1486 int ustcomm_register_enum(int sock
,
1487 int session_objd
, /* session descriptor */
1488 const char *enum_name
, /* enum name (input) */
1489 size_t nr_entries
, /* entries */
1490 const struct lttng_ust_enum_entry
* const *lttng_entries
,
1495 struct ustcomm_notify_hdr header
;
1496 struct ustcomm_notify_enum_msg m
;
1499 struct ustcomm_notify_hdr header
;
1500 struct ustcomm_notify_enum_reply r
;
1503 struct lttng_ust_ctl_enum_entry
*entries
= NULL
;
1506 memset(&msg
, 0, sizeof(msg
));
1507 msg
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_ENUM
;
1508 msg
.m
.session_objd
= session_objd
;
1509 strncpy(msg
.m
.enum_name
, enum_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1510 msg
.m
.enum_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1512 /* Calculate entries len, serialize entries. */
1513 if (nr_entries
> 0) {
1514 ret
= serialize_entries(&entries
,
1515 nr_entries
, lttng_entries
);
1520 entries_len
= sizeof(*entries
) * nr_entries
;
1521 msg
.m
.entries_len
= entries_len
;
1523 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1524 if (len
> 0 && len
!= sizeof(msg
)) {
1534 if (entries_len
> 0) {
1535 len
= ustcomm_send_unix_sock(sock
, entries
, entries_len
);
1536 if (len
> 0 && len
!= entries_len
) {
1549 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1551 case 0: /* orderly shutdown */
1554 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1555 ERR("Unexpected result message command "
1556 "expected: %u vs received: %u\n",
1557 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1560 if (reply
.r
.ret_code
> 0)
1562 if (reply
.r
.ret_code
< 0)
1563 return reply
.r
.ret_code
;
1564 *id
= reply
.r
.enum_id
;
1565 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1566 enum_name
, reply
.r
.ret_code
);
1570 /* Transport level error */
1571 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1575 ERR("incorrect message size: %zd\n", len
);
1587 * Returns 0 on success, negative error value on error.
1588 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1590 int ustcomm_register_channel(int sock
,
1591 struct lttng_ust_session
*session
,
1592 int session_objd
, /* session descriptor */
1593 int channel_objd
, /* channel descriptor */
1594 size_t nr_ctx_fields
,
1595 struct lttng_ust_ctx_field
*ctx_fields
,
1596 uint32_t *chan_id
, /* channel id (output) */
1597 int *header_type
) /* header type (output) */
1601 struct ustcomm_notify_hdr header
;
1602 struct ustcomm_notify_channel_msg m
;
1605 struct ustcomm_notify_hdr header
;
1606 struct ustcomm_notify_channel_reply r
;
1609 struct lttng_ust_ctl_field
*fields
= NULL
;
1611 size_t nr_write_fields
= 0;
1613 memset(&msg
, 0, sizeof(msg
));
1614 msg
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
;
1615 msg
.m
.session_objd
= session_objd
;
1616 msg
.m
.channel_objd
= channel_objd
;
1618 /* Calculate fields len, serialize fields. */
1619 if (nr_ctx_fields
> 0) {
1620 ret
= serialize_ctx_fields(session
, &nr_write_fields
, &fields
,
1621 nr_ctx_fields
, ctx_fields
);
1626 fields_len
= sizeof(*fields
) * nr_write_fields
;
1627 msg
.m
.ctx_fields_len
= fields_len
;
1628 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1629 if (len
> 0 && len
!= sizeof(msg
)) {
1639 if (fields_len
> 0) {
1640 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1642 if (len
> 0 && len
!= fields_len
) {
1652 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1654 case 0: /* orderly shutdown */
1657 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1658 ERR("Unexpected result message command "
1659 "expected: %u vs received: %u\n",
1660 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1663 if (reply
.r
.ret_code
> 0)
1665 if (reply
.r
.ret_code
< 0)
1666 return reply
.r
.ret_code
;
1667 *chan_id
= reply
.r
.chan_id
;
1668 switch (reply
.r
.header_type
) {
1671 *header_type
= reply
.r
.header_type
;
1674 ERR("Unexpected channel header type %u\n",
1675 reply
.r
.header_type
);
1678 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1679 reply
.r
.chan_id
, reply
.r
.header_type
);
1683 /* Transport level error */
1684 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1688 ERR("incorrect message size: %zd\n", len
);
1695 * Set socket reciving timeout.
1697 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1702 tv
.tv_sec
= msec
/ 1000;
1703 tv
.tv_usec
= (msec
* 1000 % 1000000);
1705 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1707 PERROR("setsockopt SO_RCVTIMEO");
1715 * Set socket sending timeout.
1717 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1722 tv
.tv_sec
= msec
/ 1000;
1723 tv
.tv_usec
= (msec
* 1000) % 1000000;
1725 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1727 PERROR("setsockopt SO_SNDTIMEO");