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",
79 * Receives positive error value.
80 * Return ptr to string representing a human readable
81 * error code from the ustcomm_return_code enum.
83 const char *lttng_ust_strerror(int code
)
85 if (code
== LTTNG_UST_OK
)
86 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
87 if (code
< LTTNG_UST_ERR
)
88 return strerror(code
);
89 if (code
>= LTTNG_UST_ERR_NR
)
91 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
95 * ustcomm_connect_unix_sock
97 * Connect to unix socket using the path name.
99 * Caller handles FD tracker.
101 int ustcomm_connect_unix_sock(const char *pathname
, long timeout
)
103 struct sockaddr_un sun
;
107 * libust threads require the close-on-exec flag for all
108 * resources so it does not leak file descriptors upon exec.
110 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
117 /* Give at least 10ms. */
120 ret
= ustcomm_setsockopt_snd_timeout(fd
, timeout
);
122 WARN("Error setting connect socket send timeout");
125 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
132 memset(&sun
, 0, sizeof(sun
));
133 sun
.sun_family
= AF_UNIX
;
134 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
135 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
137 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
140 * Don't print message on connect ENOENT error, because
141 * connect is used in normal execution to detect if
142 * sessiond is alive. ENOENT is when the unix socket
143 * file does not exist, and ECONNREFUSED is when the
144 * file exists but no sessiond is listening.
146 if (errno
!= ECONNREFUSED
&& errno
!= ECONNRESET
147 && errno
!= ENOENT
&& errno
!= EACCES
)
150 if (ret
== -ECONNREFUSED
|| ret
== -ECONNRESET
)
162 closeret
= close(fd
);
171 * ustcomm_accept_unix_sock
173 * Do an accept(2) on the sock and return the
174 * new file descriptor. The socket MUST be bind(2) before.
176 int ustcomm_accept_unix_sock(int sock
)
179 struct sockaddr_un sun
;
183 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
185 if (errno
!= ECONNABORTED
)
188 if (new_fd
== -ECONNABORTED
)
195 * ustcomm_create_unix_sock
197 * Creates a AF_UNIX local socket using pathname
198 * bind the socket upon creation and return the fd.
200 int ustcomm_create_unix_sock(const char *pathname
)
202 struct sockaddr_un sun
;
205 /* Create server socket */
206 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
212 memset(&sun
, 0, sizeof(sun
));
213 sun
.sun_family
= AF_UNIX
;
214 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
215 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
217 /* Unlink the old file if present */
218 (void) unlink(pathname
);
219 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
232 closeret
= close(fd
);
242 * ustcomm_listen_unix_sock
244 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
246 int ustcomm_listen_unix_sock(int sock
)
250 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
260 * ustcomm_close_unix_sock
262 * Shutdown cleanly a unix socket.
264 * Handles fd tracker internally.
266 int ustcomm_close_unix_sock(int sock
)
270 lttng_ust_lock_fd_tracker();
273 lttng_ust_delete_fd_from_tracker(sock
);
278 lttng_ust_unlock_fd_tracker();
284 * ustcomm_recv_unix_sock
286 * Receive data of size len in put that data into
287 * the buf param. Using recvmsg API.
288 * Return the size of received data.
289 * Return 0 on orderly shutdown.
291 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
298 memset(&msg
, 0, sizeof(msg
));
300 iov
[0].iov_base
= buf
;
301 iov
[0].iov_len
= len
;
306 len_last
= iov
[0].iov_len
;
307 ret
= recvmsg(sock
, &msg
, 0);
309 iov
[0].iov_base
+= ret
;
310 iov
[0].iov_len
-= ret
;
311 assert(ret
<= len_last
);
313 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
318 if (errno
!= EPIPE
&& errno
!= ECONNRESET
&& errno
!= ECONNREFUSED
)
321 if (ret
== -ECONNRESET
|| ret
== -ECONNREFUSED
)
324 shutret
= shutdown(sock
, SHUT_RDWR
);
326 ERR("Socket shutdown error");
327 } else if (ret
> 0) {
330 /* ret = 0 means an orderly shutdown. */
336 * ustcomm_send_unix_sock
338 * Send buf data of size len. Using sendmsg API.
339 * Return the size of sent data.
341 ssize_t
ustcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
347 memset(&msg
, 0, sizeof(msg
));
349 iov
[0].iov_base
= (void *) buf
;
350 iov
[0].iov_len
= len
;
355 * Using the MSG_NOSIGNAL when sending data from sessiond to
356 * libust, so libust does not receive an unhandled SIGPIPE or
357 * SIGURG. The sessiond receiver side can be made more resilient
358 * by ignoring SIGPIPE, but we don't have this luxury on the
362 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
363 } while (ret
< 0 && errno
== EINTR
);
368 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
371 if (ret
== -ECONNRESET
)
374 shutret
= shutdown(sock
, SHUT_RDWR
);
376 ERR("Socket shutdown error");
383 * Send a message accompanied by fd(s) over a unix socket.
385 * Returns the size of data sent, or negative error value.
387 ssize_t
ustcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
390 struct cmsghdr
*cmptr
;
393 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
394 char tmp
[CMSG_SPACE(sizeof_fds
)];
397 memset(&msg
, 0, sizeof(msg
));
398 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
400 if (nb_fd
> USTCOMM_MAX_SEND_FDS
)
403 msg
.msg_control
= (caddr_t
)tmp
;
404 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
406 cmptr
= CMSG_FIRSTHDR(&msg
);
409 cmptr
->cmsg_level
= SOL_SOCKET
;
410 cmptr
->cmsg_type
= SCM_RIGHTS
;
411 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
412 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
413 /* Sum of the length of all control messages in the buffer: */
414 msg
.msg_controllen
= cmptr
->cmsg_len
;
416 iov
[0].iov_base
= &dummy
;
422 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
423 } while (ret
< 0 && errno
== EINTR
);
426 * We consider EPIPE and ECONNRESET as expected.
428 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
432 if (ret
== -ECONNRESET
)
439 * Recv a message accompanied by fd(s) from a unix socket.
441 * Returns the size of received data, or negative error value.
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
)];
457 memset(&msg
, 0, sizeof(msg
));
459 /* Prepare to receive the structures */
460 iov
[0].iov_base
= &dummy
;
464 msg
.msg_control
= recv_fd
;
465 msg
.msg_controllen
= sizeof(recv_fd
);
468 ret
= recvmsg(sock
, &msg
, 0);
469 } while (ret
< 0 && errno
== EINTR
);
471 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
472 PERROR("recvmsg fds");
475 if (ret
== -ECONNRESET
)
480 /* orderly shutdown */
485 ERR("Error: Received %zd bytes, expected %d\n",
489 if (msg
.msg_flags
& MSG_CTRUNC
) {
490 ERR("Error: Control message truncated.\n");
494 cmsg
= CMSG_FIRSTHDR(&msg
);
496 ERR("Error: Invalid control message header\n");
500 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
501 ERR("Didn't received any fd\n");
505 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
506 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
507 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
511 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
517 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
521 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
529 ERR("incorrect message size: %zd\n", len
);
536 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
537 uint32_t expected_handle
, uint32_t expected_cmd
)
541 memset(lur
, 0, sizeof(*lur
));
542 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
544 case 0: /* orderly shutdown */
550 if (lur
->handle
!= expected_handle
) {
551 ERR("Unexpected result message handle: "
552 "expected: %u vs received: %u\n",
553 expected_handle
, lur
->handle
);
556 if (lur
->cmd
!= expected_cmd
) {
557 ERR("Unexpected result message command "
558 "expected: %u vs received: %u\n",
559 expected_cmd
, lur
->cmd
);
565 return lur
->ret_code
;
570 ERR("incorrect message size: %zd\n", len
);
576 int ustcomm_send_app_cmd(int sock
,
577 struct ustcomm_ust_msg
*lum
,
578 struct ustcomm_ust_reply
*lur
)
582 ret
= ustcomm_send_app_msg(sock
, lum
);
585 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
592 * chan_data is allocated internally if this function returns the
595 ssize_t
ustcomm_recv_channel_from_sessiond(int sock
,
596 void **_chan_data
, uint64_t var_len
,
603 if (var_len
> LTTNG_UST_CHANNEL_DATA_MAX_LEN
) {
607 /* Receive variable length data */
608 chan_data
= zmalloc(var_len
);
613 len
= ustcomm_recv_unix_sock(sock
, chan_data
, var_len
);
614 if (len
!= var_len
) {
618 lttng_ust_lock_fd_tracker();
619 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
621 lttng_ust_unlock_fd_tracker();
631 ret
= lttng_ust_add_fd_to_tracker(wakeup_fd
);
633 lttng_ust_unlock_fd_tracker();
634 ret
= close(wakeup_fd
);
636 PERROR("close on wakeup_fd");
643 lttng_ust_unlock_fd_tracker();
645 *_chan_data
= chan_data
;
655 int ustcomm_recv_stream_from_sessiond(int sock
,
656 uint64_t *memory_map_size
,
657 int *shm_fd
, int *wakeup_fd
)
663 /* recv shm fd and wakeup fd */
664 lttng_ust_lock_fd_tracker();
665 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
667 lttng_ust_unlock_fd_tracker();
677 ret
= lttng_ust_add_fd_to_tracker(fds
[0]);
679 lttng_ust_unlock_fd_tracker();
682 PERROR("close on received shm_fd");
689 ret
= lttng_ust_add_fd_to_tracker(fds
[1]);
691 lttng_ust_unlock_fd_tracker();
692 ret
= close(*shm_fd
);
694 PERROR("close on shm_fd");
699 PERROR("close on received wakeup_fd");
705 lttng_ust_unlock_fd_tracker();
713 * Returns 0 on success, negative error value on error.
715 int ustcomm_send_reg_msg(int sock
,
716 enum ustctl_socket_type type
,
717 uint32_t bits_per_long
,
718 uint32_t uint8_t_alignment
,
719 uint32_t uint16_t_alignment
,
720 uint32_t uint32_t_alignment
,
721 uint32_t uint64_t_alignment
,
722 uint32_t long_alignment
)
725 struct ustctl_reg_msg reg_msg
;
727 reg_msg
.magic
= LTTNG_UST_COMM_MAGIC
;
728 reg_msg
.major
= LTTNG_UST_ABI_MAJOR_VERSION
;
729 reg_msg
.minor
= LTTNG_UST_ABI_MINOR_VERSION
;
730 reg_msg
.pid
= getpid();
731 reg_msg
.ppid
= getppid();
732 reg_msg
.uid
= getuid();
733 reg_msg
.gid
= getgid();
734 reg_msg
.bits_per_long
= bits_per_long
;
735 reg_msg
.uint8_t_alignment
= uint8_t_alignment
;
736 reg_msg
.uint16_t_alignment
= uint16_t_alignment
;
737 reg_msg
.uint32_t_alignment
= uint32_t_alignment
;
738 reg_msg
.uint64_t_alignment
= uint64_t_alignment
;
739 reg_msg
.long_alignment
= long_alignment
;
740 reg_msg
.socket_type
= type
;
741 lttng_ust_getprocname(reg_msg
.name
);
742 memset(reg_msg
.padding
, 0, sizeof(reg_msg
.padding
));
744 len
= ustcomm_send_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
745 if (len
> 0 && len
!= sizeof(reg_msg
))
753 ssize_t
count_one_type(const struct lttng_type
*lt
)
764 //TODO: implement non-empty struct.
768 const struct lttng_event_field
*choices
;
772 ret
= lttng_ust_dynamic_type_choices(&nr_choices
,
777 * One field for enum, one field for variant, and
778 * one field per choice.
780 return count_fields_recursive(nr_choices
, choices
) + 2;
789 ssize_t
count_fields_recursive(size_t nr_fields
,
790 const struct lttng_event_field
*lttng_fields
)
793 ssize_t ret
, count
= 0;
795 for (i
= 0; i
< nr_fields
; i
++) {
796 const struct lttng_event_field
*lf
;
798 lf
= <tng_fields
[i
];
799 /* skip 'nowrite' fields */
802 ret
= count_one_type(&lf
->type
);
804 return ret
; /* error */
811 ssize_t
count_ctx_fields_recursive(size_t nr_fields
,
812 const struct lttng_ctx_field
*lttng_fields
)
815 ssize_t ret
, count
= 0;
817 for (i
= 0; i
< nr_fields
; i
++) {
818 const struct lttng_event_field
*lf
;
820 lf
= <tng_fields
[i
].event_field
;
821 /* skip 'nowrite' fields */
824 ret
= count_one_type(&lf
->type
);
826 return ret
; /* error */
833 int serialize_string_encoding(int32_t *ue
,
834 enum lttng_string_encodings le
)
837 case lttng_encode_none
:
838 *ue
= ustctl_encode_none
;
840 case lttng_encode_UTF8
:
841 *ue
= ustctl_encode_UTF8
;
843 case lttng_encode_ASCII
:
844 *ue
= ustctl_encode_ASCII
;
853 int serialize_integer_type(struct ustctl_integer_type
*uit
,
854 const struct lttng_integer_type
*lit
)
858 uit
->size
= lit
->size
;
859 uit
->signedness
= lit
->signedness
;
860 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
861 uit
->base
= lit
->base
;
862 if (serialize_string_encoding(&encoding
, lit
->encoding
))
864 uit
->encoding
= encoding
;
865 uit
->alignment
= lit
->alignment
;
870 int serialize_basic_type(struct lttng_session
*session
,
871 enum ustctl_abstract_types
*uatype
,
872 enum lttng_abstract_types atype
,
873 union _ustctl_basic_type
*ubt
,
874 const union _lttng_basic_type
*lbt
)
879 if (serialize_integer_type(&ubt
->integer
, &lbt
->integer
))
881 *uatype
= ustctl_atype_integer
;
888 if (serialize_string_encoding(&encoding
, lbt
->string
.encoding
))
890 ubt
->string
.encoding
= encoding
;
891 *uatype
= ustctl_atype_string
;
896 struct ustctl_float_type
*uft
;
897 const struct lttng_float_type
*lft
;
901 uft
->exp_dig
= lft
->exp_dig
;
902 uft
->mant_dig
= lft
->mant_dig
;
903 uft
->alignment
= lft
->alignment
;
904 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
905 *uatype
= ustctl_atype_float
;
910 strncpy(ubt
->enumeration
.name
, lbt
->enumeration
.desc
->name
,
911 LTTNG_UST_SYM_NAME_LEN
);
912 ubt
->enumeration
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
913 if (serialize_integer_type(&ubt
->enumeration
.container_type
,
914 &lbt
->enumeration
.container_type
))
917 const struct lttng_enum
*_enum
;
919 _enum
= lttng_ust_enum_get(session
,
920 lbt
->enumeration
.desc
->name
);
923 ubt
->enumeration
.id
= _enum
->id
;
925 ubt
->enumeration
.id
= -1ULL;
927 *uatype
= ustctl_atype_enum
;
939 int serialize_dynamic_type(struct lttng_session
*session
,
940 struct ustctl_field
*fields
, size_t *iter_output
,
941 const struct lttng_event_field
*lf
)
943 const struct lttng_event_field
*choices
;
944 char tag_field_name
[LTTNG_UST_SYM_NAME_LEN
];
945 const struct lttng_type
*tag_type
;
946 const struct lttng_event_field
*tag_field_generic
;
947 struct lttng_event_field tag_field
= {
948 .name
= tag_field_name
,
951 struct ustctl_field
*uf
;
952 size_t nr_choices
, i
;
955 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
956 tag_type
= &tag_field_generic
->type
;
958 /* Serialize enum field. */
959 strncpy(tag_field_name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
960 tag_field_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
961 strncat(tag_field_name
,
963 LTTNG_UST_SYM_NAME_LEN
- strlen(tag_field_name
) - 1);
964 tag_field
.type
= *tag_type
;
965 ret
= serialize_one_field(session
, fields
, iter_output
,
970 /* Serialize variant field. */
971 uf
= &fields
[*iter_output
];
972 ret
= lttng_ust_dynamic_type_choices(&nr_choices
, &choices
);
976 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
977 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
978 uf
->type
.atype
= ustctl_atype_variant
;
979 uf
->type
.u
.variant
.nr_choices
= nr_choices
;
980 strncpy(uf
->type
.u
.variant
.tag_name
,
982 LTTNG_UST_SYM_NAME_LEN
);
983 uf
->type
.u
.variant
.tag_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
986 /* Serialize choice fields after variant. */
987 for (i
= 0; i
< nr_choices
; i
++) {
988 ret
= serialize_one_field(session
, fields
,
989 iter_output
, &choices
[i
]);
997 int serialize_one_field(struct lttng_session
*session
,
998 struct ustctl_field
*fields
, size_t *iter_output
,
999 const struct lttng_event_field
*lf
)
1001 const struct lttng_type
*lt
= &lf
->type
;
1004 /* skip 'nowrite' fields */
1008 switch (lt
->atype
) {
1014 struct ustctl_field
*uf
= &fields
[*iter_output
];
1015 struct ustctl_type
*ut
= &uf
->type
;
1016 enum ustctl_abstract_types atype
;
1018 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1019 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1020 ret
= serialize_basic_type(session
, &atype
, lt
->atype
,
1021 &ut
->u
.basic
, <
->u
.basic
);
1030 struct ustctl_field
*uf
= &fields
[*iter_output
];
1031 struct ustctl_type
*ut
= &uf
->type
;
1032 struct ustctl_basic_type
*ubt
;
1033 const struct lttng_basic_type
*lbt
;
1034 enum ustctl_abstract_types atype
;
1036 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1037 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1038 uf
->type
.atype
= ustctl_atype_array
;
1039 ubt
= &ut
->u
.array
.elem_type
;
1040 lbt
= <
->u
.array
.elem_type
;
1041 ut
->u
.array
.length
= lt
->u
.array
.length
;
1042 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1043 &ubt
->u
.basic
, &lbt
->u
.basic
);
1047 ut
->atype
= ustctl_atype_array
;
1051 case atype_sequence
:
1053 struct ustctl_field
*uf
= &fields
[*iter_output
];
1054 struct ustctl_type
*ut
= &uf
->type
;
1055 struct ustctl_basic_type
*ubt
;
1056 const struct lttng_basic_type
*lbt
;
1057 enum ustctl_abstract_types atype
;
1060 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1061 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1062 uf
->type
.atype
= ustctl_atype_sequence
;
1063 ubt
= &ut
->u
.sequence
.length_type
;
1064 lbt
= <
->u
.sequence
.length_type
;
1065 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1066 &ubt
->u
.basic
, &lbt
->u
.basic
);
1070 ubt
= &ut
->u
.sequence
.elem_type
;
1071 lbt
= <
->u
.sequence
.elem_type
;
1072 ret
= serialize_basic_type(session
, &atype
, lbt
->atype
,
1073 &ubt
->u
.basic
, &lbt
->u
.basic
);
1077 ut
->atype
= ustctl_atype_sequence
;
1083 ret
= serialize_dynamic_type(session
, fields
, iter_output
, lf
);
1090 struct ustctl_field
*uf
= &fields
[*iter_output
];
1093 * TODO: add support for non-empty struct.
1095 if (lf
->type
.u
._struct
.nr_fields
!= 0) {
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_struct
;
1101 uf
->type
.u
._struct
.nr_fields
= 0;
1112 int serialize_fields(struct lttng_session
*session
,
1113 size_t *_nr_write_fields
,
1114 struct ustctl_field
**ustctl_fields
,
1116 const struct lttng_event_field
*lttng_fields
)
1118 struct ustctl_field
*fields
;
1120 size_t i
, iter_output
= 0;
1121 ssize_t nr_write_fields
;
1123 nr_write_fields
= count_fields_recursive(nr_fields
, lttng_fields
);
1124 if (nr_write_fields
< 0) {
1125 return (int) nr_write_fields
;
1128 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1132 for (i
= 0; i
< nr_fields
; i
++) {
1133 ret
= serialize_one_field(session
, fields
, &iter_output
,
1139 *_nr_write_fields
= nr_write_fields
;
1140 *ustctl_fields
= fields
;
1149 int serialize_entries(struct ustctl_enum_entry
**_entries
,
1151 const struct lttng_enum_entry
*lttng_entries
)
1153 struct ustctl_enum_entry
*entries
;
1156 /* Serialize the entries */
1157 entries
= zmalloc(nr_entries
* sizeof(*entries
));
1160 for (i
= 0; i
< nr_entries
; i
++) {
1161 struct ustctl_enum_entry
*uentry
;
1162 const struct lttng_enum_entry
*lentry
;
1164 uentry
= &entries
[i
];
1165 lentry
= <tng_entries
[i
];
1167 uentry
->start
.value
= lentry
->start
.value
;
1168 uentry
->start
.signedness
= lentry
->start
.signedness
;
1169 uentry
->end
.value
= lentry
->end
.value
;
1170 uentry
->end
.signedness
= lentry
->end
.signedness
;
1171 strncpy(uentry
->string
, lentry
->string
, LTTNG_UST_SYM_NAME_LEN
);
1172 uentry
->string
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1174 if (lentry
->u
.extra
.options
& LTTNG_ENUM_ENTRY_OPTION_IS_AUTO
) {
1175 uentry
->u
.extra
.options
|=
1176 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO
;
1179 *_entries
= entries
;
1184 int serialize_ctx_fields(struct lttng_session
*session
,
1185 size_t *_nr_write_fields
,
1186 struct ustctl_field
**ustctl_fields
,
1188 const struct lttng_ctx_field
*lttng_fields
)
1190 struct ustctl_field
*fields
;
1192 size_t i
, iter_output
= 0;
1193 ssize_t nr_write_fields
;
1195 nr_write_fields
= count_ctx_fields_recursive(nr_fields
,
1197 if (nr_write_fields
< 0) {
1198 return (int) nr_write_fields
;
1201 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1205 for (i
= 0; i
< nr_fields
; i
++) {
1206 ret
= serialize_one_field(session
, fields
, &iter_output
,
1207 <tng_fields
[i
].event_field
);
1212 *_nr_write_fields
= nr_write_fields
;
1213 *ustctl_fields
= fields
;
1222 * Returns 0 on success, negative error value on error.
1224 int ustcomm_register_event(int sock
,
1225 struct lttng_session
*session
,
1226 int session_objd
, /* session descriptor */
1227 int channel_objd
, /* channel descriptor */
1228 const char *event_name
, /* event name (input) */
1230 const char *signature
, /* event signature (input) */
1231 size_t nr_fields
, /* fields */
1232 const struct lttng_event_field
*lttng_fields
,
1233 const char *model_emf_uri
,
1234 uint32_t *id
) /* event id (output) */
1238 struct ustcomm_notify_hdr header
;
1239 struct ustcomm_notify_event_msg m
;
1242 struct ustcomm_notify_hdr header
;
1243 struct ustcomm_notify_event_reply r
;
1245 size_t signature_len
, fields_len
, model_emf_uri_len
;
1246 struct ustctl_field
*fields
= NULL
;
1247 size_t nr_write_fields
= 0;
1250 memset(&msg
, 0, sizeof(msg
));
1251 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1252 msg
.m
.session_objd
= session_objd
;
1253 msg
.m
.channel_objd
= channel_objd
;
1254 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_SYM_NAME_LEN
);
1255 msg
.m
.event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1256 msg
.m
.loglevel
= loglevel
;
1257 signature_len
= strlen(signature
) + 1;
1258 msg
.m
.signature_len
= signature_len
;
1260 /* Calculate fields len, serialize fields. */
1261 if (nr_fields
> 0) {
1262 ret
= serialize_fields(session
, &nr_write_fields
, &fields
,
1263 nr_fields
, lttng_fields
);
1268 fields_len
= sizeof(*fields
) * nr_write_fields
;
1269 msg
.m
.fields_len
= fields_len
;
1270 if (model_emf_uri
) {
1271 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
1273 model_emf_uri_len
= 0;
1275 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
1277 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1278 if (len
> 0 && len
!= sizeof(msg
)) {
1287 /* send signature */
1288 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
1289 if (len
> 0 && len
!= signature_len
) {
1299 if (fields_len
> 0) {
1300 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1301 if (len
> 0 && len
!= fields_len
) {
1312 if (model_emf_uri_len
) {
1313 /* send model_emf_uri */
1314 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
1316 if (len
> 0 && len
!= model_emf_uri_len
) {
1325 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1327 case 0: /* orderly shutdown */
1330 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1331 ERR("Unexpected result message command "
1332 "expected: %u vs received: %u\n",
1333 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1336 if (reply
.r
.ret_code
> 0)
1338 if (reply
.r
.ret_code
< 0)
1339 return reply
.r
.ret_code
;
1340 *id
= reply
.r
.event_id
;
1341 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1342 event_name
, reply
.r
.ret_code
, reply
.r
.event_id
);
1346 /* Transport level error */
1347 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1351 ERR("incorrect message size: %zd\n", len
);
1357 /* Error path only. */
1364 * Returns 0 on success, negative error value on error.
1365 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1367 int ustcomm_register_enum(int sock
,
1368 int session_objd
, /* session descriptor */
1369 const char *enum_name
, /* enum name (input) */
1370 size_t nr_entries
, /* entries */
1371 const struct lttng_enum_entry
*lttng_entries
,
1376 struct ustcomm_notify_hdr header
;
1377 struct ustcomm_notify_enum_msg m
;
1380 struct ustcomm_notify_hdr header
;
1381 struct ustcomm_notify_enum_reply r
;
1384 struct ustctl_enum_entry
*entries
= NULL
;
1387 memset(&msg
, 0, sizeof(msg
));
1388 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1389 msg
.m
.session_objd
= session_objd
;
1390 strncpy(msg
.m
.enum_name
, enum_name
, LTTNG_UST_SYM_NAME_LEN
);
1391 msg
.m
.enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1393 /* Calculate entries len, serialize entries. */
1394 if (nr_entries
> 0) {
1395 ret
= serialize_entries(&entries
,
1396 nr_entries
, lttng_entries
);
1401 entries_len
= sizeof(*entries
) * nr_entries
;
1402 msg
.m
.entries_len
= entries_len
;
1404 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1405 if (len
> 0 && len
!= sizeof(msg
)) {
1415 if (entries_len
> 0) {
1416 len
= ustcomm_send_unix_sock(sock
, entries
, entries_len
);
1417 if (len
> 0 && len
!= entries_len
) {
1430 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1432 case 0: /* orderly shutdown */
1435 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1436 ERR("Unexpected result message command "
1437 "expected: %u vs received: %u\n",
1438 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1441 if (reply
.r
.ret_code
> 0)
1443 if (reply
.r
.ret_code
< 0)
1444 return reply
.r
.ret_code
;
1445 *id
= reply
.r
.enum_id
;
1446 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1447 enum_name
, reply
.r
.ret_code
);
1451 /* Transport level error */
1452 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1456 ERR("incorrect message size: %zd\n", len
);
1468 * Returns 0 on success, negative error value on error.
1469 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1471 int ustcomm_register_channel(int sock
,
1472 struct lttng_session
*session
,
1473 int session_objd
, /* session descriptor */
1474 int channel_objd
, /* channel descriptor */
1475 size_t nr_ctx_fields
,
1476 const struct lttng_ctx_field
*ctx_fields
,
1477 uint32_t *chan_id
, /* channel id (output) */
1478 int *header_type
) /* header type (output) */
1482 struct ustcomm_notify_hdr header
;
1483 struct ustcomm_notify_channel_msg m
;
1486 struct ustcomm_notify_hdr header
;
1487 struct ustcomm_notify_channel_reply r
;
1490 struct ustctl_field
*fields
= NULL
;
1492 size_t nr_write_fields
= 0;
1494 memset(&msg
, 0, sizeof(msg
));
1495 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1496 msg
.m
.session_objd
= session_objd
;
1497 msg
.m
.channel_objd
= channel_objd
;
1499 /* Calculate fields len, serialize fields. */
1500 if (nr_ctx_fields
> 0) {
1501 ret
= serialize_ctx_fields(session
, &nr_write_fields
, &fields
,
1502 nr_ctx_fields
, ctx_fields
);
1507 fields_len
= sizeof(*fields
) * nr_write_fields
;
1508 msg
.m
.ctx_fields_len
= fields_len
;
1509 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1510 if (len
> 0 && len
!= sizeof(msg
)) {
1520 if (fields_len
> 0) {
1521 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1523 if (len
> 0 && len
!= fields_len
) {
1533 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1535 case 0: /* orderly shutdown */
1538 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1539 ERR("Unexpected result message command "
1540 "expected: %u vs received: %u\n",
1541 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1544 if (reply
.r
.ret_code
> 0)
1546 if (reply
.r
.ret_code
< 0)
1547 return reply
.r
.ret_code
;
1548 *chan_id
= reply
.r
.chan_id
;
1549 switch (reply
.r
.header_type
) {
1552 *header_type
= reply
.r
.header_type
;
1555 ERR("Unexpected channel header type %u\n",
1556 reply
.r
.header_type
);
1559 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1560 reply
.r
.chan_id
, reply
.r
.header_type
);
1564 /* Transport level error */
1565 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1569 ERR("incorrect message size: %zd\n", len
);
1576 * Set socket reciving timeout.
1578 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1583 tv
.tv_sec
= msec
/ 1000;
1584 tv
.tv_usec
= (msec
* 1000 % 1000000);
1586 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1588 PERROR("setsockopt SO_RCVTIMEO");
1596 * Set socket sending timeout.
1598 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1603 tv
.tv_sec
= msec
/ 1000;
1604 tv
.tv_usec
= (msec
* 1000) % 1000000;
1606 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1608 PERROR("setsockopt SO_SNDTIMEO");