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 int ustcomm_recv_stream_from_sessiond(int sock
,
673 uint64_t *memory_map_size
,
674 int *shm_fd
, int *wakeup_fd
)
680 /* recv shm fd and wakeup fd */
681 lttng_ust_lock_fd_tracker();
682 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
684 lttng_ust_unlock_fd_tracker();
694 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
698 PERROR("close on received shm_fd");
701 lttng_ust_unlock_fd_tracker();
706 ret
= lttng_ust_add_fd_to_tracker(fds
[1]);
708 ret
= close(*shm_fd
);
710 PERROR("close on shm_fd");
715 PERROR("close on received wakeup_fd");
718 lttng_ust_unlock_fd_tracker();
722 lttng_ust_unlock_fd_tracker();
730 * Returns 0 on success, negative error value on error.
732 int ustcomm_send_reg_msg(int sock
,
733 enum ustctl_socket_type type
,
734 uint32_t bits_per_long
,
735 uint32_t uint8_t_alignment
,
736 uint32_t uint16_t_alignment
,
737 uint32_t uint32_t_alignment
,
738 uint32_t uint64_t_alignment
,
739 uint32_t long_alignment
)
742 struct ustctl_reg_msg reg_msg
;
744 reg_msg
.magic
= LTTNG_UST_COMM_MAGIC
;
745 reg_msg
.major
= LTTNG_UST_ABI_MAJOR_VERSION
;
746 reg_msg
.minor
= LTTNG_UST_ABI_MINOR_VERSION
;
747 reg_msg
.pid
= getpid();
748 reg_msg
.ppid
= getppid();
749 reg_msg
.uid
= getuid();
750 reg_msg
.gid
= getgid();
751 reg_msg
.bits_per_long
= bits_per_long
;
752 reg_msg
.uint8_t_alignment
= uint8_t_alignment
;
753 reg_msg
.uint16_t_alignment
= uint16_t_alignment
;
754 reg_msg
.uint32_t_alignment
= uint32_t_alignment
;
755 reg_msg
.uint64_t_alignment
= uint64_t_alignment
;
756 reg_msg
.long_alignment
= long_alignment
;
757 reg_msg
.socket_type
= type
;
758 lttng_ust_getprocname(reg_msg
.name
);
759 memset(reg_msg
.padding
, 0, sizeof(reg_msg
.padding
));
761 len
= ustcomm_send_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
762 if (len
> 0 && len
!= sizeof(reg_msg
))
770 ssize_t
count_one_type(const struct lttng_type
*lt
)
781 return count_fields_recursive(lt
->u
.legacy
._struct
.nr_fields
,
782 lt
->u
.legacy
._struct
.fields
) + 1;
783 case atype_enum_nestable
:
784 return count_one_type(lt
->u
.enum_nestable
.container_type
) + 1;
785 case atype_array_nestable
:
786 return count_one_type(lt
->u
.array_nestable
.elem_type
) + 1;
787 case atype_sequence_nestable
:
788 return count_one_type(lt
->u
.sequence_nestable
.elem_type
) + 1;
789 case atype_struct_nestable
:
790 return count_fields_recursive(lt
->u
.struct_nestable
.nr_fields
,
791 lt
->u
.struct_nestable
.fields
) + 1;
795 const struct lttng_event_field
*choices
;
799 ret
= lttng_ust_dynamic_type_choices(&nr_choices
,
804 * Two fields for enum, one field for variant, and
805 * one field per choice.
807 return count_fields_recursive(nr_choices
, choices
) + 3;
817 ssize_t
count_fields_recursive(size_t nr_fields
,
818 const struct lttng_event_field
*lttng_fields
)
821 ssize_t ret
, count
= 0;
823 for (i
= 0; i
< nr_fields
; i
++) {
824 const struct lttng_event_field
*lf
;
826 lf
= <tng_fields
[i
];
827 /* skip 'nowrite' fields */
830 ret
= count_one_type(&lf
->type
);
832 return ret
; /* error */
839 ssize_t
count_ctx_fields_recursive(size_t nr_fields
,
840 const struct lttng_ctx_field
*lttng_fields
)
843 ssize_t ret
, count
= 0;
845 for (i
= 0; i
< nr_fields
; i
++) {
846 const struct lttng_event_field
*lf
;
848 lf
= <tng_fields
[i
].event_field
;
849 /* skip 'nowrite' fields */
852 ret
= count_one_type(&lf
->type
);
854 return ret
; /* error */
861 int serialize_string_encoding(int32_t *ue
,
862 enum lttng_string_encodings le
)
865 case lttng_encode_none
:
866 *ue
= ustctl_encode_none
;
868 case lttng_encode_UTF8
:
869 *ue
= ustctl_encode_UTF8
;
871 case lttng_encode_ASCII
:
872 *ue
= ustctl_encode_ASCII
;
881 int serialize_integer_type(struct ustctl_integer_type
*uit
,
882 const struct lttng_integer_type
*lit
)
886 uit
->size
= lit
->size
;
887 uit
->signedness
= lit
->signedness
;
888 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
889 uit
->base
= lit
->base
;
890 if (serialize_string_encoding(&encoding
, lit
->encoding
))
892 uit
->encoding
= encoding
;
893 uit
->alignment
= lit
->alignment
;
898 int serialize_basic_type(struct lttng_session
*session
,
899 enum ustctl_abstract_types
*uatype
,
900 enum lttng_abstract_types atype
,
901 union _ustctl_basic_type
*ubt
,
902 const union _lttng_basic_type
*lbt
)
907 if (serialize_integer_type(&ubt
->integer
, &lbt
->integer
))
909 *uatype
= ustctl_atype_integer
;
916 if (serialize_string_encoding(&encoding
, lbt
->string
.encoding
))
918 ubt
->string
.encoding
= encoding
;
919 *uatype
= ustctl_atype_string
;
924 struct ustctl_float_type
*uft
;
925 const struct lttng_float_type
*lft
;
929 uft
->exp_dig
= lft
->exp_dig
;
930 uft
->mant_dig
= lft
->mant_dig
;
931 uft
->alignment
= lft
->alignment
;
932 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
933 *uatype
= ustctl_atype_float
;
938 strncpy(ubt
->enumeration
.name
, lbt
->enumeration
.desc
->name
,
939 LTTNG_UST_SYM_NAME_LEN
);
940 ubt
->enumeration
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
941 if (serialize_integer_type(&ubt
->enumeration
.container_type
,
942 &lbt
->enumeration
.container_type
))
945 const struct lttng_enum
*_enum
;
947 _enum
= lttng_ust_enum_get_from_desc(session
, lbt
->enumeration
.desc
);
950 ubt
->enumeration
.id
= _enum
->id
;
952 ubt
->enumeration
.id
= -1ULL;
954 *uatype
= ustctl_atype_enum
;
958 case atype_array_nestable
:
960 case atype_sequence_nestable
:
961 case atype_enum_nestable
:
969 int serialize_dynamic_type(struct lttng_session
*session
,
970 struct ustctl_field
*fields
, size_t *iter_output
,
971 const char *field_name
)
973 const struct lttng_event_field
*choices
;
974 char tag_field_name
[LTTNG_UST_SYM_NAME_LEN
];
975 const struct lttng_type
*tag_type
;
976 const struct lttng_event_field
*tag_field_generic
;
977 struct lttng_event_field tag_field
= {
978 .name
= tag_field_name
,
981 struct ustctl_field
*uf
;
982 size_t nr_choices
, i
;
985 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
986 tag_type
= &tag_field_generic
->type
;
988 /* Serialize enum field. */
989 strncpy(tag_field_name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
990 tag_field_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
991 strncat(tag_field_name
,
993 LTTNG_UST_SYM_NAME_LEN
- strlen(tag_field_name
) - 1);
994 tag_field
.type
= *tag_type
;
995 ret
= serialize_one_field(session
, fields
, iter_output
,
1000 /* Serialize variant field. */
1001 uf
= &fields
[*iter_output
];
1002 ret
= lttng_ust_dynamic_type_choices(&nr_choices
, &choices
);
1006 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1007 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1008 uf
->type
.atype
= ustctl_atype_variant
;
1009 uf
->type
.u
.variant_nestable
.nr_choices
= nr_choices
;
1010 strncpy(uf
->type
.u
.variant_nestable
.tag_name
,
1012 LTTNG_UST_SYM_NAME_LEN
);
1013 uf
->type
.u
.variant_nestable
.tag_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1014 uf
->type
.u
.variant_nestable
.alignment
= 0;
1017 /* Serialize choice fields after variant. */
1018 for (i
= 0; i
< nr_choices
; i
++) {
1019 ret
= serialize_one_field(session
, fields
,
1020 iter_output
, &choices
[i
]);
1028 int serialize_one_type(struct lttng_session
*session
,
1029 struct ustctl_field
*fields
, size_t *iter_output
,
1030 const char *field_name
, const struct lttng_type
*lt
)
1035 * Serializing a type (rather than a field) generates a ustctl_field
1036 * entry with 0-length name.
1039 switch (lt
->atype
) {
1045 struct ustctl_field
*uf
= &fields
[*iter_output
];
1046 struct ustctl_type
*ut
= &uf
->type
;
1047 enum ustctl_abstract_types atype
;
1050 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1051 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1055 ret
= serialize_basic_type(session
, &atype
, lt
->atype
,
1056 &ut
->u
.legacy
.basic
, <
->u
.legacy
.basic
);
1065 struct ustctl_field
*uf
= &fields
[*iter_output
];
1066 struct ustctl_type
*ut
= &uf
->type
;
1067 struct ustctl_basic_type
*ubt
;
1068 const struct lttng_basic_type
*lbt
;
1069 enum ustctl_abstract_types atype
;
1072 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1073 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1077 ut
->atype
= ustctl_atype_array
;
1078 ubt
= &ut
->u
.legacy
.array
.elem_type
;
1079 lbt
= <
->u
.legacy
.array
.elem_type
;
1080 ut
->u
.legacy
.array
.length
= lt
->u
.legacy
.array
.length
;
1081 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1082 &ubt
->u
.basic
, &lbt
->u
.basic
);
1089 case atype_array_nestable
:
1091 struct ustctl_field
*uf
= &fields
[*iter_output
];
1092 struct ustctl_type
*ut
= &uf
->type
;
1095 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1096 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1100 ut
->atype
= ustctl_atype_array_nestable
;
1101 ut
->u
.array_nestable
.length
= lt
->u
.array_nestable
.length
;
1102 ut
->u
.array_nestable
.alignment
= lt
->u
.array_nestable
.alignment
;
1105 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1106 lt
->u
.array_nestable
.elem_type
);
1111 case atype_sequence
:
1113 struct ustctl_field
*uf
= &fields
[*iter_output
];
1114 struct ustctl_type
*ut
= &uf
->type
;
1115 struct ustctl_basic_type
*ubt
;
1116 const struct lttng_basic_type
*lbt
;
1117 enum ustctl_abstract_types atype
;
1120 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1121 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1125 uf
->type
.atype
= ustctl_atype_sequence
;
1126 ubt
= &ut
->u
.legacy
.sequence
.length_type
;
1127 lbt
= <
->u
.legacy
.sequence
.length_type
;
1128 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1129 &ubt
->u
.basic
, &lbt
->u
.basic
);
1133 ubt
= &ut
->u
.legacy
.sequence
.elem_type
;
1134 lbt
= <
->u
.legacy
.sequence
.elem_type
;
1135 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1136 &ubt
->u
.basic
, &lbt
->u
.basic
);
1143 case atype_sequence_nestable
:
1145 struct ustctl_field
*uf
= &fields
[*iter_output
];
1146 struct ustctl_type
*ut
= &uf
->type
;
1149 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1150 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1154 ut
->atype
= ustctl_atype_sequence_nestable
;
1155 strncpy(ut
->u
.sequence_nestable
.length_name
,
1156 lt
->u
.sequence_nestable
.length_name
,
1157 LTTNG_UST_SYM_NAME_LEN
);
1158 ut
->u
.sequence_nestable
.length_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1159 ut
->u
.sequence_nestable
.alignment
= lt
->u
.sequence_nestable
.alignment
;
1162 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1163 lt
->u
.sequence_nestable
.elem_type
);
1170 ret
= serialize_dynamic_type(session
, fields
, iter_output
,
1178 struct ustctl_field
*uf
= &fields
[*iter_output
];
1181 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1182 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1186 uf
->type
.atype
= ustctl_atype_struct
;
1187 uf
->type
.u
.legacy
._struct
.nr_fields
= lt
->u
.legacy
._struct
.nr_fields
;
1190 ret
= serialize_fields(session
, fields
, iter_output
,
1191 lt
->u
.legacy
._struct
.nr_fields
,
1192 lt
->u
.legacy
._struct
.fields
);
1197 case atype_struct_nestable
:
1199 struct ustctl_field
*uf
= &fields
[*iter_output
];
1202 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1203 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1207 uf
->type
.atype
= ustctl_atype_struct_nestable
;
1208 uf
->type
.u
.struct_nestable
.nr_fields
= lt
->u
.struct_nestable
.nr_fields
;
1209 uf
->type
.u
.struct_nestable
.alignment
= lt
->u
.struct_nestable
.alignment
;
1212 ret
= serialize_fields(session
, fields
, iter_output
,
1213 lt
->u
.struct_nestable
.nr_fields
,
1214 lt
->u
.struct_nestable
.fields
);
1219 case atype_enum_nestable
:
1221 struct ustctl_field
*uf
= &fields
[*iter_output
];
1222 struct ustctl_type
*ut
= &uf
->type
;
1225 strncpy(uf
->name
, field_name
, LTTNG_UST_SYM_NAME_LEN
);
1226 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1230 strncpy(ut
->u
.enum_nestable
.name
, lt
->u
.enum_nestable
.desc
->name
,
1231 LTTNG_UST_SYM_NAME_LEN
);
1232 ut
->u
.enum_nestable
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1233 ut
->atype
= ustctl_atype_enum_nestable
;
1236 ret
= serialize_one_type(session
, fields
, iter_output
, NULL
,
1237 lt
->u
.enum_nestable
.container_type
);
1241 const struct lttng_enum
*_enum
;
1243 _enum
= lttng_ust_enum_get_from_desc(session
, lt
->u
.enum_nestable
.desc
);
1246 ut
->u
.enum_nestable
.id
= _enum
->id
;
1248 ut
->u
.enum_nestable
.id
= -1ULL;
1259 int serialize_one_field(struct lttng_session
*session
,
1260 struct ustctl_field
*fields
, size_t *iter_output
,
1261 const struct lttng_event_field
*lf
)
1263 /* skip 'nowrite' fields */
1267 return serialize_one_type(session
, fields
, iter_output
, lf
->name
, &lf
->type
);
1271 int serialize_fields(struct lttng_session
*session
,
1272 struct ustctl_field
*ustctl_fields
,
1273 size_t *iter_output
, size_t nr_lttng_fields
,
1274 const struct lttng_event_field
*lttng_fields
)
1279 for (i
= 0; i
< nr_lttng_fields
; i
++) {
1280 ret
= serialize_one_field(session
, ustctl_fields
,
1281 iter_output
, <tng_fields
[i
]);
1289 int alloc_serialize_fields(struct lttng_session
*session
,
1290 size_t *_nr_write_fields
,
1291 struct ustctl_field
**ustctl_fields
,
1293 const struct lttng_event_field
*lttng_fields
)
1295 struct ustctl_field
*fields
;
1297 size_t iter_output
= 0;
1298 ssize_t nr_write_fields
;
1300 nr_write_fields
= count_fields_recursive(nr_fields
, lttng_fields
);
1301 if (nr_write_fields
< 0) {
1302 return (int) nr_write_fields
;
1305 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1309 ret
= serialize_fields(session
, fields
, &iter_output
, nr_fields
,
1314 *_nr_write_fields
= nr_write_fields
;
1315 *ustctl_fields
= fields
;
1324 int serialize_entries(struct ustctl_enum_entry
**_entries
,
1326 const struct lttng_enum_entry
*lttng_entries
)
1328 struct ustctl_enum_entry
*entries
;
1331 /* Serialize the entries */
1332 entries
= zmalloc(nr_entries
* sizeof(*entries
));
1335 for (i
= 0; i
< nr_entries
; i
++) {
1336 struct ustctl_enum_entry
*uentry
;
1337 const struct lttng_enum_entry
*lentry
;
1339 uentry
= &entries
[i
];
1340 lentry
= <tng_entries
[i
];
1342 uentry
->start
.value
= lentry
->start
.value
;
1343 uentry
->start
.signedness
= lentry
->start
.signedness
;
1344 uentry
->end
.value
= lentry
->end
.value
;
1345 uentry
->end
.signedness
= lentry
->end
.signedness
;
1346 strncpy(uentry
->string
, lentry
->string
, LTTNG_UST_SYM_NAME_LEN
);
1347 uentry
->string
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1349 if (lentry
->u
.extra
.options
& LTTNG_ENUM_ENTRY_OPTION_IS_AUTO
) {
1350 uentry
->u
.extra
.options
|=
1351 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO
;
1354 *_entries
= entries
;
1359 int serialize_ctx_fields(struct lttng_session
*session
,
1360 size_t *_nr_write_fields
,
1361 struct ustctl_field
**ustctl_fields
,
1363 const struct lttng_ctx_field
*lttng_fields
)
1365 struct ustctl_field
*fields
;
1367 size_t i
, iter_output
= 0;
1368 ssize_t nr_write_fields
;
1370 nr_write_fields
= count_ctx_fields_recursive(nr_fields
,
1372 if (nr_write_fields
< 0) {
1373 return (int) nr_write_fields
;
1376 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1380 for (i
= 0; i
< nr_fields
; i
++) {
1381 ret
= serialize_one_field(session
, fields
, &iter_output
,
1382 <tng_fields
[i
].event_field
);
1387 *_nr_write_fields
= nr_write_fields
;
1388 *ustctl_fields
= fields
;
1397 * Returns 0 on success, negative error value on error.
1399 int ustcomm_register_event(int sock
,
1400 struct lttng_session
*session
,
1401 int session_objd
, /* session descriptor */
1402 int channel_objd
, /* channel descriptor */
1403 const char *event_name
, /* event name (input) */
1405 const char *signature
, /* event signature (input) */
1406 size_t nr_fields
, /* fields */
1407 const struct lttng_event_field
*lttng_fields
,
1408 const char *model_emf_uri
,
1409 uint32_t *id
) /* event id (output) */
1413 struct ustcomm_notify_hdr header
;
1414 struct ustcomm_notify_event_msg m
;
1417 struct ustcomm_notify_hdr header
;
1418 struct ustcomm_notify_event_reply r
;
1420 size_t signature_len
, fields_len
, model_emf_uri_len
;
1421 struct ustctl_field
*fields
= NULL
;
1422 size_t nr_write_fields
= 0;
1425 memset(&msg
, 0, sizeof(msg
));
1426 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1427 msg
.m
.session_objd
= session_objd
;
1428 msg
.m
.channel_objd
= channel_objd
;
1429 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_SYM_NAME_LEN
);
1430 msg
.m
.event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1431 msg
.m
.loglevel
= loglevel
;
1432 signature_len
= strlen(signature
) + 1;
1433 msg
.m
.signature_len
= signature_len
;
1435 /* Calculate fields len, serialize fields. */
1436 if (nr_fields
> 0) {
1437 ret
= alloc_serialize_fields(session
, &nr_write_fields
, &fields
,
1438 nr_fields
, lttng_fields
);
1443 fields_len
= sizeof(*fields
) * nr_write_fields
;
1444 msg
.m
.fields_len
= fields_len
;
1445 if (model_emf_uri
) {
1446 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
1448 model_emf_uri_len
= 0;
1450 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
1452 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1453 if (len
> 0 && len
!= sizeof(msg
)) {
1462 /* send signature */
1463 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
1464 if (len
> 0 && len
!= signature_len
) {
1474 if (fields_len
> 0) {
1475 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1476 if (len
> 0 && len
!= fields_len
) {
1487 if (model_emf_uri_len
) {
1488 /* send model_emf_uri */
1489 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
1491 if (len
> 0 && len
!= model_emf_uri_len
) {
1500 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1502 case 0: /* orderly shutdown */
1505 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1506 ERR("Unexpected result message command "
1507 "expected: %u vs received: %u\n",
1508 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1511 if (reply
.r
.ret_code
> 0)
1513 if (reply
.r
.ret_code
< 0)
1514 return reply
.r
.ret_code
;
1515 *id
= reply
.r
.event_id
;
1516 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1517 event_name
, reply
.r
.ret_code
, reply
.r
.event_id
);
1521 /* Transport level error */
1522 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1526 ERR("incorrect message size: %zd\n", len
);
1532 /* Error path only. */
1539 * Returns 0 on success, negative error value on error.
1540 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1542 int ustcomm_register_enum(int sock
,
1543 int session_objd
, /* session descriptor */
1544 const char *enum_name
, /* enum name (input) */
1545 size_t nr_entries
, /* entries */
1546 const struct lttng_enum_entry
*lttng_entries
,
1551 struct ustcomm_notify_hdr header
;
1552 struct ustcomm_notify_enum_msg m
;
1555 struct ustcomm_notify_hdr header
;
1556 struct ustcomm_notify_enum_reply r
;
1559 struct ustctl_enum_entry
*entries
= NULL
;
1562 memset(&msg
, 0, sizeof(msg
));
1563 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1564 msg
.m
.session_objd
= session_objd
;
1565 strncpy(msg
.m
.enum_name
, enum_name
, LTTNG_UST_SYM_NAME_LEN
);
1566 msg
.m
.enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1568 /* Calculate entries len, serialize entries. */
1569 if (nr_entries
> 0) {
1570 ret
= serialize_entries(&entries
,
1571 nr_entries
, lttng_entries
);
1576 entries_len
= sizeof(*entries
) * nr_entries
;
1577 msg
.m
.entries_len
= entries_len
;
1579 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1580 if (len
> 0 && len
!= sizeof(msg
)) {
1590 if (entries_len
> 0) {
1591 len
= ustcomm_send_unix_sock(sock
, entries
, entries_len
);
1592 if (len
> 0 && len
!= entries_len
) {
1605 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1607 case 0: /* orderly shutdown */
1610 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1611 ERR("Unexpected result message command "
1612 "expected: %u vs received: %u\n",
1613 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1616 if (reply
.r
.ret_code
> 0)
1618 if (reply
.r
.ret_code
< 0)
1619 return reply
.r
.ret_code
;
1620 *id
= reply
.r
.enum_id
;
1621 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1622 enum_name
, reply
.r
.ret_code
);
1626 /* Transport level error */
1627 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1631 ERR("incorrect message size: %zd\n", len
);
1643 * Returns 0 on success, negative error value on error.
1644 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1646 int ustcomm_register_channel(int sock
,
1647 struct lttng_session
*session
,
1648 int session_objd
, /* session descriptor */
1649 int channel_objd
, /* channel descriptor */
1650 size_t nr_ctx_fields
,
1651 const struct lttng_ctx_field
*ctx_fields
,
1652 uint32_t *chan_id
, /* channel id (output) */
1653 int *header_type
) /* header type (output) */
1657 struct ustcomm_notify_hdr header
;
1658 struct ustcomm_notify_channel_msg m
;
1661 struct ustcomm_notify_hdr header
;
1662 struct ustcomm_notify_channel_reply r
;
1665 struct ustctl_field
*fields
= NULL
;
1667 size_t nr_write_fields
= 0;
1669 memset(&msg
, 0, sizeof(msg
));
1670 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1671 msg
.m
.session_objd
= session_objd
;
1672 msg
.m
.channel_objd
= channel_objd
;
1674 /* Calculate fields len, serialize fields. */
1675 if (nr_ctx_fields
> 0) {
1676 ret
= serialize_ctx_fields(session
, &nr_write_fields
, &fields
,
1677 nr_ctx_fields
, ctx_fields
);
1682 fields_len
= sizeof(*fields
) * nr_write_fields
;
1683 msg
.m
.ctx_fields_len
= fields_len
;
1684 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1685 if (len
> 0 && len
!= sizeof(msg
)) {
1695 if (fields_len
> 0) {
1696 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1698 if (len
> 0 && len
!= fields_len
) {
1708 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1710 case 0: /* orderly shutdown */
1713 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1714 ERR("Unexpected result message command "
1715 "expected: %u vs received: %u\n",
1716 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1719 if (reply
.r
.ret_code
> 0)
1721 if (reply
.r
.ret_code
< 0)
1722 return reply
.r
.ret_code
;
1723 *chan_id
= reply
.r
.chan_id
;
1724 switch (reply
.r
.header_type
) {
1727 *header_type
= reply
.r
.header_type
;
1730 ERR("Unexpected channel header type %u\n",
1731 reply
.r
.header_type
);
1734 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1735 reply
.r
.chan_id
, reply
.r
.header_type
);
1739 /* Transport level error */
1740 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1744 ERR("incorrect message size: %zd\n", len
);
1751 * Set socket reciving timeout.
1753 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1758 tv
.tv_sec
= msec
/ 1000;
1759 tv
.tv_usec
= (msec
* 1000 % 1000000);
1761 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1763 PERROR("setsockopt SO_RCVTIMEO");
1771 * Set socket sending timeout.
1773 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1778 tv
.tv_sec
= msec
/ 1000;
1779 tv
.tv_usec
= (msec
* 1000) % 1000000;
1781 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1783 PERROR("setsockopt SO_SNDTIMEO");