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
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_shutdown_unix_sock
241 * Shutdown unix socket. Keeps the file descriptor open, but shutdown
244 int ustcomm_shutdown_unix_sock(int sock
)
248 ret
= shutdown(sock
, SHUT_RDWR
);
250 PERROR("Socket shutdown error");
257 * ustcomm_recv_unix_sock
259 * Receive data of size len in put that data into
260 * the buf param. Using recvmsg API.
261 * Return the size of received data.
262 * Return 0 on orderly shutdown.
264 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
271 memset(&msg
, 0, sizeof(msg
));
273 iov
[0].iov_base
= buf
;
274 iov
[0].iov_len
= len
;
279 len_last
= iov
[0].iov_len
;
280 ret
= recvmsg(sock
, &msg
, 0);
282 iov
[0].iov_base
+= ret
;
283 iov
[0].iov_len
-= ret
;
284 assert(ret
<= len_last
);
286 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
289 if (errno
!= EPIPE
&& errno
!= ECONNRESET
&& errno
!= ECONNREFUSED
)
292 if (ret
== -ECONNRESET
|| ret
== -ECONNREFUSED
)
295 (void) ustcomm_shutdown_unix_sock(sock
);
296 } else if (ret
> 0) {
299 /* ret = 0 means an orderly shutdown. */
305 * ustcomm_send_unix_sock
307 * Send buf data of size len. Using sendmsg API.
308 * Return the size of sent data.
310 ssize_t
ustcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
316 memset(&msg
, 0, sizeof(msg
));
318 iov
[0].iov_base
= (void *) buf
;
319 iov
[0].iov_len
= len
;
324 * Using the MSG_NOSIGNAL when sending data from sessiond to
325 * libust, so libust does not receive an unhandled SIGPIPE or
326 * SIGURG. The sessiond receiver side can be made more resilient
327 * by ignoring SIGPIPE, but we don't have this luxury on the
331 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
332 } while (ret
< 0 && errno
== EINTR
);
335 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
338 if (ret
== -ECONNRESET
)
341 (void) ustcomm_shutdown_unix_sock(sock
);
348 * Send a message accompanied by fd(s) over a unix socket.
350 * Returns the size of data sent, or negative error value.
352 ssize_t
ustcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
355 struct cmsghdr
*cmptr
;
358 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
359 char tmp
[CMSG_SPACE(sizeof_fds
)];
362 memset(&msg
, 0, sizeof(msg
));
363 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
365 if (nb_fd
> USTCOMM_MAX_SEND_FDS
)
368 msg
.msg_control
= (caddr_t
)tmp
;
369 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
371 cmptr
= CMSG_FIRSTHDR(&msg
);
374 cmptr
->cmsg_level
= SOL_SOCKET
;
375 cmptr
->cmsg_type
= SCM_RIGHTS
;
376 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
377 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
378 /* Sum of the length of all control messages in the buffer: */
379 msg
.msg_controllen
= cmptr
->cmsg_len
;
381 iov
[0].iov_base
= &dummy
;
387 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
388 } while (ret
< 0 && errno
== EINTR
);
391 * We consider EPIPE and ECONNRESET as expected.
393 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
397 if (ret
== -ECONNRESET
)
404 * Recv a message accompanied by fd(s) from a unix socket.
406 * Expect at most "nb_fd" file descriptors. Returns the number of fd
407 * actually received in nb_fd.
408 * Returns -EPIPE on orderly shutdown.
410 ssize_t
ustcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
414 struct cmsghdr
*cmsg
;
415 size_t sizeof_fds
= nb_fd
* sizeof(int);
416 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
421 memset(&msg
, 0, sizeof(msg
));
423 /* Prepare to receive the structures */
424 iov
[0].iov_base
= &dummy
;
428 msg
.msg_control
= recv_fd
;
429 msg
.msg_controllen
= sizeof(recv_fd
);
432 ret
= recvmsg(sock
, &msg
, 0);
433 } while (ret
< 0 && errno
== EINTR
);
435 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
436 PERROR("recvmsg fds");
439 if (ret
== -ECONNRESET
)
444 /* orderly shutdown */
449 ERR("Error: Received %zd bytes, expected %d\n",
453 if (msg
.msg_flags
& MSG_CTRUNC
) {
454 ERR("Error: Control message truncated.\n");
458 cmsg
= CMSG_FIRSTHDR(&msg
);
460 ERR("Error: Invalid control message header\n");
464 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
465 ERR("Didn't received any fd\n");
469 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
470 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
471 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
476 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
479 for (i
= 0; i
< nb_fd
; i
++) {
480 ret
= fcntl(fds
[i
], F_SETFD
, FD_CLOEXEC
);
482 PERROR("fcntl failed to set FD_CLOEXEC on fd %d",
492 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
496 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
504 ERR("incorrect message size: %zd\n", len
);
511 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
512 uint32_t expected_handle
, uint32_t expected_cmd
)
516 memset(lur
, 0, sizeof(*lur
));
517 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
519 case 0: /* orderly shutdown */
525 if (lur
->handle
!= expected_handle
) {
526 ERR("Unexpected result message handle: "
527 "expected: %u vs received: %u\n",
528 expected_handle
, lur
->handle
);
531 if (lur
->cmd
!= expected_cmd
) {
532 ERR("Unexpected result message command "
533 "expected: %u vs received: %u\n",
534 expected_cmd
, lur
->cmd
);
540 return lur
->ret_code
;
545 ERR("incorrect message size: %zd\n", len
);
551 int ustcomm_send_app_cmd(int sock
,
552 struct ustcomm_ust_msg
*lum
,
553 struct ustcomm_ust_reply
*lur
)
557 ret
= ustcomm_send_app_msg(sock
, lum
);
560 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
567 * chan_data is allocated internally if this function returns the
570 ssize_t
ustcomm_recv_channel_from_sessiond(int sock
,
571 void **_chan_data
, uint64_t var_len
,
578 if (var_len
> LTTNG_UST_ABI_CHANNEL_DATA_MAX_LEN
) {
582 /* Receive variable length data */
583 chan_data
= zmalloc(var_len
);
588 len
= ustcomm_recv_unix_sock(sock
, chan_data
, var_len
);
589 if (len
!= var_len
) {
593 lttng_ust_lock_fd_tracker();
594 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
596 lttng_ust_unlock_fd_tracker();
606 ret
= lttng_ust_add_fd_to_tracker(wakeup_fd
);
608 ret
= close(wakeup_fd
);
610 PERROR("close on wakeup_fd");
613 lttng_ust_unlock_fd_tracker();
618 lttng_ust_unlock_fd_tracker();
620 *_chan_data
= chan_data
;
630 ssize_t
ustcomm_recv_event_notifier_notif_fd_from_sessiond(int sock
,
631 int *_event_notifier_notif_fd
)
634 int event_notifier_notif_fd
, ret
;
636 /* Receive event_notifier notification fd */
637 lttng_ust_lock_fd_tracker();
638 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &event_notifier_notif_fd
, 1);
640 lttng_ust_unlock_fd_tracker();
650 ret
= lttng_ust_add_fd_to_tracker(event_notifier_notif_fd
);
652 ret
= close(event_notifier_notif_fd
);
654 PERROR("close on event_notifier notif fd");
657 lttng_ust_unlock_fd_tracker();
661 *_event_notifier_notif_fd
= ret
;
662 lttng_ust_unlock_fd_tracker();
670 int ustcomm_recv_stream_from_sessiond(int sock
,
671 uint64_t *memory_map_size
__attribute__((unused
)),
672 int *shm_fd
, int *wakeup_fd
)
678 /* recv shm fd and wakeup fd */
679 lttng_ust_lock_fd_tracker();
680 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
682 lttng_ust_unlock_fd_tracker();
692 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
696 PERROR("close on received shm_fd");
699 lttng_ust_unlock_fd_tracker();
704 ret
= lttng_ust_add_fd_to_tracker(fds
[1]);
706 ret
= close(*shm_fd
);
708 PERROR("close on shm_fd");
713 PERROR("close on received wakeup_fd");
716 lttng_ust_unlock_fd_tracker();
720 lttng_ust_unlock_fd_tracker();
727 ssize_t
ustcomm_recv_counter_from_sessiond(int sock
,
728 void **_counter_data
, uint64_t var_len
)
733 if (var_len
> LTTNG_UST_ABI_COUNTER_DATA_MAX_LEN
) {
737 /* Receive variable length data */
738 counter_data
= zmalloc(var_len
);
743 len
= ustcomm_recv_unix_sock(sock
, counter_data
, var_len
);
744 if (len
!= var_len
) {
747 *_counter_data
= counter_data
;
757 int ustcomm_recv_counter_shm_from_sessiond(int sock
,
765 lttng_ust_lock_fd_tracker();
766 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 1);
768 lttng_ust_unlock_fd_tracker();
778 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
782 PERROR("close on received shm_fd");
785 lttng_ust_unlock_fd_tracker();
789 lttng_ust_unlock_fd_tracker();
797 * Returns 0 on success, negative error value on error.
799 int ustcomm_send_reg_msg(int sock
,
800 enum lttng_ust_ctl_socket_type type
,
801 uint32_t bits_per_long
,
802 uint32_t uint8_t_alignment
,
803 uint32_t uint16_t_alignment
,
804 uint32_t uint32_t_alignment
,
805 uint32_t uint64_t_alignment
,
806 uint32_t long_alignment
,
807 const char *procname
)
810 struct lttng_ust_ctl_reg_msg reg_msg
;
812 reg_msg
.magic
= LTTNG_UST_ABI_COMM_MAGIC
;
813 reg_msg
.major
= LTTNG_UST_ABI_MAJOR_VERSION
;
814 reg_msg
.minor
= LTTNG_UST_ABI_MINOR_VERSION
;
815 reg_msg
.pid
= getpid();
816 reg_msg
.ppid
= getppid();
817 reg_msg
.uid
= getuid();
818 reg_msg
.gid
= getgid();
819 reg_msg
.bits_per_long
= bits_per_long
;
820 reg_msg
.uint8_t_alignment
= uint8_t_alignment
;
821 reg_msg
.uint16_t_alignment
= uint16_t_alignment
;
822 reg_msg
.uint32_t_alignment
= uint32_t_alignment
;
823 reg_msg
.uint64_t_alignment
= uint64_t_alignment
;
824 reg_msg
.long_alignment
= long_alignment
;
825 reg_msg
.socket_type
= type
;
826 memset(reg_msg
.name
, 0, sizeof(reg_msg
.name
));
827 strncpy(reg_msg
.name
, procname
, sizeof(reg_msg
.name
) - 1);
828 memset(reg_msg
.padding
, 0, sizeof(reg_msg
.padding
));
830 len
= ustcomm_send_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
831 if (len
> 0 && len
!= sizeof(reg_msg
))
839 ssize_t
count_one_type(const struct lttng_ust_type_common
*lt
)
842 case lttng_ust_type_integer
:
843 case lttng_ust_type_float
:
844 case lttng_ust_type_string
:
846 case lttng_ust_type_enum
:
847 return count_one_type(lttng_ust_get_type_enum(lt
)->container_type
) + 1;
848 case lttng_ust_type_array
:
849 return count_one_type(lttng_ust_get_type_array(lt
)->elem_type
) + 1;
850 case lttng_ust_type_sequence
:
851 return count_one_type(lttng_ust_get_type_sequence(lt
)->elem_type
) + 1;
852 case lttng_ust_type_struct
:
853 return count_fields_recursive(lttng_ust_get_type_struct(lt
)->nr_fields
,
854 lttng_ust_get_type_struct(lt
)->fields
) + 1;
856 case lttng_ust_type_dynamic
:
858 const struct lttng_ust_event_field
* const *choices
;
862 ret
= lttng_ust_dynamic_type_choices(&nr_choices
,
867 * Two fields for enum, one field for variant, and
868 * one field per choice.
870 return count_fields_recursive(nr_choices
, choices
) + 3;
880 ssize_t
count_fields_recursive(size_t nr_fields
,
881 const struct lttng_ust_event_field
* const *lttng_fields
)
884 ssize_t ret
, count
= 0;
886 for (i
= 0; i
< nr_fields
; i
++) {
887 const struct lttng_ust_event_field
*lf
;
889 lf
= lttng_fields
[i
];
890 /* skip 'nowrite' fields */
893 ret
= count_one_type(lf
->type
);
895 return ret
; /* error */
902 ssize_t
count_ctx_fields_recursive(size_t nr_fields
,
903 struct lttng_ust_ctx_field
*lttng_fields
)
906 ssize_t ret
, count
= 0;
908 for (i
= 0; i
< nr_fields
; i
++) {
909 const struct lttng_ust_event_field
*lf
;
911 lf
= lttng_fields
[i
].event_field
;
912 /* skip 'nowrite' fields */
915 ret
= count_one_type(lf
->type
);
917 return ret
; /* error */
924 int serialize_string_encoding(int32_t *ue
,
925 enum lttng_ust_string_encoding le
)
928 case lttng_ust_string_encoding_none
:
929 *ue
= lttng_ust_ctl_encode_none
;
931 case lttng_ust_string_encoding_UTF8
:
932 *ue
= lttng_ust_ctl_encode_UTF8
;
934 case lttng_ust_string_encoding_ASCII
:
935 *ue
= lttng_ust_ctl_encode_ASCII
;
944 int serialize_integer_type(struct lttng_ust_ctl_integer_type
*uit
,
945 const struct lttng_ust_type_integer
*lit
,
946 enum lttng_ust_string_encoding lencoding
)
950 uit
->size
= lit
->size
;
951 uit
->signedness
= lit
->signedness
;
952 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
953 uit
->base
= lit
->base
;
954 if (serialize_string_encoding(&encoding
, lencoding
))
956 uit
->encoding
= encoding
;
957 uit
->alignment
= lit
->alignment
;
962 int serialize_dynamic_type(struct lttng_ust_session
*session
,
963 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
964 const char *field_name
)
966 const struct lttng_ust_event_field
* const *choices
;
967 char tag_field_name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
968 const struct lttng_ust_type_common
*tag_type
;
969 const struct lttng_ust_event_field
*tag_field_generic
;
970 struct lttng_ust_event_field tag_field
= {
971 .name
= tag_field_name
,
974 struct lttng_ust_ctl_field
*uf
;
975 size_t nr_choices
, i
;
978 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
979 tag_type
= tag_field_generic
->type
;
981 /* Serialize enum field. */
982 strncpy(tag_field_name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
983 tag_field_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
984 strncat(tag_field_name
,
986 LTTNG_UST_ABI_SYM_NAME_LEN
- strlen(tag_field_name
) - 1);
987 tag_field
.type
= tag_type
;
988 ret
= serialize_one_field(session
, fields
, iter_output
,
993 /* Serialize variant field. */
994 uf
= &fields
[*iter_output
];
995 ret
= lttng_ust_dynamic_type_choices(&nr_choices
, &choices
);
999 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1000 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1001 uf
->type
.atype
= lttng_ust_ctl_atype_variant
;
1002 uf
->type
.u
.variant_nestable
.nr_choices
= nr_choices
;
1003 strncpy(uf
->type
.u
.variant_nestable
.tag_name
,
1005 LTTNG_UST_ABI_SYM_NAME_LEN
);
1006 uf
->type
.u
.variant_nestable
.tag_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1007 uf
->type
.u
.variant_nestable
.alignment
= 0;
1010 /* Serialize choice fields after variant. */
1011 for (i
= 0; i
< nr_choices
; i
++) {
1012 ret
= serialize_one_field(session
, fields
,
1013 iter_output
, choices
[i
], NULL
);
1021 int serialize_one_type(struct lttng_ust_session
*session
,
1022 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
1023 const char *field_name
, const struct lttng_ust_type_common
*lt
,
1024 enum lttng_ust_string_encoding parent_encoding
,
1025 const char *prev_field_name
)
1030 * Serializing a type (rather than a field) generates a lttng_ust_ctl_field
1031 * entry with 0-length name.
1035 case lttng_ust_type_integer
:
1037 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1038 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1041 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1042 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1046 ret
= serialize_integer_type(&ut
->u
.integer
, lttng_ust_get_type_integer(lt
),
1050 ut
->atype
= lttng_ust_ctl_atype_integer
;
1054 case lttng_ust_type_float
:
1056 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1057 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1058 struct lttng_ust_ctl_float_type
*uft
;
1059 const struct lttng_ust_type_float
*lft
;
1062 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1063 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1067 uft
= &ut
->u
._float
;
1068 lft
= lttng_ust_get_type_float(lt
);
1069 uft
->exp_dig
= lft
->exp_dig
;
1070 uft
->mant_dig
= lft
->mant_dig
;
1071 uft
->alignment
= lft
->alignment
;
1072 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
1073 ut
->atype
= lttng_ust_ctl_atype_float
;
1077 case lttng_ust_type_string
:
1079 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1080 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1084 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1085 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1089 ret
= serialize_string_encoding(&encoding
, lttng_ust_get_type_string(lt
)->encoding
);
1092 ut
->u
.string
.encoding
= encoding
;
1093 ut
->atype
= lttng_ust_ctl_atype_string
;
1097 case lttng_ust_type_array
:
1099 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1100 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1103 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1104 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1108 ut
->atype
= lttng_ust_ctl_atype_array_nestable
;
1109 ut
->u
.array_nestable
.length
= lttng_ust_get_type_array(lt
)->length
;
1110 ut
->u
.array_nestable
.alignment
= lttng_ust_get_type_array(lt
)->alignment
;
1113 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1114 lttng_ust_get_type_array(lt
)->elem_type
,
1115 lttng_ust_get_type_array(lt
)->encoding
, NULL
);
1120 case lttng_ust_type_sequence
:
1122 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1123 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1124 const char *length_name
= lttng_ust_get_type_sequence(lt
)->length_name
;
1127 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1128 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1132 ut
->atype
= lttng_ust_ctl_atype_sequence_nestable
;
1134 * If length_name field is NULL, use the previous field
1138 length_name
= prev_field_name
;
1141 strncpy(ut
->u
.sequence_nestable
.length_name
,
1142 length_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1143 ut
->u
.sequence_nestable
.length_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1144 ut
->u
.sequence_nestable
.alignment
= lttng_ust_get_type_sequence(lt
)->alignment
;
1147 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1148 lttng_ust_get_type_sequence(lt
)->elem_type
,
1149 lttng_ust_get_type_sequence(lt
)->encoding
, NULL
);
1154 case lttng_ust_type_dynamic
:
1156 ret
= serialize_dynamic_type(session
, fields
, iter_output
,
1162 case lttng_ust_type_struct
:
1164 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1167 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1168 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1172 uf
->type
.atype
= lttng_ust_ctl_atype_struct_nestable
;
1173 uf
->type
.u
.struct_nestable
.nr_fields
= lttng_ust_get_type_struct(lt
)->nr_fields
;
1174 uf
->type
.u
.struct_nestable
.alignment
= lttng_ust_get_type_struct(lt
)->alignment
;
1177 ret
= serialize_fields(session
, fields
, iter_output
,
1178 lttng_ust_get_type_struct(lt
)->nr_fields
,
1179 lttng_ust_get_type_struct(lt
)->fields
);
1184 case lttng_ust_type_enum
:
1186 struct lttng_ust_ctl_field
*uf
= &fields
[*iter_output
];
1187 struct lttng_ust_ctl_type
*ut
= &uf
->type
;
1190 strncpy(uf
->name
, field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1191 uf
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1195 strncpy(ut
->u
.enum_nestable
.name
, lttng_ust_get_type_enum(lt
)->desc
->name
,
1196 LTTNG_UST_ABI_SYM_NAME_LEN
);
1197 ut
->u
.enum_nestable
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1198 ut
->atype
= lttng_ust_ctl_atype_enum_nestable
;
1201 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1202 lttng_ust_get_type_enum(lt
)->container_type
,
1203 lttng_ust_string_encoding_none
, NULL
);
1207 const struct lttng_enum
*_enum
;
1209 _enum
= lttng_ust_enum_get_from_desc(session
, lttng_ust_get_type_enum(lt
)->desc
);
1212 ut
->u
.enum_nestable
.id
= _enum
->id
;
1214 ut
->u
.enum_nestable
.id
= -1ULL;
1225 int serialize_one_field(struct lttng_ust_session
*session
,
1226 struct lttng_ust_ctl_field
*fields
, size_t *iter_output
,
1227 const struct lttng_ust_event_field
*lf
,
1228 const char **prev_field_name_p
)
1230 const char *prev_field_name
= NULL
;
1233 /* skip 'nowrite' fields */
1237 if (prev_field_name_p
)
1238 prev_field_name
= *prev_field_name_p
;
1239 ret
= serialize_one_type(session
, fields
, iter_output
, lf
->name
, lf
->type
,
1240 lttng_ust_string_encoding_none
, prev_field_name
);
1241 if (prev_field_name_p
)
1242 *prev_field_name_p
= lf
->name
;
1247 int serialize_fields(struct lttng_ust_session
*session
,
1248 struct lttng_ust_ctl_field
*lttng_ust_ctl_fields
,
1249 size_t *iter_output
, size_t nr_lttng_fields
,
1250 const struct lttng_ust_event_field
* const *lttng_fields
)
1252 const char *prev_field_name
= NULL
;
1256 for (i
= 0; i
< nr_lttng_fields
; i
++) {
1257 ret
= serialize_one_field(session
, lttng_ust_ctl_fields
,
1258 iter_output
, lttng_fields
[i
],
1267 int alloc_serialize_fields(struct lttng_ust_session
*session
,
1268 size_t *_nr_write_fields
,
1269 struct lttng_ust_ctl_field
**lttng_ust_ctl_fields
,
1271 const struct lttng_ust_event_field
* const *lttng_fields
)
1273 struct lttng_ust_ctl_field
*fields
;
1275 size_t iter_output
= 0;
1276 ssize_t nr_write_fields
;
1278 nr_write_fields
= count_fields_recursive(nr_fields
, lttng_fields
);
1279 if (nr_write_fields
< 0) {
1280 return (int) nr_write_fields
;
1283 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1287 ret
= serialize_fields(session
, fields
, &iter_output
, nr_fields
,
1292 *_nr_write_fields
= nr_write_fields
;
1293 *lttng_ust_ctl_fields
= fields
;
1302 int serialize_entries(struct lttng_ust_ctl_enum_entry
**_entries
,
1304 const struct lttng_ust_enum_entry
* const *lttng_entries
)
1306 struct lttng_ust_ctl_enum_entry
*entries
;
1309 /* Serialize the entries */
1310 entries
= zmalloc(nr_entries
* sizeof(*entries
));
1313 for (i
= 0; i
< nr_entries
; i
++) {
1314 struct lttng_ust_ctl_enum_entry
*uentry
;
1315 const struct lttng_ust_enum_entry
*lentry
;
1317 uentry
= &entries
[i
];
1318 lentry
= lttng_entries
[i
];
1320 uentry
->start
.value
= lentry
->start
.value
;
1321 uentry
->start
.signedness
= lentry
->start
.signedness
;
1322 uentry
->end
.value
= lentry
->end
.value
;
1323 uentry
->end
.signedness
= lentry
->end
.signedness
;
1324 strncpy(uentry
->string
, lentry
->string
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1325 uentry
->string
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1327 if (lentry
->options
& LTTNG_UST_ENUM_ENTRY_OPTION_IS_AUTO
) {
1328 uentry
->u
.extra
.options
|=
1329 LTTNG_UST_CTL_UST_ENUM_ENTRY_OPTION_IS_AUTO
;
1332 *_entries
= entries
;
1337 int serialize_ctx_fields(struct lttng_ust_session
*session
,
1338 size_t *_nr_write_fields
,
1339 struct lttng_ust_ctl_field
**lttng_ust_ctl_fields
,
1341 struct lttng_ust_ctx_field
*lttng_fields
)
1343 struct lttng_ust_ctl_field
*fields
;
1344 const char *prev_field_name
= NULL
;
1345 size_t i
, iter_output
= 0;
1346 ssize_t nr_write_fields
;
1349 nr_write_fields
= count_ctx_fields_recursive(nr_fields
,
1351 if (nr_write_fields
< 0) {
1352 return (int) nr_write_fields
;
1355 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1359 for (i
= 0; i
< nr_fields
; i
++) {
1360 ret
= serialize_one_field(session
, fields
, &iter_output
,
1361 lttng_fields
[i
].event_field
, &prev_field_name
);
1366 *_nr_write_fields
= nr_write_fields
;
1367 *lttng_ust_ctl_fields
= fields
;
1376 * Returns 0 on success, negative error value on error.
1378 int ustcomm_register_event(int sock
,
1379 struct lttng_ust_session
*session
,
1380 int session_objd
, /* session descriptor */
1381 int channel_objd
, /* channel descriptor */
1382 const char *event_name
, /* event name (input) */
1384 const char *signature
, /* event signature (input) */
1385 size_t nr_fields
, /* fields */
1386 const struct lttng_ust_event_field
* const *lttng_fields
,
1387 const char *model_emf_uri
,
1388 uint32_t *id
) /* event id (output) */
1392 struct ustcomm_notify_hdr header
;
1393 struct ustcomm_notify_event_msg m
;
1396 struct ustcomm_notify_hdr header
;
1397 struct ustcomm_notify_event_reply r
;
1399 size_t signature_len
, fields_len
, model_emf_uri_len
;
1400 struct lttng_ust_ctl_field
*fields
= NULL
;
1401 size_t nr_write_fields
= 0;
1404 memset(&msg
, 0, sizeof(msg
));
1405 msg
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_EVENT
;
1406 msg
.m
.session_objd
= session_objd
;
1407 msg
.m
.channel_objd
= channel_objd
;
1408 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1409 msg
.m
.event_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1410 msg
.m
.loglevel
= loglevel
;
1411 signature_len
= strlen(signature
) + 1;
1412 msg
.m
.signature_len
= signature_len
;
1414 /* Calculate fields len, serialize fields. */
1415 if (nr_fields
> 0) {
1416 ret
= alloc_serialize_fields(session
, &nr_write_fields
, &fields
,
1417 nr_fields
, lttng_fields
);
1422 fields_len
= sizeof(*fields
) * nr_write_fields
;
1423 msg
.m
.fields_len
= fields_len
;
1424 if (model_emf_uri
) {
1425 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
1427 model_emf_uri_len
= 0;
1429 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
1431 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1432 if (len
> 0 && len
!= sizeof(msg
)) {
1441 /* send signature */
1442 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
1443 if (len
> 0 && len
!= signature_len
) {
1453 if (fields_len
> 0) {
1454 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1455 if (len
> 0 && len
!= fields_len
) {
1466 if (model_emf_uri_len
) {
1467 /* send model_emf_uri */
1468 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
1470 if (len
> 0 && len
!= model_emf_uri_len
) {
1479 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1481 case 0: /* orderly shutdown */
1484 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1485 ERR("Unexpected result message command "
1486 "expected: %u vs received: %u\n",
1487 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1490 if (reply
.r
.ret_code
> 0)
1492 if (reply
.r
.ret_code
< 0)
1493 return reply
.r
.ret_code
;
1494 *id
= reply
.r
.event_id
;
1495 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1496 event_name
, reply
.r
.ret_code
, reply
.r
.event_id
);
1500 /* Transport level error */
1501 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1505 ERR("incorrect message size: %zd\n", len
);
1511 /* Error path only. */
1518 * Returns 0 on success, negative error value on error.
1519 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1521 int ustcomm_register_enum(int sock
,
1522 int session_objd
, /* session descriptor */
1523 const char *enum_name
, /* enum name (input) */
1524 size_t nr_entries
, /* entries */
1525 const struct lttng_ust_enum_entry
* const *lttng_entries
,
1530 struct ustcomm_notify_hdr header
;
1531 struct ustcomm_notify_enum_msg m
;
1534 struct ustcomm_notify_hdr header
;
1535 struct ustcomm_notify_enum_reply r
;
1538 struct lttng_ust_ctl_enum_entry
*entries
= NULL
;
1541 memset(&msg
, 0, sizeof(msg
));
1542 msg
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_ENUM
;
1543 msg
.m
.session_objd
= session_objd
;
1544 strncpy(msg
.m
.enum_name
, enum_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
1545 msg
.m
.enum_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
1547 /* Calculate entries len, serialize entries. */
1548 if (nr_entries
> 0) {
1549 ret
= serialize_entries(&entries
,
1550 nr_entries
, lttng_entries
);
1555 entries_len
= sizeof(*entries
) * nr_entries
;
1556 msg
.m
.entries_len
= entries_len
;
1558 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1559 if (len
> 0 && len
!= sizeof(msg
)) {
1569 if (entries_len
> 0) {
1570 len
= ustcomm_send_unix_sock(sock
, entries
, entries_len
);
1571 if (len
> 0 && len
!= entries_len
) {
1584 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1586 case 0: /* orderly shutdown */
1589 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1590 ERR("Unexpected result message command "
1591 "expected: %u vs received: %u\n",
1592 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1595 if (reply
.r
.ret_code
> 0)
1597 if (reply
.r
.ret_code
< 0)
1598 return reply
.r
.ret_code
;
1599 *id
= reply
.r
.enum_id
;
1600 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1601 enum_name
, reply
.r
.ret_code
);
1605 /* Transport level error */
1606 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1610 ERR("incorrect message size: %zd\n", len
);
1622 * Returns 0 on success, negative error value on error.
1623 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1625 int ustcomm_register_channel(int sock
,
1626 struct lttng_ust_session
*session
,
1627 int session_objd
, /* session descriptor */
1628 int channel_objd
, /* channel descriptor */
1629 size_t nr_ctx_fields
,
1630 struct lttng_ust_ctx_field
*ctx_fields
,
1631 uint32_t *chan_id
, /* channel id (output) */
1632 int *header_type
) /* header type (output) */
1636 struct ustcomm_notify_hdr header
;
1637 struct ustcomm_notify_channel_msg m
;
1640 struct ustcomm_notify_hdr header
;
1641 struct ustcomm_notify_channel_reply r
;
1644 struct lttng_ust_ctl_field
*fields
= NULL
;
1646 size_t nr_write_fields
= 0;
1648 memset(&msg
, 0, sizeof(msg
));
1649 msg
.header
.notify_cmd
= LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
;
1650 msg
.m
.session_objd
= session_objd
;
1651 msg
.m
.channel_objd
= channel_objd
;
1653 /* Calculate fields len, serialize fields. */
1654 if (nr_ctx_fields
> 0) {
1655 ret
= serialize_ctx_fields(session
, &nr_write_fields
, &fields
,
1656 nr_ctx_fields
, ctx_fields
);
1661 fields_len
= sizeof(*fields
) * nr_write_fields
;
1662 msg
.m
.ctx_fields_len
= fields_len
;
1663 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1664 if (len
> 0 && len
!= sizeof(msg
)) {
1674 if (fields_len
> 0) {
1675 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1677 if (len
> 0 && len
!= fields_len
) {
1687 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1689 case 0: /* orderly shutdown */
1692 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1693 ERR("Unexpected result message command "
1694 "expected: %u vs received: %u\n",
1695 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1698 if (reply
.r
.ret_code
> 0)
1700 if (reply
.r
.ret_code
< 0)
1701 return reply
.r
.ret_code
;
1702 *chan_id
= reply
.r
.chan_id
;
1703 switch (reply
.r
.header_type
) {
1706 *header_type
= reply
.r
.header_type
;
1709 ERR("Unexpected channel header type %u\n",
1710 reply
.r
.header_type
);
1713 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1714 reply
.r
.chan_id
, reply
.r
.header_type
);
1718 /* Transport level error */
1719 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1723 ERR("incorrect message size: %zd\n", len
);
1730 * Set socket receiving timeout.
1732 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1737 tv
.tv_sec
= msec
/ 1000;
1738 tv
.tv_usec
= (msec
* 1000 % 1000000);
1740 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1742 PERROR("setsockopt SO_RCVTIMEO");
1750 * Set socket sending timeout.
1752 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1757 tv
.tv_sec
= msec
/ 1000;
1758 tv
.tv_usec
= (msec
* 1000) % 1000000;
1760 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1762 PERROR("setsockopt SO_SNDTIMEO");