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
,
41 const char **prev_field_name
);
43 int serialize_fields(struct lttng_ust_session
*session
,
44 struct lttng_ust_ctl_field
*lttng_ust_ctl_fields
,
45 size_t *iter_output
, size_t nr_lttng_fields
,
46 const struct lttng_ust_event_field
* const *lttng_fields
);
49 * ustcomm_connect_unix_sock
51 * Connect to unix socket using the path name.
53 * Caller handles FD tracker.
55 int ustcomm_connect_unix_sock(const char *pathname
, long timeout
)
57 struct sockaddr_un sun
;
61 * libust threads require the close-on-exec flag for all
62 * resources so it does not leak file descriptors upon exec.
63 * SOCK_CLOEXEC is not used since it is linux specific.
65 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
72 /* Give at least 10ms. */
75 ret
= ustcomm_setsockopt_snd_timeout(fd
, timeout
);
77 WARN("Error setting connect socket send timeout");
80 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
87 memset(&sun
, 0, sizeof(sun
));
88 sun
.sun_family
= AF_UNIX
;
89 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
90 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
92 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
95 * Don't print message on connect ENOENT error, because
96 * connect is used in normal execution to detect if
97 * sessiond is alive. ENOENT is when the unix socket
98 * file does not exist, and ECONNREFUSED is when the
99 * file exists but no sessiond is listening.
101 if (errno
!= ECONNREFUSED
&& errno
!= ECONNRESET
102 && errno
!= ENOENT
&& errno
!= EACCES
)
105 if (ret
== -ECONNREFUSED
|| ret
== -ECONNRESET
)
117 closeret
= close(fd
);
126 * ustcomm_accept_unix_sock
128 * Do an accept(2) on the sock and return the
129 * new file descriptor. The socket MUST be bind(2) before.
131 int ustcomm_accept_unix_sock(int sock
)
134 struct sockaddr_un sun
;
138 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
140 if (errno
!= ECONNABORTED
)
143 if (new_fd
== -ECONNABORTED
)
150 * ustcomm_create_unix_sock
152 * Creates a AF_UNIX local socket using pathname
153 * bind the socket upon creation and return the fd.
155 int ustcomm_create_unix_sock(const char *pathname
)
157 struct sockaddr_un sun
;
160 /* Create server socket */
161 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
167 memset(&sun
, 0, sizeof(sun
));
168 sun
.sun_family
= AF_UNIX
;
169 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
170 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
172 /* Unlink the old file if present */
173 (void) unlink(pathname
);
174 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
187 closeret
= close(fd
);
197 * ustcomm_listen_unix_sock
199 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
201 int ustcomm_listen_unix_sock(int sock
)
205 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
215 * ustcomm_close_unix_sock
217 * Shutdown cleanly a unix socket.
219 * Handles fd tracker internally.
221 int ustcomm_close_unix_sock(int sock
)
225 lttng_ust_lock_fd_tracker();
228 lttng_ust_delete_fd_from_tracker(sock
);
233 lttng_ust_unlock_fd_tracker();
239 * ustcomm_recv_unix_sock
241 * Receive data of size len in put that data into
242 * the buf param. Using recvmsg API.
243 * Return the size of received data.
244 * Return 0 on orderly shutdown.
246 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
253 memset(&msg
, 0, sizeof(msg
));
255 iov
[0].iov_base
= buf
;
256 iov
[0].iov_len
= len
;
261 len_last
= iov
[0].iov_len
;
262 ret
= recvmsg(sock
, &msg
, 0);
264 iov
[0].iov_base
+= ret
;
265 iov
[0].iov_len
-= ret
;
266 assert(ret
<= len_last
);
268 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
273 if (errno
!= EPIPE
&& errno
!= ECONNRESET
&& errno
!= ECONNREFUSED
)
276 if (ret
== -ECONNRESET
|| ret
== -ECONNREFUSED
)
279 shutret
= shutdown(sock
, SHUT_RDWR
);
281 ERR("Socket shutdown error");
282 } else if (ret
> 0) {
285 /* ret = 0 means an orderly shutdown. */
291 * ustcomm_send_unix_sock
293 * Send buf data of size len. Using sendmsg API.
294 * Return the size of sent data.
296 ssize_t
ustcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
302 memset(&msg
, 0, sizeof(msg
));
304 iov
[0].iov_base
= (void *) buf
;
305 iov
[0].iov_len
= len
;
310 * Using the MSG_NOSIGNAL when sending data from sessiond to
311 * libust, so libust does not receive an unhandled SIGPIPE or
312 * SIGURG. The sessiond receiver side can be made more resilient
313 * by ignoring SIGPIPE, but we don't have this luxury on the
317 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
318 } while (ret
< 0 && errno
== EINTR
);
323 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
326 if (ret
== -ECONNRESET
)
329 shutret
= shutdown(sock
, SHUT_RDWR
);
331 ERR("Socket shutdown error");
338 * Send a message accompanied by fd(s) over a unix socket.
340 * Returns the size of data sent, or negative error value.
342 ssize_t
ustcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
345 struct cmsghdr
*cmptr
;
348 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
349 char tmp
[CMSG_SPACE(sizeof_fds
)];
352 memset(&msg
, 0, sizeof(msg
));
353 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
355 if (nb_fd
> USTCOMM_MAX_SEND_FDS
)
358 msg
.msg_control
= (caddr_t
)tmp
;
359 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
361 cmptr
= CMSG_FIRSTHDR(&msg
);
364 cmptr
->cmsg_level
= SOL_SOCKET
;
365 cmptr
->cmsg_type
= SCM_RIGHTS
;
366 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
367 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
368 /* Sum of the length of all control messages in the buffer: */
369 msg
.msg_controllen
= cmptr
->cmsg_len
;
371 iov
[0].iov_base
= &dummy
;
377 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
378 } while (ret
< 0 && errno
== EINTR
);
381 * We consider EPIPE and ECONNRESET as expected.
383 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
387 if (ret
== -ECONNRESET
)
394 * Recv a message accompanied by fd(s) from a unix socket.
396 * Expect at most "nb_fd" file descriptors. Returns the number of fd
397 * actually received in nb_fd.
398 * Returns -EPIPE on orderly shutdown.
400 ssize_t
ustcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
404 struct cmsghdr
*cmsg
;
405 size_t sizeof_fds
= nb_fd
* sizeof(int);
406 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
411 memset(&msg
, 0, sizeof(msg
));
413 /* Prepare to receive the structures */
414 iov
[0].iov_base
= &dummy
;
418 msg
.msg_control
= recv_fd
;
419 msg
.msg_controllen
= sizeof(recv_fd
);
422 ret
= recvmsg(sock
, &msg
, 0);
423 } while (ret
< 0 && errno
== EINTR
);
425 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
426 PERROR("recvmsg fds");
429 if (ret
== -ECONNRESET
)
434 /* orderly shutdown */
439 ERR("Error: Received %zd bytes, expected %d\n",
443 if (msg
.msg_flags
& MSG_CTRUNC
) {
444 ERR("Error: Control message truncated.\n");
448 cmsg
= CMSG_FIRSTHDR(&msg
);
450 ERR("Error: Invalid control message header\n");
454 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
455 ERR("Didn't received any fd\n");
459 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
460 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
461 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
466 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
469 for (i
= 0; i
< nb_fd
; i
++) {
470 ret
= fcntl(fds
[i
], F_SETFD
, FD_CLOEXEC
);
472 PERROR("fcntl failed to set FD_CLOEXEC on fd %d",
482 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
486 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
494 ERR("incorrect message size: %zd\n", len
);
501 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
502 uint32_t expected_handle
, uint32_t expected_cmd
)
506 memset(lur
, 0, sizeof(*lur
));
507 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
509 case 0: /* orderly shutdown */
515 if (lur
->handle
!= expected_handle
) {
516 ERR("Unexpected result message handle: "
517 "expected: %u vs received: %u\n",
518 expected_handle
, lur
->handle
);
521 if (lur
->cmd
!= expected_cmd
) {
522 ERR("Unexpected result message command "
523 "expected: %u vs received: %u\n",
524 expected_cmd
, lur
->cmd
);
530 return lur
->ret_code
;
535 ERR("incorrect message size: %zd\n", len
);
541 int ustcomm_send_app_cmd(int sock
,
542 struct ustcomm_ust_msg
*lum
,
543 struct ustcomm_ust_reply
*lur
)
547 ret
= ustcomm_send_app_msg(sock
, lum
);
550 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
557 * chan_data is allocated internally if this function returns the
560 ssize_t
ustcomm_recv_channel_from_sessiond(int sock
,
561 void **_chan_data
, uint64_t var_len
,
568 if (var_len
> LTTNG_UST_ABI_CHANNEL_DATA_MAX_LEN
) {
572 /* Receive variable length data */
573 chan_data
= zmalloc(var_len
);
578 len
= ustcomm_recv_unix_sock(sock
, chan_data
, var_len
);
579 if (len
!= var_len
) {
583 lttng_ust_lock_fd_tracker();
584 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
586 lttng_ust_unlock_fd_tracker();
596 ret
= lttng_ust_add_fd_to_tracker(wakeup_fd
);
598 ret
= close(wakeup_fd
);
600 PERROR("close on wakeup_fd");
603 lttng_ust_unlock_fd_tracker();
608 lttng_ust_unlock_fd_tracker();
610 *_chan_data
= chan_data
;
620 ssize_t
ustcomm_recv_event_notifier_notif_fd_from_sessiond(int sock
,
621 int *_event_notifier_notif_fd
)
624 int event_notifier_notif_fd
, ret
;
626 /* Receive event_notifier notification fd */
627 lttng_ust_lock_fd_tracker();
628 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &event_notifier_notif_fd
, 1);
630 lttng_ust_unlock_fd_tracker();
640 ret
= lttng_ust_add_fd_to_tracker(event_notifier_notif_fd
);
642 ret
= close(event_notifier_notif_fd
);
644 PERROR("close on event_notifier notif fd");
647 lttng_ust_unlock_fd_tracker();
651 *_event_notifier_notif_fd
= ret
;
652 lttng_ust_unlock_fd_tracker();
660 int ustcomm_recv_stream_from_sessiond(int sock
,
661 uint64_t *memory_map_size
__attribute__((unused
)),
662 int *shm_fd
, int *wakeup_fd
)
668 /* recv shm fd and wakeup fd */
669 lttng_ust_lock_fd_tracker();
670 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
672 lttng_ust_unlock_fd_tracker();
682 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
686 PERROR("close on received shm_fd");
689 lttng_ust_unlock_fd_tracker();
694 ret
= lttng_ust_add_fd_to_tracker(fds
[1]);
696 ret
= close(*shm_fd
);
698 PERROR("close on shm_fd");
703 PERROR("close on received wakeup_fd");
706 lttng_ust_unlock_fd_tracker();
710 lttng_ust_unlock_fd_tracker();
717 ssize_t
ustcomm_recv_counter_from_sessiond(int sock
,
718 void **_counter_data
, uint64_t var_len
)
723 if (var_len
> LTTNG_UST_ABI_COUNTER_DATA_MAX_LEN
) {
727 /* Receive variable length data */
728 counter_data
= zmalloc(var_len
);
733 len
= ustcomm_recv_unix_sock(sock
, counter_data
, var_len
);
734 if (len
!= var_len
) {
737 *_counter_data
= counter_data
;
747 int ustcomm_recv_counter_shm_from_sessiond(int sock
,
755 lttng_ust_lock_fd_tracker();
756 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 1);
758 lttng_ust_unlock_fd_tracker();
768 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
772 PERROR("close on received shm_fd");
775 lttng_ust_unlock_fd_tracker();
779 lttng_ust_unlock_fd_tracker();
787 * Returns 0 on success, negative error value on error.
789 int ustcomm_send_reg_msg(int sock
,
790 enum lttng_ust_ctl_socket_type type
,
791 uint32_t bits_per_long
,
792 uint32_t uint8_t_alignment
,
793 uint32_t uint16_t_alignment
,
794 uint32_t uint32_t_alignment
,
795 uint32_t uint64_t_alignment
,
796 uint32_t long_alignment
)
799 struct lttng_ust_ctl_reg_msg reg_msg
;
801 reg_msg
.magic
= LTTNG_UST_ABI_COMM_MAGIC
;
802 reg_msg
.major
= LTTNG_UST_ABI_MAJOR_VERSION
;
803 reg_msg
.minor
= LTTNG_UST_ABI_MINOR_VERSION
;
804 reg_msg
.pid
= getpid();
805 reg_msg
.ppid
= getppid();
806 reg_msg
.uid
= getuid();
807 reg_msg
.gid
= getgid();
808 reg_msg
.bits_per_long
= bits_per_long
;
809 reg_msg
.uint8_t_alignment
= uint8_t_alignment
;
810 reg_msg
.uint16_t_alignment
= uint16_t_alignment
;
811 reg_msg
.uint32_t_alignment
= uint32_t_alignment
;
812 reg_msg
.uint64_t_alignment
= uint64_t_alignment
;
813 reg_msg
.long_alignment
= long_alignment
;
814 reg_msg
.socket_type
= type
;
815 lttng_pthread_getname_np(reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
816 memset(reg_msg
.padding
, 0, sizeof(reg_msg
.padding
));
818 len
= ustcomm_send_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
819 if (len
> 0 && len
!= sizeof(reg_msg
))
827 ssize_t
count_one_type(const struct lttng_ust_type_common
*lt
)
830 case lttng_ust_type_integer
:
831 case lttng_ust_type_float
:
832 case lttng_ust_type_string
:
834 case lttng_ust_type_enum
:
835 return count_one_type(lttng_ust_get_type_enum(lt
)->container_type
) + 1;
836 case lttng_ust_type_array
:
837 return count_one_type(lttng_ust_get_type_array(lt
)->elem_type
) + 1;
838 case lttng_ust_type_sequence
:
839 return count_one_type(lttng_ust_get_type_sequence(lt
)->elem_type
) + 1;
840 case lttng_ust_type_struct
:
841 return count_fields_recursive(lttng_ust_get_type_struct(lt
)->nr_fields
,
842 lttng_ust_get_type_struct(lt
)->fields
) + 1;
844 case lttng_ust_type_dynamic
:
846 const struct lttng_ust_event_field
* const *choices
;
850 ret
= lttng_ust_dynamic_type_choices(&nr_choices
,
855 * Two fields for enum, one field for variant, and
856 * one field per choice.
858 return count_fields_recursive(nr_choices
, choices
) + 3;
868 ssize_t
count_fields_recursive(size_t nr_fields
,
869 const struct lttng_ust_event_field
* const *lttng_fields
)
872 ssize_t ret
, count
= 0;
874 for (i
= 0; i
< nr_fields
; i
++) {
875 const struct lttng_ust_event_field
*lf
;
877 lf
= lttng_fields
[i
];
878 /* skip 'nowrite' fields */
881 ret
= count_one_type(lf
->type
);
883 return ret
; /* error */
890 ssize_t
count_ctx_fields_recursive(size_t nr_fields
,
891 struct lttng_ust_ctx_field
*lttng_fields
)
894 ssize_t ret
, count
= 0;
896 for (i
= 0; i
< nr_fields
; i
++) {
897 const struct lttng_ust_event_field
*lf
;
899 lf
= lttng_fields
[i
].event_field
;
900 /* skip 'nowrite' fields */
903 ret
= count_one_type(lf
->type
);
905 return ret
; /* error */
912 int serialize_string_encoding(int32_t *ue
,
913 enum lttng_ust_string_encoding le
)
916 case lttng_ust_string_encoding_none
:
917 *ue
= lttng_ust_ctl_encode_none
;
919 case lttng_ust_string_encoding_UTF8
:
920 *ue
= lttng_ust_ctl_encode_UTF8
;
922 case lttng_ust_string_encoding_ASCII
:
923 *ue
= lttng_ust_ctl_encode_ASCII
;
932 int serialize_integer_type(struct lttng_ust_ctl_integer_type
*uit
,
933 const struct lttng_ust_type_integer
*lit
,
934 enum lttng_ust_string_encoding lencoding
)
938 uit
->size
= lit
->size
;
939 uit
->signedness
= lit
->signedness
;
940 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
941 uit
->base
= lit
->base
;
942 if (serialize_string_encoding(&encoding
, lencoding
))
944 uit
->encoding
= encoding
;
945 uit
->alignment
= lit
->alignment
;
950 int serialize_dynamic_type(struct lttng_ust_session
*session
,
951 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
952 const char *field_name
)
954 const struct lttng_ust_event_field
* const *choices
;
955 char tag_field_name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
956 const struct lttng_ust_type_common
*tag_type
;
957 const struct lttng_ust_event_field
*tag_field_generic
;
958 struct lttng_ust_event_field tag_field
= {
959 .name
= tag_field_name
,
962 struct lttng_ust_ctl_field
*uf
;
963 size_t nr_choices
, i
;
966 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
967 tag_type
= tag_field_generic
->type
;
969 /* Serialize enum field. */
970 strncpy(tag_field_name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
971 tag_field_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
972 strncat(tag_field_name
,
974 LTTNG_UST_ABI_SYM_NAME_LEN
- strlen(tag_field_name
) - 1);
975 tag_field
.type
= tag_type
;
976 ret
= serialize_one_field(session
, fields
, iter_output
,
981 /* Serialize variant field. */
982 uf
= &fields
[*iter_output
];
983 ret
= lttng_ust_dynamic_type_choices(&nr_choices
, &choices
);
987 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
988 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
989 uf
->type
.atype
= lttng_ust_ctl_atype_variant
;
990 uf
->type
.u
.variant_nestable
.nr_choices
= nr_choices
;
991 strncpy(uf
->type
.u
.variant_nestable
.tag_name
,
993 LTTNG_UST_ABI_SYM_NAME_LEN
);
994 uf
->type
.u
.variant_nestable
.tag_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
995 uf
->type
.u
.variant_nestable
.alignment
= 0;
998 /* Serialize choice fields after variant. */
999 for (i
= 0; i
< nr_choices
; i
++) {
1000 ret
= serialize_one_field(session
, fields
,
1001 iter_output
, choices
[i
], NULL
);
1009 int serialize_one_type(struct lttng_ust_session
*session
,
1010 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
1011 const char *field_name
, const struct lttng_ust_type_common
*lt
,
1012 enum lttng_ust_string_encoding parent_encoding
,
1013 const char *prev_field_name
)
1018 * Serializing a type (rather than a field) generates a lttng_ust_ctl_field
1019 * entry with 0-length name.
1023 case lttng_ust_type_integer
:
1025 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1026 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1029 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1030 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1034 ret
= serialize_integer_type(&ut
->u
.integer
, lttng_ust_get_type_integer(lt
),
1038 ut
->atype
= lttng_ust_ctl_atype_integer
;
1042 case lttng_ust_type_float
:
1044 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1045 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1046 struct lttng_ust_ctl_float_type
*uft
;
1047 const struct lttng_ust_type_float
*lft
;
1050 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1051 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1055 uft
= &ut
->u
._float
;
1056 lft
= lttng_ust_get_type_float(lt
);
1057 uft
->exp_dig
= lft
->exp_dig
;
1058 uft
->mant_dig
= lft
->mant_dig
;
1059 uft
->alignment
= lft
->alignment
;
1060 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
1061 ut
->atype
= lttng_ust_ctl_atype_float
;
1065 case lttng_ust_type_string
:
1067 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1068 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1072 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1073 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1077 ret
= serialize_string_encoding(&encoding
, lttng_ust_get_type_string(lt
)->encoding
);
1080 ut
->u
.string
.encoding
= encoding
;
1081 ut
->atype
= lttng_ust_ctl_atype_string
;
1085 case lttng_ust_type_array
:
1087 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1088 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1091 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1092 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1096 ut
->atype
= lttng_ust_ctl_atype_array_nestable
;
1097 ut
->u
.array_nestable
.length
= lttng_ust_get_type_array(lt
)->length
;
1098 ut
->u
.array_nestable
.alignment
= lttng_ust_get_type_array(lt
)->alignment
;
1101 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1102 lttng_ust_get_type_array(lt
)->elem_type
,
1103 lttng_ust_get_type_array(lt
)->encoding
, NULL
);
1108 case lttng_ust_type_sequence
:
1110 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1111 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1112 const char *length_name
= lttng_ust_get_type_sequence(lt
)->length_name
;
1115 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1116 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1120 ut
->atype
= lttng_ust_ctl_atype_sequence_nestable
;
1122 * If length_name field is NULL, use the previous field
1126 length_name
= prev_field_name
;
1129 strncpy(ut
->u
.sequence_nestable
.length_name
,
1130 length_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1131 ut
->u
.sequence_nestable
.length_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1132 ut
->u
.sequence_nestable
.alignment
= lttng_ust_get_type_sequence(lt
)->alignment
;
1135 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1136 lttng_ust_get_type_sequence(lt
)->elem_type
,
1137 lttng_ust_get_type_sequence(lt
)->encoding
, NULL
);
1142 case lttng_ust_type_dynamic
:
1144 ret
= serialize_dynamic_type(session
, fields
, iter_output
,
1150 case lttng_ust_type_struct
:
1152 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1155 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1156 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1160 uf
->type
.atype
= lttng_ust_ctl_atype_struct_nestable
;
1161 uf
->type
.u
.struct_nestable
.nr_fields
= lttng_ust_get_type_struct(lt
)->nr_fields
;
1162 uf
->type
.u
.struct_nestable
.alignment
= lttng_ust_get_type_struct(lt
)->alignment
;
1165 ret
= serialize_fields(session
, fields
, iter_output
,
1166 lttng_ust_get_type_struct(lt
)->nr_fields
,
1167 lttng_ust_get_type_struct(lt
)->fields
);
1172 case lttng_ust_type_enum
:
1174 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1175 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1178 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1179 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1183 strncpy(ut
->u
.enum_nestable
.name
, lttng_ust_get_type_enum(lt
)->desc
->name
,
1184 LTTNG_UST_ABI_SYM_NAME_LEN
);
1185 ut
->u
.enum_nestable
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1186 ut
->atype
= lttng_ust_ctl_atype_enum_nestable
;
1189 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1190 lttng_ust_get_type_enum(lt
)->container_type
,
1191 lttng_ust_string_encoding_none
, NULL
);
1195 const struct lttng_enum
*_enum
;
1197 _enum
= lttng_ust_enum_get_from_desc(session
, lttng_ust_get_type_enum(lt
)->desc
);
1200 ut
->u
.enum_nestable
.id
= _enum
->id
;
1202 ut
->u
.enum_nestable
.id
= -1ULL;
1213 int serialize_one_field(struct lttng_ust_session
*session
,
1214 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
1215 const struct lttng_ust_event_field
*lf
,
1216 const char **prev_field_name_p
)
1218 const char *prev_field_name
= NULL
;
1221 /* skip 'nowrite' fields */
1225 if (prev_field_name_p
)
1226 prev_field_name
= *prev_field_name_p
;
1227 ret
= serialize_one_type(session
, fields
, iter_output
, lf
->name
, lf
->type
,
1228 lttng_ust_string_encoding_none
, prev_field_name
);
1229 if (prev_field_name_p
)
1230 *prev_field_name_p
= lf
->name
;
1235 int serialize_fields(struct lttng_ust_session
*session
,
1236 struct lttng_ust_ctl_field
*lttng_ust_ctl_fields
,
1237 size_t *iter_output
, size_t nr_lttng_fields
,
1238 const struct lttng_ust_event_field
* const *lttng_fields
)
1240 const char *prev_field_name
= NULL
;
1244 for (i
= 0; i
< nr_lttng_fields
; i
++) {
1245 ret
= serialize_one_field(session
, lttng_ust_ctl_fields
,
1246 iter_output
, lttng_fields
[i
],
1255 int alloc_serialize_fields(struct lttng_ust_session
*session
,
1256 size_t *_nr_write_fields
,
1257 struct lttng_ust_ctl_field
**lttng_ust_ctl_fields
,
1259 const struct lttng_ust_event_field
* const *lttng_fields
)
1261 struct lttng_ust_ctl_field
*fields
;
1263 size_t iter_output
= 0;
1264 ssize_t nr_write_fields
;
1266 nr_write_fields
= count_fields_recursive(nr_fields
, lttng_fields
);
1267 if (nr_write_fields
< 0) {
1268 return (int) nr_write_fields
;
1271 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1275 ret
= serialize_fields(session
, fields
, &iter_output
, nr_fields
,
1280 *_nr_write_fields
= nr_write_fields
;
1281 *lttng_ust_ctl_fields
= fields
;
1290 int serialize_entries(struct lttng_ust_ctl_enum_entry
**_entries
,
1292 const struct lttng_ust_enum_entry
* const *lttng_entries
)
1294 struct lttng_ust_ctl_enum_entry
*entries
;
1297 /* Serialize the entries */
1298 entries
= zmalloc(nr_entries
* sizeof(*entries
));
1301 for (i
= 0; i
< nr_entries
; i
++) {
1302 struct lttng_ust_ctl_enum_entry
*uentry
;
1303 const struct lttng_ust_enum_entry
*lentry
;
1305 uentry
= &entries
[i
];
1306 lentry
= lttng_entries
[i
];
1308 uentry
->start
.value
= lentry
->start
.value
;
1309 uentry
->start
.signedness
= lentry
->start
.signedness
;
1310 uentry
->end
.value
= lentry
->end
.value
;
1311 uentry
->end
.signedness
= lentry
->end
.signedness
;
1312 strncpy(uentry
->string
, lentry
->string
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1313 uentry
->string
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1315 if (lentry
->options
& LTTNG_UST_ENUM_ENTRY_OPTION_IS_AUTO
) {
1316 uentry
->u
.extra
.options
|=
1317 LTTNG_UST_CTL_UST_ENUM_ENTRY_OPTION_IS_AUTO
;
1320 *_entries
= entries
;
1325 int serialize_ctx_fields(struct lttng_ust_session
*session
,
1326 size_t *_nr_write_fields
,
1327 struct lttng_ust_ctl_field
**lttng_ust_ctl_fields
,
1329 struct lttng_ust_ctx_field
*lttng_fields
)
1331 struct lttng_ust_ctl_field
*fields
;
1332 const char *prev_field_name
= NULL
;
1333 size_t i
, iter_output
= 0;
1334 ssize_t nr_write_fields
;
1337 nr_write_fields
= count_ctx_fields_recursive(nr_fields
,
1339 if (nr_write_fields
< 0) {
1340 return (int) nr_write_fields
;
1343 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1347 for (i
= 0; i
< nr_fields
; i
++) {
1348 ret
= serialize_one_field(session
, fields
, &iter_output
,
1349 lttng_fields
[i
].event_field
, &prev_field_name
);
1354 *_nr_write_fields
= nr_write_fields
;
1355 *lttng_ust_ctl_fields
= fields
;
1364 * Returns 0 on success, negative error value on error.
1366 int ustcomm_register_event(int sock
,
1367 struct lttng_ust_session
*session
,
1368 int session_objd
, /* session descriptor */
1369 int channel_objd
, /* channel descriptor */
1370 const char *event_name
, /* event name (input) */
1372 const char *signature
, /* event signature (input) */
1373 size_t nr_fields
, /* fields */
1374 const struct lttng_ust_event_field
* const *lttng_fields
,
1375 const char *model_emf_uri
,
1376 uint32_t *id
) /* event id (output) */
1380 struct ustcomm_notify_hdr header
;
1381 struct ustcomm_notify_event_msg m
;
1384 struct ustcomm_notify_hdr header
;
1385 struct ustcomm_notify_event_reply r
;
1387 size_t signature_len
, fields_len
, model_emf_uri_len
;
1388 struct lttng_ust_ctl_field
*fields
= NULL
;
1389 size_t nr_write_fields
= 0;
1392 memset(&msg
, 0, sizeof(msg
));
1393 msg
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_EVENT
;
1394 msg
.m
.session_objd
= session_objd
;
1395 msg
.m
.channel_objd
= channel_objd
;
1396 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1397 msg
.m
.event_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1398 msg
.m
.loglevel
= loglevel
;
1399 signature_len
= strlen(signature
) + 1;
1400 msg
.m
.signature_len
= signature_len
;
1402 /* Calculate fields len, serialize fields. */
1403 if (nr_fields
> 0) {
1404 ret
= alloc_serialize_fields(session
, &nr_write_fields
, &fields
,
1405 nr_fields
, lttng_fields
);
1410 fields_len
= sizeof(*fields
) * nr_write_fields
;
1411 msg
.m
.fields_len
= fields_len
;
1412 if (model_emf_uri
) {
1413 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
1415 model_emf_uri_len
= 0;
1417 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
1419 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1420 if (len
> 0 && len
!= sizeof(msg
)) {
1429 /* send signature */
1430 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
1431 if (len
> 0 && len
!= signature_len
) {
1441 if (fields_len
> 0) {
1442 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1443 if (len
> 0 && len
!= fields_len
) {
1454 if (model_emf_uri_len
) {
1455 /* send model_emf_uri */
1456 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
1458 if (len
> 0 && len
!= model_emf_uri_len
) {
1467 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1469 case 0: /* orderly shutdown */
1472 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1473 ERR("Unexpected result message command "
1474 "expected: %u vs received: %u\n",
1475 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1478 if (reply
.r
.ret_code
> 0)
1480 if (reply
.r
.ret_code
< 0)
1481 return reply
.r
.ret_code
;
1482 *id
= reply
.r
.event_id
;
1483 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1484 event_name
, reply
.r
.ret_code
, reply
.r
.event_id
);
1488 /* Transport level error */
1489 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1493 ERR("incorrect message size: %zd\n", len
);
1499 /* Error path only. */
1506 * Returns 0 on success, negative error value on error.
1507 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1509 int ustcomm_register_enum(int sock
,
1510 int session_objd
, /* session descriptor */
1511 const char *enum_name
, /* enum name (input) */
1512 size_t nr_entries
, /* entries */
1513 const struct lttng_ust_enum_entry
* const *lttng_entries
,
1518 struct ustcomm_notify_hdr header
;
1519 struct ustcomm_notify_enum_msg m
;
1522 struct ustcomm_notify_hdr header
;
1523 struct ustcomm_notify_enum_reply r
;
1526 struct lttng_ust_ctl_enum_entry
*entries
= NULL
;
1529 memset(&msg
, 0, sizeof(msg
));
1530 msg
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_ENUM
;
1531 msg
.m
.session_objd
= session_objd
;
1532 strncpy(msg
.m
.enum_name
, enum_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1533 msg
.m
.enum_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1535 /* Calculate entries len, serialize entries. */
1536 if (nr_entries
> 0) {
1537 ret
= serialize_entries(&entries
,
1538 nr_entries
, lttng_entries
);
1543 entries_len
= sizeof(*entries
) * nr_entries
;
1544 msg
.m
.entries_len
= entries_len
;
1546 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1547 if (len
> 0 && len
!= sizeof(msg
)) {
1557 if (entries_len
> 0) {
1558 len
= ustcomm_send_unix_sock(sock
, entries
, entries_len
);
1559 if (len
> 0 && len
!= entries_len
) {
1572 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1574 case 0: /* orderly shutdown */
1577 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1578 ERR("Unexpected result message command "
1579 "expected: %u vs received: %u\n",
1580 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1583 if (reply
.r
.ret_code
> 0)
1585 if (reply
.r
.ret_code
< 0)
1586 return reply
.r
.ret_code
;
1587 *id
= reply
.r
.enum_id
;
1588 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1589 enum_name
, reply
.r
.ret_code
);
1593 /* Transport level error */
1594 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1598 ERR("incorrect message size: %zd\n", len
);
1610 * Returns 0 on success, negative error value on error.
1611 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1613 int ustcomm_register_channel(int sock
,
1614 struct lttng_ust_session
*session
,
1615 int session_objd
, /* session descriptor */
1616 int channel_objd
, /* channel descriptor */
1617 size_t nr_ctx_fields
,
1618 struct lttng_ust_ctx_field
*ctx_fields
,
1619 uint32_t *chan_id
, /* channel id (output) */
1620 int *header_type
) /* header type (output) */
1624 struct ustcomm_notify_hdr header
;
1625 struct ustcomm_notify_channel_msg m
;
1628 struct ustcomm_notify_hdr header
;
1629 struct ustcomm_notify_channel_reply r
;
1632 struct lttng_ust_ctl_field
*fields
= NULL
;
1634 size_t nr_write_fields
= 0;
1636 memset(&msg
, 0, sizeof(msg
));
1637 msg
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
;
1638 msg
.m
.session_objd
= session_objd
;
1639 msg
.m
.channel_objd
= channel_objd
;
1641 /* Calculate fields len, serialize fields. */
1642 if (nr_ctx_fields
> 0) {
1643 ret
= serialize_ctx_fields(session
, &nr_write_fields
, &fields
,
1644 nr_ctx_fields
, ctx_fields
);
1649 fields_len
= sizeof(*fields
) * nr_write_fields
;
1650 msg
.m
.ctx_fields_len
= fields_len
;
1651 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1652 if (len
> 0 && len
!= sizeof(msg
)) {
1662 if (fields_len
> 0) {
1663 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1665 if (len
> 0 && len
!= fields_len
) {
1675 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1677 case 0: /* orderly shutdown */
1680 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1681 ERR("Unexpected result message command "
1682 "expected: %u vs received: %u\n",
1683 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1686 if (reply
.r
.ret_code
> 0)
1688 if (reply
.r
.ret_code
< 0)
1689 return reply
.r
.ret_code
;
1690 *chan_id
= reply
.r
.chan_id
;
1691 switch (reply
.r
.header_type
) {
1694 *header_type
= reply
.r
.header_type
;
1697 ERR("Unexpected channel header type %u\n",
1698 reply
.r
.header_type
);
1701 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1702 reply
.r
.chan_id
, reply
.r
.header_type
);
1706 /* Transport level error */
1707 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1711 ERR("incorrect message size: %zd\n", len
);
1718 * Set socket reciving timeout.
1720 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1725 tv
.tv_sec
= msec
/ 1000;
1726 tv
.tv_usec
= (msec
* 1000 % 1000000);
1728 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1730 PERROR("setsockopt SO_RCVTIMEO");
1738 * Set socket sending timeout.
1740 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1745 tv
.tv_sec
= msec
/ 1000;
1746 tv
.tv_usec
= (msec
* 1000) % 1000000;
1748 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1750 PERROR("setsockopt SO_SNDTIMEO");