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.
112 fd
= socket(PF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0);
119 /* Give at least 10ms. */
122 ret
= ustcomm_setsockopt_snd_timeout(fd
, timeout
);
124 WARN("Error setting connect socket send timeout");
128 memset(&sun
, 0, sizeof(sun
));
129 sun
.sun_family
= AF_UNIX
;
130 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
131 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
133 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
136 * Don't print message on connect ENOENT error, because
137 * connect is used in normal execution to detect if
138 * sessiond is alive. ENOENT is when the unix socket
139 * file does not exist, and ECONNREFUSED is when the
140 * file exists but no sessiond is listening.
142 if (errno
!= ECONNREFUSED
&& errno
!= ECONNRESET
143 && errno
!= ENOENT
&& errno
!= EACCES
)
146 if (ret
== -ECONNREFUSED
|| ret
== -ECONNRESET
)
157 closeret
= close(fd
);
166 * ustcomm_accept_unix_sock
168 * Do an accept(2) on the sock and return the
169 * new file descriptor. The socket MUST be bind(2) before.
171 int ustcomm_accept_unix_sock(int sock
)
174 struct sockaddr_un sun
;
178 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
180 if (errno
!= ECONNABORTED
)
183 if (new_fd
== -ECONNABORTED
)
190 * ustcomm_create_unix_sock
192 * Creates a AF_UNIX local socket using pathname
193 * bind the socket upon creation and return the fd.
195 int ustcomm_create_unix_sock(const char *pathname
)
197 struct sockaddr_un sun
;
200 /* Create server socket */
201 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
207 memset(&sun
, 0, sizeof(sun
));
208 sun
.sun_family
= AF_UNIX
;
209 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
210 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
212 /* Unlink the old file if present */
213 (void) unlink(pathname
);
214 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
227 closeret
= close(fd
);
237 * ustcomm_listen_unix_sock
239 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
241 int ustcomm_listen_unix_sock(int sock
)
245 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
255 * ustcomm_close_unix_sock
257 * Shutdown cleanly a unix socket.
259 * Handles fd tracker internally.
261 int ustcomm_close_unix_sock(int sock
)
265 lttng_ust_lock_fd_tracker();
268 lttng_ust_delete_fd_from_tracker(sock
);
273 lttng_ust_unlock_fd_tracker();
279 * ustcomm_recv_unix_sock
281 * Receive data of size len in put that data into
282 * the buf param. Using recvmsg API.
283 * Return the size of received data.
284 * Return 0 on orderly shutdown.
286 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
293 memset(&msg
, 0, sizeof(msg
));
295 iov
[0].iov_base
= buf
;
296 iov
[0].iov_len
= len
;
301 len_last
= iov
[0].iov_len
;
302 ret
= recvmsg(sock
, &msg
, 0);
304 iov
[0].iov_base
+= ret
;
305 iov
[0].iov_len
-= ret
;
306 assert(ret
<= len_last
);
308 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
313 if (errno
!= EPIPE
&& errno
!= ECONNRESET
&& errno
!= ECONNREFUSED
)
316 if (ret
== -ECONNRESET
|| ret
== -ECONNREFUSED
)
319 shutret
= shutdown(sock
, SHUT_RDWR
);
321 ERR("Socket shutdown error");
322 } else if (ret
> 0) {
325 /* ret = 0 means an orderly shutdown. */
331 * ustcomm_send_unix_sock
333 * Send buf data of size len. Using sendmsg API.
334 * Return the size of sent data.
336 ssize_t
ustcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
342 memset(&msg
, 0, sizeof(msg
));
344 iov
[0].iov_base
= (void *) buf
;
345 iov
[0].iov_len
= len
;
350 * Using the MSG_NOSIGNAL when sending data from sessiond to
351 * libust, so libust does not receive an unhandled SIGPIPE or
352 * SIGURG. The sessiond receiver side can be made more resilient
353 * by ignoring SIGPIPE, but we don't have this luxury on the
357 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
358 } while (ret
< 0 && errno
== EINTR
);
363 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
366 if (ret
== -ECONNRESET
)
369 shutret
= shutdown(sock
, SHUT_RDWR
);
371 ERR("Socket shutdown error");
378 * Send a message accompanied by fd(s) over a unix socket.
380 * Returns the size of data sent, or negative error value.
382 ssize_t
ustcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
385 struct cmsghdr
*cmptr
;
388 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
389 char tmp
[CMSG_SPACE(sizeof_fds
)];
392 memset(&msg
, 0, sizeof(msg
));
393 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
395 if (nb_fd
> USTCOMM_MAX_SEND_FDS
)
398 msg
.msg_control
= (caddr_t
)tmp
;
399 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
401 cmptr
= CMSG_FIRSTHDR(&msg
);
404 cmptr
->cmsg_level
= SOL_SOCKET
;
405 cmptr
->cmsg_type
= SCM_RIGHTS
;
406 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
407 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
408 /* Sum of the length of all control messages in the buffer: */
409 msg
.msg_controllen
= cmptr
->cmsg_len
;
411 iov
[0].iov_base
= &dummy
;
417 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
418 } while (ret
< 0 && errno
== EINTR
);
421 * We consider EPIPE and ECONNRESET as expected.
423 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
427 if (ret
== -ECONNRESET
)
434 * Recv a message accompanied by fd(s) from a unix socket.
436 * Expect at most "nb_fd" file descriptors. Returns the number of fd
437 * actually received in nb_fd.
438 * Returns -EPIPE on orderly shutdown.
440 ssize_t
ustcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
444 struct cmsghdr
*cmsg
;
445 size_t sizeof_fds
= nb_fd
* sizeof(int);
446 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
450 memset(&msg
, 0, sizeof(msg
));
452 /* Prepare to receive the structures */
453 iov
[0].iov_base
= &dummy
;
457 msg
.msg_control
= recv_fd
;
458 msg
.msg_controllen
= sizeof(recv_fd
);
461 ret
= recvmsg(sock
, &msg
, MSG_CMSG_CLOEXEC
);
462 } while (ret
< 0 && errno
== EINTR
);
464 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
465 PERROR("recvmsg fds");
468 if (ret
== -ECONNRESET
)
473 /* orderly shutdown */
478 ERR("Error: Received %zd bytes, expected %d\n",
482 if (msg
.msg_flags
& MSG_CTRUNC
) {
483 ERR("Error: Control message truncated.\n");
487 cmsg
= CMSG_FIRSTHDR(&msg
);
489 ERR("Error: Invalid control message header\n");
493 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
494 ERR("Didn't received any fd\n");
498 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
499 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
500 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
505 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
512 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
516 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
524 ERR("incorrect message size: %zd\n", len
);
531 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
532 uint32_t expected_handle
, uint32_t expected_cmd
)
536 memset(lur
, 0, sizeof(*lur
));
537 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
539 case 0: /* orderly shutdown */
545 if (lur
->handle
!= expected_handle
) {
546 ERR("Unexpected result message handle: "
547 "expected: %u vs received: %u\n",
548 expected_handle
, lur
->handle
);
551 if (lur
->cmd
!= expected_cmd
) {
552 ERR("Unexpected result message command "
553 "expected: %u vs received: %u\n",
554 expected_cmd
, lur
->cmd
);
560 return lur
->ret_code
;
565 ERR("incorrect message size: %zd\n", len
);
571 int ustcomm_send_app_cmd(int sock
,
572 struct ustcomm_ust_msg
*lum
,
573 struct ustcomm_ust_reply
*lur
)
577 ret
= ustcomm_send_app_msg(sock
, lum
);
580 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
587 * chan_data is allocated internally if this function returns the
590 ssize_t
ustcomm_recv_channel_from_sessiond(int sock
,
591 void **_chan_data
, uint64_t var_len
,
598 if (var_len
> LTTNG_UST_CHANNEL_DATA_MAX_LEN
) {
602 /* Receive variable length data */
603 chan_data
= zmalloc(var_len
);
608 len
= ustcomm_recv_unix_sock(sock
, chan_data
, var_len
);
609 if (len
!= var_len
) {
613 lttng_ust_lock_fd_tracker();
614 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
616 lttng_ust_unlock_fd_tracker();
626 ret
= lttng_ust_add_fd_to_tracker(wakeup_fd
);
628 lttng_ust_unlock_fd_tracker();
629 ret
= close(wakeup_fd
);
631 PERROR("close on wakeup_fd");
638 lttng_ust_unlock_fd_tracker();
640 *_chan_data
= chan_data
;
650 int ustcomm_recv_stream_from_sessiond(int sock
,
651 uint64_t *memory_map_size
,
652 int *shm_fd
, int *wakeup_fd
)
658 /* recv shm fd and wakeup fd */
659 lttng_ust_lock_fd_tracker();
660 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
662 lttng_ust_unlock_fd_tracker();
672 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
674 lttng_ust_unlock_fd_tracker();
677 PERROR("close on received shm_fd");
684 ret
= lttng_ust_add_fd_to_tracker(fds
[1]);
686 lttng_ust_unlock_fd_tracker();
687 ret
= close(*shm_fd
);
689 PERROR("close on shm_fd");
694 PERROR("close on received wakeup_fd");
700 lttng_ust_unlock_fd_tracker();
708 * Returns 0 on success, negative error value on error.
710 int ustcomm_send_reg_msg(int sock
,
711 enum ustctl_socket_type type
,
712 uint32_t bits_per_long
,
713 uint32_t uint8_t_alignment
,
714 uint32_t uint16_t_alignment
,
715 uint32_t uint32_t_alignment
,
716 uint32_t uint64_t_alignment
,
717 uint32_t long_alignment
)
720 struct ustctl_reg_msg reg_msg
;
722 reg_msg
.magic
= LTTNG_UST_COMM_MAGIC
;
723 reg_msg
.major
= LTTNG_UST_ABI_MAJOR_VERSION
;
724 reg_msg
.minor
= LTTNG_UST_ABI_MINOR_VERSION
;
725 reg_msg
.pid
= getpid();
726 reg_msg
.ppid
= getppid();
727 reg_msg
.uid
= getuid();
728 reg_msg
.gid
= getgid();
729 reg_msg
.bits_per_long
= bits_per_long
;
730 reg_msg
.uint8_t_alignment
= uint8_t_alignment
;
731 reg_msg
.uint16_t_alignment
= uint16_t_alignment
;
732 reg_msg
.uint32_t_alignment
= uint32_t_alignment
;
733 reg_msg
.uint64_t_alignment
= uint64_t_alignment
;
734 reg_msg
.long_alignment
= long_alignment
;
735 reg_msg
.socket_type
= type
;
736 lttng_ust_getprocname(reg_msg
.name
);
737 memset(reg_msg
.padding
, 0, sizeof(reg_msg
.padding
));
739 len
= ustcomm_send_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
740 if (len
> 0 && len
!= sizeof(reg_msg
))
748 ssize_t
count_one_type(const struct lttng_type
*lt
)
759 //TODO: implement non-empty struct.
763 const struct lttng_event_field
*choices
;
767 ret
= lttng_ust_dynamic_type_choices(&nr_choices
,
772 * One field for enum, one field for variant, and
773 * one field per choice.
775 return count_fields_recursive(nr_choices
, choices
) + 2;
784 ssize_t
count_fields_recursive(size_t nr_fields
,
785 const struct lttng_event_field
*lttng_fields
)
788 ssize_t ret
, count
= 0;
790 for (i
= 0; i
< nr_fields
; i
++) {
791 const struct lttng_event_field
*lf
;
793 lf
= <tng_fields
[i
];
794 /* skip 'nowrite' fields */
797 ret
= count_one_type(&lf
->type
);
799 return ret
; /* error */
806 ssize_t
count_ctx_fields_recursive(size_t nr_fields
,
807 const struct lttng_ctx_field
*lttng_fields
)
810 ssize_t ret
, count
= 0;
812 for (i
= 0; i
< nr_fields
; i
++) {
813 const struct lttng_event_field
*lf
;
815 lf
= <tng_fields
[i
].event_field
;
816 /* skip 'nowrite' fields */
819 ret
= count_one_type(&lf
->type
);
821 return ret
; /* error */
828 int serialize_string_encoding(int32_t *ue
,
829 enum lttng_string_encodings le
)
832 case lttng_encode_none
:
833 *ue
= ustctl_encode_none
;
835 case lttng_encode_UTF8
:
836 *ue
= ustctl_encode_UTF8
;
838 case lttng_encode_ASCII
:
839 *ue
= ustctl_encode_ASCII
;
848 int serialize_integer_type(struct ustctl_integer_type
*uit
,
849 const struct lttng_integer_type
*lit
)
853 uit
->size
= lit
->size
;
854 uit
->signedness
= lit
->signedness
;
855 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
856 uit
->base
= lit
->base
;
857 if (serialize_string_encoding(&encoding
, lit
->encoding
))
859 uit
->encoding
= encoding
;
860 uit
->alignment
= lit
->alignment
;
865 int serialize_basic_type(struct lttng_session
*session
,
866 enum ustctl_abstract_types
*uatype
,
867 enum lttng_abstract_types atype
,
868 union _ustctl_basic_type
*ubt
,
869 const union _lttng_basic_type
*lbt
)
874 if (serialize_integer_type(&ubt
->integer
, &lbt
->integer
))
876 *uatype
= ustctl_atype_integer
;
883 if (serialize_string_encoding(&encoding
, lbt
->string
.encoding
))
885 ubt
->string
.encoding
= encoding
;
886 *uatype
= ustctl_atype_string
;
891 struct ustctl_float_type
*uft
;
892 const struct lttng_float_type
*lft
;
896 uft
->exp_dig
= lft
->exp_dig
;
897 uft
->mant_dig
= lft
->mant_dig
;
898 uft
->alignment
= lft
->alignment
;
899 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
900 *uatype
= ustctl_atype_float
;
905 strncpy(ubt
->enumeration
.name
, lbt
->enumeration
.desc
->name
,
906 LTTNG_UST_SYM_NAME_LEN
);
907 ubt
->enumeration
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
908 if (serialize_integer_type(&ubt
->enumeration
.container_type
,
909 &lbt
->enumeration
.container_type
))
912 const struct lttng_enum
*_enum
;
914 _enum
= lttng_ust_enum_get_from_desc(session
, lbt
->enumeration
.desc
);
917 ubt
->enumeration
.id
= _enum
->id
;
919 ubt
->enumeration
.id
= -1ULL;
921 *uatype
= ustctl_atype_enum
;
933 int serialize_dynamic_type(struct lttng_session
*session
,
934 struct ustctl_field
*fields
, size_t *iter_output
,
935 const struct lttng_event_field
*lf
)
937 const struct lttng_event_field
*choices
;
938 char tag_field_name
[LTTNG_UST_SYM_NAME_LEN
];
939 const struct lttng_type
*tag_type
;
940 const struct lttng_event_field
*tag_field_generic
;
941 struct lttng_event_field tag_field
= {
942 .name
= tag_field_name
,
945 struct ustctl_field
*uf
;
946 size_t nr_choices
, i
;
949 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
950 tag_type
= &tag_field_generic
->type
;
952 /* Serialize enum field. */
953 strncpy(tag_field_name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
954 tag_field_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
955 strncat(tag_field_name
,
957 LTTNG_UST_SYM_NAME_LEN
- strlen(tag_field_name
) - 1);
958 tag_field
.type
= *tag_type
;
959 ret
= serialize_one_field(session
, fields
, iter_output
,
964 /* Serialize variant field. */
965 uf
= &fields
[*iter_output
];
966 ret
= lttng_ust_dynamic_type_choices(&nr_choices
, &choices
);
970 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
971 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
972 uf
->type
.atype
= ustctl_atype_variant
;
973 uf
->type
.u
.variant
.nr_choices
= nr_choices
;
974 strncpy(uf
->type
.u
.variant
.tag_name
,
976 LTTNG_UST_SYM_NAME_LEN
);
977 uf
->type
.u
.variant
.tag_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
980 /* Serialize choice fields after variant. */
981 for (i
= 0; i
< nr_choices
; i
++) {
982 ret
= serialize_one_field(session
, fields
,
983 iter_output
, &choices
[i
]);
991 int serialize_one_field(struct lttng_session
*session
,
992 struct ustctl_field
*fields
, size_t *iter_output
,
993 const struct lttng_event_field
*lf
)
995 const struct lttng_type
*lt
= &lf
->type
;
998 /* skip 'nowrite' fields */
1002 switch (lt
->atype
) {
1008 struct ustctl_field
*uf
= &fields
[*iter_output
];
1009 struct ustctl_type
*ut
= &uf
->type
;
1010 enum ustctl_abstract_types atype
;
1012 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1013 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1014 ret
= serialize_basic_type(session
, &atype
, lt
->atype
,
1015 &ut
->u
.basic
, <
->u
.basic
);
1024 struct ustctl_field
*uf
= &fields
[*iter_output
];
1025 struct ustctl_type
*ut
= &uf
->type
;
1026 struct ustctl_basic_type
*ubt
;
1027 const struct lttng_basic_type
*lbt
;
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 uf
->type
.atype
= ustctl_atype_array
;
1033 ubt
= &ut
->u
.array
.elem_type
;
1034 lbt
= <
->u
.array
.elem_type
;
1035 ut
->u
.array
.length
= lt
->u
.array
.length
;
1036 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1037 &ubt
->u
.basic
, &lbt
->u
.basic
);
1041 ut
->atype
= ustctl_atype_array
;
1045 case atype_sequence
:
1047 struct ustctl_field
*uf
= &fields
[*iter_output
];
1048 struct ustctl_type
*ut
= &uf
->type
;
1049 struct ustctl_basic_type
*ubt
;
1050 const struct lttng_basic_type
*lbt
;
1051 enum ustctl_abstract_types atype
;
1054 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1055 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1056 uf
->type
.atype
= ustctl_atype_sequence
;
1057 ubt
= &ut
->u
.sequence
.length_type
;
1058 lbt
= <
->u
.sequence
.length_type
;
1059 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1060 &ubt
->u
.basic
, &lbt
->u
.basic
);
1064 ubt
= &ut
->u
.sequence
.elem_type
;
1065 lbt
= <
->u
.sequence
.elem_type
;
1066 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1067 &ubt
->u
.basic
, &lbt
->u
.basic
);
1071 ut
->atype
= ustctl_atype_sequence
;
1077 ret
= serialize_dynamic_type(session
, fields
, iter_output
, lf
);
1084 struct ustctl_field
*uf
= &fields
[*iter_output
];
1087 * TODO: add support for non-empty struct.
1089 if (lf
->type
.u
._struct
.nr_fields
!= 0) {
1092 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1093 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1094 uf
->type
.atype
= ustctl_atype_struct
;
1095 uf
->type
.u
._struct
.nr_fields
= 0;
1106 int serialize_fields(struct lttng_session
*session
,
1107 size_t *_nr_write_fields
,
1108 struct ustctl_field
**ustctl_fields
,
1110 const struct lttng_event_field
*lttng_fields
)
1112 struct ustctl_field
*fields
;
1114 size_t i
, iter_output
= 0;
1115 ssize_t nr_write_fields
;
1117 nr_write_fields
= count_fields_recursive(nr_fields
, lttng_fields
);
1118 if (nr_write_fields
< 0) {
1119 return (int) nr_write_fields
;
1122 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1126 for (i
= 0; i
< nr_fields
; i
++) {
1127 ret
= serialize_one_field(session
, fields
, &iter_output
,
1133 *_nr_write_fields
= nr_write_fields
;
1134 *ustctl_fields
= fields
;
1143 int serialize_entries(struct ustctl_enum_entry
**_entries
,
1145 const struct lttng_enum_entry
*lttng_entries
)
1147 struct ustctl_enum_entry
*entries
;
1150 /* Serialize the entries */
1151 entries
= zmalloc(nr_entries
* sizeof(*entries
));
1154 for (i
= 0; i
< nr_entries
; i
++) {
1155 struct ustctl_enum_entry
*uentry
;
1156 const struct lttng_enum_entry
*lentry
;
1158 uentry
= &entries
[i
];
1159 lentry
= <tng_entries
[i
];
1161 uentry
->start
.value
= lentry
->start
.value
;
1162 uentry
->start
.signedness
= lentry
->start
.signedness
;
1163 uentry
->end
.value
= lentry
->end
.value
;
1164 uentry
->end
.signedness
= lentry
->end
.signedness
;
1165 strncpy(uentry
->string
, lentry
->string
, LTTNG_UST_SYM_NAME_LEN
);
1166 uentry
->string
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1168 if (lentry
->u
.extra
.options
& LTTNG_ENUM_ENTRY_OPTION_IS_AUTO
) {
1169 uentry
->u
.extra
.options
|=
1170 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO
;
1173 *_entries
= entries
;
1178 int serialize_ctx_fields(struct lttng_session
*session
,
1179 size_t *_nr_write_fields
,
1180 struct ustctl_field
**ustctl_fields
,
1182 const struct lttng_ctx_field
*lttng_fields
)
1184 struct ustctl_field
*fields
;
1186 size_t i
, iter_output
= 0;
1187 ssize_t nr_write_fields
;
1189 nr_write_fields
= count_ctx_fields_recursive(nr_fields
,
1191 if (nr_write_fields
< 0) {
1192 return (int) nr_write_fields
;
1195 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1199 for (i
= 0; i
< nr_fields
; i
++) {
1200 ret
= serialize_one_field(session
, fields
, &iter_output
,
1201 <tng_fields
[i
].event_field
);
1206 *_nr_write_fields
= nr_write_fields
;
1207 *ustctl_fields
= fields
;
1216 * Returns 0 on success, negative error value on error.
1218 int ustcomm_register_event(int sock
,
1219 struct lttng_session
*session
,
1220 int session_objd
, /* session descriptor */
1221 int channel_objd
, /* channel descriptor */
1222 const char *event_name
, /* event name (input) */
1224 const char *signature
, /* event signature (input) */
1225 size_t nr_fields
, /* fields */
1226 const struct lttng_event_field
*lttng_fields
,
1227 const char *model_emf_uri
,
1228 uint32_t *id
) /* event id (output) */
1232 struct ustcomm_notify_hdr header
;
1233 struct ustcomm_notify_event_msg m
;
1236 struct ustcomm_notify_hdr header
;
1237 struct ustcomm_notify_event_reply r
;
1239 size_t signature_len
, fields_len
, model_emf_uri_len
;
1240 struct ustctl_field
*fields
= NULL
;
1241 size_t nr_write_fields
= 0;
1244 memset(&msg
, 0, sizeof(msg
));
1245 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1246 msg
.m
.session_objd
= session_objd
;
1247 msg
.m
.channel_objd
= channel_objd
;
1248 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_SYM_NAME_LEN
);
1249 msg
.m
.event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1250 msg
.m
.loglevel
= loglevel
;
1251 signature_len
= strlen(signature
) + 1;
1252 msg
.m
.signature_len
= signature_len
;
1254 /* Calculate fields len, serialize fields. */
1255 if (nr_fields
> 0) {
1256 ret
= serialize_fields(session
, &nr_write_fields
, &fields
,
1257 nr_fields
, lttng_fields
);
1262 fields_len
= sizeof(*fields
) * nr_write_fields
;
1263 msg
.m
.fields_len
= fields_len
;
1264 if (model_emf_uri
) {
1265 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
1267 model_emf_uri_len
= 0;
1269 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
1271 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1272 if (len
> 0 && len
!= sizeof(msg
)) {
1281 /* send signature */
1282 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
1283 if (len
> 0 && len
!= signature_len
) {
1293 if (fields_len
> 0) {
1294 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1295 if (len
> 0 && len
!= fields_len
) {
1306 if (model_emf_uri_len
) {
1307 /* send model_emf_uri */
1308 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
1310 if (len
> 0 && len
!= model_emf_uri_len
) {
1319 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1321 case 0: /* orderly shutdown */
1324 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1325 ERR("Unexpected result message command "
1326 "expected: %u vs received: %u\n",
1327 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1330 if (reply
.r
.ret_code
> 0)
1332 if (reply
.r
.ret_code
< 0)
1333 return reply
.r
.ret_code
;
1334 *id
= reply
.r
.event_id
;
1335 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1336 event_name
, reply
.r
.ret_code
, reply
.r
.event_id
);
1340 /* Transport level error */
1341 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1345 ERR("incorrect message size: %zd\n", len
);
1351 /* Error path only. */
1358 * Returns 0 on success, negative error value on error.
1359 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1361 int ustcomm_register_enum(int sock
,
1362 int session_objd
, /* session descriptor */
1363 const char *enum_name
, /* enum name (input) */
1364 size_t nr_entries
, /* entries */
1365 const struct lttng_enum_entry
*lttng_entries
,
1370 struct ustcomm_notify_hdr header
;
1371 struct ustcomm_notify_enum_msg m
;
1374 struct ustcomm_notify_hdr header
;
1375 struct ustcomm_notify_enum_reply r
;
1378 struct ustctl_enum_entry
*entries
= NULL
;
1381 memset(&msg
, 0, sizeof(msg
));
1382 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1383 msg
.m
.session_objd
= session_objd
;
1384 strncpy(msg
.m
.enum_name
, enum_name
, LTTNG_UST_SYM_NAME_LEN
);
1385 msg
.m
.enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1387 /* Calculate entries len, serialize entries. */
1388 if (nr_entries
> 0) {
1389 ret
= serialize_entries(&entries
,
1390 nr_entries
, lttng_entries
);
1395 entries_len
= sizeof(*entries
) * nr_entries
;
1396 msg
.m
.entries_len
= entries_len
;
1398 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1399 if (len
> 0 && len
!= sizeof(msg
)) {
1409 if (entries_len
> 0) {
1410 len
= ustcomm_send_unix_sock(sock
, entries
, entries_len
);
1411 if (len
> 0 && len
!= entries_len
) {
1424 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1426 case 0: /* orderly shutdown */
1429 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1430 ERR("Unexpected result message command "
1431 "expected: %u vs received: %u\n",
1432 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1435 if (reply
.r
.ret_code
> 0)
1437 if (reply
.r
.ret_code
< 0)
1438 return reply
.r
.ret_code
;
1439 *id
= reply
.r
.enum_id
;
1440 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1441 enum_name
, reply
.r
.ret_code
);
1445 /* Transport level error */
1446 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1450 ERR("incorrect message size: %zd\n", len
);
1462 * Returns 0 on success, negative error value on error.
1463 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1465 int ustcomm_register_channel(int sock
,
1466 struct lttng_session
*session
,
1467 int session_objd
, /* session descriptor */
1468 int channel_objd
, /* channel descriptor */
1469 size_t nr_ctx_fields
,
1470 const struct lttng_ctx_field
*ctx_fields
,
1471 uint32_t *chan_id
, /* channel id (output) */
1472 int *header_type
) /* header type (output) */
1476 struct ustcomm_notify_hdr header
;
1477 struct ustcomm_notify_channel_msg m
;
1480 struct ustcomm_notify_hdr header
;
1481 struct ustcomm_notify_channel_reply r
;
1484 struct ustctl_field
*fields
= NULL
;
1486 size_t nr_write_fields
= 0;
1488 memset(&msg
, 0, sizeof(msg
));
1489 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1490 msg
.m
.session_objd
= session_objd
;
1491 msg
.m
.channel_objd
= channel_objd
;
1493 /* Calculate fields len, serialize fields. */
1494 if (nr_ctx_fields
> 0) {
1495 ret
= serialize_ctx_fields(session
, &nr_write_fields
, &fields
,
1496 nr_ctx_fields
, ctx_fields
);
1501 fields_len
= sizeof(*fields
) * nr_write_fields
;
1502 msg
.m
.ctx_fields_len
= fields_len
;
1503 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1504 if (len
> 0 && len
!= sizeof(msg
)) {
1514 if (fields_len
> 0) {
1515 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1517 if (len
> 0 && len
!= fields_len
) {
1527 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1529 case 0: /* orderly shutdown */
1532 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1533 ERR("Unexpected result message command "
1534 "expected: %u vs received: %u\n",
1535 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1538 if (reply
.r
.ret_code
> 0)
1540 if (reply
.r
.ret_code
< 0)
1541 return reply
.r
.ret_code
;
1542 *chan_id
= reply
.r
.chan_id
;
1543 switch (reply
.r
.header_type
) {
1546 *header_type
= reply
.r
.header_type
;
1549 ERR("Unexpected channel header type %u\n",
1550 reply
.r
.header_type
);
1553 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1554 reply
.r
.chan_id
, reply
.r
.header_type
);
1558 /* Transport level error */
1559 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1563 ERR("incorrect message size: %zd\n", len
);
1570 * Set socket reciving timeout.
1572 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1577 tv
.tv_sec
= msec
/ 1000;
1578 tv
.tv_usec
= (msec
* 1000 % 1000000);
1580 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1582 PERROR("setsockopt SO_RCVTIMEO");
1590 * Set socket sending timeout.
1592 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1597 tv
.tv_sec
= msec
/ 1000;
1598 tv
.tv_usec
= (msec
* 1000) % 1000000;
1600 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1602 PERROR("setsockopt SO_SNDTIMEO");