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
25 #include <sys/socket.h>
27 #include <sys/types.h>
34 #include <lttng/ust-ctl.h>
38 #include <lttng/ust-error.h>
39 #include <lttng/ust-events.h>
40 #include <lttng/ust-dynamic-type.h>
41 #include <usterr-signal-safe.h>
43 #include "../liblttng-ust/compat.h"
45 #define USTCOMM_CODE_OFFSET(code) \
46 (code == LTTNG_UST_OK ? 0 : (code - LTTNG_UST_ERR + 1))
48 #define USTCOMM_MAX_SEND_FDS 4
51 ssize_t
count_fields_recursive(size_t nr_fields
,
52 const struct lttng_event_field
*lttng_fields
);
54 int serialize_one_field(struct lttng_session
*session
,
55 struct ustctl_field
*fields
, size_t *iter_output
,
56 const struct lttng_event_field
*lf
);
59 * Human readable error message.
61 static const char *ustcomm_readable_code
[] = {
62 [ USTCOMM_CODE_OFFSET(LTTNG_UST_OK
) ] = "Success",
63 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR
) ] = "Unknown error",
64 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOENT
) ] = "No entry",
65 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXIST
) ] = "Object already exists",
66 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL
) ] = "Invalid argument",
67 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PERM
) ] = "Permission denied",
68 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOSYS
) ] = "Not implemented",
69 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXITING
) ] = "Process is exiting",
71 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_MAGIC
) ] = "Invalid magic number",
72 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_SOCKET_TYPE
) ] = "Invalid socket type",
73 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_UNSUP_MAJOR
) ] = "Unsupported major version",
74 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PEERCRED
) ] = "Cannot get unix socket peer credentials",
75 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PEERCRED_PID
) ] = "Peer credentials PID is invalid. Socket appears to belong to a distinct, non-nested pid namespace.",
81 * Receives positive error value.
82 * Return ptr to string representing a human readable
83 * error code from the ustcomm_return_code enum.
85 const char *lttng_ust_strerror(int code
)
87 if (code
== LTTNG_UST_OK
)
88 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
89 if (code
< LTTNG_UST_ERR
)
90 return strerror(code
);
91 if (code
>= LTTNG_UST_ERR_NR
)
93 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
97 * ustcomm_connect_unix_sock
99 * Connect to unix socket using the path name.
101 * Caller handles FD tracker.
103 int ustcomm_connect_unix_sock(const char *pathname
, long timeout
)
105 struct sockaddr_un sun
;
109 * libust threads require the close-on-exec flag for all
110 * resources so it does not leak file descriptors upon exec.
111 * SOCK_CLOEXEC is not used since it is linux specific.
113 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
120 /* Give at least 10ms. */
123 ret
= ustcomm_setsockopt_snd_timeout(fd
, timeout
);
125 WARN("Error setting connect socket send timeout");
128 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
135 memset(&sun
, 0, sizeof(sun
));
136 sun
.sun_family
= AF_UNIX
;
137 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
138 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
140 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
143 * Don't print message on connect ENOENT error, because
144 * connect is used in normal execution to detect if
145 * sessiond is alive. ENOENT is when the unix socket
146 * file does not exist, and ECONNREFUSED is when the
147 * file exists but no sessiond is listening.
149 if (errno
!= ECONNREFUSED
&& errno
!= ECONNRESET
150 && errno
!= ENOENT
&& errno
!= EACCES
)
153 if (ret
== -ECONNREFUSED
|| ret
== -ECONNRESET
)
165 closeret
= close(fd
);
174 * ustcomm_accept_unix_sock
176 * Do an accept(2) on the sock and return the
177 * new file descriptor. The socket MUST be bind(2) before.
179 int ustcomm_accept_unix_sock(int sock
)
182 struct sockaddr_un sun
;
186 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
188 if (errno
!= ECONNABORTED
)
191 if (new_fd
== -ECONNABORTED
)
198 * ustcomm_create_unix_sock
200 * Creates a AF_UNIX local socket using pathname
201 * bind the socket upon creation and return the fd.
203 int ustcomm_create_unix_sock(const char *pathname
)
205 struct sockaddr_un sun
;
208 /* Create server socket */
209 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
215 memset(&sun
, 0, sizeof(sun
));
216 sun
.sun_family
= AF_UNIX
;
217 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
218 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
220 /* Unlink the old file if present */
221 (void) unlink(pathname
);
222 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
235 closeret
= close(fd
);
245 * ustcomm_listen_unix_sock
247 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
249 int ustcomm_listen_unix_sock(int sock
)
253 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
263 * ustcomm_close_unix_sock
265 * Shutdown cleanly a unix socket.
267 * Handles fd tracker internally.
269 int ustcomm_close_unix_sock(int sock
)
273 lttng_ust_lock_fd_tracker();
276 lttng_ust_delete_fd_from_tracker(sock
);
281 lttng_ust_unlock_fd_tracker();
287 * ustcomm_recv_unix_sock
289 * Receive data of size len in put that data into
290 * the buf param. Using recvmsg API.
291 * Return the size of received data.
292 * Return 0 on orderly shutdown.
294 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
301 memset(&msg
, 0, sizeof(msg
));
303 iov
[0].iov_base
= buf
;
304 iov
[0].iov_len
= len
;
309 len_last
= iov
[0].iov_len
;
310 ret
= recvmsg(sock
, &msg
, 0);
312 iov
[0].iov_base
+= ret
;
313 iov
[0].iov_len
-= ret
;
314 assert(ret
<= len_last
);
316 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
321 if (errno
!= EPIPE
&& errno
!= ECONNRESET
&& errno
!= ECONNREFUSED
)
324 if (ret
== -ECONNRESET
|| ret
== -ECONNREFUSED
)
327 shutret
= shutdown(sock
, SHUT_RDWR
);
329 ERR("Socket shutdown error");
330 } else if (ret
> 0) {
333 /* ret = 0 means an orderly shutdown. */
339 * ustcomm_send_unix_sock
341 * Send buf data of size len. Using sendmsg API.
342 * Return the size of sent data.
344 ssize_t
ustcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
350 memset(&msg
, 0, sizeof(msg
));
352 iov
[0].iov_base
= (void *) buf
;
353 iov
[0].iov_len
= len
;
358 * Using the MSG_NOSIGNAL when sending data from sessiond to
359 * libust, so libust does not receive an unhandled SIGPIPE or
360 * SIGURG. The sessiond receiver side can be made more resilient
361 * by ignoring SIGPIPE, but we don't have this luxury on the
365 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
366 } while (ret
< 0 && errno
== EINTR
);
371 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
374 if (ret
== -ECONNRESET
)
377 shutret
= shutdown(sock
, SHUT_RDWR
);
379 ERR("Socket shutdown error");
386 * Send a message accompanied by fd(s) over a unix socket.
388 * Returns the size of data sent, or negative error value.
390 ssize_t
ustcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
393 struct cmsghdr
*cmptr
;
396 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
397 char tmp
[CMSG_SPACE(sizeof_fds
)];
400 memset(&msg
, 0, sizeof(msg
));
401 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
403 if (nb_fd
> USTCOMM_MAX_SEND_FDS
)
406 msg
.msg_control
= (caddr_t
)tmp
;
407 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
409 cmptr
= CMSG_FIRSTHDR(&msg
);
412 cmptr
->cmsg_level
= SOL_SOCKET
;
413 cmptr
->cmsg_type
= SCM_RIGHTS
;
414 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
415 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
416 /* Sum of the length of all control messages in the buffer: */
417 msg
.msg_controllen
= cmptr
->cmsg_len
;
419 iov
[0].iov_base
= &dummy
;
425 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
426 } while (ret
< 0 && errno
== EINTR
);
429 * We consider EPIPE and ECONNRESET as expected.
431 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
435 if (ret
== -ECONNRESET
)
442 * Recv a message accompanied by fd(s) from a unix socket.
444 * Expect at most "nb_fd" file descriptors. Returns the number of fd
445 * actually received in nb_fd.
446 * Returns -EPIPE on orderly shutdown.
448 ssize_t
ustcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
452 struct cmsghdr
*cmsg
;
453 size_t sizeof_fds
= nb_fd
* sizeof(int);
454 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
459 memset(&msg
, 0, sizeof(msg
));
461 /* Prepare to receive the structures */
462 iov
[0].iov_base
= &dummy
;
466 msg
.msg_control
= recv_fd
;
467 msg
.msg_controllen
= sizeof(recv_fd
);
470 ret
= recvmsg(sock
, &msg
, 0);
471 } while (ret
< 0 && errno
== EINTR
);
473 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
474 PERROR("recvmsg fds");
477 if (ret
== -ECONNRESET
)
482 /* orderly shutdown */
487 ERR("Error: Received %zd bytes, expected %d\n",
491 if (msg
.msg_flags
& MSG_CTRUNC
) {
492 ERR("Error: Control message truncated.\n");
496 cmsg
= CMSG_FIRSTHDR(&msg
);
498 ERR("Error: Invalid control message header\n");
502 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
503 ERR("Didn't received any fd\n");
507 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
508 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
509 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
514 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
517 for (i
= 0; i
< nb_fd
; i
++) {
518 ret
= fcntl(fds
[i
], F_SETFD
, FD_CLOEXEC
);
520 PERROR("fcntl failed to set FD_CLOEXEC on fd %d",
530 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
534 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
542 ERR("incorrect message size: %zd\n", len
);
549 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
550 uint32_t expected_handle
, uint32_t expected_cmd
)
554 memset(lur
, 0, sizeof(*lur
));
555 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
557 case 0: /* orderly shutdown */
563 if (lur
->handle
!= expected_handle
) {
564 ERR("Unexpected result message handle: "
565 "expected: %u vs received: %u\n",
566 expected_handle
, lur
->handle
);
569 if (lur
->cmd
!= expected_cmd
) {
570 ERR("Unexpected result message command "
571 "expected: %u vs received: %u\n",
572 expected_cmd
, lur
->cmd
);
578 return lur
->ret_code
;
583 ERR("incorrect message size: %zd\n", len
);
589 int ustcomm_send_app_cmd(int sock
,
590 struct ustcomm_ust_msg
*lum
,
591 struct ustcomm_ust_reply
*lur
)
595 ret
= ustcomm_send_app_msg(sock
, lum
);
598 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
605 * chan_data is allocated internally if this function returns the
608 ssize_t
ustcomm_recv_channel_from_sessiond(int sock
,
609 void **_chan_data
, uint64_t var_len
,
616 if (var_len
> LTTNG_UST_CHANNEL_DATA_MAX_LEN
) {
620 /* Receive variable length data */
621 chan_data
= zmalloc(var_len
);
626 len
= ustcomm_recv_unix_sock(sock
, chan_data
, var_len
);
627 if (len
!= var_len
) {
631 lttng_ust_lock_fd_tracker();
632 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
634 lttng_ust_unlock_fd_tracker();
644 ret
= lttng_ust_add_fd_to_tracker(wakeup_fd
);
646 lttng_ust_unlock_fd_tracker();
647 ret
= close(wakeup_fd
);
649 PERROR("close on wakeup_fd");
656 lttng_ust_unlock_fd_tracker();
658 *_chan_data
= chan_data
;
668 int ustcomm_recv_stream_from_sessiond(int sock
,
669 uint64_t *memory_map_size
,
670 int *shm_fd
, int *wakeup_fd
)
676 /* recv shm fd and wakeup fd */
677 lttng_ust_lock_fd_tracker();
678 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
680 lttng_ust_unlock_fd_tracker();
690 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
692 lttng_ust_unlock_fd_tracker();
695 PERROR("close on received shm_fd");
702 ret
= lttng_ust_add_fd_to_tracker(fds
[1]);
704 lttng_ust_unlock_fd_tracker();
705 ret
= close(*shm_fd
);
707 PERROR("close on shm_fd");
712 PERROR("close on received wakeup_fd");
718 lttng_ust_unlock_fd_tracker();
726 * Returns 0 on success, negative error value on error.
728 int ustcomm_send_reg_msg(int sock
,
729 enum ustctl_socket_type type
,
730 uint32_t bits_per_long
,
731 uint32_t uint8_t_alignment
,
732 uint32_t uint16_t_alignment
,
733 uint32_t uint32_t_alignment
,
734 uint32_t uint64_t_alignment
,
735 uint32_t long_alignment
)
738 struct ustctl_reg_msg reg_msg
;
740 reg_msg
.magic
= LTTNG_UST_COMM_MAGIC
;
741 reg_msg
.major
= LTTNG_UST_ABI_MAJOR_VERSION
;
742 reg_msg
.minor
= LTTNG_UST_ABI_MINOR_VERSION
;
743 reg_msg
.pid
= getpid();
744 reg_msg
.ppid
= getppid();
745 reg_msg
.uid
= getuid();
746 reg_msg
.gid
= getgid();
747 reg_msg
.bits_per_long
= bits_per_long
;
748 reg_msg
.uint8_t_alignment
= uint8_t_alignment
;
749 reg_msg
.uint16_t_alignment
= uint16_t_alignment
;
750 reg_msg
.uint32_t_alignment
= uint32_t_alignment
;
751 reg_msg
.uint64_t_alignment
= uint64_t_alignment
;
752 reg_msg
.long_alignment
= long_alignment
;
753 reg_msg
.socket_type
= type
;
754 lttng_ust_getprocname(reg_msg
.name
);
755 memset(reg_msg
.padding
, 0, sizeof(reg_msg
.padding
));
757 len
= ustcomm_send_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
758 if (len
> 0 && len
!= sizeof(reg_msg
))
766 ssize_t
count_one_type(const struct lttng_type
*lt
)
777 //TODO: implement non-empty struct.
781 const struct lttng_event_field
*choices
;
785 ret
= lttng_ust_dynamic_type_choices(&nr_choices
,
790 * One field for enum, one field for variant, and
791 * one field per choice.
793 return count_fields_recursive(nr_choices
, choices
) + 2;
802 ssize_t
count_fields_recursive(size_t nr_fields
,
803 const struct lttng_event_field
*lttng_fields
)
806 ssize_t ret
, count
= 0;
808 for (i
= 0; i
< nr_fields
; i
++) {
809 const struct lttng_event_field
*lf
;
811 lf
= <tng_fields
[i
];
812 /* skip 'nowrite' fields */
815 ret
= count_one_type(&lf
->type
);
817 return ret
; /* error */
824 ssize_t
count_ctx_fields_recursive(size_t nr_fields
,
825 const struct lttng_ctx_field
*lttng_fields
)
828 ssize_t ret
, count
= 0;
830 for (i
= 0; i
< nr_fields
; i
++) {
831 const struct lttng_event_field
*lf
;
833 lf
= <tng_fields
[i
].event_field
;
834 /* skip 'nowrite' fields */
837 ret
= count_one_type(&lf
->type
);
839 return ret
; /* error */
846 int serialize_string_encoding(int32_t *ue
,
847 enum lttng_string_encodings le
)
850 case lttng_encode_none
:
851 *ue
= ustctl_encode_none
;
853 case lttng_encode_UTF8
:
854 *ue
= ustctl_encode_UTF8
;
856 case lttng_encode_ASCII
:
857 *ue
= ustctl_encode_ASCII
;
866 int serialize_integer_type(struct ustctl_integer_type
*uit
,
867 const struct lttng_integer_type
*lit
)
871 uit
->size
= lit
->size
;
872 uit
->signedness
= lit
->signedness
;
873 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
874 uit
->base
= lit
->base
;
875 if (serialize_string_encoding(&encoding
, lit
->encoding
))
877 uit
->encoding
= encoding
;
878 uit
->alignment
= lit
->alignment
;
883 int serialize_basic_type(struct lttng_session
*session
,
884 enum ustctl_abstract_types
*uatype
,
885 enum lttng_abstract_types atype
,
886 union _ustctl_basic_type
*ubt
,
887 const union _lttng_basic_type
*lbt
)
892 if (serialize_integer_type(&ubt
->integer
, &lbt
->integer
))
894 *uatype
= ustctl_atype_integer
;
901 if (serialize_string_encoding(&encoding
, lbt
->string
.encoding
))
903 ubt
->string
.encoding
= encoding
;
904 *uatype
= ustctl_atype_string
;
909 struct ustctl_float_type
*uft
;
910 const struct lttng_float_type
*lft
;
914 uft
->exp_dig
= lft
->exp_dig
;
915 uft
->mant_dig
= lft
->mant_dig
;
916 uft
->alignment
= lft
->alignment
;
917 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
918 *uatype
= ustctl_atype_float
;
923 strncpy(ubt
->enumeration
.name
, lbt
->enumeration
.desc
->name
,
924 LTTNG_UST_SYM_NAME_LEN
);
925 ubt
->enumeration
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
926 if (serialize_integer_type(&ubt
->enumeration
.container_type
,
927 &lbt
->enumeration
.container_type
))
930 const struct lttng_enum
*_enum
;
932 _enum
= lttng_ust_enum_get_from_desc(session
, lbt
->enumeration
.desc
);
935 ubt
->enumeration
.id
= _enum
->id
;
937 ubt
->enumeration
.id
= -1ULL;
939 *uatype
= ustctl_atype_enum
;
951 int serialize_dynamic_type(struct lttng_session
*session
,
952 struct ustctl_field
*fields
, size_t *iter_output
,
953 const struct lttng_event_field
*lf
)
955 const struct lttng_event_field
*choices
;
956 char tag_field_name
[LTTNG_UST_SYM_NAME_LEN
];
957 const struct lttng_type
*tag_type
;
958 const struct lttng_event_field
*tag_field_generic
;
959 struct lttng_event_field tag_field
= {
960 .name
= tag_field_name
,
963 struct ustctl_field
*uf
;
964 size_t nr_choices
, i
;
967 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
968 tag_type
= &tag_field_generic
->type
;
970 /* Serialize enum field. */
971 strncpy(tag_field_name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
972 tag_field_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
973 strncat(tag_field_name
,
975 LTTNG_UST_SYM_NAME_LEN
- strlen(tag_field_name
) - 1);
976 tag_field
.type
= *tag_type
;
977 ret
= serialize_one_field(session
, fields
, iter_output
,
982 /* Serialize variant field. */
983 uf
= &fields
[*iter_output
];
984 ret
= lttng_ust_dynamic_type_choices(&nr_choices
, &choices
);
988 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
989 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
990 uf
->type
.atype
= ustctl_atype_variant
;
991 uf
->type
.u
.variant
.nr_choices
= nr_choices
;
992 strncpy(uf
->type
.u
.variant
.tag_name
,
994 LTTNG_UST_SYM_NAME_LEN
);
995 uf
->type
.u
.variant
.tag_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
998 /* Serialize choice fields after variant. */
999 for (i
= 0; i
< nr_choices
; i
++) {
1000 ret
= serialize_one_field(session
, fields
,
1001 iter_output
, &choices
[i
]);
1009 int serialize_one_field(struct lttng_session
*session
,
1010 struct ustctl_field
*fields
, size_t *iter_output
,
1011 const struct lttng_event_field
*lf
)
1013 const struct lttng_type
*lt
= &lf
->type
;
1016 /* skip 'nowrite' fields */
1020 switch (lt
->atype
) {
1026 struct ustctl_field
*uf
= &fields
[*iter_output
];
1027 struct ustctl_type
*ut
= &uf
->type
;
1028 enum ustctl_abstract_types atype
;
1030 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1031 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1032 ret
= serialize_basic_type(session
, &atype
, lt
->atype
,
1033 &ut
->u
.basic
, <
->u
.basic
);
1042 struct ustctl_field
*uf
= &fields
[*iter_output
];
1043 struct ustctl_type
*ut
= &uf
->type
;
1044 struct ustctl_basic_type
*ubt
;
1045 const struct lttng_basic_type
*lbt
;
1046 enum ustctl_abstract_types atype
;
1048 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1049 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1050 uf
->type
.atype
= ustctl_atype_array
;
1051 ubt
= &ut
->u
.array
.elem_type
;
1052 lbt
= <
->u
.array
.elem_type
;
1053 ut
->u
.array
.length
= lt
->u
.array
.length
;
1054 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1055 &ubt
->u
.basic
, &lbt
->u
.basic
);
1059 ut
->atype
= ustctl_atype_array
;
1063 case atype_sequence
:
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
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1073 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1074 uf
->type
.atype
= ustctl_atype_sequence
;
1075 ubt
= &ut
->u
.sequence
.length_type
;
1076 lbt
= <
->u
.sequence
.length_type
;
1077 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1078 &ubt
->u
.basic
, &lbt
->u
.basic
);
1082 ubt
= &ut
->u
.sequence
.elem_type
;
1083 lbt
= <
->u
.sequence
.elem_type
;
1084 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1085 &ubt
->u
.basic
, &lbt
->u
.basic
);
1089 ut
->atype
= ustctl_atype_sequence
;
1095 ret
= serialize_dynamic_type(session
, fields
, iter_output
, lf
);
1102 struct ustctl_field
*uf
= &fields
[*iter_output
];
1105 * TODO: add support for non-empty struct.
1107 if (lf
->type
.u
._struct
.nr_fields
!= 0) {
1110 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1111 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1112 uf
->type
.atype
= ustctl_atype_struct
;
1113 uf
->type
.u
._struct
.nr_fields
= 0;
1124 int serialize_fields(struct lttng_session
*session
,
1125 size_t *_nr_write_fields
,
1126 struct ustctl_field
**ustctl_fields
,
1128 const struct lttng_event_field
*lttng_fields
)
1130 struct ustctl_field
*fields
;
1132 size_t i
, iter_output
= 0;
1133 ssize_t nr_write_fields
;
1135 nr_write_fields
= count_fields_recursive(nr_fields
, lttng_fields
);
1136 if (nr_write_fields
< 0) {
1137 return (int) nr_write_fields
;
1140 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1144 for (i
= 0; i
< nr_fields
; i
++) {
1145 ret
= serialize_one_field(session
, fields
, &iter_output
,
1151 *_nr_write_fields
= nr_write_fields
;
1152 *ustctl_fields
= fields
;
1161 int serialize_entries(struct ustctl_enum_entry
**_entries
,
1163 const struct lttng_enum_entry
*lttng_entries
)
1165 struct ustctl_enum_entry
*entries
;
1168 /* Serialize the entries */
1169 entries
= zmalloc(nr_entries
* sizeof(*entries
));
1172 for (i
= 0; i
< nr_entries
; i
++) {
1173 struct ustctl_enum_entry
*uentry
;
1174 const struct lttng_enum_entry
*lentry
;
1176 uentry
= &entries
[i
];
1177 lentry
= <tng_entries
[i
];
1179 uentry
->start
.value
= lentry
->start
.value
;
1180 uentry
->start
.signedness
= lentry
->start
.signedness
;
1181 uentry
->end
.value
= lentry
->end
.value
;
1182 uentry
->end
.signedness
= lentry
->end
.signedness
;
1183 strncpy(uentry
->string
, lentry
->string
, LTTNG_UST_SYM_NAME_LEN
);
1184 uentry
->string
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1186 if (lentry
->u
.extra
.options
& LTTNG_ENUM_ENTRY_OPTION_IS_AUTO
) {
1187 uentry
->u
.extra
.options
|=
1188 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO
;
1191 *_entries
= entries
;
1196 int serialize_ctx_fields(struct lttng_session
*session
,
1197 size_t *_nr_write_fields
,
1198 struct ustctl_field
**ustctl_fields
,
1200 const struct lttng_ctx_field
*lttng_fields
)
1202 struct ustctl_field
*fields
;
1204 size_t i
, iter_output
= 0;
1205 ssize_t nr_write_fields
;
1207 nr_write_fields
= count_ctx_fields_recursive(nr_fields
,
1209 if (nr_write_fields
< 0) {
1210 return (int) nr_write_fields
;
1213 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1217 for (i
= 0; i
< nr_fields
; i
++) {
1218 ret
= serialize_one_field(session
, fields
, &iter_output
,
1219 <tng_fields
[i
].event_field
);
1224 *_nr_write_fields
= nr_write_fields
;
1225 *ustctl_fields
= fields
;
1234 * Returns 0 on success, negative error value on error.
1236 int ustcomm_register_event(int sock
,
1237 struct lttng_session
*session
,
1238 int session_objd
, /* session descriptor */
1239 int channel_objd
, /* channel descriptor */
1240 const char *event_name
, /* event name (input) */
1242 const char *signature
, /* event signature (input) */
1243 size_t nr_fields
, /* fields */
1244 const struct lttng_event_field
*lttng_fields
,
1245 const char *model_emf_uri
,
1246 uint32_t *id
) /* event id (output) */
1250 struct ustcomm_notify_hdr header
;
1251 struct ustcomm_notify_event_msg m
;
1254 struct ustcomm_notify_hdr header
;
1255 struct ustcomm_notify_event_reply r
;
1257 size_t signature_len
, fields_len
, model_emf_uri_len
;
1258 struct ustctl_field
*fields
= NULL
;
1259 size_t nr_write_fields
= 0;
1262 memset(&msg
, 0, sizeof(msg
));
1263 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1264 msg
.m
.session_objd
= session_objd
;
1265 msg
.m
.channel_objd
= channel_objd
;
1266 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_SYM_NAME_LEN
);
1267 msg
.m
.event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1268 msg
.m
.loglevel
= loglevel
;
1269 signature_len
= strlen(signature
) + 1;
1270 msg
.m
.signature_len
= signature_len
;
1272 /* Calculate fields len, serialize fields. */
1273 if (nr_fields
> 0) {
1274 ret
= serialize_fields(session
, &nr_write_fields
, &fields
,
1275 nr_fields
, lttng_fields
);
1280 fields_len
= sizeof(*fields
) * nr_write_fields
;
1281 msg
.m
.fields_len
= fields_len
;
1282 if (model_emf_uri
) {
1283 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
1285 model_emf_uri_len
= 0;
1287 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
1289 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1290 if (len
> 0 && len
!= sizeof(msg
)) {
1299 /* send signature */
1300 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
1301 if (len
> 0 && len
!= signature_len
) {
1311 if (fields_len
> 0) {
1312 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1313 if (len
> 0 && len
!= fields_len
) {
1324 if (model_emf_uri_len
) {
1325 /* send model_emf_uri */
1326 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
1328 if (len
> 0 && len
!= model_emf_uri_len
) {
1337 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1339 case 0: /* orderly shutdown */
1342 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1343 ERR("Unexpected result message command "
1344 "expected: %u vs received: %u\n",
1345 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1348 if (reply
.r
.ret_code
> 0)
1350 if (reply
.r
.ret_code
< 0)
1351 return reply
.r
.ret_code
;
1352 *id
= reply
.r
.event_id
;
1353 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1354 event_name
, reply
.r
.ret_code
, reply
.r
.event_id
);
1358 /* Transport level error */
1359 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1363 ERR("incorrect message size: %zd\n", len
);
1369 /* Error path only. */
1376 * Returns 0 on success, negative error value on error.
1377 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1379 int ustcomm_register_enum(int sock
,
1380 int session_objd
, /* session descriptor */
1381 const char *enum_name
, /* enum name (input) */
1382 size_t nr_entries
, /* entries */
1383 const struct lttng_enum_entry
*lttng_entries
,
1388 struct ustcomm_notify_hdr header
;
1389 struct ustcomm_notify_enum_msg m
;
1392 struct ustcomm_notify_hdr header
;
1393 struct ustcomm_notify_enum_reply r
;
1396 struct ustctl_enum_entry
*entries
= NULL
;
1399 memset(&msg
, 0, sizeof(msg
));
1400 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1401 msg
.m
.session_objd
= session_objd
;
1402 strncpy(msg
.m
.enum_name
, enum_name
, LTTNG_UST_SYM_NAME_LEN
);
1403 msg
.m
.enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1405 /* Calculate entries len, serialize entries. */
1406 if (nr_entries
> 0) {
1407 ret
= serialize_entries(&entries
,
1408 nr_entries
, lttng_entries
);
1413 entries_len
= sizeof(*entries
) * nr_entries
;
1414 msg
.m
.entries_len
= entries_len
;
1416 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1417 if (len
> 0 && len
!= sizeof(msg
)) {
1427 if (entries_len
> 0) {
1428 len
= ustcomm_send_unix_sock(sock
, entries
, entries_len
);
1429 if (len
> 0 && len
!= entries_len
) {
1442 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1444 case 0: /* orderly shutdown */
1447 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1448 ERR("Unexpected result message command "
1449 "expected: %u vs received: %u\n",
1450 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1453 if (reply
.r
.ret_code
> 0)
1455 if (reply
.r
.ret_code
< 0)
1456 return reply
.r
.ret_code
;
1457 *id
= reply
.r
.enum_id
;
1458 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1459 enum_name
, reply
.r
.ret_code
);
1463 /* Transport level error */
1464 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1468 ERR("incorrect message size: %zd\n", len
);
1480 * Returns 0 on success, negative error value on error.
1481 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1483 int ustcomm_register_channel(int sock
,
1484 struct lttng_session
*session
,
1485 int session_objd
, /* session descriptor */
1486 int channel_objd
, /* channel descriptor */
1487 size_t nr_ctx_fields
,
1488 const struct lttng_ctx_field
*ctx_fields
,
1489 uint32_t *chan_id
, /* channel id (output) */
1490 int *header_type
) /* header type (output) */
1494 struct ustcomm_notify_hdr header
;
1495 struct ustcomm_notify_channel_msg m
;
1498 struct ustcomm_notify_hdr header
;
1499 struct ustcomm_notify_channel_reply r
;
1502 struct ustctl_field
*fields
= NULL
;
1504 size_t nr_write_fields
= 0;
1506 memset(&msg
, 0, sizeof(msg
));
1507 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1508 msg
.m
.session_objd
= session_objd
;
1509 msg
.m
.channel_objd
= channel_objd
;
1511 /* Calculate fields len, serialize fields. */
1512 if (nr_ctx_fields
> 0) {
1513 ret
= serialize_ctx_fields(session
, &nr_write_fields
, &fields
,
1514 nr_ctx_fields
, ctx_fields
);
1519 fields_len
= sizeof(*fields
) * nr_write_fields
;
1520 msg
.m
.ctx_fields_len
= fields_len
;
1521 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1522 if (len
> 0 && len
!= sizeof(msg
)) {
1532 if (fields_len
> 0) {
1533 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1535 if (len
> 0 && len
!= fields_len
) {
1545 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1547 case 0: /* orderly shutdown */
1550 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1551 ERR("Unexpected result message command "
1552 "expected: %u vs received: %u\n",
1553 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1556 if (reply
.r
.ret_code
> 0)
1558 if (reply
.r
.ret_code
< 0)
1559 return reply
.r
.ret_code
;
1560 *chan_id
= reply
.r
.chan_id
;
1561 switch (reply
.r
.header_type
) {
1564 *header_type
= reply
.r
.header_type
;
1567 ERR("Unexpected channel header type %u\n",
1568 reply
.r
.header_type
);
1571 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1572 reply
.r
.chan_id
, reply
.r
.header_type
);
1576 /* Transport level error */
1577 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1581 ERR("incorrect message size: %zd\n", len
);
1588 * Set socket reciving timeout.
1590 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1595 tv
.tv_sec
= msec
/ 1000;
1596 tv
.tv_usec
= (msec
* 1000 % 1000000);
1598 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1600 PERROR("setsockopt SO_RCVTIMEO");
1608 * Set socket sending timeout.
1610 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1615 tv
.tv_sec
= msec
/ 1000;
1616 tv
.tv_usec
= (msec
* 1000) % 1000000;
1618 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1620 PERROR("setsockopt SO_SNDTIMEO");