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
);
60 * Human readable error message.
62 static const char *ustcomm_readable_code
[] = {
63 [ USTCOMM_CODE_OFFSET(LTTNG_UST_OK
) ] = "Success",
64 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR
) ] = "Unknown error",
65 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOENT
) ] = "No entry",
66 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXIST
) ] = "Object already exists",
67 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL
) ] = "Invalid argument",
68 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PERM
) ] = "Permission denied",
69 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOSYS
) ] = "Not implemented",
70 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXITING
) ] = "Process is exiting",
72 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_MAGIC
) ] = "Invalid magic number",
73 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_SOCKET_TYPE
) ] = "Invalid socket type",
74 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_UNSUP_MAJOR
) ] = "Unsupported major version",
80 * Receives positive error value.
81 * Return ptr to string representing a human readable
82 * error code from the ustcomm_return_code enum.
84 const char *lttng_ust_strerror(int code
)
86 if (code
== LTTNG_UST_OK
)
87 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
88 if (code
< LTTNG_UST_ERR
)
89 return strerror(code
);
90 if (code
>= LTTNG_UST_ERR_NR
)
92 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
96 * ustcomm_connect_unix_sock
98 * Connect to unix socket using the path name.
100 * Caller handles FD tracker.
102 int ustcomm_connect_unix_sock(const char *pathname
, long timeout
)
104 struct sockaddr_un sun
;
108 * libust threads require the close-on-exec flag for all
109 * resources so it does not leak file descriptors upon exec.
110 * SOCK_CLOEXEC is not used since it is linux specific.
112 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
119 /* Give at least 10ms. */
122 ret
= ustcomm_setsockopt_snd_timeout(fd
, timeout
);
124 WARN("Error setting connect socket send timeout");
127 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
134 memset(&sun
, 0, sizeof(sun
));
135 sun
.sun_family
= AF_UNIX
;
136 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
137 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
139 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
142 * Don't print message on connect ENOENT error, because
143 * connect is used in normal execution to detect if
144 * sessiond is alive. ENOENT is when the unix socket
145 * file does not exist, and ECONNREFUSED is when the
146 * file exists but no sessiond is listening.
148 if (errno
!= ECONNREFUSED
&& errno
!= ECONNRESET
149 && errno
!= ENOENT
&& errno
!= EACCES
)
152 if (ret
== -ECONNREFUSED
|| ret
== -ECONNRESET
)
164 closeret
= close(fd
);
173 * ustcomm_accept_unix_sock
175 * Do an accept(2) on the sock and return the
176 * new file descriptor. The socket MUST be bind(2) before.
178 int ustcomm_accept_unix_sock(int sock
)
181 struct sockaddr_un sun
;
185 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
187 if (errno
!= ECONNABORTED
)
190 if (new_fd
== -ECONNABORTED
)
197 * ustcomm_create_unix_sock
199 * Creates a AF_UNIX local socket using pathname
200 * bind the socket upon creation and return the fd.
202 int ustcomm_create_unix_sock(const char *pathname
)
204 struct sockaddr_un sun
;
207 /* Create server socket */
208 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
214 memset(&sun
, 0, sizeof(sun
));
215 sun
.sun_family
= AF_UNIX
;
216 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
217 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
219 /* Unlink the old file if present */
220 (void) unlink(pathname
);
221 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
234 closeret
= close(fd
);
244 * ustcomm_listen_unix_sock
246 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
248 int ustcomm_listen_unix_sock(int sock
)
252 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
262 * ustcomm_close_unix_sock
264 * Shutdown cleanly a unix socket.
266 * Handles fd tracker internally.
268 int ustcomm_close_unix_sock(int sock
)
272 lttng_ust_lock_fd_tracker();
275 lttng_ust_delete_fd_from_tracker(sock
);
280 lttng_ust_unlock_fd_tracker();
286 * ustcomm_recv_unix_sock
288 * Receive data of size len in put that data into
289 * the buf param. Using recvmsg API.
290 * Return the size of received data.
291 * Return 0 on orderly shutdown.
293 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
300 memset(&msg
, 0, sizeof(msg
));
302 iov
[0].iov_base
= buf
;
303 iov
[0].iov_len
= len
;
308 len_last
= iov
[0].iov_len
;
309 ret
= recvmsg(sock
, &msg
, 0);
311 iov
[0].iov_base
+= ret
;
312 iov
[0].iov_len
-= ret
;
313 assert(ret
<= len_last
);
315 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
320 if (errno
!= EPIPE
&& errno
!= ECONNRESET
&& errno
!= ECONNREFUSED
)
323 if (ret
== -ECONNRESET
|| ret
== -ECONNREFUSED
)
326 shutret
= shutdown(sock
, SHUT_RDWR
);
328 ERR("Socket shutdown error");
329 } else if (ret
> 0) {
332 /* ret = 0 means an orderly shutdown. */
338 * ustcomm_send_unix_sock
340 * Send buf data of size len. Using sendmsg API.
341 * Return the size of sent data.
343 ssize_t
ustcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
349 memset(&msg
, 0, sizeof(msg
));
351 iov
[0].iov_base
= (void *) buf
;
352 iov
[0].iov_len
= len
;
357 * Using the MSG_NOSIGNAL when sending data from sessiond to
358 * libust, so libust does not receive an unhandled SIGPIPE or
359 * SIGURG. The sessiond receiver side can be made more resilient
360 * by ignoring SIGPIPE, but we don't have this luxury on the
364 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
365 } while (ret
< 0 && errno
== EINTR
);
370 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
373 if (ret
== -ECONNRESET
)
376 shutret
= shutdown(sock
, SHUT_RDWR
);
378 ERR("Socket shutdown error");
385 * Send a message accompanied by fd(s) over a unix socket.
387 * Returns the size of data sent, or negative error value.
389 ssize_t
ustcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
392 struct cmsghdr
*cmptr
;
395 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
396 char tmp
[CMSG_SPACE(sizeof_fds
)];
399 memset(&msg
, 0, sizeof(msg
));
400 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
402 if (nb_fd
> USTCOMM_MAX_SEND_FDS
)
405 msg
.msg_control
= (caddr_t
)tmp
;
406 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
408 cmptr
= CMSG_FIRSTHDR(&msg
);
411 cmptr
->cmsg_level
= SOL_SOCKET
;
412 cmptr
->cmsg_type
= SCM_RIGHTS
;
413 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
414 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
415 /* Sum of the length of all control messages in the buffer: */
416 msg
.msg_controllen
= cmptr
->cmsg_len
;
418 iov
[0].iov_base
= &dummy
;
424 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
425 } while (ret
< 0 && errno
== EINTR
);
428 * We consider EPIPE and ECONNRESET as expected.
430 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
434 if (ret
== -ECONNRESET
)
441 * Recv a message accompanied by fd(s) from a unix socket.
443 * Expect at most "nb_fd" file descriptors. Returns the number of fd
444 * actually received in nb_fd.
445 * Returns -EPIPE on orderly shutdown.
447 ssize_t
ustcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
451 struct cmsghdr
*cmsg
;
452 size_t sizeof_fds
= nb_fd
* sizeof(int);
453 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
458 memset(&msg
, 0, sizeof(msg
));
460 /* Prepare to receive the structures */
461 iov
[0].iov_base
= &dummy
;
465 msg
.msg_control
= recv_fd
;
466 msg
.msg_controllen
= sizeof(recv_fd
);
469 ret
= recvmsg(sock
, &msg
, 0);
470 } while (ret
< 0 && errno
== EINTR
);
472 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
473 PERROR("recvmsg fds");
476 if (ret
== -ECONNRESET
)
481 /* orderly shutdown */
486 ERR("Error: Received %zd bytes, expected %d\n",
490 if (msg
.msg_flags
& MSG_CTRUNC
) {
491 ERR("Error: Control message truncated.\n");
495 cmsg
= CMSG_FIRSTHDR(&msg
);
497 ERR("Error: Invalid control message header\n");
501 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
502 ERR("Didn't received any fd\n");
506 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
507 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
508 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
513 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
516 for (i
= 0; i
< nb_fd
; i
++) {
517 ret
= fcntl(fds
[i
], F_SETFD
, FD_CLOEXEC
);
519 PERROR("fcntl failed to set FD_CLOEXEC on fd %d",
529 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
533 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
541 ERR("incorrect message size: %zd\n", len
);
548 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
549 uint32_t expected_handle
, uint32_t expected_cmd
)
553 memset(lur
, 0, sizeof(*lur
));
554 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
556 case 0: /* orderly shutdown */
562 if (lur
->handle
!= expected_handle
) {
563 ERR("Unexpected result message handle: "
564 "expected: %u vs received: %u\n",
565 expected_handle
, lur
->handle
);
568 if (lur
->cmd
!= expected_cmd
) {
569 ERR("Unexpected result message command "
570 "expected: %u vs received: %u\n",
571 expected_cmd
, lur
->cmd
);
577 return lur
->ret_code
;
582 ERR("incorrect message size: %zd\n", len
);
588 int ustcomm_send_app_cmd(int sock
,
589 struct ustcomm_ust_msg
*lum
,
590 struct ustcomm_ust_reply
*lur
)
594 ret
= ustcomm_send_app_msg(sock
, lum
);
597 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
604 * chan_data is allocated internally if this function returns the
607 ssize_t
ustcomm_recv_channel_from_sessiond(int sock
,
608 void **_chan_data
, uint64_t var_len
,
615 if (var_len
> LTTNG_UST_CHANNEL_DATA_MAX_LEN
) {
619 /* Receive variable length data */
620 chan_data
= zmalloc(var_len
);
625 len
= ustcomm_recv_unix_sock(sock
, chan_data
, var_len
);
626 if (len
!= var_len
) {
630 lttng_ust_lock_fd_tracker();
631 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
633 lttng_ust_unlock_fd_tracker();
643 ret
= lttng_ust_add_fd_to_tracker(wakeup_fd
);
645 ret
= close(wakeup_fd
);
647 PERROR("close on wakeup_fd");
650 lttng_ust_unlock_fd_tracker();
655 lttng_ust_unlock_fd_tracker();
657 *_chan_data
= chan_data
;
667 int ustcomm_recv_stream_from_sessiond(int sock
,
668 uint64_t *memory_map_size
,
669 int *shm_fd
, int *wakeup_fd
)
675 /* recv shm fd and wakeup fd */
676 lttng_ust_lock_fd_tracker();
677 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
679 lttng_ust_unlock_fd_tracker();
689 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
693 PERROR("close on received shm_fd");
696 lttng_ust_unlock_fd_tracker();
701 ret
= lttng_ust_add_fd_to_tracker(fds
[1]);
703 ret
= close(*shm_fd
);
705 PERROR("close on shm_fd");
710 PERROR("close on received wakeup_fd");
713 lttng_ust_unlock_fd_tracker();
717 lttng_ust_unlock_fd_tracker();
725 * Returns 0 on success, negative error value on error.
727 int ustcomm_send_reg_msg(int sock
,
728 enum ustctl_socket_type type
,
729 uint32_t bits_per_long
,
730 uint32_t uint8_t_alignment
,
731 uint32_t uint16_t_alignment
,
732 uint32_t uint32_t_alignment
,
733 uint32_t uint64_t_alignment
,
734 uint32_t long_alignment
)
737 struct ustctl_reg_msg reg_msg
;
739 reg_msg
.magic
= LTTNG_UST_COMM_MAGIC
;
740 reg_msg
.major
= LTTNG_UST_ABI_MAJOR_VERSION
;
741 reg_msg
.minor
= LTTNG_UST_ABI_MINOR_VERSION
;
742 reg_msg
.pid
= getpid();
743 reg_msg
.ppid
= getppid();
744 reg_msg
.uid
= getuid();
745 reg_msg
.gid
= getgid();
746 reg_msg
.bits_per_long
= bits_per_long
;
747 reg_msg
.uint8_t_alignment
= uint8_t_alignment
;
748 reg_msg
.uint16_t_alignment
= uint16_t_alignment
;
749 reg_msg
.uint32_t_alignment
= uint32_t_alignment
;
750 reg_msg
.uint64_t_alignment
= uint64_t_alignment
;
751 reg_msg
.long_alignment
= long_alignment
;
752 reg_msg
.socket_type
= type
;
753 lttng_ust_getprocname(reg_msg
.name
);
754 memset(reg_msg
.padding
, 0, sizeof(reg_msg
.padding
));
756 len
= ustcomm_send_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
757 if (len
> 0 && len
!= sizeof(reg_msg
))
765 ssize_t
count_one_type(const struct lttng_type
*lt
)
776 //TODO: implement non-empty struct.
780 const struct lttng_event_field
*choices
;
784 ret
= lttng_ust_dynamic_type_choices(&nr_choices
,
789 * One field for enum, one field for variant, and
790 * one field per choice.
792 return count_fields_recursive(nr_choices
, choices
) + 2;
801 ssize_t
count_fields_recursive(size_t nr_fields
,
802 const struct lttng_event_field
*lttng_fields
)
805 ssize_t ret
, count
= 0;
807 for (i
= 0; i
< nr_fields
; i
++) {
808 const struct lttng_event_field
*lf
;
810 lf
= <tng_fields
[i
];
811 /* skip 'nowrite' fields */
814 ret
= count_one_type(&lf
->type
);
816 return ret
; /* error */
823 ssize_t
count_ctx_fields_recursive(size_t nr_fields
,
824 const struct lttng_ctx_field
*lttng_fields
)
827 ssize_t ret
, count
= 0;
829 for (i
= 0; i
< nr_fields
; i
++) {
830 const struct lttng_event_field
*lf
;
832 lf
= <tng_fields
[i
].event_field
;
833 /* skip 'nowrite' fields */
836 ret
= count_one_type(&lf
->type
);
838 return ret
; /* error */
845 int serialize_string_encoding(int32_t *ue
,
846 enum lttng_string_encodings le
)
849 case lttng_encode_none
:
850 *ue
= ustctl_encode_none
;
852 case lttng_encode_UTF8
:
853 *ue
= ustctl_encode_UTF8
;
855 case lttng_encode_ASCII
:
856 *ue
= ustctl_encode_ASCII
;
865 int serialize_integer_type(struct ustctl_integer_type
*uit
,
866 const struct lttng_integer_type
*lit
)
870 uit
->size
= lit
->size
;
871 uit
->signedness
= lit
->signedness
;
872 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
873 uit
->base
= lit
->base
;
874 if (serialize_string_encoding(&encoding
, lit
->encoding
))
876 uit
->encoding
= encoding
;
877 uit
->alignment
= lit
->alignment
;
882 int serialize_basic_type(struct lttng_session
*session
,
883 enum ustctl_abstract_types
*uatype
,
884 enum lttng_abstract_types atype
,
885 union _ustctl_basic_type
*ubt
,
886 const union _lttng_basic_type
*lbt
)
891 if (serialize_integer_type(&ubt
->integer
, &lbt
->integer
))
893 *uatype
= ustctl_atype_integer
;
900 if (serialize_string_encoding(&encoding
, lbt
->string
.encoding
))
902 ubt
->string
.encoding
= encoding
;
903 *uatype
= ustctl_atype_string
;
908 struct ustctl_float_type
*uft
;
909 const struct lttng_float_type
*lft
;
913 uft
->exp_dig
= lft
->exp_dig
;
914 uft
->mant_dig
= lft
->mant_dig
;
915 uft
->alignment
= lft
->alignment
;
916 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
917 *uatype
= ustctl_atype_float
;
922 strncpy(ubt
->enumeration
.name
, lbt
->enumeration
.desc
->name
,
923 LTTNG_UST_SYM_NAME_LEN
);
924 ubt
->enumeration
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
925 if (serialize_integer_type(&ubt
->enumeration
.container_type
,
926 &lbt
->enumeration
.container_type
))
929 const struct lttng_enum
*_enum
;
931 _enum
= lttng_ust_enum_get_from_desc(session
, lbt
->enumeration
.desc
);
934 ubt
->enumeration
.id
= _enum
->id
;
936 ubt
->enumeration
.id
= -1ULL;
938 *uatype
= ustctl_atype_enum
;
950 int serialize_dynamic_type(struct lttng_session
*session
,
951 struct ustctl_field
*fields
, size_t *iter_output
,
952 const struct lttng_event_field
*lf
)
954 const struct lttng_event_field
*choices
;
955 char tag_field_name
[LTTNG_UST_SYM_NAME_LEN
];
956 const struct lttng_type
*tag_type
;
957 const struct lttng_event_field
*tag_field_generic
;
958 struct lttng_event_field tag_field
= {
959 .name
= tag_field_name
,
962 struct ustctl_field
*uf
;
963 size_t nr_choices
, i
;
966 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
967 tag_type
= &tag_field_generic
->type
;
969 /* Serialize enum field. */
970 strncpy(tag_field_name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
971 tag_field_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
972 strncat(tag_field_name
,
974 LTTNG_UST_SYM_NAME_LEN
- strlen(tag_field_name
) - 1);
975 tag_field
.type
= *tag_type
;
976 ret
= serialize_one_field(session
, fields
, iter_output
,
981 /* Serialize variant field. */
982 uf
= &fields
[*iter_output
];
983 ret
= lttng_ust_dynamic_type_choices(&nr_choices
, &choices
);
987 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
988 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
989 uf
->type
.atype
= ustctl_atype_variant
;
990 uf
->type
.u
.variant
.nr_choices
= nr_choices
;
991 strncpy(uf
->type
.u
.variant
.tag_name
,
993 LTTNG_UST_SYM_NAME_LEN
);
994 uf
->type
.u
.variant
.tag_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
997 /* Serialize choice fields after variant. */
998 for (i
= 0; i
< nr_choices
; i
++) {
999 ret
= serialize_one_field(session
, fields
,
1000 iter_output
, &choices
[i
]);
1008 int serialize_one_field(struct lttng_session
*session
,
1009 struct ustctl_field
*fields
, size_t *iter_output
,
1010 const struct lttng_event_field
*lf
)
1012 const struct lttng_type
*lt
= &lf
->type
;
1015 /* skip 'nowrite' fields */
1019 switch (lt
->atype
) {
1025 struct ustctl_field
*uf
= &fields
[*iter_output
];
1026 struct ustctl_type
*ut
= &uf
->type
;
1027 enum ustctl_abstract_types atype
;
1029 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1030 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1031 ret
= serialize_basic_type(session
, &atype
, lt
->atype
,
1032 &ut
->u
.basic
, <
->u
.basic
);
1041 struct ustctl_field
*uf
= &fields
[*iter_output
];
1042 struct ustctl_type
*ut
= &uf
->type
;
1043 struct ustctl_basic_type
*ubt
;
1044 const struct lttng_basic_type
*lbt
;
1045 enum ustctl_abstract_types atype
;
1047 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1048 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1049 uf
->type
.atype
= ustctl_atype_array
;
1050 ubt
= &ut
->u
.array
.elem_type
;
1051 lbt
= <
->u
.array
.elem_type
;
1052 ut
->u
.array
.length
= lt
->u
.array
.length
;
1053 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1054 &ubt
->u
.basic
, &lbt
->u
.basic
);
1058 ut
->atype
= ustctl_atype_array
;
1062 case atype_sequence
:
1064 struct ustctl_field
*uf
= &fields
[*iter_output
];
1065 struct ustctl_type
*ut
= &uf
->type
;
1066 struct ustctl_basic_type
*ubt
;
1067 const struct lttng_basic_type
*lbt
;
1068 enum ustctl_abstract_types atype
;
1071 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1072 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1073 uf
->type
.atype
= ustctl_atype_sequence
;
1074 ubt
= &ut
->u
.sequence
.length_type
;
1075 lbt
= <
->u
.sequence
.length_type
;
1076 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1077 &ubt
->u
.basic
, &lbt
->u
.basic
);
1081 ubt
= &ut
->u
.sequence
.elem_type
;
1082 lbt
= <
->u
.sequence
.elem_type
;
1083 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1084 &ubt
->u
.basic
, &lbt
->u
.basic
);
1088 ut
->atype
= ustctl_atype_sequence
;
1094 ret
= serialize_dynamic_type(session
, fields
, iter_output
, lf
);
1101 struct ustctl_field
*uf
= &fields
[*iter_output
];
1104 * TODO: add support for non-empty struct.
1106 if (lf
->type
.u
._struct
.nr_fields
!= 0) {
1109 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1110 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1111 uf
->type
.atype
= ustctl_atype_struct
;
1112 uf
->type
.u
._struct
.nr_fields
= 0;
1123 int serialize_fields(struct lttng_session
*session
,
1124 size_t *_nr_write_fields
,
1125 struct ustctl_field
**ustctl_fields
,
1127 const struct lttng_event_field
*lttng_fields
)
1129 struct ustctl_field
*fields
;
1131 size_t i
, iter_output
= 0;
1132 ssize_t nr_write_fields
;
1134 nr_write_fields
= count_fields_recursive(nr_fields
, lttng_fields
);
1135 if (nr_write_fields
< 0) {
1136 return (int) nr_write_fields
;
1139 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1143 for (i
= 0; i
< nr_fields
; i
++) {
1144 ret
= serialize_one_field(session
, fields
, &iter_output
,
1150 *_nr_write_fields
= nr_write_fields
;
1151 *ustctl_fields
= fields
;
1160 int serialize_entries(struct ustctl_enum_entry
**_entries
,
1162 const struct lttng_enum_entry
*lttng_entries
)
1164 struct ustctl_enum_entry
*entries
;
1167 /* Serialize the entries */
1168 entries
= zmalloc(nr_entries
* sizeof(*entries
));
1171 for (i
= 0; i
< nr_entries
; i
++) {
1172 struct ustctl_enum_entry
*uentry
;
1173 const struct lttng_enum_entry
*lentry
;
1175 uentry
= &entries
[i
];
1176 lentry
= <tng_entries
[i
];
1178 uentry
->start
.value
= lentry
->start
.value
;
1179 uentry
->start
.signedness
= lentry
->start
.signedness
;
1180 uentry
->end
.value
= lentry
->end
.value
;
1181 uentry
->end
.signedness
= lentry
->end
.signedness
;
1182 strncpy(uentry
->string
, lentry
->string
, LTTNG_UST_SYM_NAME_LEN
);
1183 uentry
->string
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1185 if (lentry
->u
.extra
.options
& LTTNG_ENUM_ENTRY_OPTION_IS_AUTO
) {
1186 uentry
->u
.extra
.options
|=
1187 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO
;
1190 *_entries
= entries
;
1195 int serialize_ctx_fields(struct lttng_session
*session
,
1196 size_t *_nr_write_fields
,
1197 struct ustctl_field
**ustctl_fields
,
1199 const struct lttng_ctx_field
*lttng_fields
)
1201 struct ustctl_field
*fields
;
1203 size_t i
, iter_output
= 0;
1204 ssize_t nr_write_fields
;
1206 nr_write_fields
= count_ctx_fields_recursive(nr_fields
,
1208 if (nr_write_fields
< 0) {
1209 return (int) nr_write_fields
;
1212 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1216 for (i
= 0; i
< nr_fields
; i
++) {
1217 ret
= serialize_one_field(session
, fields
, &iter_output
,
1218 <tng_fields
[i
].event_field
);
1223 *_nr_write_fields
= nr_write_fields
;
1224 *ustctl_fields
= fields
;
1233 * Returns 0 on success, negative error value on error.
1235 int ustcomm_register_event(int sock
,
1236 struct lttng_session
*session
,
1237 int session_objd
, /* session descriptor */
1238 int channel_objd
, /* channel descriptor */
1239 const char *event_name
, /* event name (input) */
1241 const char *signature
, /* event signature (input) */
1242 size_t nr_fields
, /* fields */
1243 const struct lttng_event_field
*lttng_fields
,
1244 const char *model_emf_uri
,
1245 uint32_t *id
) /* event id (output) */
1249 struct ustcomm_notify_hdr header
;
1250 struct ustcomm_notify_event_msg m
;
1253 struct ustcomm_notify_hdr header
;
1254 struct ustcomm_notify_event_reply r
;
1256 size_t signature_len
, fields_len
, model_emf_uri_len
;
1257 struct ustctl_field
*fields
= NULL
;
1258 size_t nr_write_fields
= 0;
1261 memset(&msg
, 0, sizeof(msg
));
1262 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1263 msg
.m
.session_objd
= session_objd
;
1264 msg
.m
.channel_objd
= channel_objd
;
1265 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_SYM_NAME_LEN
);
1266 msg
.m
.event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1267 msg
.m
.loglevel
= loglevel
;
1268 signature_len
= strlen(signature
) + 1;
1269 msg
.m
.signature_len
= signature_len
;
1271 /* Calculate fields len, serialize fields. */
1272 if (nr_fields
> 0) {
1273 ret
= serialize_fields(session
, &nr_write_fields
, &fields
,
1274 nr_fields
, lttng_fields
);
1279 fields_len
= sizeof(*fields
) * nr_write_fields
;
1280 msg
.m
.fields_len
= fields_len
;
1281 if (model_emf_uri
) {
1282 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
1284 model_emf_uri_len
= 0;
1286 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
1288 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1289 if (len
> 0 && len
!= sizeof(msg
)) {
1298 /* send signature */
1299 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
1300 if (len
> 0 && len
!= signature_len
) {
1310 if (fields_len
> 0) {
1311 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1312 if (len
> 0 && len
!= fields_len
) {
1323 if (model_emf_uri_len
) {
1324 /* send model_emf_uri */
1325 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
1327 if (len
> 0 && len
!= model_emf_uri_len
) {
1336 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1338 case 0: /* orderly shutdown */
1341 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1342 ERR("Unexpected result message command "
1343 "expected: %u vs received: %u\n",
1344 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1347 if (reply
.r
.ret_code
> 0)
1349 if (reply
.r
.ret_code
< 0)
1350 return reply
.r
.ret_code
;
1351 *id
= reply
.r
.event_id
;
1352 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1353 event_name
, reply
.r
.ret_code
, reply
.r
.event_id
);
1357 /* Transport level error */
1358 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1362 ERR("incorrect message size: %zd\n", len
);
1368 /* Error path only. */
1375 * Returns 0 on success, negative error value on error.
1376 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1378 int ustcomm_register_enum(int sock
,
1379 int session_objd
, /* session descriptor */
1380 const char *enum_name
, /* enum name (input) */
1381 size_t nr_entries
, /* entries */
1382 const struct lttng_enum_entry
*lttng_entries
,
1387 struct ustcomm_notify_hdr header
;
1388 struct ustcomm_notify_enum_msg m
;
1391 struct ustcomm_notify_hdr header
;
1392 struct ustcomm_notify_enum_reply r
;
1395 struct ustctl_enum_entry
*entries
= NULL
;
1398 memset(&msg
, 0, sizeof(msg
));
1399 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1400 msg
.m
.session_objd
= session_objd
;
1401 strncpy(msg
.m
.enum_name
, enum_name
, LTTNG_UST_SYM_NAME_LEN
);
1402 msg
.m
.enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1404 /* Calculate entries len, serialize entries. */
1405 if (nr_entries
> 0) {
1406 ret
= serialize_entries(&entries
,
1407 nr_entries
, lttng_entries
);
1412 entries_len
= sizeof(*entries
) * nr_entries
;
1413 msg
.m
.entries_len
= entries_len
;
1415 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1416 if (len
> 0 && len
!= sizeof(msg
)) {
1426 if (entries_len
> 0) {
1427 len
= ustcomm_send_unix_sock(sock
, entries
, entries_len
);
1428 if (len
> 0 && len
!= entries_len
) {
1441 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1443 case 0: /* orderly shutdown */
1446 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1447 ERR("Unexpected result message command "
1448 "expected: %u vs received: %u\n",
1449 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1452 if (reply
.r
.ret_code
> 0)
1454 if (reply
.r
.ret_code
< 0)
1455 return reply
.r
.ret_code
;
1456 *id
= reply
.r
.enum_id
;
1457 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1458 enum_name
, reply
.r
.ret_code
);
1462 /* Transport level error */
1463 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1467 ERR("incorrect message size: %zd\n", len
);
1479 * Returns 0 on success, negative error value on error.
1480 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1482 int ustcomm_register_channel(int sock
,
1483 struct lttng_session
*session
,
1484 int session_objd
, /* session descriptor */
1485 int channel_objd
, /* channel descriptor */
1486 size_t nr_ctx_fields
,
1487 const struct lttng_ctx_field
*ctx_fields
,
1488 uint32_t *chan_id
, /* channel id (output) */
1489 int *header_type
) /* header type (output) */
1493 struct ustcomm_notify_hdr header
;
1494 struct ustcomm_notify_channel_msg m
;
1497 struct ustcomm_notify_hdr header
;
1498 struct ustcomm_notify_channel_reply r
;
1501 struct ustctl_field
*fields
= NULL
;
1503 size_t nr_write_fields
= 0;
1505 memset(&msg
, 0, sizeof(msg
));
1506 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1507 msg
.m
.session_objd
= session_objd
;
1508 msg
.m
.channel_objd
= channel_objd
;
1510 /* Calculate fields len, serialize fields. */
1511 if (nr_ctx_fields
> 0) {
1512 ret
= serialize_ctx_fields(session
, &nr_write_fields
, &fields
,
1513 nr_ctx_fields
, ctx_fields
);
1518 fields_len
= sizeof(*fields
) * nr_write_fields
;
1519 msg
.m
.ctx_fields_len
= fields_len
;
1520 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1521 if (len
> 0 && len
!= sizeof(msg
)) {
1531 if (fields_len
> 0) {
1532 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1534 if (len
> 0 && len
!= fields_len
) {
1544 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1546 case 0: /* orderly shutdown */
1549 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1550 ERR("Unexpected result message command "
1551 "expected: %u vs received: %u\n",
1552 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1555 if (reply
.r
.ret_code
> 0)
1557 if (reply
.r
.ret_code
< 0)
1558 return reply
.r
.ret_code
;
1559 *chan_id
= reply
.r
.chan_id
;
1560 switch (reply
.r
.header_type
) {
1563 *header_type
= reply
.r
.header_type
;
1566 ERR("Unexpected channel header type %u\n",
1567 reply
.r
.header_type
);
1570 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1571 reply
.r
.chan_id
, reply
.r
.header_type
);
1575 /* Transport level error */
1576 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1580 ERR("incorrect message size: %zd\n", len
);
1587 * Set socket reciving timeout.
1589 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1594 tv
.tv_sec
= msec
/ 1000;
1595 tv
.tv_usec
= (msec
* 1000 % 1000000);
1597 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1599 PERROR("setsockopt SO_RCVTIMEO");
1607 * Set socket sending timeout.
1609 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1614 tv
.tv_sec
= msec
/ 1000;
1615 tv
.tv_usec
= (msec
* 1000) % 1000000;
1617 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1619 PERROR("setsockopt SO_SNDTIMEO");