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>
37 #include <lttng/ust-error.h>
38 #include <lttng/ust-events.h>
39 #include <lttng/ust-dynamic-type.h>
40 #include <usterr-signal-safe.h>
42 #include "../liblttng-ust/compat.h"
44 #define USTCOMM_CODE_OFFSET(code) \
45 (code == LTTNG_UST_OK ? 0 : (code - LTTNG_UST_ERR + 1))
47 #define USTCOMM_MAX_SEND_FDS 4
50 ssize_t
count_fields_recursive(size_t nr_fields
,
51 const struct lttng_event_field
*lttng_fields
);
53 int serialize_one_field(struct lttng_session
*session
,
54 struct ustctl_field
*fields
, size_t *iter_output
,
55 const struct lttng_event_field
*lf
);
58 * Human readable error message.
60 static const char *ustcomm_readable_code
[] = {
61 [ USTCOMM_CODE_OFFSET(LTTNG_UST_OK
) ] = "Success",
62 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR
) ] = "Unknown error",
63 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOENT
) ] = "No entry",
64 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXIST
) ] = "Object already exists",
65 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL
) ] = "Invalid argument",
66 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PERM
) ] = "Permission denied",
67 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOSYS
) ] = "Not implemented",
68 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXITING
) ] = "Process is exiting",
70 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_MAGIC
) ] = "Invalid magic number",
71 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_SOCKET_TYPE
) ] = "Invalid socket type",
72 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_UNSUP_MAJOR
) ] = "Unsupported major version",
78 * Receives positive error value.
79 * Return ptr to string representing a human readable
80 * error code from the ustcomm_return_code enum.
82 const char *lttng_ust_strerror(int code
)
84 if (code
== LTTNG_UST_OK
)
85 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
86 if (code
< LTTNG_UST_ERR
)
87 return strerror(code
);
88 if (code
>= LTTNG_UST_ERR_NR
)
90 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
94 * ustcomm_connect_unix_sock
96 * Connect to unix socket using the path name.
98 int ustcomm_connect_unix_sock(const char *pathname
)
100 struct sockaddr_un sun
;
104 * libust threads require the close-on-exec flag for all
105 * resources so it does not leak file descriptors upon exec.
107 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
113 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
120 memset(&sun
, 0, sizeof(sun
));
121 sun
.sun_family
= AF_UNIX
;
122 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
123 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
125 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
128 * Don't print message on connect ENOENT error, because
129 * connect is used in normal execution to detect if
130 * sessiond is alive. ENOENT is when the unix socket
131 * file does not exist, and ECONNREFUSED is when the
132 * file exists but no sessiond is listening.
134 if (errno
!= ECONNREFUSED
&& errno
!= ECONNRESET
135 && errno
!= ENOENT
&& errno
!= EACCES
)
138 if (ret
== -ECONNREFUSED
|| ret
== -ECONNRESET
)
150 closeret
= close(fd
);
159 * ustcomm_accept_unix_sock
161 * Do an accept(2) on the sock and return the
162 * new file descriptor. The socket MUST be bind(2) before.
164 int ustcomm_accept_unix_sock(int sock
)
167 struct sockaddr_un sun
;
171 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
173 if (errno
!= ECONNABORTED
)
176 if (new_fd
== -ECONNABORTED
)
183 * ustcomm_create_unix_sock
185 * Creates a AF_UNIX local socket using pathname
186 * bind the socket upon creation and return the fd.
188 int ustcomm_create_unix_sock(const char *pathname
)
190 struct sockaddr_un sun
;
193 /* Create server socket */
194 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
200 memset(&sun
, 0, sizeof(sun
));
201 sun
.sun_family
= AF_UNIX
;
202 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
203 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
205 /* Unlink the old file if present */
206 (void) unlink(pathname
);
207 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
220 closeret
= close(fd
);
230 * ustcomm_listen_unix_sock
232 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
234 int ustcomm_listen_unix_sock(int sock
)
238 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
248 * ustcomm_close_unix_sock
250 * Shutdown cleanly a unix socket.
252 int ustcomm_close_unix_sock(int sock
)
266 * ustcomm_recv_unix_sock
268 * Receive data of size len in put that data into
269 * the buf param. Using recvmsg API.
270 * Return the size of received data.
271 * Return 0 on orderly shutdown.
273 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
280 memset(&msg
, 0, sizeof(msg
));
282 iov
[0].iov_base
= buf
;
283 iov
[0].iov_len
= len
;
288 len_last
= iov
[0].iov_len
;
289 ret
= recvmsg(sock
, &msg
, 0);
291 iov
[0].iov_base
+= ret
;
292 iov
[0].iov_len
-= ret
;
293 assert(ret
<= len_last
);
295 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
300 if (errno
!= EPIPE
&& errno
!= ECONNRESET
&& errno
!= ECONNREFUSED
)
303 if (ret
== -ECONNRESET
|| ret
== -ECONNREFUSED
)
306 shutret
= shutdown(sock
, SHUT_RDWR
);
308 ERR("Socket shutdown error");
309 } else if (ret
> 0) {
312 /* ret = 0 means an orderly shutdown. */
318 * ustcomm_send_unix_sock
320 * Send buf data of size len. Using sendmsg API.
321 * Return the size of sent data.
323 ssize_t
ustcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
329 memset(&msg
, 0, sizeof(msg
));
331 iov
[0].iov_base
= (void *) buf
;
332 iov
[0].iov_len
= len
;
337 * Using the MSG_NOSIGNAL when sending data from sessiond to
338 * libust, so libust does not receive an unhandled SIGPIPE or
339 * SIGURG. The sessiond receiver side can be made more resilient
340 * by ignoring SIGPIPE, but we don't have this luxury on the
344 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
345 } while (ret
< 0 && errno
== EINTR
);
350 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
353 if (ret
== -ECONNRESET
)
356 shutret
= shutdown(sock
, SHUT_RDWR
);
358 ERR("Socket shutdown error");
365 * Send a message accompanied by fd(s) over a unix socket.
367 * Returns the size of data sent, or negative error value.
369 ssize_t
ustcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
372 struct cmsghdr
*cmptr
;
375 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
376 char tmp
[CMSG_SPACE(sizeof_fds
)];
379 memset(&msg
, 0, sizeof(msg
));
380 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
382 if (nb_fd
> USTCOMM_MAX_SEND_FDS
)
385 msg
.msg_control
= (caddr_t
)tmp
;
386 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
388 cmptr
= CMSG_FIRSTHDR(&msg
);
391 cmptr
->cmsg_level
= SOL_SOCKET
;
392 cmptr
->cmsg_type
= SCM_RIGHTS
;
393 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
394 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
395 /* Sum of the length of all control messages in the buffer: */
396 msg
.msg_controllen
= cmptr
->cmsg_len
;
398 iov
[0].iov_base
= &dummy
;
404 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
405 } while (ret
< 0 && errno
== EINTR
);
408 * We consider EPIPE and ECONNRESET as expected.
410 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
414 if (ret
== -ECONNRESET
)
421 * Recv a message accompanied by fd(s) from a unix socket.
423 * Returns the size of received data, or negative error value.
425 * Expect at most "nb_fd" file descriptors. Returns the number of fd
426 * actually received in nb_fd.
427 * Returns -EPIPE on orderly shutdown.
429 ssize_t
ustcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
433 struct cmsghdr
*cmsg
;
434 size_t sizeof_fds
= nb_fd
* sizeof(int);
435 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
439 memset(&msg
, 0, sizeof(msg
));
441 /* Prepare to receive the structures */
442 iov
[0].iov_base
= &dummy
;
446 msg
.msg_control
= recv_fd
;
447 msg
.msg_controllen
= sizeof(recv_fd
);
450 ret
= recvmsg(sock
, &msg
, 0);
451 } while (ret
< 0 && errno
== EINTR
);
453 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
454 PERROR("recvmsg fds");
457 if (ret
== -ECONNRESET
)
462 /* orderly shutdown */
467 ERR("Error: Received %zd bytes, expected %d\n",
471 if (msg
.msg_flags
& MSG_CTRUNC
) {
472 ERR("Error: Control message truncated.\n");
476 cmsg
= CMSG_FIRSTHDR(&msg
);
478 ERR("Error: Invalid control message header\n");
482 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
483 ERR("Didn't received any fd\n");
487 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
488 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
489 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
493 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
499 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
503 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
511 ERR("incorrect message size: %zd\n", len
);
518 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
519 uint32_t expected_handle
, uint32_t expected_cmd
)
523 memset(lur
, 0, sizeof(*lur
));
524 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
526 case 0: /* orderly shutdown */
532 if (lur
->handle
!= expected_handle
) {
533 ERR("Unexpected result message handle: "
534 "expected: %u vs received: %u\n",
535 expected_handle
, lur
->handle
);
538 if (lur
->cmd
!= expected_cmd
) {
539 ERR("Unexpected result message command "
540 "expected: %u vs received: %u\n",
541 expected_cmd
, lur
->cmd
);
547 return lur
->ret_code
;
552 ERR("incorrect message size: %zd\n", len
);
558 int ustcomm_send_app_cmd(int sock
,
559 struct ustcomm_ust_msg
*lum
,
560 struct ustcomm_ust_reply
*lur
)
564 ret
= ustcomm_send_app_msg(sock
, lum
);
567 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
574 * chan_data is allocated internally if this function returns the
577 ssize_t
ustcomm_recv_channel_from_sessiond(int sock
,
578 void **_chan_data
, uint64_t var_len
,
585 if (var_len
> LTTNG_UST_CHANNEL_DATA_MAX_LEN
) {
589 /* Receive variable length data */
590 chan_data
= zmalloc(var_len
);
595 len
= ustcomm_recv_unix_sock(sock
, chan_data
, var_len
);
596 if (len
!= var_len
) {
600 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
610 *_wakeup_fd
= wakeup_fd
;
611 *_chan_data
= chan_data
;
621 int ustcomm_recv_stream_from_sessiond(int sock
,
622 uint64_t *memory_map_size
,
623 int *shm_fd
, int *wakeup_fd
)
629 /* recv shm fd and wakeup fd */
630 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
649 * Returns 0 on success, negative error value on error.
651 int ustcomm_send_reg_msg(int sock
,
652 enum ustctl_socket_type type
,
653 uint32_t bits_per_long
,
654 uint32_t uint8_t_alignment
,
655 uint32_t uint16_t_alignment
,
656 uint32_t uint32_t_alignment
,
657 uint32_t uint64_t_alignment
,
658 uint32_t long_alignment
)
661 struct ustctl_reg_msg reg_msg
;
663 reg_msg
.magic
= LTTNG_UST_COMM_MAGIC
;
664 reg_msg
.major
= LTTNG_UST_ABI_MAJOR_VERSION
;
665 reg_msg
.minor
= LTTNG_UST_ABI_MINOR_VERSION
;
666 reg_msg
.pid
= getpid();
667 reg_msg
.ppid
= getppid();
668 reg_msg
.uid
= getuid();
669 reg_msg
.gid
= getgid();
670 reg_msg
.bits_per_long
= bits_per_long
;
671 reg_msg
.uint8_t_alignment
= uint8_t_alignment
;
672 reg_msg
.uint16_t_alignment
= uint16_t_alignment
;
673 reg_msg
.uint32_t_alignment
= uint32_t_alignment
;
674 reg_msg
.uint64_t_alignment
= uint64_t_alignment
;
675 reg_msg
.long_alignment
= long_alignment
;
676 reg_msg
.socket_type
= type
;
677 lttng_ust_getprocname(reg_msg
.name
);
678 memset(reg_msg
.padding
, 0, sizeof(reg_msg
.padding
));
680 len
= ustcomm_send_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
681 if (len
> 0 && len
!= sizeof(reg_msg
))
689 ssize_t
count_one_type(const struct lttng_type
*lt
)
700 //TODO: implement non-empty struct.
704 const struct lttng_event_field
*choices
;
708 ret
= lttng_ust_dynamic_type_choices(&nr_choices
,
713 * One field for enum, one field for variant, and
714 * one field per choice.
716 return count_fields_recursive(nr_choices
, choices
) + 2;
725 ssize_t
count_fields_recursive(size_t nr_fields
,
726 const struct lttng_event_field
*lttng_fields
)
729 ssize_t ret
, count
= 0;
731 for (i
= 0; i
< nr_fields
; i
++) {
732 const struct lttng_event_field
*lf
;
734 lf
= <tng_fields
[i
];
735 /* skip 'nowrite' fields */
738 ret
= count_one_type(&lf
->type
);
740 return ret
; /* error */
747 ssize_t
count_ctx_fields_recursive(size_t nr_fields
,
748 const struct lttng_ctx_field
*lttng_fields
)
751 ssize_t ret
, count
= 0;
753 for (i
= 0; i
< nr_fields
; i
++) {
754 const struct lttng_event_field
*lf
;
756 lf
= <tng_fields
[i
].event_field
;
757 /* skip 'nowrite' fields */
760 ret
= count_one_type(&lf
->type
);
762 return ret
; /* error */
769 int serialize_string_encoding(enum ustctl_string_encodings
*ue
,
770 enum lttng_string_encodings le
)
773 case lttng_encode_none
:
774 *ue
= ustctl_encode_none
;
776 case lttng_encode_UTF8
:
777 *ue
= ustctl_encode_UTF8
;
779 case lttng_encode_ASCII
:
780 *ue
= ustctl_encode_ASCII
;
789 int serialize_integer_type(struct ustctl_integer_type
*uit
,
790 const struct lttng_integer_type
*lit
)
792 uit
->size
= lit
->size
;
793 uit
->signedness
= lit
->signedness
;
794 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
795 uit
->base
= lit
->base
;
796 if (serialize_string_encoding(&uit
->encoding
, lit
->encoding
))
798 uit
->alignment
= lit
->alignment
;
803 int serialize_basic_type(struct lttng_session
*session
,
804 enum ustctl_abstract_types
*uatype
,
805 enum lttng_abstract_types atype
,
806 union _ustctl_basic_type
*ubt
,
807 const union _lttng_basic_type
*lbt
)
812 if (serialize_integer_type(&ubt
->integer
, &lbt
->integer
))
814 *uatype
= ustctl_atype_integer
;
819 if (serialize_string_encoding(&ubt
->string
.encoding
,
820 lbt
->string
.encoding
))
822 *uatype
= ustctl_atype_string
;
827 struct ustctl_float_type
*uft
;
828 const struct lttng_float_type
*lft
;
832 uft
->exp_dig
= lft
->exp_dig
;
833 uft
->mant_dig
= lft
->mant_dig
;
834 uft
->alignment
= lft
->alignment
;
835 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
836 *uatype
= ustctl_atype_float
;
841 strncpy(ubt
->enumeration
.name
, lbt
->enumeration
.desc
->name
,
842 LTTNG_UST_SYM_NAME_LEN
);
843 ubt
->enumeration
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
844 if (serialize_integer_type(&ubt
->enumeration
.container_type
,
845 &lbt
->enumeration
.container_type
))
848 const struct lttng_enum
*_enum
;
850 _enum
= lttng_ust_enum_get(session
,
851 lbt
->enumeration
.desc
->name
);
854 ubt
->enumeration
.id
= _enum
->id
;
856 ubt
->enumeration
.id
= -1ULL;
858 *uatype
= ustctl_atype_enum
;
870 int serialize_dynamic_type(struct lttng_session
*session
,
871 struct ustctl_field
*fields
, size_t *iter_output
,
872 const struct lttng_event_field
*lf
)
874 const struct lttng_event_field
*choices
;
875 char tag_field_name
[LTTNG_UST_SYM_NAME_LEN
];
876 const struct lttng_type
*tag_type
;
877 const struct lttng_event_field
*tag_field_generic
;
878 struct lttng_event_field tag_field
= {
879 .name
= tag_field_name
,
882 struct ustctl_field
*uf
;
883 size_t nr_choices
, i
;
886 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
887 tag_type
= &tag_field_generic
->type
;
889 /* Serialize enum field. */
890 strncpy(tag_field_name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
891 tag_field_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
892 strncat(tag_field_name
,
894 LTTNG_UST_SYM_NAME_LEN
- strlen(tag_field_name
) - 1);
895 tag_field
.type
= *tag_type
;
896 ret
= serialize_one_field(session
, fields
, iter_output
,
901 /* Serialize variant field. */
902 uf
= &fields
[*iter_output
];
903 ret
= lttng_ust_dynamic_type_choices(&nr_choices
, &choices
);
907 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
908 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
909 uf
->type
.atype
= ustctl_atype_variant
;
910 uf
->type
.u
.variant
.nr_choices
= nr_choices
;
911 strncpy(uf
->type
.u
.variant
.tag_name
,
913 LTTNG_UST_SYM_NAME_LEN
);
914 uf
->type
.u
.variant
.tag_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
917 /* Serialize choice fields after variant. */
918 for (i
= 0; i
< nr_choices
; i
++) {
919 ret
= serialize_one_field(session
, fields
,
920 iter_output
, &choices
[i
]);
928 int serialize_one_field(struct lttng_session
*session
,
929 struct ustctl_field
*fields
, size_t *iter_output
,
930 const struct lttng_event_field
*lf
)
932 const struct lttng_type
*lt
= &lf
->type
;
935 /* skip 'nowrite' fields */
945 struct ustctl_field
*uf
= &fields
[*iter_output
];
946 struct ustctl_type
*ut
= &uf
->type
;
948 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
949 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
950 ret
= serialize_basic_type(session
, &ut
->atype
, lt
->atype
,
951 &ut
->u
.basic
, <
->u
.basic
);
959 struct ustctl_field
*uf
= &fields
[*iter_output
];
960 struct ustctl_type
*ut
= &uf
->type
;
961 struct ustctl_basic_type
*ubt
;
962 const struct lttng_basic_type
*lbt
;
964 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
965 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
966 uf
->type
.atype
= ustctl_atype_array
;
967 ubt
= &ut
->u
.array
.elem_type
;
968 lbt
= <
->u
.array
.elem_type
;
969 ut
->u
.array
.length
= lt
->u
.array
.length
;
970 ret
= serialize_basic_type(session
, &ubt
->atype
, lbt
->atype
,
971 &ubt
->u
.basic
, &lbt
->u
.basic
);
974 ut
->atype
= ustctl_atype_array
;
980 struct ustctl_field
*uf
= &fields
[*iter_output
];
981 struct ustctl_type
*ut
= &uf
->type
;
982 struct ustctl_basic_type
*ubt
;
983 const struct lttng_basic_type
*lbt
;
986 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
987 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
988 uf
->type
.atype
= ustctl_atype_sequence
;
989 ubt
= &ut
->u
.sequence
.length_type
;
990 lbt
= <
->u
.sequence
.length_type
;
991 ret
= serialize_basic_type(session
, &ubt
->atype
, lbt
->atype
,
992 &ubt
->u
.basic
, &lbt
->u
.basic
);
995 ubt
= &ut
->u
.sequence
.elem_type
;
996 lbt
= <
->u
.sequence
.elem_type
;
997 ret
= serialize_basic_type(session
, &ubt
->atype
, lbt
->atype
,
998 &ubt
->u
.basic
, &lbt
->u
.basic
);
1001 ut
->atype
= ustctl_atype_sequence
;
1007 ret
= serialize_dynamic_type(session
, fields
, iter_output
, lf
);
1014 struct ustctl_field
*uf
= &fields
[*iter_output
];
1017 * TODO: add support for non-empty struct.
1019 if (lf
->type
.u
._struct
.nr_fields
!= 0) {
1022 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1023 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1024 uf
->type
.atype
= ustctl_atype_struct
;
1025 uf
->type
.u
._struct
.nr_fields
= 0;
1036 int serialize_fields(struct lttng_session
*session
,
1037 size_t *_nr_write_fields
,
1038 struct ustctl_field
**ustctl_fields
,
1040 const struct lttng_event_field
*lttng_fields
)
1042 struct ustctl_field
*fields
;
1044 size_t i
, iter_output
= 0;
1045 ssize_t nr_write_fields
;
1047 nr_write_fields
= count_fields_recursive(nr_fields
, lttng_fields
);
1048 if (nr_write_fields
< 0) {
1049 return (int) nr_write_fields
;
1052 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1056 for (i
= 0; i
< nr_fields
; i
++) {
1057 ret
= serialize_one_field(session
, fields
, &iter_output
,
1063 *_nr_write_fields
= nr_write_fields
;
1064 *ustctl_fields
= fields
;
1073 int serialize_entries(struct ustctl_enum_entry
**_entries
,
1075 const struct lttng_enum_entry
*lttng_entries
)
1077 struct ustctl_enum_entry
*entries
;
1080 /* Serialize the entries */
1081 entries
= zmalloc(nr_entries
* sizeof(*entries
));
1084 for (i
= 0; i
< nr_entries
; i
++) {
1085 struct ustctl_enum_entry
*uentry
;
1086 const struct lttng_enum_entry
*lentry
;
1088 uentry
= &entries
[i
];
1089 lentry
= <tng_entries
[i
];
1091 uentry
->start
.value
= lentry
->start
.value
;
1092 uentry
->start
.signedness
= lentry
->start
.signedness
;
1093 uentry
->end
.value
= lentry
->end
.value
;
1094 uentry
->end
.signedness
= lentry
->end
.signedness
;
1095 strncpy(uentry
->string
, lentry
->string
, LTTNG_UST_SYM_NAME_LEN
);
1096 uentry
->string
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1098 *_entries
= entries
;
1103 int serialize_ctx_fields(struct lttng_session
*session
,
1104 size_t *_nr_write_fields
,
1105 struct ustctl_field
**ustctl_fields
,
1107 const struct lttng_ctx_field
*lttng_fields
)
1109 struct ustctl_field
*fields
;
1111 size_t i
, iter_output
= 0;
1112 ssize_t nr_write_fields
;
1114 nr_write_fields
= count_ctx_fields_recursive(nr_fields
,
1116 if (nr_write_fields
< 0) {
1117 return (int) nr_write_fields
;
1120 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1124 for (i
= 0; i
< nr_fields
; i
++) {
1125 ret
= serialize_one_field(session
, fields
, &iter_output
,
1126 <tng_fields
[i
].event_field
);
1131 *_nr_write_fields
= nr_write_fields
;
1132 *ustctl_fields
= fields
;
1141 * Returns 0 on success, negative error value on error.
1143 int ustcomm_register_event(int sock
,
1144 struct lttng_session
*session
,
1145 int session_objd
, /* session descriptor */
1146 int channel_objd
, /* channel descriptor */
1147 const char *event_name
, /* event name (input) */
1149 const char *signature
, /* event signature (input) */
1150 size_t nr_fields
, /* fields */
1151 const struct lttng_event_field
*lttng_fields
,
1152 const char *model_emf_uri
,
1153 uint32_t *id
) /* event id (output) */
1157 struct ustcomm_notify_hdr header
;
1158 struct ustcomm_notify_event_msg m
;
1161 struct ustcomm_notify_hdr header
;
1162 struct ustcomm_notify_event_reply r
;
1164 size_t signature_len
, fields_len
, model_emf_uri_len
;
1165 struct ustctl_field
*fields
= NULL
;
1166 size_t nr_write_fields
= 0;
1169 memset(&msg
, 0, sizeof(msg
));
1170 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1171 msg
.m
.session_objd
= session_objd
;
1172 msg
.m
.channel_objd
= channel_objd
;
1173 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_SYM_NAME_LEN
);
1174 msg
.m
.event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1175 msg
.m
.loglevel
= loglevel
;
1176 signature_len
= strlen(signature
) + 1;
1177 msg
.m
.signature_len
= signature_len
;
1179 /* Calculate fields len, serialize fields. */
1180 if (nr_fields
> 0) {
1181 ret
= serialize_fields(session
, &nr_write_fields
, &fields
,
1182 nr_fields
, lttng_fields
);
1187 fields_len
= sizeof(*fields
) * nr_write_fields
;
1188 msg
.m
.fields_len
= fields_len
;
1189 if (model_emf_uri
) {
1190 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
1192 model_emf_uri_len
= 0;
1194 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
1196 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1197 if (len
> 0 && len
!= sizeof(msg
)) {
1206 /* send signature */
1207 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
1208 if (len
> 0 && len
!= signature_len
) {
1218 if (fields_len
> 0) {
1219 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1220 if (len
> 0 && len
!= fields_len
) {
1231 if (model_emf_uri_len
) {
1232 /* send model_emf_uri */
1233 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
1235 if (len
> 0 && len
!= model_emf_uri_len
) {
1244 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1246 case 0: /* orderly shutdown */
1249 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1250 ERR("Unexpected result message command "
1251 "expected: %u vs received: %u\n",
1252 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1255 if (reply
.r
.ret_code
> 0)
1257 if (reply
.r
.ret_code
< 0)
1258 return reply
.r
.ret_code
;
1259 *id
= reply
.r
.event_id
;
1260 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1261 event_name
, reply
.r
.ret_code
, reply
.r
.event_id
);
1265 /* Transport level error */
1266 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1270 ERR("incorrect message size: %zd\n", len
);
1276 /* Error path only. */
1283 * Returns 0 on success, negative error value on error.
1284 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1286 int ustcomm_register_enum(int sock
,
1287 int session_objd
, /* session descriptor */
1288 const char *enum_name
, /* enum name (input) */
1289 size_t nr_entries
, /* entries */
1290 const struct lttng_enum_entry
*lttng_entries
,
1295 struct ustcomm_notify_hdr header
;
1296 struct ustcomm_notify_enum_msg m
;
1299 struct ustcomm_notify_hdr header
;
1300 struct ustcomm_notify_enum_reply r
;
1303 struct ustctl_enum_entry
*entries
= NULL
;
1306 memset(&msg
, 0, sizeof(msg
));
1307 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1308 msg
.m
.session_objd
= session_objd
;
1309 strncpy(msg
.m
.enum_name
, enum_name
, LTTNG_UST_SYM_NAME_LEN
);
1310 msg
.m
.enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1312 /* Calculate entries len, serialize entries. */
1313 if (nr_entries
> 0) {
1314 ret
= serialize_entries(&entries
,
1315 nr_entries
, lttng_entries
);
1320 entries_len
= sizeof(*entries
) * nr_entries
;
1321 msg
.m
.entries_len
= entries_len
;
1323 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1324 if (len
> 0 && len
!= sizeof(msg
)) {
1334 if (entries_len
> 0) {
1335 len
= ustcomm_send_unix_sock(sock
, entries
, entries_len
);
1336 if (len
> 0 && len
!= entries_len
) {
1349 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1351 case 0: /* orderly shutdown */
1354 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1355 ERR("Unexpected result message command "
1356 "expected: %u vs received: %u\n",
1357 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1360 if (reply
.r
.ret_code
> 0)
1362 if (reply
.r
.ret_code
< 0)
1363 return reply
.r
.ret_code
;
1364 *id
= reply
.r
.enum_id
;
1365 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1366 enum_name
, reply
.r
.ret_code
);
1370 /* Transport level error */
1371 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1375 ERR("incorrect message size: %zd\n", len
);
1387 * Returns 0 on success, negative error value on error.
1388 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1390 int ustcomm_register_channel(int sock
,
1391 struct lttng_session
*session
,
1392 int session_objd
, /* session descriptor */
1393 int channel_objd
, /* channel descriptor */
1394 size_t nr_ctx_fields
,
1395 const struct lttng_ctx_field
*ctx_fields
,
1396 uint32_t *chan_id
, /* channel id (output) */
1397 int *header_type
) /* header type (output) */
1401 struct ustcomm_notify_hdr header
;
1402 struct ustcomm_notify_channel_msg m
;
1405 struct ustcomm_notify_hdr header
;
1406 struct ustcomm_notify_channel_reply r
;
1409 struct ustctl_field
*fields
= NULL
;
1411 size_t nr_write_fields
= 0;
1413 memset(&msg
, 0, sizeof(msg
));
1414 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1415 msg
.m
.session_objd
= session_objd
;
1416 msg
.m
.channel_objd
= channel_objd
;
1418 /* Calculate fields len, serialize fields. */
1419 if (nr_ctx_fields
> 0) {
1420 ret
= serialize_ctx_fields(session
, &nr_write_fields
, &fields
,
1421 nr_ctx_fields
, ctx_fields
);
1426 fields_len
= sizeof(*fields
) * nr_write_fields
;
1427 msg
.m
.ctx_fields_len
= fields_len
;
1428 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1429 if (len
> 0 && len
!= sizeof(msg
)) {
1439 if (fields_len
> 0) {
1440 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1442 if (len
> 0 && len
!= fields_len
) {
1452 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1454 case 0: /* orderly shutdown */
1457 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1458 ERR("Unexpected result message command "
1459 "expected: %u vs received: %u\n",
1460 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1463 if (reply
.r
.ret_code
> 0)
1465 if (reply
.r
.ret_code
< 0)
1466 return reply
.r
.ret_code
;
1467 *chan_id
= reply
.r
.chan_id
;
1468 switch (reply
.r
.header_type
) {
1471 *header_type
= reply
.r
.header_type
;
1474 ERR("Unexpected channel header type %u\n",
1475 reply
.r
.header_type
);
1478 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1479 reply
.r
.chan_id
, reply
.r
.header_type
);
1483 /* Transport level error */
1484 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1488 ERR("incorrect message size: %zd\n", len
);
1495 * Set socket reciving timeout.
1497 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1502 tv
.tv_sec
= msec
/ 1000;
1503 tv
.tv_usec
= (msec
* 1000 % 1000000);
1505 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1507 PERROR("setsockopt SO_RCVTIMEO");
1515 * Set socket sending timeout.
1517 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1522 tv
.tv_sec
= msec
/ 1000;
1523 tv
.tv_usec
= (msec
* 1000) % 1000000;
1525 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1527 PERROR("setsockopt SO_SNDTIMEO");