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.
111 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
118 /* Give at least 10ms. */
121 ret
= ustcomm_setsockopt_snd_timeout(fd
, timeout
);
123 WARN("Error setting connect socket send timeout");
126 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
133 memset(&sun
, 0, sizeof(sun
));
134 sun
.sun_family
= AF_UNIX
;
135 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
136 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
138 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
141 * Don't print message on connect ENOENT error, because
142 * connect is used in normal execution to detect if
143 * sessiond is alive. ENOENT is when the unix socket
144 * file does not exist, and ECONNREFUSED is when the
145 * file exists but no sessiond is listening.
147 if (errno
!= ECONNREFUSED
&& errno
!= ECONNRESET
148 && errno
!= ENOENT
&& errno
!= EACCES
)
151 if (ret
== -ECONNREFUSED
|| ret
== -ECONNRESET
)
163 closeret
= close(fd
);
172 * ustcomm_accept_unix_sock
174 * Do an accept(2) on the sock and return the
175 * new file descriptor. The socket MUST be bind(2) before.
177 int ustcomm_accept_unix_sock(int sock
)
180 struct sockaddr_un sun
;
184 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
186 if (errno
!= ECONNABORTED
)
189 if (new_fd
== -ECONNABORTED
)
196 * ustcomm_create_unix_sock
198 * Creates a AF_UNIX local socket using pathname
199 * bind the socket upon creation and return the fd.
201 int ustcomm_create_unix_sock(const char *pathname
)
203 struct sockaddr_un sun
;
206 /* Create server socket */
207 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
213 memset(&sun
, 0, sizeof(sun
));
214 sun
.sun_family
= AF_UNIX
;
215 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
216 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
218 /* Unlink the old file if present */
219 (void) unlink(pathname
);
220 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
233 closeret
= close(fd
);
243 * ustcomm_listen_unix_sock
245 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
247 int ustcomm_listen_unix_sock(int sock
)
251 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
261 * ustcomm_close_unix_sock
263 * Shutdown cleanly a unix socket.
265 * Handles fd tracker internally.
267 int ustcomm_close_unix_sock(int sock
)
271 lttng_ust_lock_fd_tracker();
274 lttng_ust_delete_fd_from_tracker(sock
);
279 lttng_ust_unlock_fd_tracker();
285 * ustcomm_recv_unix_sock
287 * Receive data of size len in put that data into
288 * the buf param. Using recvmsg API.
289 * Return the size of received data.
290 * Return 0 on orderly shutdown.
292 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
299 memset(&msg
, 0, sizeof(msg
));
301 iov
[0].iov_base
= buf
;
302 iov
[0].iov_len
= len
;
307 len_last
= iov
[0].iov_len
;
308 ret
= recvmsg(sock
, &msg
, 0);
310 iov
[0].iov_base
+= ret
;
311 iov
[0].iov_len
-= ret
;
312 assert(ret
<= len_last
);
314 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
319 if (errno
!= EPIPE
&& errno
!= ECONNRESET
&& errno
!= ECONNREFUSED
)
322 if (ret
== -ECONNRESET
|| ret
== -ECONNREFUSED
)
325 shutret
= shutdown(sock
, SHUT_RDWR
);
327 ERR("Socket shutdown error");
328 } else if (ret
> 0) {
331 /* ret = 0 means an orderly shutdown. */
337 * ustcomm_send_unix_sock
339 * Send buf data of size len. Using sendmsg API.
340 * Return the size of sent data.
342 ssize_t
ustcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
348 memset(&msg
, 0, sizeof(msg
));
350 iov
[0].iov_base
= (void *) buf
;
351 iov
[0].iov_len
= len
;
356 * Using the MSG_NOSIGNAL when sending data from sessiond to
357 * libust, so libust does not receive an unhandled SIGPIPE or
358 * SIGURG. The sessiond receiver side can be made more resilient
359 * by ignoring SIGPIPE, but we don't have this luxury on the
363 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
364 } while (ret
< 0 && errno
== EINTR
);
369 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
372 if (ret
== -ECONNRESET
)
375 shutret
= shutdown(sock
, SHUT_RDWR
);
377 ERR("Socket shutdown error");
384 * Send a message accompanied by fd(s) over a unix socket.
386 * Returns the size of data sent, or negative error value.
388 ssize_t
ustcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
391 struct cmsghdr
*cmptr
;
394 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
395 char tmp
[CMSG_SPACE(sizeof_fds
)];
398 memset(&msg
, 0, sizeof(msg
));
399 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
401 if (nb_fd
> USTCOMM_MAX_SEND_FDS
)
404 msg
.msg_control
= (caddr_t
)tmp
;
405 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
407 cmptr
= CMSG_FIRSTHDR(&msg
);
410 cmptr
->cmsg_level
= SOL_SOCKET
;
411 cmptr
->cmsg_type
= SCM_RIGHTS
;
412 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
413 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
414 /* Sum of the length of all control messages in the buffer: */
415 msg
.msg_controllen
= cmptr
->cmsg_len
;
417 iov
[0].iov_base
= &dummy
;
423 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
424 } while (ret
< 0 && errno
== EINTR
);
427 * We consider EPIPE and ECONNRESET as expected.
429 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
433 if (ret
== -ECONNRESET
)
440 * Recv a message accompanied by fd(s) from a unix socket.
442 * Expect at most "nb_fd" file descriptors. Returns the number of fd
443 * actually received in nb_fd.
444 * Returns -EPIPE on orderly shutdown.
446 ssize_t
ustcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
450 struct cmsghdr
*cmsg
;
451 size_t sizeof_fds
= nb_fd
* sizeof(int);
452 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
456 memset(&msg
, 0, sizeof(msg
));
458 /* Prepare to receive the structures */
459 iov
[0].iov_base
= &dummy
;
463 msg
.msg_control
= recv_fd
;
464 msg
.msg_controllen
= sizeof(recv_fd
);
467 ret
= recvmsg(sock
, &msg
, 0);
468 } while (ret
< 0 && errno
== EINTR
);
470 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
471 PERROR("recvmsg fds");
474 if (ret
== -ECONNRESET
)
479 /* orderly shutdown */
484 ERR("Error: Received %zd bytes, expected %d\n",
488 if (msg
.msg_flags
& MSG_CTRUNC
) {
489 ERR("Error: Control message truncated.\n");
493 cmsg
= CMSG_FIRSTHDR(&msg
);
495 ERR("Error: Invalid control message header\n");
499 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
500 ERR("Didn't received any fd\n");
504 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
505 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
506 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
510 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
516 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
520 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
528 ERR("incorrect message size: %zd\n", len
);
535 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
536 uint32_t expected_handle
, uint32_t expected_cmd
)
540 memset(lur
, 0, sizeof(*lur
));
541 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
543 case 0: /* orderly shutdown */
549 if (lur
->handle
!= expected_handle
) {
550 ERR("Unexpected result message handle: "
551 "expected: %u vs received: %u\n",
552 expected_handle
, lur
->handle
);
555 if (lur
->cmd
!= expected_cmd
) {
556 ERR("Unexpected result message command "
557 "expected: %u vs received: %u\n",
558 expected_cmd
, lur
->cmd
);
564 return lur
->ret_code
;
569 ERR("incorrect message size: %zd\n", len
);
575 int ustcomm_send_app_cmd(int sock
,
576 struct ustcomm_ust_msg
*lum
,
577 struct ustcomm_ust_reply
*lur
)
581 ret
= ustcomm_send_app_msg(sock
, lum
);
584 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
591 * chan_data is allocated internally if this function returns the
594 ssize_t
ustcomm_recv_channel_from_sessiond(int sock
,
595 void **_chan_data
, uint64_t var_len
,
602 if (var_len
> LTTNG_UST_CHANNEL_DATA_MAX_LEN
) {
606 /* Receive variable length data */
607 chan_data
= zmalloc(var_len
);
612 len
= ustcomm_recv_unix_sock(sock
, chan_data
, var_len
);
613 if (len
!= var_len
) {
617 lttng_ust_lock_fd_tracker();
618 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
620 lttng_ust_unlock_fd_tracker();
630 ret
= lttng_ust_add_fd_to_tracker(wakeup_fd
);
632 ret
= close(wakeup_fd
);
634 PERROR("close on wakeup_fd");
637 lttng_ust_unlock_fd_tracker();
642 lttng_ust_unlock_fd_tracker();
644 *_chan_data
= chan_data
;
654 ssize_t
ustcomm_recv_trigger_notif_fd_from_sessiond(int sock
,
655 int *_trigger_notif_fd
)
658 int trigger_notif_fd
, ret
;
660 /* Receive trigger notification fd */
661 lttng_ust_lock_fd_tracker();
662 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &trigger_notif_fd
, 1);
664 lttng_ust_unlock_fd_tracker();
674 ret
= lttng_ust_add_fd_to_tracker(trigger_notif_fd
);
676 ret
= close(trigger_notif_fd
);
678 PERROR("close on trigger notif fd");
681 lttng_ust_unlock_fd_tracker();
685 *_trigger_notif_fd
= ret
;
686 lttng_ust_unlock_fd_tracker();
694 int ustcomm_recv_stream_from_sessiond(int sock
,
695 uint64_t *memory_map_size
,
696 int *shm_fd
, int *wakeup_fd
)
702 /* recv shm fd and wakeup fd */
703 lttng_ust_lock_fd_tracker();
704 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
706 lttng_ust_unlock_fd_tracker();
716 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
720 PERROR("close on received shm_fd");
723 lttng_ust_unlock_fd_tracker();
728 ret
= lttng_ust_add_fd_to_tracker(fds
[1]);
730 ret
= close(*shm_fd
);
732 PERROR("close on shm_fd");
737 PERROR("close on received wakeup_fd");
740 lttng_ust_unlock_fd_tracker();
744 lttng_ust_unlock_fd_tracker();
752 * Returns 0 on success, negative error value on error.
754 int ustcomm_send_reg_msg(int sock
,
755 enum ustctl_socket_type type
,
756 uint32_t bits_per_long
,
757 uint32_t uint8_t_alignment
,
758 uint32_t uint16_t_alignment
,
759 uint32_t uint32_t_alignment
,
760 uint32_t uint64_t_alignment
,
761 uint32_t long_alignment
)
764 struct ustctl_reg_msg reg_msg
;
766 reg_msg
.magic
= LTTNG_UST_COMM_MAGIC
;
767 reg_msg
.major
= LTTNG_UST_ABI_MAJOR_VERSION
;
768 reg_msg
.minor
= LTTNG_UST_ABI_MINOR_VERSION
;
769 reg_msg
.pid
= getpid();
770 reg_msg
.ppid
= getppid();
771 reg_msg
.uid
= getuid();
772 reg_msg
.gid
= getgid();
773 reg_msg
.bits_per_long
= bits_per_long
;
774 reg_msg
.uint8_t_alignment
= uint8_t_alignment
;
775 reg_msg
.uint16_t_alignment
= uint16_t_alignment
;
776 reg_msg
.uint32_t_alignment
= uint32_t_alignment
;
777 reg_msg
.uint64_t_alignment
= uint64_t_alignment
;
778 reg_msg
.long_alignment
= long_alignment
;
779 reg_msg
.socket_type
= type
;
780 lttng_ust_getprocname(reg_msg
.name
);
781 memset(reg_msg
.padding
, 0, sizeof(reg_msg
.padding
));
783 len
= ustcomm_send_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
784 if (len
> 0 && len
!= sizeof(reg_msg
))
792 ssize_t
count_one_type(const struct lttng_type
*lt
)
803 //TODO: implement non-empty struct.
807 const struct lttng_event_field
*choices
;
811 ret
= lttng_ust_dynamic_type_choices(&nr_choices
,
816 * One field for enum, one field for variant, and
817 * one field per choice.
819 return count_fields_recursive(nr_choices
, choices
) + 2;
828 ssize_t
count_fields_recursive(size_t nr_fields
,
829 const struct lttng_event_field
*lttng_fields
)
832 ssize_t ret
, count
= 0;
834 for (i
= 0; i
< nr_fields
; i
++) {
835 const struct lttng_event_field
*lf
;
837 lf
= <tng_fields
[i
];
838 /* skip 'nowrite' fields */
841 ret
= count_one_type(&lf
->type
);
843 return ret
; /* error */
850 ssize_t
count_ctx_fields_recursive(size_t nr_fields
,
851 const struct lttng_ctx_field
*lttng_fields
)
854 ssize_t ret
, count
= 0;
856 for (i
= 0; i
< nr_fields
; i
++) {
857 const struct lttng_event_field
*lf
;
859 lf
= <tng_fields
[i
].event_field
;
860 /* skip 'nowrite' fields */
863 ret
= count_one_type(&lf
->type
);
865 return ret
; /* error */
872 int serialize_string_encoding(int32_t *ue
,
873 enum lttng_string_encodings le
)
876 case lttng_encode_none
:
877 *ue
= ustctl_encode_none
;
879 case lttng_encode_UTF8
:
880 *ue
= ustctl_encode_UTF8
;
882 case lttng_encode_ASCII
:
883 *ue
= ustctl_encode_ASCII
;
892 int serialize_integer_type(struct ustctl_integer_type
*uit
,
893 const struct lttng_integer_type
*lit
)
897 uit
->size
= lit
->size
;
898 uit
->signedness
= lit
->signedness
;
899 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
900 uit
->base
= lit
->base
;
901 if (serialize_string_encoding(&encoding
, lit
->encoding
))
903 uit
->encoding
= encoding
;
904 uit
->alignment
= lit
->alignment
;
909 int serialize_basic_type(struct lttng_session
*session
,
910 enum ustctl_abstract_types
*uatype
,
911 enum lttng_abstract_types atype
,
912 union _ustctl_basic_type
*ubt
,
913 const union _lttng_basic_type
*lbt
)
918 if (serialize_integer_type(&ubt
->integer
, &lbt
->integer
))
920 *uatype
= ustctl_atype_integer
;
927 if (serialize_string_encoding(&encoding
, lbt
->string
.encoding
))
929 ubt
->string
.encoding
= encoding
;
930 *uatype
= ustctl_atype_string
;
935 struct ustctl_float_type
*uft
;
936 const struct lttng_float_type
*lft
;
940 uft
->exp_dig
= lft
->exp_dig
;
941 uft
->mant_dig
= lft
->mant_dig
;
942 uft
->alignment
= lft
->alignment
;
943 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
944 *uatype
= ustctl_atype_float
;
949 strncpy(ubt
->enumeration
.name
, lbt
->enumeration
.desc
->name
,
950 LTTNG_UST_SYM_NAME_LEN
);
951 ubt
->enumeration
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
952 if (serialize_integer_type(&ubt
->enumeration
.container_type
,
953 &lbt
->enumeration
.container_type
))
956 const struct lttng_enum
*_enum
;
958 _enum
= lttng_ust_enum_get_from_desc(session
, lbt
->enumeration
.desc
);
961 ubt
->enumeration
.id
= _enum
->id
;
963 ubt
->enumeration
.id
= -1ULL;
965 *uatype
= ustctl_atype_enum
;
977 int serialize_dynamic_type(struct lttng_session
*session
,
978 struct ustctl_field
*fields
, size_t *iter_output
,
979 const struct lttng_event_field
*lf
)
981 const struct lttng_event_field
*choices
;
982 char tag_field_name
[LTTNG_UST_SYM_NAME_LEN
];
983 const struct lttng_type
*tag_type
;
984 const struct lttng_event_field
*tag_field_generic
;
985 struct lttng_event_field tag_field
= {
986 .name
= tag_field_name
,
989 struct ustctl_field
*uf
;
990 size_t nr_choices
, i
;
993 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
994 tag_type
= &tag_field_generic
->type
;
996 /* Serialize enum field. */
997 strncpy(tag_field_name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
998 tag_field_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
999 strncat(tag_field_name
,
1001 LTTNG_UST_SYM_NAME_LEN
- strlen(tag_field_name
) - 1);
1002 tag_field
.type
= *tag_type
;
1003 ret
= serialize_one_field(session
, fields
, iter_output
,
1008 /* Serialize variant field. */
1009 uf
= &fields
[*iter_output
];
1010 ret
= lttng_ust_dynamic_type_choices(&nr_choices
, &choices
);
1014 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1015 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1016 uf
->type
.atype
= ustctl_atype_variant
;
1017 uf
->type
.u
.variant
.nr_choices
= nr_choices
;
1018 strncpy(uf
->type
.u
.variant
.tag_name
,
1020 LTTNG_UST_SYM_NAME_LEN
);
1021 uf
->type
.u
.variant
.tag_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1024 /* Serialize choice fields after variant. */
1025 for (i
= 0; i
< nr_choices
; i
++) {
1026 ret
= serialize_one_field(session
, fields
,
1027 iter_output
, &choices
[i
]);
1035 int serialize_one_field(struct lttng_session
*session
,
1036 struct ustctl_field
*fields
, size_t *iter_output
,
1037 const struct lttng_event_field
*lf
)
1039 const struct lttng_type
*lt
= &lf
->type
;
1042 /* skip 'nowrite' fields */
1046 switch (lt
->atype
) {
1052 struct ustctl_field
*uf
= &fields
[*iter_output
];
1053 struct ustctl_type
*ut
= &uf
->type
;
1054 enum ustctl_abstract_types atype
;
1056 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1057 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1058 ret
= serialize_basic_type(session
, &atype
, lt
->atype
,
1059 &ut
->u
.basic
, <
->u
.basic
);
1068 struct ustctl_field
*uf
= &fields
[*iter_output
];
1069 struct ustctl_type
*ut
= &uf
->type
;
1070 struct ustctl_basic_type
*ubt
;
1071 const struct lttng_basic_type
*lbt
;
1072 enum ustctl_abstract_types atype
;
1074 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1075 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1076 uf
->type
.atype
= ustctl_atype_array
;
1077 ubt
= &ut
->u
.array
.elem_type
;
1078 lbt
= <
->u
.array
.elem_type
;
1079 ut
->u
.array
.length
= lt
->u
.array
.length
;
1080 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1081 &ubt
->u
.basic
, &lbt
->u
.basic
);
1085 ut
->atype
= ustctl_atype_array
;
1089 case atype_sequence
:
1091 struct ustctl_field
*uf
= &fields
[*iter_output
];
1092 struct ustctl_type
*ut
= &uf
->type
;
1093 struct ustctl_basic_type
*ubt
;
1094 const struct lttng_basic_type
*lbt
;
1095 enum ustctl_abstract_types atype
;
1098 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1099 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1100 uf
->type
.atype
= ustctl_atype_sequence
;
1101 ubt
= &ut
->u
.sequence
.length_type
;
1102 lbt
= <
->u
.sequence
.length_type
;
1103 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1104 &ubt
->u
.basic
, &lbt
->u
.basic
);
1108 ubt
= &ut
->u
.sequence
.elem_type
;
1109 lbt
= <
->u
.sequence
.elem_type
;
1110 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1111 &ubt
->u
.basic
, &lbt
->u
.basic
);
1115 ut
->atype
= ustctl_atype_sequence
;
1121 ret
= serialize_dynamic_type(session
, fields
, iter_output
, lf
);
1128 struct ustctl_field
*uf
= &fields
[*iter_output
];
1131 * TODO: add support for non-empty struct.
1133 if (lf
->type
.u
._struct
.nr_fields
!= 0) {
1136 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1137 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1138 uf
->type
.atype
= ustctl_atype_struct
;
1139 uf
->type
.u
._struct
.nr_fields
= 0;
1150 int serialize_fields(struct lttng_session
*session
,
1151 size_t *_nr_write_fields
,
1152 struct ustctl_field
**ustctl_fields
,
1154 const struct lttng_event_field
*lttng_fields
)
1156 struct ustctl_field
*fields
;
1158 size_t i
, iter_output
= 0;
1159 ssize_t nr_write_fields
;
1161 nr_write_fields
= count_fields_recursive(nr_fields
, lttng_fields
);
1162 if (nr_write_fields
< 0) {
1163 return (int) nr_write_fields
;
1166 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1170 for (i
= 0; i
< nr_fields
; i
++) {
1171 ret
= serialize_one_field(session
, fields
, &iter_output
,
1177 *_nr_write_fields
= nr_write_fields
;
1178 *ustctl_fields
= fields
;
1187 int serialize_entries(struct ustctl_enum_entry
**_entries
,
1189 const struct lttng_enum_entry
*lttng_entries
)
1191 struct ustctl_enum_entry
*entries
;
1194 /* Serialize the entries */
1195 entries
= zmalloc(nr_entries
* sizeof(*entries
));
1198 for (i
= 0; i
< nr_entries
; i
++) {
1199 struct ustctl_enum_entry
*uentry
;
1200 const struct lttng_enum_entry
*lentry
;
1202 uentry
= &entries
[i
];
1203 lentry
= <tng_entries
[i
];
1205 uentry
->start
.value
= lentry
->start
.value
;
1206 uentry
->start
.signedness
= lentry
->start
.signedness
;
1207 uentry
->end
.value
= lentry
->end
.value
;
1208 uentry
->end
.signedness
= lentry
->end
.signedness
;
1209 strncpy(uentry
->string
, lentry
->string
, LTTNG_UST_SYM_NAME_LEN
);
1210 uentry
->string
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1212 if (lentry
->u
.extra
.options
& LTTNG_ENUM_ENTRY_OPTION_IS_AUTO
) {
1213 uentry
->u
.extra
.options
|=
1214 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO
;
1217 *_entries
= entries
;
1222 int serialize_ctx_fields(struct lttng_session
*session
,
1223 size_t *_nr_write_fields
,
1224 struct ustctl_field
**ustctl_fields
,
1226 const struct lttng_ctx_field
*lttng_fields
)
1228 struct ustctl_field
*fields
;
1230 size_t i
, iter_output
= 0;
1231 ssize_t nr_write_fields
;
1233 nr_write_fields
= count_ctx_fields_recursive(nr_fields
,
1235 if (nr_write_fields
< 0) {
1236 return (int) nr_write_fields
;
1239 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1243 for (i
= 0; i
< nr_fields
; i
++) {
1244 ret
= serialize_one_field(session
, fields
, &iter_output
,
1245 <tng_fields
[i
].event_field
);
1250 *_nr_write_fields
= nr_write_fields
;
1251 *ustctl_fields
= fields
;
1260 * Returns 0 on success, negative error value on error.
1262 int ustcomm_register_event(int sock
,
1263 struct lttng_session
*session
,
1264 int session_objd
, /* session descriptor */
1265 int channel_objd
, /* channel descriptor */
1266 const char *event_name
, /* event name (input) */
1268 const char *signature
, /* event signature (input) */
1269 size_t nr_fields
, /* fields */
1270 const struct lttng_event_field
*lttng_fields
,
1271 const char *model_emf_uri
,
1272 uint32_t *id
) /* event id (output) */
1276 struct ustcomm_notify_hdr header
;
1277 struct ustcomm_notify_event_msg m
;
1280 struct ustcomm_notify_hdr header
;
1281 struct ustcomm_notify_event_reply r
;
1283 size_t signature_len
, fields_len
, model_emf_uri_len
;
1284 struct ustctl_field
*fields
= NULL
;
1285 size_t nr_write_fields
= 0;
1288 memset(&msg
, 0, sizeof(msg
));
1289 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1290 msg
.m
.session_objd
= session_objd
;
1291 msg
.m
.channel_objd
= channel_objd
;
1292 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_SYM_NAME_LEN
);
1293 msg
.m
.event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1294 msg
.m
.loglevel
= loglevel
;
1295 signature_len
= strlen(signature
) + 1;
1296 msg
.m
.signature_len
= signature_len
;
1298 /* Calculate fields len, serialize fields. */
1299 if (nr_fields
> 0) {
1300 ret
= serialize_fields(session
, &nr_write_fields
, &fields
,
1301 nr_fields
, lttng_fields
);
1306 fields_len
= sizeof(*fields
) * nr_write_fields
;
1307 msg
.m
.fields_len
= fields_len
;
1308 if (model_emf_uri
) {
1309 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
1311 model_emf_uri_len
= 0;
1313 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
1315 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1316 if (len
> 0 && len
!= sizeof(msg
)) {
1325 /* send signature */
1326 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
1327 if (len
> 0 && len
!= signature_len
) {
1337 if (fields_len
> 0) {
1338 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1339 if (len
> 0 && len
!= fields_len
) {
1350 if (model_emf_uri_len
) {
1351 /* send model_emf_uri */
1352 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
1354 if (len
> 0 && len
!= model_emf_uri_len
) {
1363 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1365 case 0: /* orderly shutdown */
1368 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1369 ERR("Unexpected result message command "
1370 "expected: %u vs received: %u\n",
1371 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1374 if (reply
.r
.ret_code
> 0)
1376 if (reply
.r
.ret_code
< 0)
1377 return reply
.r
.ret_code
;
1378 *id
= reply
.r
.event_id
;
1379 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1380 event_name
, reply
.r
.ret_code
, reply
.r
.event_id
);
1384 /* Transport level error */
1385 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1389 ERR("incorrect message size: %zd\n", len
);
1395 /* Error path only. */
1402 * Returns 0 on success, negative error value on error.
1403 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1405 int ustcomm_register_enum(int sock
,
1406 int session_objd
, /* session descriptor */
1407 const char *enum_name
, /* enum name (input) */
1408 size_t nr_entries
, /* entries */
1409 const struct lttng_enum_entry
*lttng_entries
,
1414 struct ustcomm_notify_hdr header
;
1415 struct ustcomm_notify_enum_msg m
;
1418 struct ustcomm_notify_hdr header
;
1419 struct ustcomm_notify_enum_reply r
;
1422 struct ustctl_enum_entry
*entries
= NULL
;
1425 memset(&msg
, 0, sizeof(msg
));
1426 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1427 msg
.m
.session_objd
= session_objd
;
1428 strncpy(msg
.m
.enum_name
, enum_name
, LTTNG_UST_SYM_NAME_LEN
);
1429 msg
.m
.enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1431 /* Calculate entries len, serialize entries. */
1432 if (nr_entries
> 0) {
1433 ret
= serialize_entries(&entries
,
1434 nr_entries
, lttng_entries
);
1439 entries_len
= sizeof(*entries
) * nr_entries
;
1440 msg
.m
.entries_len
= entries_len
;
1442 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1443 if (len
> 0 && len
!= sizeof(msg
)) {
1453 if (entries_len
> 0) {
1454 len
= ustcomm_send_unix_sock(sock
, entries
, entries_len
);
1455 if (len
> 0 && len
!= entries_len
) {
1468 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1470 case 0: /* orderly shutdown */
1473 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1474 ERR("Unexpected result message command "
1475 "expected: %u vs received: %u\n",
1476 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1479 if (reply
.r
.ret_code
> 0)
1481 if (reply
.r
.ret_code
< 0)
1482 return reply
.r
.ret_code
;
1483 *id
= reply
.r
.enum_id
;
1484 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1485 enum_name
, reply
.r
.ret_code
);
1489 /* Transport level error */
1490 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1494 ERR("incorrect message size: %zd\n", len
);
1506 * Returns 0 on success, negative error value on error.
1507 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1509 int ustcomm_register_channel(int sock
,
1510 struct lttng_session
*session
,
1511 int session_objd
, /* session descriptor */
1512 int channel_objd
, /* channel descriptor */
1513 size_t nr_ctx_fields
,
1514 const struct lttng_ctx_field
*ctx_fields
,
1515 uint32_t *chan_id
, /* channel id (output) */
1516 int *header_type
) /* header type (output) */
1520 struct ustcomm_notify_hdr header
;
1521 struct ustcomm_notify_channel_msg m
;
1524 struct ustcomm_notify_hdr header
;
1525 struct ustcomm_notify_channel_reply r
;
1528 struct ustctl_field
*fields
= NULL
;
1530 size_t nr_write_fields
= 0;
1532 memset(&msg
, 0, sizeof(msg
));
1533 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1534 msg
.m
.session_objd
= session_objd
;
1535 msg
.m
.channel_objd
= channel_objd
;
1537 /* Calculate fields len, serialize fields. */
1538 if (nr_ctx_fields
> 0) {
1539 ret
= serialize_ctx_fields(session
, &nr_write_fields
, &fields
,
1540 nr_ctx_fields
, ctx_fields
);
1545 fields_len
= sizeof(*fields
) * nr_write_fields
;
1546 msg
.m
.ctx_fields_len
= fields_len
;
1547 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1548 if (len
> 0 && len
!= sizeof(msg
)) {
1558 if (fields_len
> 0) {
1559 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1561 if (len
> 0 && len
!= fields_len
) {
1571 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1573 case 0: /* orderly shutdown */
1576 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1577 ERR("Unexpected result message command "
1578 "expected: %u vs received: %u\n",
1579 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1582 if (reply
.r
.ret_code
> 0)
1584 if (reply
.r
.ret_code
< 0)
1585 return reply
.r
.ret_code
;
1586 *chan_id
= reply
.r
.chan_id
;
1587 switch (reply
.r
.header_type
) {
1590 *header_type
= reply
.r
.header_type
;
1593 ERR("Unexpected channel header type %u\n",
1594 reply
.r
.header_type
);
1597 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1598 reply
.r
.chan_id
, reply
.r
.header_type
);
1602 /* Transport level error */
1603 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1607 ERR("incorrect message size: %zd\n", len
);
1614 * Set socket reciving timeout.
1616 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1621 tv
.tv_sec
= msec
/ 1000;
1622 tv
.tv_usec
= (msec
* 1000 % 1000000);
1624 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1626 PERROR("setsockopt SO_RCVTIMEO");
1634 * Set socket sending timeout.
1636 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1641 tv
.tv_sec
= msec
/ 1000;
1642 tv
.tv_usec
= (msec
* 1000) % 1000000;
1644 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1646 PERROR("setsockopt SO_SNDTIMEO");