2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; only
8 * version 2.1 of the License.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <sys/socket.h>
28 #include <sys/types.h>
35 #include <lttng/ust-ctl.h>
39 #include <lttng/ust-error.h>
40 #include <lttng/ust-events.h>
41 #include <lttng/ust-dynamic-type.h>
42 #include <usterr-signal-safe.h>
44 #include "../liblttng-ust/compat.h"
46 #define USTCOMM_CODE_OFFSET(code) \
47 (code == LTTNG_UST_OK ? 0 : (code - LTTNG_UST_ERR + 1))
49 #define USTCOMM_MAX_SEND_FDS 4
52 ssize_t
count_fields_recursive(size_t nr_fields
,
53 const struct lttng_event_field
*lttng_fields
);
55 int serialize_one_field(struct lttng_session
*session
,
56 struct ustctl_field
*fields
, size_t *iter_output
,
57 const struct lttng_event_field
*lf
);
59 int serialize_fields(struct lttng_session
*session
,
60 struct ustctl_field
*ustctl_fields
,
61 size_t *iter_output
, size_t nr_lttng_fields
,
62 const struct lttng_event_field
*lttng_fields
);
65 * Human readable error message.
67 static const char *ustcomm_readable_code
[] = {
68 [ USTCOMM_CODE_OFFSET(LTTNG_UST_OK
) ] = "Success",
69 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR
) ] = "Unknown error",
70 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOENT
) ] = "No entry",
71 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXIST
) ] = "Object already exists",
72 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL
) ] = "Invalid argument",
73 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PERM
) ] = "Permission denied",
74 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOSYS
) ] = "Not implemented",
75 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXITING
) ] = "Process is exiting",
77 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_MAGIC
) ] = "Invalid magic number",
78 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_SOCKET_TYPE
) ] = "Invalid socket type",
79 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_UNSUP_MAJOR
) ] = "Unsupported major version",
85 * Receives positive error value.
86 * Return ptr to string representing a human readable
87 * error code from the ustcomm_return_code enum.
89 const char *lttng_ust_strerror(int code
)
91 if (code
== LTTNG_UST_OK
)
92 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
93 if (code
< LTTNG_UST_ERR
)
94 return strerror(code
);
95 if (code
>= LTTNG_UST_ERR_NR
)
97 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
101 * ustcomm_connect_unix_sock
103 * Connect to unix socket using the path name.
105 * Caller handles FD tracker.
107 int ustcomm_connect_unix_sock(const char *pathname
, long timeout
)
109 struct sockaddr_un sun
;
113 * libust threads require the close-on-exec flag for all
114 * resources so it does not leak file descriptors upon exec.
115 * SOCK_CLOEXEC is not used since it is linux specific.
117 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
124 /* Give at least 10ms. */
127 ret
= ustcomm_setsockopt_snd_timeout(fd
, timeout
);
129 WARN("Error setting connect socket send timeout");
132 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
139 memset(&sun
, 0, sizeof(sun
));
140 sun
.sun_family
= AF_UNIX
;
141 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
142 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
144 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
147 * Don't print message on connect ENOENT error, because
148 * connect is used in normal execution to detect if
149 * sessiond is alive. ENOENT is when the unix socket
150 * file does not exist, and ECONNREFUSED is when the
151 * file exists but no sessiond is listening.
153 if (errno
!= ECONNREFUSED
&& errno
!= ECONNRESET
154 && errno
!= ENOENT
&& errno
!= EACCES
)
157 if (ret
== -ECONNREFUSED
|| ret
== -ECONNRESET
)
169 closeret
= close(fd
);
178 * ustcomm_accept_unix_sock
180 * Do an accept(2) on the sock and return the
181 * new file descriptor. The socket MUST be bind(2) before.
183 int ustcomm_accept_unix_sock(int sock
)
186 struct sockaddr_un sun
;
190 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
192 if (errno
!= ECONNABORTED
)
195 if (new_fd
== -ECONNABORTED
)
202 * ustcomm_create_unix_sock
204 * Creates a AF_UNIX local socket using pathname
205 * bind the socket upon creation and return the fd.
207 int ustcomm_create_unix_sock(const char *pathname
)
209 struct sockaddr_un sun
;
212 /* Create server socket */
213 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
219 memset(&sun
, 0, sizeof(sun
));
220 sun
.sun_family
= AF_UNIX
;
221 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
222 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
224 /* Unlink the old file if present */
225 (void) unlink(pathname
);
226 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
239 closeret
= close(fd
);
249 * ustcomm_listen_unix_sock
251 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
253 int ustcomm_listen_unix_sock(int sock
)
257 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
267 * ustcomm_close_unix_sock
269 * Shutdown cleanly a unix socket.
271 * Handles fd tracker internally.
273 int ustcomm_close_unix_sock(int sock
)
277 lttng_ust_lock_fd_tracker();
280 lttng_ust_delete_fd_from_tracker(sock
);
285 lttng_ust_unlock_fd_tracker();
291 * ustcomm_recv_unix_sock
293 * Receive data of size len in put that data into
294 * the buf param. Using recvmsg API.
295 * Return the size of received data.
296 * Return 0 on orderly shutdown.
298 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
305 memset(&msg
, 0, sizeof(msg
));
307 iov
[0].iov_base
= buf
;
308 iov
[0].iov_len
= len
;
313 len_last
= iov
[0].iov_len
;
314 ret
= recvmsg(sock
, &msg
, 0);
316 iov
[0].iov_base
+= ret
;
317 iov
[0].iov_len
-= ret
;
318 assert(ret
<= len_last
);
320 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
325 if (errno
!= EPIPE
&& errno
!= ECONNRESET
&& errno
!= ECONNREFUSED
)
328 if (ret
== -ECONNRESET
|| ret
== -ECONNREFUSED
)
331 shutret
= shutdown(sock
, SHUT_RDWR
);
333 ERR("Socket shutdown error");
334 } else if (ret
> 0) {
337 /* ret = 0 means an orderly shutdown. */
343 * ustcomm_send_unix_sock
345 * Send buf data of size len. Using sendmsg API.
346 * Return the size of sent data.
348 ssize_t
ustcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
354 memset(&msg
, 0, sizeof(msg
));
356 iov
[0].iov_base
= (void *) buf
;
357 iov
[0].iov_len
= len
;
362 * Using the MSG_NOSIGNAL when sending data from sessiond to
363 * libust, so libust does not receive an unhandled SIGPIPE or
364 * SIGURG. The sessiond receiver side can be made more resilient
365 * by ignoring SIGPIPE, but we don't have this luxury on the
369 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
370 } while (ret
< 0 && errno
== EINTR
);
375 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
378 if (ret
== -ECONNRESET
)
381 shutret
= shutdown(sock
, SHUT_RDWR
);
383 ERR("Socket shutdown error");
390 * Send a message accompanied by fd(s) over a unix socket.
392 * Returns the size of data sent, or negative error value.
394 ssize_t
ustcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
397 struct cmsghdr
*cmptr
;
400 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
401 char tmp
[CMSG_SPACE(sizeof_fds
)];
404 memset(&msg
, 0, sizeof(msg
));
405 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
407 if (nb_fd
> USTCOMM_MAX_SEND_FDS
)
410 msg
.msg_control
= (caddr_t
)tmp
;
411 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
413 cmptr
= CMSG_FIRSTHDR(&msg
);
416 cmptr
->cmsg_level
= SOL_SOCKET
;
417 cmptr
->cmsg_type
= SCM_RIGHTS
;
418 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
419 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
420 /* Sum of the length of all control messages in the buffer: */
421 msg
.msg_controllen
= cmptr
->cmsg_len
;
423 iov
[0].iov_base
= &dummy
;
429 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
430 } while (ret
< 0 && errno
== EINTR
);
433 * We consider EPIPE and ECONNRESET as expected.
435 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
439 if (ret
== -ECONNRESET
)
446 * Recv a message accompanied by fd(s) from a unix socket.
448 * Expect at most "nb_fd" file descriptors. Returns the number of fd
449 * actually received in nb_fd.
450 * Returns -EPIPE on orderly shutdown.
452 ssize_t
ustcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
456 struct cmsghdr
*cmsg
;
457 size_t sizeof_fds
= nb_fd
* sizeof(int);
458 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
463 memset(&msg
, 0, sizeof(msg
));
465 /* Prepare to receive the structures */
466 iov
[0].iov_base
= &dummy
;
470 msg
.msg_control
= recv_fd
;
471 msg
.msg_controllen
= sizeof(recv_fd
);
474 ret
= recvmsg(sock
, &msg
, 0);
475 } while (ret
< 0 && errno
== EINTR
);
477 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
478 PERROR("recvmsg fds");
481 if (ret
== -ECONNRESET
)
486 /* orderly shutdown */
491 ERR("Error: Received %zd bytes, expected %d\n",
495 if (msg
.msg_flags
& MSG_CTRUNC
) {
496 ERR("Error: Control message truncated.\n");
500 cmsg
= CMSG_FIRSTHDR(&msg
);
502 ERR("Error: Invalid control message header\n");
506 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
507 ERR("Didn't received any fd\n");
511 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
512 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
513 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
518 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
521 for (i
= 0; i
< nb_fd
; i
++) {
522 ret
= fcntl(fds
[i
], F_SETFD
, FD_CLOEXEC
);
524 PERROR("fcntl failed to set FD_CLOEXEC on fd %d",
534 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
538 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
546 ERR("incorrect message size: %zd\n", len
);
553 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
554 uint32_t expected_handle
, uint32_t expected_cmd
)
558 memset(lur
, 0, sizeof(*lur
));
559 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
561 case 0: /* orderly shutdown */
567 if (lur
->handle
!= expected_handle
) {
568 ERR("Unexpected result message handle: "
569 "expected: %u vs received: %u\n",
570 expected_handle
, lur
->handle
);
573 if (lur
->cmd
!= expected_cmd
) {
574 ERR("Unexpected result message command "
575 "expected: %u vs received: %u\n",
576 expected_cmd
, lur
->cmd
);
582 return lur
->ret_code
;
587 ERR("incorrect message size: %zd\n", len
);
593 int ustcomm_send_app_cmd(int sock
,
594 struct ustcomm_ust_msg
*lum
,
595 struct ustcomm_ust_reply
*lur
)
599 ret
= ustcomm_send_app_msg(sock
, lum
);
602 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
609 * chan_data is allocated internally if this function returns the
612 ssize_t
ustcomm_recv_channel_from_sessiond(int sock
,
613 void **_chan_data
, uint64_t var_len
,
620 if (var_len
> LTTNG_UST_CHANNEL_DATA_MAX_LEN
) {
624 /* Receive variable length data */
625 chan_data
= zmalloc(var_len
);
630 len
= ustcomm_recv_unix_sock(sock
, chan_data
, var_len
);
631 if (len
!= var_len
) {
635 lttng_ust_lock_fd_tracker();
636 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
638 lttng_ust_unlock_fd_tracker();
648 ret
= lttng_ust_add_fd_to_tracker(wakeup_fd
);
650 ret
= close(wakeup_fd
);
652 PERROR("close on wakeup_fd");
655 lttng_ust_unlock_fd_tracker();
660 lttng_ust_unlock_fd_tracker();
662 *_chan_data
= chan_data
;
672 ssize_t
ustcomm_recv_trigger_notif_fd_from_sessiond(int sock
,
673 int *_trigger_notif_fd
)
676 int trigger_notif_fd
, ret
;
678 /* Receive trigger notification fd */
679 lttng_ust_lock_fd_tracker();
680 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &trigger_notif_fd
, 1);
682 lttng_ust_unlock_fd_tracker();
692 ret
= lttng_ust_add_fd_to_tracker(trigger_notif_fd
);
694 ret
= close(trigger_notif_fd
);
696 PERROR("close on trigger notif fd");
699 lttng_ust_unlock_fd_tracker();
703 *_trigger_notif_fd
= ret
;
704 lttng_ust_unlock_fd_tracker();
712 int ustcomm_recv_stream_from_sessiond(int sock
,
713 uint64_t *memory_map_size
,
714 int *shm_fd
, int *wakeup_fd
)
720 /* recv shm fd and wakeup fd */
721 lttng_ust_lock_fd_tracker();
722 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
724 lttng_ust_unlock_fd_tracker();
734 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
738 PERROR("close on received shm_fd");
741 lttng_ust_unlock_fd_tracker();
746 ret
= lttng_ust_add_fd_to_tracker(fds
[1]);
748 ret
= close(*shm_fd
);
750 PERROR("close on shm_fd");
755 PERROR("close on received wakeup_fd");
758 lttng_ust_unlock_fd_tracker();
762 lttng_ust_unlock_fd_tracker();
769 ssize_t
ustcomm_recv_counter_from_sessiond(int sock
,
770 void **_counter_data
, uint64_t var_len
)
775 if (var_len
> LTTNG_UST_COUNTER_DATA_MAX_LEN
) {
779 /* Receive variable length data */
780 counter_data
= zmalloc(var_len
);
785 len
= ustcomm_recv_unix_sock(sock
, counter_data
, var_len
);
786 if (len
!= var_len
) {
789 *_counter_data
= counter_data
;
799 int ustcomm_recv_counter_shm_from_sessiond(int sock
,
807 lttng_ust_lock_fd_tracker();
808 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 1);
810 lttng_ust_unlock_fd_tracker();
820 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
824 PERROR("close on received shm_fd");
827 lttng_ust_unlock_fd_tracker();
831 lttng_ust_unlock_fd_tracker();
839 * Returns 0 on success, negative error value on error.
841 int ustcomm_send_reg_msg(int sock
,
842 enum ustctl_socket_type type
,
843 uint32_t bits_per_long
,
844 uint32_t uint8_t_alignment
,
845 uint32_t uint16_t_alignment
,
846 uint32_t uint32_t_alignment
,
847 uint32_t uint64_t_alignment
,
848 uint32_t long_alignment
)
851 struct ustctl_reg_msg reg_msg
;
853 reg_msg
.magic
= LTTNG_UST_COMM_MAGIC
;
854 reg_msg
.major
= LTTNG_UST_ABI_MAJOR_VERSION
;
855 reg_msg
.minor
= LTTNG_UST_ABI_MINOR_VERSION
;
856 reg_msg
.pid
= getpid();
857 reg_msg
.ppid
= getppid();
858 reg_msg
.uid
= getuid();
859 reg_msg
.gid
= getgid();
860 reg_msg
.bits_per_long
= bits_per_long
;
861 reg_msg
.uint8_t_alignment
= uint8_t_alignment
;
862 reg_msg
.uint16_t_alignment
= uint16_t_alignment
;
863 reg_msg
.uint32_t_alignment
= uint32_t_alignment
;
864 reg_msg
.uint64_t_alignment
= uint64_t_alignment
;
865 reg_msg
.long_alignment
= long_alignment
;
866 reg_msg
.socket_type
= type
;
867 lttng_ust_getprocname(reg_msg
.name
);
868 memset(reg_msg
.padding
, 0, sizeof(reg_msg
.padding
));
870 len
= ustcomm_send_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
871 if (len
> 0 && len
!= sizeof(reg_msg
))
879 ssize_t
count_one_type(const struct lttng_type
*lt
)
890 return count_fields_recursive(lt
->u
.legacy
._struct
.nr_fields
,
891 lt
->u
.legacy
._struct
.fields
) + 1;
892 case atype_enum_nestable
:
893 return count_one_type(lt
->u
.enum_nestable
.container_type
) + 1;
894 case atype_array_nestable
:
895 return count_one_type(lt
->u
.array_nestable
.elem_type
) + 1;
896 case atype_sequence_nestable
:
897 return count_one_type(lt
->u
.sequence_nestable
.elem_type
) + 1;
898 case atype_struct_nestable
:
899 return count_fields_recursive(lt
->u
.struct_nestable
.nr_fields
,
900 lt
->u
.struct_nestable
.fields
) + 1;
904 const struct lttng_event_field
*choices
;
908 ret
= lttng_ust_dynamic_type_choices(&nr_choices
,
913 * Two fields for enum, one field for variant, and
914 * one field per choice.
916 return count_fields_recursive(nr_choices
, choices
) + 3;
926 ssize_t
count_fields_recursive(size_t nr_fields
,
927 const struct lttng_event_field
*lttng_fields
)
930 ssize_t ret
, count
= 0;
932 for (i
= 0; i
< nr_fields
; i
++) {
933 const struct lttng_event_field
*lf
;
935 lf
= <tng_fields
[i
];
936 /* skip 'nowrite' fields */
939 ret
= count_one_type(&lf
->type
);
941 return ret
; /* error */
948 ssize_t
count_ctx_fields_recursive(size_t nr_fields
,
949 const struct lttng_ctx_field
*lttng_fields
)
952 ssize_t ret
, count
= 0;
954 for (i
= 0; i
< nr_fields
; i
++) {
955 const struct lttng_event_field
*lf
;
957 lf
= <tng_fields
[i
].event_field
;
958 /* skip 'nowrite' fields */
961 ret
= count_one_type(&lf
->type
);
963 return ret
; /* error */
970 int serialize_string_encoding(int32_t *ue
,
971 enum lttng_string_encodings le
)
974 case lttng_encode_none
:
975 *ue
= ustctl_encode_none
;
977 case lttng_encode_UTF8
:
978 *ue
= ustctl_encode_UTF8
;
980 case lttng_encode_ASCII
:
981 *ue
= ustctl_encode_ASCII
;
990 int serialize_integer_type(struct ustctl_integer_type
*uit
,
991 const struct lttng_integer_type
*lit
)
995 uit
->size
= lit
->size
;
996 uit
->signedness
= lit
->signedness
;
997 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
998 uit
->base
= lit
->base
;
999 if (serialize_string_encoding(&encoding
, lit
->encoding
))
1001 uit
->encoding
= encoding
;
1002 uit
->alignment
= lit
->alignment
;
1007 int serialize_basic_type(struct lttng_session
*session
,
1008 enum ustctl_abstract_types
*uatype
,
1009 enum lttng_abstract_types atype
,
1010 union _ustctl_basic_type
*ubt
,
1011 const union _lttng_basic_type
*lbt
)
1016 if (serialize_integer_type(&ubt
->integer
, &lbt
->integer
))
1018 *uatype
= ustctl_atype_integer
;
1025 if (serialize_string_encoding(&encoding
, lbt
->string
.encoding
))
1027 ubt
->string
.encoding
= encoding
;
1028 *uatype
= ustctl_atype_string
;
1033 struct ustctl_float_type
*uft
;
1034 const struct lttng_float_type
*lft
;
1038 uft
->exp_dig
= lft
->exp_dig
;
1039 uft
->mant_dig
= lft
->mant_dig
;
1040 uft
->alignment
= lft
->alignment
;
1041 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
1042 *uatype
= ustctl_atype_float
;
1047 strncpy(ubt
->enumeration
.name
, lbt
->enumeration
.desc
->name
,
1048 LTTNG_UST_SYM_NAME_LEN
);
1049 ubt
->enumeration
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1050 if (serialize_integer_type(&ubt
->enumeration
.container_type
,
1051 &lbt
->enumeration
.container_type
))
1054 const struct lttng_enum
*_enum
;
1056 _enum
= lttng_ust_enum_get_from_desc(session
, lbt
->enumeration
.desc
);
1059 ubt
->enumeration
.id
= _enum
->id
;
1061 ubt
->enumeration
.id
= -1ULL;
1063 *uatype
= ustctl_atype_enum
;
1067 case atype_array_nestable
:
1068 case atype_sequence
:
1069 case atype_sequence_nestable
:
1070 case atype_enum_nestable
:
1078 int serialize_dynamic_type(struct lttng_session
*session
,
1079 struct ustctl_field
*fields
, size_t *iter_output
,
1080 const char *field_name
)
1082 const struct lttng_event_field
*choices
;
1083 char tag_field_name
[LTTNG_UST_SYM_NAME_LEN
];
1084 const struct lttng_type
*tag_type
;
1085 const struct lttng_event_field
*tag_field_generic
;
1086 struct lttng_event_field tag_field
= {
1087 .name
= tag_field_name
,
1090 struct ustctl_field
*uf
;
1091 size_t nr_choices
, i
;
1094 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
1095 tag_type
= &tag_field_generic
->type
;
1097 /* Serialize enum field. */
1098 strncpy(tag_field_name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1099 tag_field_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1100 strncat(tag_field_name
,
1102 LTTNG_UST_SYM_NAME_LEN
- strlen(tag_field_name
) - 1);
1103 tag_field
.type
= *tag_type
;
1104 ret
= serialize_one_field(session
, fields
, iter_output
,
1109 /* Serialize variant field. */
1110 uf
= &fields
[*iter_output
];
1111 ret
= lttng_ust_dynamic_type_choices(&nr_choices
, &choices
);
1115 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1116 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1117 uf
->type
.atype
= ustctl_atype_variant
;
1118 uf
->type
.u
.variant_nestable
.nr_choices
= nr_choices
;
1119 strncpy(uf
->type
.u
.variant_nestable
.tag_name
,
1121 LTTNG_UST_SYM_NAME_LEN
);
1122 uf
->type
.u
.variant_nestable
.tag_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1123 uf
->type
.u
.variant_nestable
.alignment
= 0;
1126 /* Serialize choice fields after variant. */
1127 for (i
= 0; i
< nr_choices
; i
++) {
1128 ret
= serialize_one_field(session
, fields
,
1129 iter_output
, &choices
[i
]);
1137 int serialize_one_type(struct lttng_session
*session
,
1138 struct ustctl_field
*fields
, size_t *iter_output
,
1139 const char *field_name
, const struct lttng_type
*lt
)
1144 * Serializing a type (rather than a field) generates a ustctl_field
1145 * entry with 0-length name.
1148 switch (lt
->atype
) {
1154 struct ustctl_field
*uf
= &fields
[*iter_output
];
1155 struct ustctl_type
*ut
= &uf
->type
;
1156 enum ustctl_abstract_types atype
;
1159 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1160 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1164 ret
= serialize_basic_type(session
, &atype
, lt
->atype
,
1165 &ut
->u
.legacy
.basic
, <
->u
.legacy
.basic
);
1174 struct ustctl_field
*uf
= &fields
[*iter_output
];
1175 struct ustctl_type
*ut
= &uf
->type
;
1176 struct ustctl_basic_type
*ubt
;
1177 const struct lttng_basic_type
*lbt
;
1178 enum ustctl_abstract_types atype
;
1181 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1182 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1186 ut
->atype
= ustctl_atype_array
;
1187 ubt
= &ut
->u
.legacy
.array
.elem_type
;
1188 lbt
= <
->u
.legacy
.array
.elem_type
;
1189 ut
->u
.legacy
.array
.length
= lt
->u
.legacy
.array
.length
;
1190 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1191 &ubt
->u
.basic
, &lbt
->u
.basic
);
1198 case atype_array_nestable
:
1200 struct ustctl_field
*uf
= &fields
[*iter_output
];
1201 struct ustctl_type
*ut
= &uf
->type
;
1204 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1205 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1209 ut
->atype
= ustctl_atype_array_nestable
;
1210 ut
->u
.array_nestable
.length
= lt
->u
.array_nestable
.length
;
1211 ut
->u
.array_nestable
.alignment
= lt
->u
.array_nestable
.alignment
;
1214 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1215 lt
->u
.array_nestable
.elem_type
);
1220 case atype_sequence
:
1222 struct ustctl_field
*uf
= &fields
[*iter_output
];
1223 struct ustctl_type
*ut
= &uf
->type
;
1224 struct ustctl_basic_type
*ubt
;
1225 const struct lttng_basic_type
*lbt
;
1226 enum ustctl_abstract_types atype
;
1229 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1230 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1234 uf
->type
.atype
= ustctl_atype_sequence
;
1235 ubt
= &ut
->u
.legacy
.sequence
.length_type
;
1236 lbt
= <
->u
.legacy
.sequence
.length_type
;
1237 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1238 &ubt
->u
.basic
, &lbt
->u
.basic
);
1242 ubt
= &ut
->u
.legacy
.sequence
.elem_type
;
1243 lbt
= <
->u
.legacy
.sequence
.elem_type
;
1244 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1245 &ubt
->u
.basic
, &lbt
->u
.basic
);
1252 case atype_sequence_nestable
:
1254 struct ustctl_field
*uf
= &fields
[*iter_output
];
1255 struct ustctl_type
*ut
= &uf
->type
;
1258 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1259 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1263 ut
->atype
= ustctl_atype_sequence_nestable
;
1264 strncpy(ut
->u
.sequence_nestable
.length_name
,
1265 lt
->u
.sequence_nestable
.length_name
,
1266 LTTNG_UST_SYM_NAME_LEN
);
1267 ut
->u
.sequence_nestable
.length_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1268 ut
->u
.sequence_nestable
.alignment
= lt
->u
.sequence_nestable
.alignment
;
1271 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1272 lt
->u
.sequence_nestable
.elem_type
);
1279 ret
= serialize_dynamic_type(session
, fields
, iter_output
,
1287 struct ustctl_field
*uf
= &fields
[*iter_output
];
1290 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1291 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1295 uf
->type
.atype
= ustctl_atype_struct
;
1296 uf
->type
.u
.legacy
._struct
.nr_fields
= lt
->u
.legacy
._struct
.nr_fields
;
1299 ret
= serialize_fields(session
, fields
, iter_output
,
1300 lt
->u
.legacy
._struct
.nr_fields
,
1301 lt
->u
.legacy
._struct
.fields
);
1306 case atype_struct_nestable
:
1308 struct ustctl_field
*uf
= &fields
[*iter_output
];
1311 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1312 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1316 uf
->type
.atype
= ustctl_atype_struct_nestable
;
1317 uf
->type
.u
.struct_nestable
.nr_fields
= lt
->u
.struct_nestable
.nr_fields
;
1318 uf
->type
.u
.struct_nestable
.alignment
= lt
->u
.struct_nestable
.alignment
;
1321 ret
= serialize_fields(session
, fields
, iter_output
,
1322 lt
->u
.struct_nestable
.nr_fields
,
1323 lt
->u
.struct_nestable
.fields
);
1328 case atype_enum_nestable
:
1330 struct ustctl_field
*uf
= &fields
[*iter_output
];
1331 struct ustctl_type
*ut
= &uf
->type
;
1334 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1335 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1339 strncpy(ut
->u
.enum_nestable
.name
, lt
->u
.enum_nestable
.desc
->name
,
1340 LTTNG_UST_SYM_NAME_LEN
);
1341 ut
->u
.enum_nestable
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1342 ut
->atype
= ustctl_atype_enum_nestable
;
1345 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1346 lt
->u
.enum_nestable
.container_type
);
1350 const struct lttng_enum
*_enum
;
1352 _enum
= lttng_ust_enum_get_from_desc(session
, lt
->u
.enum_nestable
.desc
);
1355 ut
->u
.enum_nestable
.id
= _enum
->id
;
1357 ut
->u
.enum_nestable
.id
= -1ULL;
1368 int serialize_one_field(struct lttng_session
*session
,
1369 struct ustctl_field
*fields
, size_t *iter_output
,
1370 const struct lttng_event_field
*lf
)
1372 /* skip 'nowrite' fields */
1376 return serialize_one_type(session
, fields
, iter_output
, lf
->name
, &lf
->type
);
1380 int serialize_fields(struct lttng_session
*session
,
1381 struct ustctl_field
*ustctl_fields
,
1382 size_t *iter_output
, size_t nr_lttng_fields
,
1383 const struct lttng_event_field
*lttng_fields
)
1388 for (i
= 0; i
< nr_lttng_fields
; i
++) {
1389 ret
= serialize_one_field(session
, ustctl_fields
,
1390 iter_output
, <tng_fields
[i
]);
1398 int alloc_serialize_fields(struct lttng_session
*session
,
1399 size_t *_nr_write_fields
,
1400 struct ustctl_field
**ustctl_fields
,
1402 const struct lttng_event_field
*lttng_fields
)
1404 struct ustctl_field
*fields
;
1406 size_t iter_output
= 0;
1407 ssize_t nr_write_fields
;
1409 nr_write_fields
= count_fields_recursive(nr_fields
, lttng_fields
);
1410 if (nr_write_fields
< 0) {
1411 return (int) nr_write_fields
;
1414 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1418 ret
= serialize_fields(session
, fields
, &iter_output
, nr_fields
,
1423 *_nr_write_fields
= nr_write_fields
;
1424 *ustctl_fields
= fields
;
1433 int serialize_entries(struct ustctl_enum_entry
**_entries
,
1435 const struct lttng_enum_entry
*lttng_entries
)
1437 struct ustctl_enum_entry
*entries
;
1440 /* Serialize the entries */
1441 entries
= zmalloc(nr_entries
* sizeof(*entries
));
1444 for (i
= 0; i
< nr_entries
; i
++) {
1445 struct ustctl_enum_entry
*uentry
;
1446 const struct lttng_enum_entry
*lentry
;
1448 uentry
= &entries
[i
];
1449 lentry
= <tng_entries
[i
];
1451 uentry
->start
.value
= lentry
->start
.value
;
1452 uentry
->start
.signedness
= lentry
->start
.signedness
;
1453 uentry
->end
.value
= lentry
->end
.value
;
1454 uentry
->end
.signedness
= lentry
->end
.signedness
;
1455 strncpy(uentry
->string
, lentry
->string
, LTTNG_UST_SYM_NAME_LEN
);
1456 uentry
->string
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1458 if (lentry
->u
.extra
.options
& LTTNG_ENUM_ENTRY_OPTION_IS_AUTO
) {
1459 uentry
->u
.extra
.options
|=
1460 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO
;
1463 *_entries
= entries
;
1468 int serialize_ctx_fields(struct lttng_session
*session
,
1469 size_t *_nr_write_fields
,
1470 struct ustctl_field
**ustctl_fields
,
1472 const struct lttng_ctx_field
*lttng_fields
)
1474 struct ustctl_field
*fields
;
1476 size_t i
, iter_output
= 0;
1477 ssize_t nr_write_fields
;
1479 nr_write_fields
= count_ctx_fields_recursive(nr_fields
,
1481 if (nr_write_fields
< 0) {
1482 return (int) nr_write_fields
;
1485 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1489 for (i
= 0; i
< nr_fields
; i
++) {
1490 ret
= serialize_one_field(session
, fields
, &iter_output
,
1491 <tng_fields
[i
].event_field
);
1496 *_nr_write_fields
= nr_write_fields
;
1497 *ustctl_fields
= fields
;
1506 * Returns 0 on success, negative error value on error.
1508 int ustcomm_register_event(int sock
,
1509 struct lttng_session
*session
,
1510 int session_objd
, /* session descriptor */
1511 int channel_objd
, /* channel descriptor */
1512 const char *event_name
, /* event name (input) */
1514 const char *signature
, /* event signature (input) */
1515 size_t nr_fields
, /* fields */
1516 const struct lttng_event_field
*lttng_fields
,
1517 const char *model_emf_uri
,
1518 uint32_t *id
) /* event id (output) */
1522 struct ustcomm_notify_hdr header
;
1523 struct ustcomm_notify_event_msg m
;
1526 struct ustcomm_notify_hdr header
;
1527 struct ustcomm_notify_event_reply r
;
1529 size_t signature_len
, fields_len
, model_emf_uri_len
;
1530 struct ustctl_field
*fields
= NULL
;
1531 size_t nr_write_fields
= 0;
1534 memset(&msg
, 0, sizeof(msg
));
1535 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1536 msg
.m
.session_objd
= session_objd
;
1537 msg
.m
.channel_objd
= channel_objd
;
1538 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_SYM_NAME_LEN
);
1539 msg
.m
.event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1540 msg
.m
.loglevel
= loglevel
;
1541 signature_len
= strlen(signature
) + 1;
1542 msg
.m
.signature_len
= signature_len
;
1544 /* Calculate fields len, serialize fields. */
1545 if (nr_fields
> 0) {
1546 ret
= alloc_serialize_fields(session
, &nr_write_fields
, &fields
,
1547 nr_fields
, lttng_fields
);
1552 fields_len
= sizeof(*fields
) * nr_write_fields
;
1553 msg
.m
.fields_len
= fields_len
;
1554 if (model_emf_uri
) {
1555 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
1557 model_emf_uri_len
= 0;
1559 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
1561 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1562 if (len
> 0 && len
!= sizeof(msg
)) {
1571 /* send signature */
1572 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
1573 if (len
> 0 && len
!= signature_len
) {
1583 if (fields_len
> 0) {
1584 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1585 if (len
> 0 && len
!= fields_len
) {
1596 if (model_emf_uri_len
) {
1597 /* send model_emf_uri */
1598 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
1600 if (len
> 0 && len
!= model_emf_uri_len
) {
1609 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1611 case 0: /* orderly shutdown */
1614 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1615 ERR("Unexpected result message command "
1616 "expected: %u vs received: %u\n",
1617 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1620 if (reply
.r
.ret_code
> 0)
1622 if (reply
.r
.ret_code
< 0)
1623 return reply
.r
.ret_code
;
1624 *id
= reply
.r
.event_id
;
1625 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1626 event_name
, reply
.r
.ret_code
, reply
.r
.event_id
);
1630 /* Transport level error */
1631 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1635 ERR("incorrect message size: %zd\n", len
);
1641 /* Error path only. */
1648 * Returns 0 on success, negative error value on error.
1649 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1651 int ustcomm_register_enum(int sock
,
1652 int session_objd
, /* session descriptor */
1653 const char *enum_name
, /* enum name (input) */
1654 size_t nr_entries
, /* entries */
1655 const struct lttng_enum_entry
*lttng_entries
,
1660 struct ustcomm_notify_hdr header
;
1661 struct ustcomm_notify_enum_msg m
;
1664 struct ustcomm_notify_hdr header
;
1665 struct ustcomm_notify_enum_reply r
;
1668 struct ustctl_enum_entry
*entries
= NULL
;
1671 memset(&msg
, 0, sizeof(msg
));
1672 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1673 msg
.m
.session_objd
= session_objd
;
1674 strncpy(msg
.m
.enum_name
, enum_name
, LTTNG_UST_SYM_NAME_LEN
);
1675 msg
.m
.enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1677 /* Calculate entries len, serialize entries. */
1678 if (nr_entries
> 0) {
1679 ret
= serialize_entries(&entries
,
1680 nr_entries
, lttng_entries
);
1685 entries_len
= sizeof(*entries
) * nr_entries
;
1686 msg
.m
.entries_len
= entries_len
;
1688 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1689 if (len
> 0 && len
!= sizeof(msg
)) {
1699 if (entries_len
> 0) {
1700 len
= ustcomm_send_unix_sock(sock
, entries
, entries_len
);
1701 if (len
> 0 && len
!= entries_len
) {
1714 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1716 case 0: /* orderly shutdown */
1719 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1720 ERR("Unexpected result message command "
1721 "expected: %u vs received: %u\n",
1722 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1725 if (reply
.r
.ret_code
> 0)
1727 if (reply
.r
.ret_code
< 0)
1728 return reply
.r
.ret_code
;
1729 *id
= reply
.r
.enum_id
;
1730 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1731 enum_name
, reply
.r
.ret_code
);
1735 /* Transport level error */
1736 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1740 ERR("incorrect message size: %zd\n", len
);
1752 * Returns 0 on success, negative error value on error.
1753 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1755 int ustcomm_register_channel(int sock
,
1756 struct lttng_session
*session
,
1757 int session_objd
, /* session descriptor */
1758 int channel_objd
, /* channel descriptor */
1759 size_t nr_ctx_fields
,
1760 const struct lttng_ctx_field
*ctx_fields
,
1761 uint32_t *chan_id
, /* channel id (output) */
1762 int *header_type
) /* header type (output) */
1766 struct ustcomm_notify_hdr header
;
1767 struct ustcomm_notify_channel_msg m
;
1770 struct ustcomm_notify_hdr header
;
1771 struct ustcomm_notify_channel_reply r
;
1774 struct ustctl_field
*fields
= NULL
;
1776 size_t nr_write_fields
= 0;
1778 memset(&msg
, 0, sizeof(msg
));
1779 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1780 msg
.m
.session_objd
= session_objd
;
1781 msg
.m
.channel_objd
= channel_objd
;
1783 /* Calculate fields len, serialize fields. */
1784 if (nr_ctx_fields
> 0) {
1785 ret
= serialize_ctx_fields(session
, &nr_write_fields
, &fields
,
1786 nr_ctx_fields
, ctx_fields
);
1791 fields_len
= sizeof(*fields
) * nr_write_fields
;
1792 msg
.m
.ctx_fields_len
= fields_len
;
1793 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1794 if (len
> 0 && len
!= sizeof(msg
)) {
1804 if (fields_len
> 0) {
1805 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1807 if (len
> 0 && len
!= fields_len
) {
1817 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1819 case 0: /* orderly shutdown */
1822 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1823 ERR("Unexpected result message command "
1824 "expected: %u vs received: %u\n",
1825 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1828 if (reply
.r
.ret_code
> 0)
1830 if (reply
.r
.ret_code
< 0)
1831 return reply
.r
.ret_code
;
1832 *chan_id
= reply
.r
.chan_id
;
1833 switch (reply
.r
.header_type
) {
1836 *header_type
= reply
.r
.header_type
;
1839 ERR("Unexpected channel header type %u\n",
1840 reply
.r
.header_type
);
1843 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1844 reply
.r
.chan_id
, reply
.r
.header_type
);
1848 /* Transport level error */
1849 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1853 ERR("incorrect message size: %zd\n", len
);
1860 * Set socket reciving timeout.
1862 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1867 tv
.tv_sec
= msec
/ 1000;
1868 tv
.tv_usec
= (msec
* 1000 % 1000000);
1870 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1872 PERROR("setsockopt SO_RCVTIMEO");
1880 * Set socket sending timeout.
1882 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1887 tv
.tv_sec
= msec
/ 1000;
1888 tv
.tv_usec
= (msec
* 1000) % 1000000;
1890 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1892 PERROR("setsockopt SO_SNDTIMEO");