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>
23 #include <lttng/ust-ctl.h>
24 #include "common/ustcomm.h"
25 #include "common/ust-fd.h"
26 #include "common/macros.h"
27 #include "common/dynamic-type.h"
28 #include "common/logging.h"
30 #include "common/events.h"
31 #include "common/compat/pthread.h"
33 #define USTCOMM_MAX_SEND_FDS 4
36 ssize_t
count_fields_recursive(size_t nr_fields
,
37 const struct lttng_ust_event_field
* const *lttng_fields
);
39 int serialize_one_field(struct lttng_ust_session
*session
,
40 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
41 const struct lttng_ust_event_field
*lf
,
42 const char **prev_field_name
);
44 int serialize_fields(struct lttng_ust_session
*session
,
45 struct lttng_ust_ctl_field
*lttng_ust_ctl_fields
,
46 size_t *iter_output
, size_t nr_lttng_fields
,
47 const struct lttng_ust_event_field
* const *lttng_fields
);
50 * ustcomm_connect_unix_sock
52 * Connect to unix socket using the path name.
54 * Caller handles FD tracker.
56 int ustcomm_connect_unix_sock(const char *pathname
, long timeout
)
58 struct sockaddr_un sun
;
62 * libust threads require the close-on-exec flag for all
63 * resources so it does not leak file descriptors upon exec.
65 fd
= socket(PF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0);
72 /* Give at least 10ms. */
75 ret
= ustcomm_setsockopt_snd_timeout(fd
, timeout
);
77 WARN("Error setting connect socket send timeout");
81 memset(&sun
, 0, sizeof(sun
));
82 sun
.sun_family
= AF_UNIX
;
83 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
84 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
86 DBG("Connecting to '%s'", sun
.sun_path
);
87 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
90 * Don't print message on connect ENOENT error, because
91 * connect is used in normal execution to detect if
92 * sessiond is alive. ENOENT is when the unix socket
93 * file does not exist, and ECONNREFUSED is when the
94 * file exists but no sessiond is listening.
96 if (errno
!= ECONNREFUSED
&& errno
!= ECONNRESET
97 && errno
!= ENOENT
&& errno
!= EACCES
)
100 if (ret
== -ECONNREFUSED
|| ret
== -ECONNRESET
)
111 closeret
= close(fd
);
120 * ustcomm_accept_unix_sock
122 * Do an accept(2) on the sock and return the
123 * new file descriptor. The socket MUST be bind(2) before.
125 int ustcomm_accept_unix_sock(int sock
)
128 struct sockaddr_un sun
;
132 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
134 if (errno
!= ECONNABORTED
)
137 if (new_fd
== -ECONNABORTED
)
144 * ustcomm_create_unix_sock
146 * Creates a AF_UNIX local socket using pathname
147 * bind the socket upon creation and return the fd.
149 int ustcomm_create_unix_sock(const char *pathname
)
151 struct sockaddr_un sun
;
154 /* Create server socket */
155 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
161 memset(&sun
, 0, sizeof(sun
));
162 sun
.sun_family
= AF_UNIX
;
163 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
164 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
166 /* Unlink the old file if present */
167 (void) unlink(pathname
);
168 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
181 closeret
= close(fd
);
191 * ustcomm_listen_unix_sock
193 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
195 int ustcomm_listen_unix_sock(int sock
)
199 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
209 * ustcomm_close_unix_sock
213 * Handles fd tracker internally.
215 int ustcomm_close_unix_sock(int sock
)
219 lttng_ust_lock_fd_tracker();
222 lttng_ust_delete_fd_from_tracker(sock
);
227 lttng_ust_unlock_fd_tracker();
233 * ustcomm_shutdown_unix_sock
235 * Shutdown unix socket. Keeps the file descriptor open, but shutdown
238 int ustcomm_shutdown_unix_sock(int sock
)
242 ret
= shutdown(sock
, SHUT_RDWR
);
244 PERROR("Socket shutdown error");
251 * ustcomm_recv_unix_sock
253 * Receive data of size len in put that data into
254 * the buf param. Using recvmsg API.
255 * Return the size of received data.
256 * Return 0 on orderly shutdown.
258 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
265 memset(&msg
, 0, sizeof(msg
));
267 iov
[0].iov_base
= buf
;
268 iov
[0].iov_len
= len
;
273 len_last
= iov
[0].iov_len
;
274 ret
= recvmsg(sock
, &msg
, 0);
276 iov
[0].iov_base
+= ret
;
277 iov
[0].iov_len
-= ret
;
278 assert(ret
<= len_last
);
280 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
283 if (errno
!= EPIPE
&& errno
!= ECONNRESET
&& errno
!= ECONNREFUSED
)
286 if (ret
== -ECONNRESET
|| ret
== -ECONNREFUSED
)
289 (void) ustcomm_shutdown_unix_sock(sock
);
290 } else if (ret
> 0) {
293 /* ret = 0 means an orderly shutdown. */
299 * ustcomm_send_unix_sock
301 * Send buf data of size len. Using sendmsg API.
302 * Return the size of sent data.
304 ssize_t
ustcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
310 memset(&msg
, 0, sizeof(msg
));
312 iov
[0].iov_base
= (void *) buf
;
313 iov
[0].iov_len
= len
;
318 * Using the MSG_NOSIGNAL when sending data from sessiond to
319 * libust, so libust does not receive an unhandled SIGPIPE or
320 * SIGURG. The sessiond receiver side can be made more resilient
321 * by ignoring SIGPIPE, but we don't have this luxury on the
325 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
326 } while (ret
< 0 && errno
== EINTR
);
329 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
332 if (ret
== -ECONNRESET
)
335 (void) ustcomm_shutdown_unix_sock(sock
);
342 * Send a message accompanied by fd(s) over a unix socket.
344 * Returns the size of data sent, or negative error value.
346 ssize_t
ustcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
349 struct cmsghdr
*cmptr
;
352 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
353 char tmp
[CMSG_SPACE(sizeof_fds
)];
356 memset(&msg
, 0, sizeof(msg
));
357 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
359 if (nb_fd
> USTCOMM_MAX_SEND_FDS
)
362 msg
.msg_control
= (caddr_t
)tmp
;
363 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
365 cmptr
= CMSG_FIRSTHDR(&msg
);
368 cmptr
->cmsg_level
= SOL_SOCKET
;
369 cmptr
->cmsg_type
= SCM_RIGHTS
;
370 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
371 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
372 /* Sum of the length of all control messages in the buffer: */
373 msg
.msg_controllen
= cmptr
->cmsg_len
;
375 iov
[0].iov_base
= &dummy
;
381 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
382 } while (ret
< 0 && errno
== EINTR
);
385 * We consider EPIPE and ECONNRESET as expected.
387 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
391 if (ret
== -ECONNRESET
)
398 * Recv a message accompanied by fd(s) from a unix socket.
400 * Expect at most "nb_fd" file descriptors. Returns the number of fd
401 * actually received in nb_fd.
402 * Returns -EPIPE on orderly shutdown.
404 ssize_t
ustcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
408 struct cmsghdr
*cmsg
;
409 size_t sizeof_fds
= nb_fd
* sizeof(int);
410 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
414 memset(&msg
, 0, sizeof(msg
));
416 /* Prepare to receive the structures */
417 iov
[0].iov_base
= &dummy
;
421 msg
.msg_control
= recv_fd
;
422 msg
.msg_controllen
= sizeof(recv_fd
);
425 ret
= recvmsg(sock
, &msg
, MSG_CMSG_CLOEXEC
);
426 } while (ret
< 0 && errno
== EINTR
);
428 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
429 PERROR("recvmsg fds");
432 if (ret
== -ECONNRESET
)
437 /* orderly shutdown */
442 ERR("Error: Received %zd bytes, expected %d\n",
446 if (msg
.msg_flags
& MSG_CTRUNC
) {
447 ERR("Error: Control message truncated.\n");
451 cmsg
= CMSG_FIRSTHDR(&msg
);
453 ERR("Error: Invalid control message header\n");
457 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
458 ERR("Didn't received any fd\n");
462 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
463 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
464 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
469 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
476 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
480 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
488 ERR("incorrect message size: %zd\n", len
);
495 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
496 uint32_t expected_handle
, uint32_t expected_cmd
)
500 memset(lur
, 0, sizeof(*lur
));
501 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
503 case 0: /* orderly shutdown */
509 if (lur
->handle
!= expected_handle
) {
510 ERR("Unexpected result message handle: "
511 "expected: %u vs received: %u\n",
512 expected_handle
, lur
->handle
);
515 if (lur
->cmd
!= expected_cmd
) {
516 ERR("Unexpected result message command "
517 "expected: %u vs received: %u\n",
518 expected_cmd
, lur
->cmd
);
524 return lur
->ret_code
;
529 ERR("incorrect message size: %zd\n", len
);
535 int ustcomm_send_app_cmd(int sock
,
536 struct ustcomm_ust_msg
*lum
,
537 struct ustcomm_ust_reply
*lur
)
541 ret
= ustcomm_send_app_msg(sock
, lum
);
544 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
551 * chan_data is allocated internally if this function returns the
554 ssize_t
ustcomm_recv_channel_from_sessiond(int sock
,
555 void **_chan_data
, uint64_t var_len
,
562 if (var_len
> LTTNG_UST_ABI_CHANNEL_DATA_MAX_LEN
) {
566 /* Receive variable length data */
567 chan_data
= zmalloc(var_len
);
572 len
= ustcomm_recv_unix_sock(sock
, chan_data
, var_len
);
573 if (len
!= var_len
) {
577 lttng_ust_lock_fd_tracker();
578 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
580 lttng_ust_unlock_fd_tracker();
590 ret
= lttng_ust_add_fd_to_tracker(wakeup_fd
);
592 ret
= close(wakeup_fd
);
594 PERROR("close on wakeup_fd");
597 lttng_ust_unlock_fd_tracker();
602 lttng_ust_unlock_fd_tracker();
604 *_chan_data
= chan_data
;
614 ssize_t
ustcomm_recv_event_notifier_notif_fd_from_sessiond(int sock
,
615 int *_event_notifier_notif_fd
)
618 int event_notifier_notif_fd
, ret
;
620 /* Receive event_notifier notification fd */
621 lttng_ust_lock_fd_tracker();
622 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &event_notifier_notif_fd
, 1);
624 lttng_ust_unlock_fd_tracker();
634 ret
= lttng_ust_add_fd_to_tracker(event_notifier_notif_fd
);
636 ret
= close(event_notifier_notif_fd
);
638 PERROR("close on event_notifier notif fd");
641 lttng_ust_unlock_fd_tracker();
645 *_event_notifier_notif_fd
= ret
;
646 lttng_ust_unlock_fd_tracker();
654 int ustcomm_recv_stream_from_sessiond(int sock
,
655 uint64_t *memory_map_size
__attribute__((unused
)),
656 int *shm_fd
, int *wakeup_fd
)
662 /* recv shm fd and wakeup fd */
663 lttng_ust_lock_fd_tracker();
664 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
666 lttng_ust_unlock_fd_tracker();
676 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
680 PERROR("close on received shm_fd");
683 lttng_ust_unlock_fd_tracker();
688 ret
= lttng_ust_add_fd_to_tracker(fds
[1]);
690 ret
= close(*shm_fd
);
692 PERROR("close on shm_fd");
697 PERROR("close on received wakeup_fd");
700 lttng_ust_unlock_fd_tracker();
704 lttng_ust_unlock_fd_tracker();
711 ssize_t
ustcomm_recv_var_len_cmd_from_sessiond(int sock
,
712 void **_data
, uint32_t var_len
)
717 if (var_len
> LTTNG_UST_ABI_CMD_MAX_LEN
) {
721 /* Receive variable length data */
722 data
= zmalloc(var_len
);
727 len
= ustcomm_recv_unix_sock(sock
, data
, var_len
);
728 if (len
!= var_len
) {
741 int ustcomm_recv_counter_shm_from_sessiond(int sock
,
749 lttng_ust_lock_fd_tracker();
750 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 1);
752 lttng_ust_unlock_fd_tracker();
762 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
766 PERROR("close on received shm_fd");
769 lttng_ust_unlock_fd_tracker();
773 lttng_ust_unlock_fd_tracker();
781 * Returns 0 on success, negative error value on error.
783 int ustcomm_send_reg_msg(int sock
,
784 enum lttng_ust_ctl_socket_type type
,
785 uint32_t bits_per_long
,
786 uint32_t uint8_t_alignment
,
787 uint32_t uint16_t_alignment
,
788 uint32_t uint32_t_alignment
,
789 uint32_t uint64_t_alignment
,
790 uint32_t long_alignment
,
791 const char *procname
)
794 struct lttng_ust_ctl_reg_msg reg_msg
;
796 reg_msg
.magic
= LTTNG_UST_ABI_COMM_MAGIC
;
797 reg_msg
.major
= LTTNG_UST_ABI_MAJOR_VERSION
;
798 reg_msg
.minor
= LTTNG_UST_ABI_MINOR_VERSION
;
799 reg_msg
.pid
= getpid();
800 reg_msg
.ppid
= getppid();
801 reg_msg
.uid
= getuid();
802 reg_msg
.gid
= getgid();
803 reg_msg
.bits_per_long
= bits_per_long
;
804 reg_msg
.uint8_t_alignment
= uint8_t_alignment
;
805 reg_msg
.uint16_t_alignment
= uint16_t_alignment
;
806 reg_msg
.uint32_t_alignment
= uint32_t_alignment
;
807 reg_msg
.uint64_t_alignment
= uint64_t_alignment
;
808 reg_msg
.long_alignment
= long_alignment
;
809 reg_msg
.socket_type
= type
;
810 memset(reg_msg
.name
, 0, sizeof(reg_msg
.name
));
811 strncpy(reg_msg
.name
, procname
, sizeof(reg_msg
.name
) - 1);
812 memset(reg_msg
.padding
, 0, sizeof(reg_msg
.padding
));
814 len
= ustcomm_send_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
815 if (len
> 0 && len
!= sizeof(reg_msg
))
823 ssize_t
count_one_type(const struct lttng_ust_type_common
*lt
)
826 case lttng_ust_type_integer
:
827 case lttng_ust_type_float
:
828 case lttng_ust_type_string
:
830 case lttng_ust_type_enum
:
831 return count_one_type(lttng_ust_get_type_enum(lt
)->container_type
) + 1;
832 case lttng_ust_type_array
:
833 return count_one_type(lttng_ust_get_type_array(lt
)->elem_type
) + 1;
834 case lttng_ust_type_sequence
:
835 return count_one_type(lttng_ust_get_type_sequence(lt
)->elem_type
) + 1;
836 case lttng_ust_type_struct
:
837 return count_fields_recursive(lttng_ust_get_type_struct(lt
)->nr_fields
,
838 lttng_ust_get_type_struct(lt
)->fields
) + 1;
840 case lttng_ust_type_dynamic
:
842 const struct lttng_ust_event_field
* const *choices
;
846 ret
= lttng_ust_dynamic_type_choices(&nr_choices
,
851 * Two fields for enum, one field for variant, and
852 * one field per choice.
854 return count_fields_recursive(nr_choices
, choices
) + 3;
864 ssize_t
count_fields_recursive(size_t nr_fields
,
865 const struct lttng_ust_event_field
* const *lttng_fields
)
868 ssize_t ret
, count
= 0;
870 for (i
= 0; i
< nr_fields
; i
++) {
871 const struct lttng_ust_event_field
*lf
;
873 lf
= lttng_fields
[i
];
874 /* skip 'nowrite' fields */
877 ret
= count_one_type(lf
->type
);
879 return ret
; /* error */
886 ssize_t
count_ctx_fields_recursive(size_t nr_fields
,
887 struct lttng_ust_ctx_field
*lttng_fields
)
890 ssize_t ret
, count
= 0;
892 for (i
= 0; i
< nr_fields
; i
++) {
893 const struct lttng_ust_event_field
*lf
;
895 lf
= lttng_fields
[i
].event_field
;
896 /* skip 'nowrite' fields */
899 ret
= count_one_type(lf
->type
);
901 return ret
; /* error */
908 int serialize_string_encoding(int32_t *ue
,
909 enum lttng_ust_string_encoding le
)
912 case lttng_ust_string_encoding_none
:
913 *ue
= lttng_ust_ctl_encode_none
;
915 case lttng_ust_string_encoding_UTF8
:
916 *ue
= lttng_ust_ctl_encode_UTF8
;
918 case lttng_ust_string_encoding_ASCII
:
919 *ue
= lttng_ust_ctl_encode_ASCII
;
928 int serialize_integer_type(struct lttng_ust_ctl_integer_type
*uit
,
929 const struct lttng_ust_type_integer
*lit
,
930 enum lttng_ust_string_encoding lencoding
)
934 uit
->size
= lit
->size
;
935 uit
->signedness
= lit
->signedness
;
936 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
937 uit
->base
= lit
->base
;
938 if (serialize_string_encoding(&encoding
, lencoding
))
940 uit
->encoding
= encoding
;
941 uit
->alignment
= lit
->alignment
;
946 int serialize_dynamic_type(struct lttng_ust_session
*session
,
947 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
948 const char *field_name
)
950 const struct lttng_ust_event_field
* const *choices
;
951 char tag_field_name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
952 const struct lttng_ust_type_common
*tag_type
;
953 const struct lttng_ust_event_field
*tag_field_generic
;
954 struct lttng_ust_event_field tag_field
= {
955 .name
= tag_field_name
,
958 struct lttng_ust_ctl_field
*uf
;
959 size_t nr_choices
, i
;
962 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
963 tag_type
= tag_field_generic
->type
;
965 /* Serialize enum field. */
966 strncpy(tag_field_name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
967 tag_field_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
968 strncat(tag_field_name
,
970 LTTNG_UST_ABI_SYM_NAME_LEN
- strlen(tag_field_name
) - 1);
971 tag_field
.type
= tag_type
;
972 ret
= serialize_one_field(session
, fields
, iter_output
,
977 /* Serialize variant field. */
978 uf
= &fields
[*iter_output
];
979 ret
= lttng_ust_dynamic_type_choices(&nr_choices
, &choices
);
983 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
984 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
985 uf
->type
.atype
= lttng_ust_ctl_atype_variant_nestable
;
986 uf
->type
.u
.variant_nestable
.nr_choices
= nr_choices
;
987 strncpy(uf
->type
.u
.variant_nestable
.tag_name
,
989 LTTNG_UST_ABI_SYM_NAME_LEN
);
990 uf
->type
.u
.variant_nestable
.tag_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
991 uf
->type
.u
.variant_nestable
.alignment
= 0;
994 /* Serialize choice fields after variant. */
995 for (i
= 0; i
< nr_choices
; i
++) {
996 ret
= serialize_one_field(session
, fields
,
997 iter_output
, choices
[i
], NULL
);
1005 int serialize_one_type(struct lttng_ust_session
*session
,
1006 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
1007 const char *field_name
, const struct lttng_ust_type_common
*lt
,
1008 enum lttng_ust_string_encoding parent_encoding
,
1009 const char *prev_field_name
)
1014 * Serializing a type (rather than a field) generates a lttng_ust_ctl_field
1015 * entry with 0-length name.
1019 case lttng_ust_type_integer
:
1021 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1022 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1025 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1026 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1030 ret
= serialize_integer_type(&ut
->u
.integer
, lttng_ust_get_type_integer(lt
),
1034 ut
->atype
= lttng_ust_ctl_atype_integer
;
1038 case lttng_ust_type_float
:
1040 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1041 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1042 struct lttng_ust_ctl_float_type
*uft
;
1043 const struct lttng_ust_type_float
*lft
;
1046 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1047 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1051 uft
= &ut
->u
._float
;
1052 lft
= lttng_ust_get_type_float(lt
);
1053 uft
->exp_dig
= lft
->exp_dig
;
1054 uft
->mant_dig
= lft
->mant_dig
;
1055 uft
->alignment
= lft
->alignment
;
1056 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
1057 ut
->atype
= lttng_ust_ctl_atype_float
;
1061 case lttng_ust_type_string
:
1063 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1064 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1068 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1069 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1073 ret
= serialize_string_encoding(&encoding
, lttng_ust_get_type_string(lt
)->encoding
);
1076 ut
->u
.string
.encoding
= encoding
;
1077 ut
->atype
= lttng_ust_ctl_atype_string
;
1081 case lttng_ust_type_array
:
1083 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1084 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1087 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1088 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1092 ut
->atype
= lttng_ust_ctl_atype_array_nestable
;
1093 ut
->u
.array_nestable
.length
= lttng_ust_get_type_array(lt
)->length
;
1094 ut
->u
.array_nestable
.alignment
= lttng_ust_get_type_array(lt
)->alignment
;
1097 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1098 lttng_ust_get_type_array(lt
)->elem_type
,
1099 lttng_ust_get_type_array(lt
)->encoding
, NULL
);
1104 case lttng_ust_type_sequence
:
1106 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1107 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1108 const char *length_name
= lttng_ust_get_type_sequence(lt
)->length_name
;
1111 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1112 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1116 ut
->atype
= lttng_ust_ctl_atype_sequence_nestable
;
1118 * If length_name field is NULL, use the previous field
1122 length_name
= prev_field_name
;
1125 strncpy(ut
->u
.sequence_nestable
.length_name
,
1126 length_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1127 ut
->u
.sequence_nestable
.length_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1128 ut
->u
.sequence_nestable
.alignment
= lttng_ust_get_type_sequence(lt
)->alignment
;
1131 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1132 lttng_ust_get_type_sequence(lt
)->elem_type
,
1133 lttng_ust_get_type_sequence(lt
)->encoding
, NULL
);
1138 case lttng_ust_type_dynamic
:
1140 ret
= serialize_dynamic_type(session
, fields
, iter_output
,
1146 case lttng_ust_type_struct
:
1148 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1151 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1152 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1156 uf
->type
.atype
= lttng_ust_ctl_atype_struct_nestable
;
1157 uf
->type
.u
.struct_nestable
.nr_fields
= lttng_ust_get_type_struct(lt
)->nr_fields
;
1158 uf
->type
.u
.struct_nestable
.alignment
= lttng_ust_get_type_struct(lt
)->alignment
;
1161 ret
= serialize_fields(session
, fields
, iter_output
,
1162 lttng_ust_get_type_struct(lt
)->nr_fields
,
1163 lttng_ust_get_type_struct(lt
)->fields
);
1168 case lttng_ust_type_enum
:
1170 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1171 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1174 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1175 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1179 strncpy(ut
->u
.enum_nestable
.name
, lttng_ust_get_type_enum(lt
)->desc
->name
,
1180 LTTNG_UST_ABI_SYM_NAME_LEN
);
1181 ut
->u
.enum_nestable
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1182 ut
->atype
= lttng_ust_ctl_atype_enum_nestable
;
1185 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1186 lttng_ust_get_type_enum(lt
)->container_type
,
1187 lttng_ust_string_encoding_none
, NULL
);
1191 const struct lttng_enum
*_enum
;
1193 _enum
= lttng_ust_enum_get_from_desc(session
, lttng_ust_get_type_enum(lt
)->desc
);
1196 ut
->u
.enum_nestable
.id
= _enum
->id
;
1198 ut
->u
.enum_nestable
.id
= -1ULL;
1209 int serialize_one_field(struct lttng_ust_session
*session
,
1210 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
1211 const struct lttng_ust_event_field
*lf
,
1212 const char **prev_field_name_p
)
1214 const char *prev_field_name
= NULL
;
1217 /* skip 'nowrite' fields */
1221 if (prev_field_name_p
)
1222 prev_field_name
= *prev_field_name_p
;
1223 ret
= serialize_one_type(session
, fields
, iter_output
, lf
->name
, lf
->type
,
1224 lttng_ust_string_encoding_none
, prev_field_name
);
1225 if (prev_field_name_p
)
1226 *prev_field_name_p
= lf
->name
;
1231 int serialize_fields(struct lttng_ust_session
*session
,
1232 struct lttng_ust_ctl_field
*lttng_ust_ctl_fields
,
1233 size_t *iter_output
, size_t nr_lttng_fields
,
1234 const struct lttng_ust_event_field
* const *lttng_fields
)
1236 const char *prev_field_name
= NULL
;
1240 for (i
= 0; i
< nr_lttng_fields
; i
++) {
1241 ret
= serialize_one_field(session
, lttng_ust_ctl_fields
,
1242 iter_output
, lttng_fields
[i
],
1251 int alloc_serialize_fields(struct lttng_ust_session
*session
,
1252 size_t *_nr_write_fields
,
1253 struct lttng_ust_ctl_field
**lttng_ust_ctl_fields
,
1255 const struct lttng_ust_event_field
* const *lttng_fields
)
1257 struct lttng_ust_ctl_field
*fields
;
1259 size_t iter_output
= 0;
1260 ssize_t nr_write_fields
;
1262 nr_write_fields
= count_fields_recursive(nr_fields
, lttng_fields
);
1263 if (nr_write_fields
< 0) {
1264 return (int) nr_write_fields
;
1267 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1271 ret
= serialize_fields(session
, fields
, &iter_output
, nr_fields
,
1276 *_nr_write_fields
= nr_write_fields
;
1277 *lttng_ust_ctl_fields
= fields
;
1286 int serialize_entries(struct lttng_ust_ctl_enum_entry
**_entries
,
1288 const struct lttng_ust_enum_entry
* const *lttng_entries
)
1290 struct lttng_ust_ctl_enum_entry
*entries
;
1293 /* Serialize the entries */
1294 entries
= zmalloc(nr_entries
* sizeof(*entries
));
1297 for (i
= 0; i
< nr_entries
; i
++) {
1298 struct lttng_ust_ctl_enum_entry
*uentry
;
1299 const struct lttng_ust_enum_entry
*lentry
;
1301 uentry
= &entries
[i
];
1302 lentry
= lttng_entries
[i
];
1304 uentry
->start
.value
= lentry
->start
.value
;
1305 uentry
->start
.signedness
= lentry
->start
.signedness
;
1306 uentry
->end
.value
= lentry
->end
.value
;
1307 uentry
->end
.signedness
= lentry
->end
.signedness
;
1308 strncpy(uentry
->string
, lentry
->string
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1309 uentry
->string
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1311 if (lentry
->options
& LTTNG_UST_ENUM_ENTRY_OPTION_IS_AUTO
) {
1312 uentry
->u
.extra
.options
|=
1313 LTTNG_UST_CTL_UST_ENUM_ENTRY_OPTION_IS_AUTO
;
1316 *_entries
= entries
;
1321 int serialize_ctx_fields(struct lttng_ust_session
*session
,
1322 size_t *_nr_write_fields
,
1323 struct lttng_ust_ctl_field
**lttng_ust_ctl_fields
,
1325 struct lttng_ust_ctx_field
*lttng_fields
)
1327 struct lttng_ust_ctl_field
*fields
;
1328 const char *prev_field_name
= NULL
;
1329 size_t i
, iter_output
= 0;
1330 ssize_t nr_write_fields
;
1333 nr_write_fields
= count_ctx_fields_recursive(nr_fields
,
1335 if (nr_write_fields
< 0) {
1336 return (int) nr_write_fields
;
1339 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1343 for (i
= 0; i
< nr_fields
; i
++) {
1344 ret
= serialize_one_field(session
, fields
, &iter_output
,
1345 lttng_fields
[i
].event_field
, &prev_field_name
);
1350 *_nr_write_fields
= nr_write_fields
;
1351 *lttng_ust_ctl_fields
= fields
;
1360 * Returns 0 on success, negative error value on error.
1362 int ustcomm_register_event(int sock
,
1363 struct lttng_ust_session
*session
,
1364 int session_objd
, /* session descriptor */
1365 int channel_objd
, /* channel descriptor */
1366 const char *event_name
, /* event name (input) */
1368 const char *signature
, /* event signature (input) */
1369 size_t nr_fields
, /* fields */
1370 const struct lttng_ust_event_field
* const *lttng_fields
,
1371 const char *model_emf_uri
,
1372 uint64_t user_token
,
1373 uint32_t *id
) /* event id (output) */
1377 struct ustcomm_notify_hdr header
;
1378 struct ustcomm_notify_event_msg m
;
1381 struct ustcomm_notify_hdr header
;
1382 struct ustcomm_notify_event_reply r
;
1384 size_t signature_len
, fields_len
, model_emf_uri_len
;
1385 struct lttng_ust_ctl_field
*fields
= NULL
;
1386 size_t nr_write_fields
= 0;
1389 memset(&msg
, 0, sizeof(msg
));
1390 msg
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_EVENT
;
1391 msg
.m
.session_objd
= session_objd
;
1392 msg
.m
.channel_objd
= channel_objd
;
1393 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1394 msg
.m
.event_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1395 msg
.m
.loglevel
= loglevel
;
1396 msg
.m
.user_token
= user_token
;
1397 signature_len
= strlen(signature
) + 1;
1398 msg
.m
.signature_len
= signature_len
;
1400 /* Calculate fields len, serialize fields. */
1401 if (nr_fields
> 0) {
1402 ret
= alloc_serialize_fields(session
, &nr_write_fields
, &fields
,
1403 nr_fields
, lttng_fields
);
1408 fields_len
= sizeof(*fields
) * nr_write_fields
;
1409 msg
.m
.fields_len
= fields_len
;
1410 if (model_emf_uri
) {
1411 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
1413 model_emf_uri_len
= 0;
1415 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
1417 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1418 if (len
> 0 && len
!= sizeof(msg
)) {
1427 /* send signature */
1428 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
1429 if (len
> 0 && len
!= signature_len
) {
1439 if (fields_len
> 0) {
1440 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1441 if (len
> 0 && len
!= fields_len
) {
1452 if (model_emf_uri_len
) {
1453 /* send model_emf_uri */
1454 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
1456 if (len
> 0 && len
!= model_emf_uri_len
) {
1465 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1467 case 0: /* orderly shutdown */
1470 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1471 ERR("Unexpected result message command "
1472 "expected: %u vs received: %u\n",
1473 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1476 if (reply
.r
.ret_code
> 0)
1478 if (reply
.r
.ret_code
< 0)
1479 return reply
.r
.ret_code
;
1481 DBG("Sent register event notification for name \"%s\": ret_code %d, id %" PRIu32
"\n",
1482 event_name
, reply
.r
.ret_code
, reply
.r
.id
);
1486 /* Transport level error */
1487 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1491 ERR("incorrect message size: %zd\n", len
);
1497 /* Error path only. */
1504 * Returns 0 on success, negative error value on error.
1505 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1507 int ustcomm_register_key(int sock
,
1508 int session_objd
, /* session descriptor */
1509 int map_objd
, /* map descriptor */
1511 const uint64_t *dimension_indexes
,
1512 const char *key_string
, /* key string (input) */
1513 uint64_t user_token
,
1514 uint64_t *index
) /* (output) */
1518 struct ustcomm_notify_hdr header
;
1519 struct ustcomm_notify_key_msg m
;
1522 struct ustcomm_notify_hdr header
;
1523 struct ustcomm_notify_key_reply r
;
1525 size_t dimension_indexes_len
;
1528 memset(&msg
, 0, sizeof(msg
));
1529 msg
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_KEY
;
1530 msg
.m
.session_objd
= session_objd
;
1531 msg
.m
.map_objd
= map_objd
;
1532 msg
.m
.dimension
= dimension
;
1533 dimension_indexes_len
= sizeof(uint64_t) * dimension
;
1534 msg
.m
.key_string_len
= strlen(key_string
) + 1;
1535 msg
.m
.user_token
= user_token
;
1537 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1538 if (len
> 0 && len
!= sizeof(msg
)) {
1547 /* send dimension_indexes */
1548 if (dimension_indexes
) {
1549 len
= ustcomm_send_unix_sock(sock
, dimension_indexes
, dimension_indexes_len
);
1550 if (len
> 0 && len
!= dimension_indexes_len
) {
1552 goto error_dimension_indexes
;
1556 goto error_dimension_indexes
;
1560 /* send key_string */
1561 len
= ustcomm_send_unix_sock(sock
, key_string
, msg
.m
.key_string_len
);
1562 if (len
> 0 && len
!= dimension_indexes_len
) {
1564 goto error_dimension_indexes
;
1568 goto error_dimension_indexes
;
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 *index
= reply
.r
.index
;
1588 DBG("Sent register key notification for key \"%s\": ret_code %d, index %" PRIu64
"\n",
1589 key_string
, reply
.r
.ret_code
, reply
.r
.index
);
1593 /* Transport level error */
1594 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1598 ERR("incorrect message size: %zd\n", len
);
1604 /* Error path only. */
1605 error_dimension_indexes
:
1611 * Returns 0 on success, negative error value on error.
1612 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1614 int ustcomm_register_enum(int sock
,
1615 int session_objd
, /* session descriptor */
1616 const char *enum_name
, /* enum name (input) */
1617 size_t nr_entries
, /* entries */
1618 const struct lttng_ust_enum_entry
* const *lttng_entries
,
1623 struct ustcomm_notify_hdr header
;
1624 struct ustcomm_notify_enum_msg m
;
1627 struct ustcomm_notify_hdr header
;
1628 struct ustcomm_notify_enum_reply r
;
1631 struct lttng_ust_ctl_enum_entry
*entries
= NULL
;
1634 memset(&msg
, 0, sizeof(msg
));
1635 msg
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_ENUM
;
1636 msg
.m
.session_objd
= session_objd
;
1637 strncpy(msg
.m
.enum_name
, enum_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1638 msg
.m
.enum_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1640 /* Calculate entries len, serialize entries. */
1641 if (nr_entries
> 0) {
1642 ret
= serialize_entries(&entries
,
1643 nr_entries
, lttng_entries
);
1648 entries_len
= sizeof(*entries
) * nr_entries
;
1649 msg
.m
.entries_len
= entries_len
;
1651 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1652 if (len
> 0 && len
!= sizeof(msg
)) {
1662 if (entries_len
> 0) {
1663 len
= ustcomm_send_unix_sock(sock
, entries
, entries_len
);
1664 if (len
> 0 && len
!= entries_len
) {
1677 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1679 case 0: /* orderly shutdown */
1682 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1683 ERR("Unexpected result message command "
1684 "expected: %u vs received: %u\n",
1685 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1688 if (reply
.r
.ret_code
> 0)
1690 if (reply
.r
.ret_code
< 0)
1691 return reply
.r
.ret_code
;
1692 *id
= reply
.r
.enum_id
;
1693 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1694 enum_name
, reply
.r
.ret_code
);
1698 /* Transport level error */
1699 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1703 ERR("incorrect message size: %zd\n", len
);
1715 * Returns 0 on success, negative error value on error.
1716 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1718 int ustcomm_register_channel(int sock
,
1719 struct lttng_ust_session
*session
,
1720 int session_objd
, /* session descriptor */
1721 int channel_objd
, /* channel descriptor */
1722 size_t nr_ctx_fields
,
1723 struct lttng_ust_ctx_field
*ctx_fields
,
1724 uint32_t *chan_id
, /* channel id (output) */
1725 int *header_type
) /* header type (output) */
1729 struct ustcomm_notify_hdr header
;
1730 struct ustcomm_notify_channel_msg m
;
1733 struct ustcomm_notify_hdr header
;
1734 struct ustcomm_notify_channel_reply r
;
1737 struct lttng_ust_ctl_field
*fields
= NULL
;
1739 size_t nr_write_fields
= 0;
1741 memset(&msg
, 0, sizeof(msg
));
1742 msg
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
;
1743 msg
.m
.session_objd
= session_objd
;
1744 msg
.m
.channel_objd
= channel_objd
;
1746 /* Calculate fields len, serialize fields. */
1747 if (nr_ctx_fields
> 0) {
1748 ret
= serialize_ctx_fields(session
, &nr_write_fields
, &fields
,
1749 nr_ctx_fields
, ctx_fields
);
1754 fields_len
= sizeof(*fields
) * nr_write_fields
;
1755 msg
.m
.ctx_fields_len
= fields_len
;
1756 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1757 if (len
> 0 && len
!= sizeof(msg
)) {
1767 if (fields_len
> 0) {
1768 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1770 if (len
> 0 && len
!= fields_len
) {
1780 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1782 case 0: /* orderly shutdown */
1785 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1786 ERR("Unexpected result message command "
1787 "expected: %u vs received: %u\n",
1788 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1791 if (reply
.r
.ret_code
> 0)
1793 if (reply
.r
.ret_code
< 0)
1794 return reply
.r
.ret_code
;
1795 *chan_id
= reply
.r
.chan_id
;
1796 switch (reply
.r
.header_type
) {
1799 *header_type
= reply
.r
.header_type
;
1802 ERR("Unexpected channel header type %u\n",
1803 reply
.r
.header_type
);
1806 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1807 reply
.r
.chan_id
, reply
.r
.header_type
);
1811 /* Transport level error */
1812 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1816 ERR("incorrect message size: %zd\n", len
);
1823 * Set socket receiving timeout.
1825 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1830 tv
.tv_sec
= msec
/ 1000;
1831 tv
.tv_usec
= (msec
* 1000 % 1000000);
1833 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1835 PERROR("setsockopt SO_RCVTIMEO");
1843 * Set socket sending timeout.
1845 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1850 tv
.tv_sec
= msec
/ 1000;
1851 tv
.tv_usec
= (msec
* 1000) % 1000000;
1853 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1855 PERROR("setsockopt SO_SNDTIMEO");