2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2011 EfficiOS Inc.
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.
64 fd
= socket(PF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0);
71 /* Give at least 10ms. */
74 ret
= ustcomm_setsockopt_snd_timeout(fd
, timeout
);
76 WARN("Error setting connect socket send timeout");
80 memset(&sun
, 0, sizeof(sun
));
81 sun
.sun_family
= AF_UNIX
;
82 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
83 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
85 DBG("Connecting to '%s'", sun
.sun_path
);
86 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
89 * Don't print message on connect ENOENT error, because
90 * connect is used in normal execution to detect if
91 * sessiond is alive. ENOENT is when the unix socket
92 * file does not exist, and ECONNREFUSED is when the
93 * file exists but no sessiond is listening.
95 if (errno
!= ECONNREFUSED
&& errno
!= ECONNRESET
96 && errno
!= ENOENT
&& errno
!= EACCES
)
99 if (ret
== -ECONNREFUSED
|| ret
== -ECONNRESET
)
110 closeret
= close(fd
);
119 * ustcomm_accept_unix_sock
121 * Do an accept(2) on the sock and return the
122 * new file descriptor. The socket MUST be bind(2) before.
124 int ustcomm_accept_unix_sock(int sock
)
127 struct sockaddr_un sun
;
131 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
133 if (errno
!= ECONNABORTED
)
136 if (new_fd
== -ECONNABORTED
)
143 * ustcomm_create_unix_sock
145 * Creates a AF_UNIX local socket using pathname
146 * bind the socket upon creation and return the fd.
148 int ustcomm_create_unix_sock(const char *pathname
)
150 struct sockaddr_un sun
;
153 /* Create server socket */
154 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
160 memset(&sun
, 0, sizeof(sun
));
161 sun
.sun_family
= AF_UNIX
;
162 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
163 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
165 /* Unlink the old file if present */
166 (void) unlink(pathname
);
167 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
180 closeret
= close(fd
);
190 * ustcomm_listen_unix_sock
192 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
194 int ustcomm_listen_unix_sock(int sock
)
198 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
208 * ustcomm_close_unix_sock
212 * Handles fd tracker internally.
214 int ustcomm_close_unix_sock(int sock
)
218 lttng_ust_lock_fd_tracker();
221 lttng_ust_delete_fd_from_tracker(sock
);
226 lttng_ust_unlock_fd_tracker();
232 * ustcomm_shutdown_unix_sock
234 * Shutdown unix socket. Keeps the file descriptor open, but shutdown
237 int ustcomm_shutdown_unix_sock(int sock
)
241 ret
= shutdown(sock
, SHUT_RDWR
);
243 PERROR("Socket shutdown error");
250 * ustcomm_recv_unix_sock
252 * Receive data of size len in put that data into
253 * the buf param. Using recvmsg API.
254 * Return the size of received data.
255 * Return 0 on orderly shutdown.
257 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
264 memset(&msg
, 0, sizeof(msg
));
266 iov
[0].iov_base
= buf
;
267 iov
[0].iov_len
= len
;
272 len_last
= iov
[0].iov_len
;
273 ret
= recvmsg(sock
, &msg
, 0);
275 iov
[0].iov_base
+= ret
;
276 iov
[0].iov_len
-= ret
;
277 assert(ret
<= len_last
);
279 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
282 if (errno
!= EPIPE
&& errno
!= ECONNRESET
&& errno
!= ECONNREFUSED
)
285 if (ret
== -ECONNRESET
|| ret
== -ECONNREFUSED
)
288 (void) ustcomm_shutdown_unix_sock(sock
);
289 } else if (ret
> 0) {
292 /* ret = 0 means an orderly shutdown. */
298 * ustcomm_send_unix_sock
300 * Send buf data of size len. Using sendmsg API.
301 * Return the size of sent data.
303 ssize_t
ustcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
309 memset(&msg
, 0, sizeof(msg
));
311 iov
[0].iov_base
= (void *) buf
;
312 iov
[0].iov_len
= len
;
317 * Using the MSG_NOSIGNAL when sending data from sessiond to
318 * libust, so libust does not receive an unhandled SIGPIPE or
319 * SIGURG. The sessiond receiver side can be made more resilient
320 * by ignoring SIGPIPE, but we don't have this luxury on the
324 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
325 } while (ret
< 0 && errno
== EINTR
);
328 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
331 if (ret
== -ECONNRESET
)
334 (void) ustcomm_shutdown_unix_sock(sock
);
341 * Send a message accompanied by fd(s) over a unix socket.
343 * Returns the size of data sent, or negative error value.
345 ssize_t
ustcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
348 struct cmsghdr
*cmptr
;
351 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
352 char tmp
[CMSG_SPACE(sizeof_fds
)];
355 memset(&msg
, 0, sizeof(msg
));
356 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
358 if (nb_fd
> USTCOMM_MAX_SEND_FDS
)
361 msg
.msg_control
= (caddr_t
)tmp
;
362 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
364 cmptr
= CMSG_FIRSTHDR(&msg
);
367 cmptr
->cmsg_level
= SOL_SOCKET
;
368 cmptr
->cmsg_type
= SCM_RIGHTS
;
369 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
370 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
371 /* Sum of the length of all control messages in the buffer: */
372 msg
.msg_controllen
= cmptr
->cmsg_len
;
374 iov
[0].iov_base
= &dummy
;
380 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
381 } while (ret
< 0 && errno
== EINTR
);
384 * We consider EPIPE and ECONNRESET as expected.
386 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
390 if (ret
== -ECONNRESET
)
397 * Recv a message accompanied by fd(s) from a unix socket.
399 * Expect at most "nb_fd" file descriptors. Returns the number of fd
400 * actually received in nb_fd.
401 * Returns -EPIPE on orderly shutdown.
403 ssize_t
ustcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
407 struct cmsghdr
*cmsg
;
408 size_t sizeof_fds
= nb_fd
* sizeof(int);
409 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
413 memset(&msg
, 0, sizeof(msg
));
415 /* Prepare to receive the structures */
416 iov
[0].iov_base
= &dummy
;
420 msg
.msg_control
= recv_fd
;
421 msg
.msg_controllen
= sizeof(recv_fd
);
424 ret
= recvmsg(sock
, &msg
, MSG_CMSG_CLOEXEC
);
425 } while (ret
< 0 && errno
== EINTR
);
427 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
428 PERROR("recvmsg fds");
431 if (ret
== -ECONNRESET
)
436 /* orderly shutdown */
441 ERR("Error: Received %zd bytes, expected %d\n",
445 if (msg
.msg_flags
& MSG_CTRUNC
) {
446 ERR("Error: Control message truncated.\n");
450 cmsg
= CMSG_FIRSTHDR(&msg
);
452 ERR("Error: Invalid control message header\n");
456 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
457 ERR("Didn't received any fd\n");
461 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
462 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
463 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
468 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
475 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
479 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
487 ERR("incorrect message size: %zd\n", len
);
494 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
495 uint32_t expected_handle
, uint32_t expected_cmd
)
499 memset(lur
, 0, sizeof(*lur
));
500 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
502 case 0: /* orderly shutdown */
508 if (lur
->handle
!= expected_handle
) {
509 ERR("Unexpected result message handle: "
510 "expected: %u vs received: %u\n",
511 expected_handle
, lur
->handle
);
514 if (lur
->cmd
!= expected_cmd
) {
515 ERR("Unexpected result message command "
516 "expected: %u vs received: %u\n",
517 expected_cmd
, lur
->cmd
);
523 return lur
->ret_code
;
528 ERR("incorrect message size: %zd\n", len
);
534 int ustcomm_send_app_cmd(int sock
,
535 struct ustcomm_ust_msg
*lum
,
536 struct ustcomm_ust_reply
*lur
)
540 ret
= ustcomm_send_app_msg(sock
, lum
);
543 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
550 * chan_data is allocated internally if this function returns the
553 ssize_t
ustcomm_recv_channel_from_sessiond(int sock
,
554 void **_chan_data
, uint64_t var_len
,
561 if (var_len
> LTTNG_UST_ABI_CHANNEL_DATA_MAX_LEN
) {
565 /* Receive variable length data */
566 chan_data
= zmalloc(var_len
);
571 len
= ustcomm_recv_unix_sock(sock
, chan_data
, var_len
);
572 if (len
!= var_len
) {
576 lttng_ust_lock_fd_tracker();
577 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
579 lttng_ust_unlock_fd_tracker();
589 ret
= lttng_ust_add_fd_to_tracker(wakeup_fd
);
591 ret
= close(wakeup_fd
);
593 PERROR("close on wakeup_fd");
596 lttng_ust_unlock_fd_tracker();
601 lttng_ust_unlock_fd_tracker();
603 *_chan_data
= chan_data
;
613 ssize_t
ustcomm_recv_event_notifier_notif_fd_from_sessiond(int sock
,
614 int *_event_notifier_notif_fd
)
617 int event_notifier_notif_fd
, ret
;
619 /* Receive event_notifier notification fd */
620 lttng_ust_lock_fd_tracker();
621 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &event_notifier_notif_fd
, 1);
623 lttng_ust_unlock_fd_tracker();
633 ret
= lttng_ust_add_fd_to_tracker(event_notifier_notif_fd
);
635 ret
= close(event_notifier_notif_fd
);
637 PERROR("close on event_notifier notif fd");
640 lttng_ust_unlock_fd_tracker();
644 *_event_notifier_notif_fd
= ret
;
645 lttng_ust_unlock_fd_tracker();
653 int ustcomm_recv_stream_from_sessiond(int sock
,
654 uint64_t *memory_map_size
__attribute__((unused
)),
655 int *shm_fd
, int *wakeup_fd
)
661 /* recv shm fd and wakeup fd */
662 lttng_ust_lock_fd_tracker();
663 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
665 lttng_ust_unlock_fd_tracker();
675 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
679 PERROR("close on received shm_fd");
682 lttng_ust_unlock_fd_tracker();
687 ret
= lttng_ust_add_fd_to_tracker(fds
[1]);
689 ret
= close(*shm_fd
);
691 PERROR("close on shm_fd");
696 PERROR("close on received wakeup_fd");
699 lttng_ust_unlock_fd_tracker();
703 lttng_ust_unlock_fd_tracker();
710 ssize_t
ustcomm_recv_counter_from_sessiond(int sock
,
711 void **_counter_data
, uint64_t var_len
)
716 if (var_len
> LTTNG_UST_ABI_COUNTER_DATA_MAX_LEN
) {
720 /* Receive variable length data */
721 counter_data
= zmalloc(var_len
);
726 len
= ustcomm_recv_unix_sock(sock
, counter_data
, var_len
);
727 if (len
!= var_len
) {
730 *_counter_data
= counter_data
;
740 int ustcomm_recv_counter_shm_from_sessiond(int sock
,
748 lttng_ust_lock_fd_tracker();
749 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 1);
751 lttng_ust_unlock_fd_tracker();
761 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
765 PERROR("close on received shm_fd");
768 lttng_ust_unlock_fd_tracker();
772 lttng_ust_unlock_fd_tracker();
780 * Returns 0 on success, negative error value on error.
782 int ustcomm_send_reg_msg(int sock
,
783 enum lttng_ust_ctl_socket_type type
,
784 uint32_t bits_per_long
,
785 uint32_t uint8_t_alignment
,
786 uint32_t uint16_t_alignment
,
787 uint32_t uint32_t_alignment
,
788 uint32_t uint64_t_alignment
,
789 uint32_t long_alignment
,
790 const char *procname
)
793 struct lttng_ust_ctl_reg_msg reg_msg
;
795 reg_msg
.magic
= LTTNG_UST_ABI_COMM_MAGIC
;
796 reg_msg
.major
= LTTNG_UST_ABI_MAJOR_VERSION
;
797 reg_msg
.minor
= LTTNG_UST_ABI_MINOR_VERSION
;
798 reg_msg
.pid
= getpid();
799 reg_msg
.ppid
= getppid();
800 reg_msg
.uid
= getuid();
801 reg_msg
.gid
= getgid();
802 reg_msg
.bits_per_long
= bits_per_long
;
803 reg_msg
.uint8_t_alignment
= uint8_t_alignment
;
804 reg_msg
.uint16_t_alignment
= uint16_t_alignment
;
805 reg_msg
.uint32_t_alignment
= uint32_t_alignment
;
806 reg_msg
.uint64_t_alignment
= uint64_t_alignment
;
807 reg_msg
.long_alignment
= long_alignment
;
808 reg_msg
.socket_type
= type
;
809 memset(reg_msg
.name
, 0, sizeof(reg_msg
.name
));
810 strncpy(reg_msg
.name
, procname
, sizeof(reg_msg
.name
) - 1);
811 memset(reg_msg
.padding
, 0, sizeof(reg_msg
.padding
));
813 len
= ustcomm_send_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
814 if (len
> 0 && len
!= sizeof(reg_msg
))
822 ssize_t
count_one_type(const struct lttng_ust_type_common
*lt
)
825 case lttng_ust_type_integer
:
826 case lttng_ust_type_float
:
827 case lttng_ust_type_string
:
829 case lttng_ust_type_enum
:
830 return count_one_type(lttng_ust_get_type_enum(lt
)->container_type
) + 1;
831 case lttng_ust_type_array
:
832 return count_one_type(lttng_ust_get_type_array(lt
)->elem_type
) + 1;
833 case lttng_ust_type_sequence
:
834 return count_one_type(lttng_ust_get_type_sequence(lt
)->elem_type
) + 1;
835 case lttng_ust_type_struct
:
836 return count_fields_recursive(lttng_ust_get_type_struct(lt
)->nr_fields
,
837 lttng_ust_get_type_struct(lt
)->fields
) + 1;
839 case lttng_ust_type_dynamic
:
841 const struct lttng_ust_event_field
* const *choices
;
845 ret
= lttng_ust_dynamic_type_choices(&nr_choices
,
850 * Two fields for enum, one field for variant, and
851 * one field per choice.
853 return count_fields_recursive(nr_choices
, choices
) + 3;
863 ssize_t
count_fields_recursive(size_t nr_fields
,
864 const struct lttng_ust_event_field
* const *lttng_fields
)
867 ssize_t ret
, count
= 0;
869 for (i
= 0; i
< nr_fields
; i
++) {
870 const struct lttng_ust_event_field
*lf
;
872 lf
= lttng_fields
[i
];
873 /* skip 'nowrite' fields */
876 ret
= count_one_type(lf
->type
);
878 return ret
; /* error */
885 ssize_t
count_ctx_fields_recursive(size_t nr_fields
,
886 struct lttng_ust_ctx_field
*lttng_fields
)
889 ssize_t ret
, count
= 0;
891 for (i
= 0; i
< nr_fields
; i
++) {
892 const struct lttng_ust_event_field
*lf
;
894 lf
= lttng_fields
[i
].event_field
;
895 /* skip 'nowrite' fields */
898 ret
= count_one_type(lf
->type
);
900 return ret
; /* error */
907 int serialize_string_encoding(int32_t *ue
,
908 enum lttng_ust_string_encoding le
)
911 case lttng_ust_string_encoding_none
:
912 *ue
= lttng_ust_ctl_encode_none
;
914 case lttng_ust_string_encoding_UTF8
:
915 *ue
= lttng_ust_ctl_encode_UTF8
;
917 case lttng_ust_string_encoding_ASCII
:
918 *ue
= lttng_ust_ctl_encode_ASCII
;
927 int serialize_integer_type(struct lttng_ust_ctl_integer_type
*uit
,
928 const struct lttng_ust_type_integer
*lit
,
929 enum lttng_ust_string_encoding lencoding
)
933 uit
->size
= lit
->size
;
934 uit
->signedness
= lit
->signedness
;
935 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
936 uit
->base
= lit
->base
;
937 if (serialize_string_encoding(&encoding
, lencoding
))
939 uit
->encoding
= encoding
;
940 uit
->alignment
= lit
->alignment
;
945 int serialize_dynamic_type(struct lttng_ust_session
*session
,
946 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
947 const char *field_name
)
949 const struct lttng_ust_event_field
* const *choices
;
950 char tag_field_name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
951 const struct lttng_ust_type_common
*tag_type
;
952 const struct lttng_ust_event_field
*tag_field_generic
;
953 struct lttng_ust_event_field tag_field
= {
954 .name
= tag_field_name
,
957 struct lttng_ust_ctl_field
*uf
;
958 size_t nr_choices
, i
;
961 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
962 tag_type
= tag_field_generic
->type
;
964 /* Serialize enum field. */
965 strncpy(tag_field_name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
966 tag_field_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
967 strncat(tag_field_name
,
969 LTTNG_UST_ABI_SYM_NAME_LEN
- strlen(tag_field_name
) - 1);
970 tag_field
.type
= tag_type
;
971 ret
= serialize_one_field(session
, fields
, iter_output
,
976 /* Serialize variant field. */
977 uf
= &fields
[*iter_output
];
978 ret
= lttng_ust_dynamic_type_choices(&nr_choices
, &choices
);
982 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
983 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
984 uf
->type
.atype
= lttng_ust_ctl_atype_variant_nestable
;
985 uf
->type
.u
.variant_nestable
.nr_choices
= nr_choices
;
986 strncpy(uf
->type
.u
.variant_nestable
.tag_name
,
988 LTTNG_UST_ABI_SYM_NAME_LEN
);
989 uf
->type
.u
.variant_nestable
.tag_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
990 uf
->type
.u
.variant_nestable
.alignment
= 0;
993 /* Serialize choice fields after variant. */
994 for (i
= 0; i
< nr_choices
; i
++) {
995 ret
= serialize_one_field(session
, fields
,
996 iter_output
, choices
[i
], NULL
);
1004 int serialize_one_type(struct lttng_ust_session
*session
,
1005 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
1006 const char *field_name
, const struct lttng_ust_type_common
*lt
,
1007 enum lttng_ust_string_encoding parent_encoding
,
1008 const char *prev_field_name
)
1013 * Serializing a type (rather than a field) generates a lttng_ust_ctl_field
1014 * entry with 0-length name.
1018 case lttng_ust_type_integer
:
1020 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1021 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1024 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1025 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1029 ret
= serialize_integer_type(&ut
->u
.integer
, lttng_ust_get_type_integer(lt
),
1033 ut
->atype
= lttng_ust_ctl_atype_integer
;
1037 case lttng_ust_type_float
:
1039 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1040 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1041 struct lttng_ust_ctl_float_type
*uft
;
1042 const struct lttng_ust_type_float
*lft
;
1045 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1046 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1050 uft
= &ut
->u
._float
;
1051 lft
= lttng_ust_get_type_float(lt
);
1052 uft
->exp_dig
= lft
->exp_dig
;
1053 uft
->mant_dig
= lft
->mant_dig
;
1054 uft
->alignment
= lft
->alignment
;
1055 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
1056 ut
->atype
= lttng_ust_ctl_atype_float
;
1060 case lttng_ust_type_string
:
1062 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1063 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1067 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1068 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1072 ret
= serialize_string_encoding(&encoding
, lttng_ust_get_type_string(lt
)->encoding
);
1075 ut
->u
.string
.encoding
= encoding
;
1076 ut
->atype
= lttng_ust_ctl_atype_string
;
1080 case lttng_ust_type_array
:
1082 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1083 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1086 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1087 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1091 ut
->atype
= lttng_ust_ctl_atype_array_nestable
;
1092 ut
->u
.array_nestable
.length
= lttng_ust_get_type_array(lt
)->length
;
1093 ut
->u
.array_nestable
.alignment
= lttng_ust_get_type_array(lt
)->alignment
;
1096 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1097 lttng_ust_get_type_array(lt
)->elem_type
,
1098 lttng_ust_get_type_array(lt
)->encoding
, NULL
);
1103 case lttng_ust_type_sequence
:
1105 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1106 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1107 const char *length_name
= lttng_ust_get_type_sequence(lt
)->length_name
;
1110 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1111 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1115 ut
->atype
= lttng_ust_ctl_atype_sequence_nestable
;
1117 * If length_name field is NULL, use the previous field
1121 length_name
= prev_field_name
;
1124 strncpy(ut
->u
.sequence_nestable
.length_name
,
1125 length_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1126 ut
->u
.sequence_nestable
.length_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1127 ut
->u
.sequence_nestable
.alignment
= lttng_ust_get_type_sequence(lt
)->alignment
;
1130 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1131 lttng_ust_get_type_sequence(lt
)->elem_type
,
1132 lttng_ust_get_type_sequence(lt
)->encoding
, NULL
);
1137 case lttng_ust_type_dynamic
:
1139 ret
= serialize_dynamic_type(session
, fields
, iter_output
,
1145 case lttng_ust_type_struct
:
1147 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1150 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1151 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1155 uf
->type
.atype
= lttng_ust_ctl_atype_struct_nestable
;
1156 uf
->type
.u
.struct_nestable
.nr_fields
= lttng_ust_get_type_struct(lt
)->nr_fields
;
1157 uf
->type
.u
.struct_nestable
.alignment
= lttng_ust_get_type_struct(lt
)->alignment
;
1160 ret
= serialize_fields(session
, fields
, iter_output
,
1161 lttng_ust_get_type_struct(lt
)->nr_fields
,
1162 lttng_ust_get_type_struct(lt
)->fields
);
1167 case lttng_ust_type_enum
:
1169 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1170 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1173 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1174 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1178 strncpy(ut
->u
.enum_nestable
.name
, lttng_ust_get_type_enum(lt
)->desc
->name
,
1179 LTTNG_UST_ABI_SYM_NAME_LEN
);
1180 ut
->u
.enum_nestable
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1181 ut
->atype
= lttng_ust_ctl_atype_enum_nestable
;
1184 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1185 lttng_ust_get_type_enum(lt
)->container_type
,
1186 lttng_ust_string_encoding_none
, NULL
);
1190 const struct lttng_enum
*_enum
;
1192 _enum
= lttng_ust_enum_get_from_desc(session
, lttng_ust_get_type_enum(lt
)->desc
);
1195 ut
->u
.enum_nestable
.id
= _enum
->id
;
1197 ut
->u
.enum_nestable
.id
= -1ULL;
1208 int serialize_one_field(struct lttng_ust_session
*session
,
1209 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
1210 const struct lttng_ust_event_field
*lf
,
1211 const char **prev_field_name_p
)
1213 const char *prev_field_name
= NULL
;
1216 /* skip 'nowrite' fields */
1220 if (prev_field_name_p
)
1221 prev_field_name
= *prev_field_name_p
;
1222 ret
= serialize_one_type(session
, fields
, iter_output
, lf
->name
, lf
->type
,
1223 lttng_ust_string_encoding_none
, prev_field_name
);
1224 if (prev_field_name_p
)
1225 *prev_field_name_p
= lf
->name
;
1230 int serialize_fields(struct lttng_ust_session
*session
,
1231 struct lttng_ust_ctl_field
*lttng_ust_ctl_fields
,
1232 size_t *iter_output
, size_t nr_lttng_fields
,
1233 const struct lttng_ust_event_field
* const *lttng_fields
)
1235 const char *prev_field_name
= NULL
;
1239 for (i
= 0; i
< nr_lttng_fields
; i
++) {
1240 ret
= serialize_one_field(session
, lttng_ust_ctl_fields
,
1241 iter_output
, lttng_fields
[i
],
1250 int alloc_serialize_fields(struct lttng_ust_session
*session
,
1251 size_t *_nr_write_fields
,
1252 struct lttng_ust_ctl_field
**lttng_ust_ctl_fields
,
1254 const struct lttng_ust_event_field
* const *lttng_fields
)
1256 struct lttng_ust_ctl_field
*fields
;
1258 size_t iter_output
= 0;
1259 ssize_t nr_write_fields
;
1261 nr_write_fields
= count_fields_recursive(nr_fields
, lttng_fields
);
1262 if (nr_write_fields
< 0) {
1263 return (int) nr_write_fields
;
1266 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1270 ret
= serialize_fields(session
, fields
, &iter_output
, nr_fields
,
1275 *_nr_write_fields
= nr_write_fields
;
1276 *lttng_ust_ctl_fields
= fields
;
1285 int serialize_entries(struct lttng_ust_ctl_enum_entry
**_entries
,
1287 const struct lttng_ust_enum_entry
* const *lttng_entries
)
1289 struct lttng_ust_ctl_enum_entry
*entries
;
1292 /* Serialize the entries */
1293 entries
= zmalloc(nr_entries
* sizeof(*entries
));
1296 for (i
= 0; i
< nr_entries
; i
++) {
1297 struct lttng_ust_ctl_enum_entry
*uentry
;
1298 const struct lttng_ust_enum_entry
*lentry
;
1300 uentry
= &entries
[i
];
1301 lentry
= lttng_entries
[i
];
1303 uentry
->start
.value
= lentry
->start
.value
;
1304 uentry
->start
.signedness
= lentry
->start
.signedness
;
1305 uentry
->end
.value
= lentry
->end
.value
;
1306 uentry
->end
.signedness
= lentry
->end
.signedness
;
1307 strncpy(uentry
->string
, lentry
->string
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1308 uentry
->string
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1310 if (lentry
->options
& LTTNG_UST_ENUM_ENTRY_OPTION_IS_AUTO
) {
1311 uentry
->u
.extra
.options
|=
1312 LTTNG_UST_CTL_UST_ENUM_ENTRY_OPTION_IS_AUTO
;
1315 *_entries
= entries
;
1320 int serialize_ctx_fields(struct lttng_ust_session
*session
,
1321 size_t *_nr_write_fields
,
1322 struct lttng_ust_ctl_field
**lttng_ust_ctl_fields
,
1324 struct lttng_ust_ctx_field
*lttng_fields
)
1326 struct lttng_ust_ctl_field
*fields
;
1327 const char *prev_field_name
= NULL
;
1328 size_t i
, iter_output
= 0;
1329 ssize_t nr_write_fields
;
1332 nr_write_fields
= count_ctx_fields_recursive(nr_fields
,
1334 if (nr_write_fields
< 0) {
1335 return (int) nr_write_fields
;
1338 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1342 for (i
= 0; i
< nr_fields
; i
++) {
1343 ret
= serialize_one_field(session
, fields
, &iter_output
,
1344 lttng_fields
[i
].event_field
, &prev_field_name
);
1349 *_nr_write_fields
= nr_write_fields
;
1350 *lttng_ust_ctl_fields
= fields
;
1359 * Returns 0 on success, negative error value on error.
1361 int ustcomm_register_event(int sock
,
1362 struct lttng_ust_session
*session
,
1363 int session_objd
, /* session descriptor */
1364 int channel_objd
, /* channel descriptor */
1365 const char *event_name
, /* event name (input) */
1367 const char *signature
, /* event signature (input) */
1368 size_t nr_fields
, /* fields */
1369 const struct lttng_ust_event_field
* const *lttng_fields
,
1370 const char *model_emf_uri
,
1371 uint32_t *id
) /* event id (output) */
1375 struct ustcomm_notify_hdr header
;
1376 struct ustcomm_notify_event_msg m
;
1379 struct ustcomm_notify_hdr header
;
1380 struct ustcomm_notify_event_reply r
;
1382 size_t signature_len
, fields_len
, model_emf_uri_len
;
1383 struct lttng_ust_ctl_field
*fields
= NULL
;
1384 size_t nr_write_fields
= 0;
1387 memset(&msg
, 0, sizeof(msg
));
1388 msg
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_EVENT
;
1389 msg
.m
.session_objd
= session_objd
;
1390 msg
.m
.channel_objd
= channel_objd
;
1391 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1392 msg
.m
.event_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1393 msg
.m
.loglevel
= loglevel
;
1394 signature_len
= strlen(signature
) + 1;
1395 msg
.m
.signature_len
= signature_len
;
1397 /* Calculate fields len, serialize fields. */
1398 if (nr_fields
> 0) {
1399 ret
= alloc_serialize_fields(session
, &nr_write_fields
, &fields
,
1400 nr_fields
, lttng_fields
);
1405 fields_len
= sizeof(*fields
) * nr_write_fields
;
1406 msg
.m
.fields_len
= fields_len
;
1407 if (model_emf_uri
) {
1408 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
1410 model_emf_uri_len
= 0;
1412 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
1414 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1415 if (len
> 0 && len
!= sizeof(msg
)) {
1424 /* send signature */
1425 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
1426 if (len
> 0 && len
!= signature_len
) {
1436 if (fields_len
> 0) {
1437 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1438 if (len
> 0 && len
!= fields_len
) {
1449 if (model_emf_uri_len
) {
1450 /* send model_emf_uri */
1451 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
1453 if (len
> 0 && len
!= model_emf_uri_len
) {
1462 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1464 case 0: /* orderly shutdown */
1467 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1468 ERR("Unexpected result message command "
1469 "expected: %u vs received: %u\n",
1470 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1473 if (reply
.r
.ret_code
> 0)
1475 if (reply
.r
.ret_code
< 0)
1476 return reply
.r
.ret_code
;
1477 *id
= reply
.r
.event_id
;
1478 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1479 event_name
, reply
.r
.ret_code
, reply
.r
.event_id
);
1483 /* Transport level error */
1484 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1488 ERR("incorrect message size: %zd\n", len
);
1494 /* Error path only. */
1501 * Returns 0 on success, negative error value on error.
1502 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1504 int ustcomm_register_enum(int sock
,
1505 int session_objd
, /* session descriptor */
1506 const char *enum_name
, /* enum name (input) */
1507 size_t nr_entries
, /* entries */
1508 const struct lttng_ust_enum_entry
* const *lttng_entries
,
1513 struct ustcomm_notify_hdr header
;
1514 struct ustcomm_notify_enum_msg m
;
1517 struct ustcomm_notify_hdr header
;
1518 struct ustcomm_notify_enum_reply r
;
1521 struct lttng_ust_ctl_enum_entry
*entries
= NULL
;
1524 memset(&msg
, 0, sizeof(msg
));
1525 msg
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_ENUM
;
1526 msg
.m
.session_objd
= session_objd
;
1527 strncpy(msg
.m
.enum_name
, enum_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1528 msg
.m
.enum_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1530 /* Calculate entries len, serialize entries. */
1531 if (nr_entries
> 0) {
1532 ret
= serialize_entries(&entries
,
1533 nr_entries
, lttng_entries
);
1538 entries_len
= sizeof(*entries
) * nr_entries
;
1539 msg
.m
.entries_len
= entries_len
;
1541 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1542 if (len
> 0 && len
!= sizeof(msg
)) {
1552 if (entries_len
> 0) {
1553 len
= ustcomm_send_unix_sock(sock
, entries
, entries_len
);
1554 if (len
> 0 && len
!= entries_len
) {
1567 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1569 case 0: /* orderly shutdown */
1572 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1573 ERR("Unexpected result message command "
1574 "expected: %u vs received: %u\n",
1575 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1578 if (reply
.r
.ret_code
> 0)
1580 if (reply
.r
.ret_code
< 0)
1581 return reply
.r
.ret_code
;
1582 *id
= reply
.r
.enum_id
;
1583 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1584 enum_name
, reply
.r
.ret_code
);
1588 /* Transport level error */
1589 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1593 ERR("incorrect message size: %zd\n", len
);
1605 * Returns 0 on success, negative error value on error.
1606 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1608 int ustcomm_register_channel(int sock
,
1609 struct lttng_ust_session
*session
,
1610 int session_objd
, /* session descriptor */
1611 int channel_objd
, /* channel descriptor */
1612 size_t nr_ctx_fields
,
1613 struct lttng_ust_ctx_field
*ctx_fields
,
1614 uint32_t *chan_id
, /* channel id (output) */
1615 int *header_type
) /* header type (output) */
1619 struct ustcomm_notify_hdr header
;
1620 struct ustcomm_notify_channel_msg m
;
1623 struct ustcomm_notify_hdr header
;
1624 struct ustcomm_notify_channel_reply r
;
1627 struct lttng_ust_ctl_field
*fields
= NULL
;
1629 size_t nr_write_fields
= 0;
1631 memset(&msg
, 0, sizeof(msg
));
1632 msg
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
;
1633 msg
.m
.session_objd
= session_objd
;
1634 msg
.m
.channel_objd
= channel_objd
;
1636 /* Calculate fields len, serialize fields. */
1637 if (nr_ctx_fields
> 0) {
1638 ret
= serialize_ctx_fields(session
, &nr_write_fields
, &fields
,
1639 nr_ctx_fields
, ctx_fields
);
1644 fields_len
= sizeof(*fields
) * nr_write_fields
;
1645 msg
.m
.ctx_fields_len
= fields_len
;
1646 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1647 if (len
> 0 && len
!= sizeof(msg
)) {
1657 if (fields_len
> 0) {
1658 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1660 if (len
> 0 && len
!= fields_len
) {
1670 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1672 case 0: /* orderly shutdown */
1675 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1676 ERR("Unexpected result message command "
1677 "expected: %u vs received: %u\n",
1678 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1681 if (reply
.r
.ret_code
> 0)
1683 if (reply
.r
.ret_code
< 0)
1684 return reply
.r
.ret_code
;
1685 *chan_id
= reply
.r
.chan_id
;
1686 switch (reply
.r
.header_type
) {
1689 *header_type
= reply
.r
.header_type
;
1692 ERR("Unexpected channel header type %u\n",
1693 reply
.r
.header_type
);
1696 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1697 reply
.r
.chan_id
, reply
.r
.header_type
);
1701 /* Transport level error */
1702 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1706 ERR("incorrect message size: %zd\n", len
);
1713 * Set socket receiving timeout.
1715 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1720 tv
.tv_sec
= msec
/ 1000;
1721 tv
.tv_usec
= (msec
* 1000 % 1000000);
1723 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1725 PERROR("setsockopt SO_RCVTIMEO");
1733 * Set socket sending timeout.
1735 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1740 tv
.tv_sec
= msec
/ 1000;
1741 tv
.tv_usec
= (msec
* 1000) % 1000000;
1743 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1745 PERROR("setsockopt SO_SNDTIMEO");