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
, long timeout
)
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);
114 /* Give at least 10ms. */
117 ret
= ustcomm_setsockopt_snd_timeout(fd
, timeout
);
119 WARN("Error setting connect socket send timeout");
122 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
129 memset(&sun
, 0, sizeof(sun
));
130 sun
.sun_family
= AF_UNIX
;
131 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
132 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
134 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
137 * Don't print message on connect ENOENT error, because
138 * connect is used in normal execution to detect if
139 * sessiond is alive. ENOENT is when the unix socket
140 * file does not exist, and ECONNREFUSED is when the
141 * file exists but no sessiond is listening.
143 if (errno
!= ECONNREFUSED
&& errno
!= ECONNRESET
144 && errno
!= ENOENT
&& errno
!= EACCES
)
147 if (ret
== -ECONNREFUSED
|| ret
== -ECONNRESET
)
159 closeret
= close(fd
);
168 * ustcomm_accept_unix_sock
170 * Do an accept(2) on the sock and return the
171 * new file descriptor. The socket MUST be bind(2) before.
173 int ustcomm_accept_unix_sock(int sock
)
176 struct sockaddr_un sun
;
180 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
182 if (errno
!= ECONNABORTED
)
185 if (new_fd
== -ECONNABORTED
)
192 * ustcomm_create_unix_sock
194 * Creates a AF_UNIX local socket using pathname
195 * bind the socket upon creation and return the fd.
197 int ustcomm_create_unix_sock(const char *pathname
)
199 struct sockaddr_un sun
;
202 /* Create server socket */
203 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
209 memset(&sun
, 0, sizeof(sun
));
210 sun
.sun_family
= AF_UNIX
;
211 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
212 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
214 /* Unlink the old file if present */
215 (void) unlink(pathname
);
216 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
229 closeret
= close(fd
);
239 * ustcomm_listen_unix_sock
241 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
243 int ustcomm_listen_unix_sock(int sock
)
247 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
257 * ustcomm_close_unix_sock
259 * Shutdown cleanly a unix socket.
261 int ustcomm_close_unix_sock(int sock
)
275 * ustcomm_recv_unix_sock
277 * Receive data of size len in put that data into
278 * the buf param. Using recvmsg API.
279 * Return the size of received data.
280 * Return 0 on orderly shutdown.
282 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
289 memset(&msg
, 0, sizeof(msg
));
291 iov
[0].iov_base
= buf
;
292 iov
[0].iov_len
= len
;
297 len_last
= iov
[0].iov_len
;
298 ret
= recvmsg(sock
, &msg
, 0);
300 iov
[0].iov_base
+= ret
;
301 iov
[0].iov_len
-= ret
;
302 assert(ret
<= len_last
);
304 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
309 if (errno
!= EPIPE
&& errno
!= ECONNRESET
&& errno
!= ECONNREFUSED
)
312 if (ret
== -ECONNRESET
|| ret
== -ECONNREFUSED
)
315 shutret
= shutdown(sock
, SHUT_RDWR
);
317 ERR("Socket shutdown error");
318 } else if (ret
> 0) {
321 /* ret = 0 means an orderly shutdown. */
327 * ustcomm_send_unix_sock
329 * Send buf data of size len. Using sendmsg API.
330 * Return the size of sent data.
332 ssize_t
ustcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
338 memset(&msg
, 0, sizeof(msg
));
340 iov
[0].iov_base
= (void *) buf
;
341 iov
[0].iov_len
= len
;
346 * Using the MSG_NOSIGNAL when sending data from sessiond to
347 * libust, so libust does not receive an unhandled SIGPIPE or
348 * SIGURG. The sessiond receiver side can be made more resilient
349 * by ignoring SIGPIPE, but we don't have this luxury on the
353 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
354 } while (ret
< 0 && errno
== EINTR
);
359 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
362 if (ret
== -ECONNRESET
)
365 shutret
= shutdown(sock
, SHUT_RDWR
);
367 ERR("Socket shutdown error");
374 * Send a message accompanied by fd(s) over a unix socket.
376 * Returns the size of data sent, or negative error value.
378 ssize_t
ustcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
381 struct cmsghdr
*cmptr
;
384 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
385 char tmp
[CMSG_SPACE(sizeof_fds
)];
388 memset(&msg
, 0, sizeof(msg
));
389 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
391 if (nb_fd
> USTCOMM_MAX_SEND_FDS
)
394 msg
.msg_control
= (caddr_t
)tmp
;
395 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
397 cmptr
= CMSG_FIRSTHDR(&msg
);
400 cmptr
->cmsg_level
= SOL_SOCKET
;
401 cmptr
->cmsg_type
= SCM_RIGHTS
;
402 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
403 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
404 /* Sum of the length of all control messages in the buffer: */
405 msg
.msg_controllen
= cmptr
->cmsg_len
;
407 iov
[0].iov_base
= &dummy
;
413 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
414 } while (ret
< 0 && errno
== EINTR
);
417 * We consider EPIPE and ECONNRESET as expected.
419 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
423 if (ret
== -ECONNRESET
)
430 * Recv a message accompanied by fd(s) from a unix socket.
432 * Returns the size of received data, or negative error value.
434 * Expect at most "nb_fd" file descriptors. Returns the number of fd
435 * actually received in nb_fd.
436 * Returns -EPIPE on orderly shutdown.
438 ssize_t
ustcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
442 struct cmsghdr
*cmsg
;
443 size_t sizeof_fds
= nb_fd
* sizeof(int);
444 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
448 memset(&msg
, 0, sizeof(msg
));
450 /* Prepare to receive the structures */
451 iov
[0].iov_base
= &dummy
;
455 msg
.msg_control
= recv_fd
;
456 msg
.msg_controllen
= sizeof(recv_fd
);
459 ret
= recvmsg(sock
, &msg
, 0);
460 } while (ret
< 0 && errno
== EINTR
);
462 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
463 PERROR("recvmsg fds");
466 if (ret
== -ECONNRESET
)
471 /* orderly shutdown */
476 ERR("Error: Received %zd bytes, expected %d\n",
480 if (msg
.msg_flags
& MSG_CTRUNC
) {
481 ERR("Error: Control message truncated.\n");
485 cmsg
= CMSG_FIRSTHDR(&msg
);
487 ERR("Error: Invalid control message header\n");
491 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
492 ERR("Didn't received any fd\n");
496 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
497 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
498 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
502 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
508 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
512 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
520 ERR("incorrect message size: %zd\n", len
);
527 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
528 uint32_t expected_handle
, uint32_t expected_cmd
)
532 memset(lur
, 0, sizeof(*lur
));
533 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
535 case 0: /* orderly shutdown */
541 if (lur
->handle
!= expected_handle
) {
542 ERR("Unexpected result message handle: "
543 "expected: %u vs received: %u\n",
544 expected_handle
, lur
->handle
);
547 if (lur
->cmd
!= expected_cmd
) {
548 ERR("Unexpected result message command "
549 "expected: %u vs received: %u\n",
550 expected_cmd
, lur
->cmd
);
556 return lur
->ret_code
;
561 ERR("incorrect message size: %zd\n", len
);
567 int ustcomm_send_app_cmd(int sock
,
568 struct ustcomm_ust_msg
*lum
,
569 struct ustcomm_ust_reply
*lur
)
573 ret
= ustcomm_send_app_msg(sock
, lum
);
576 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
583 * chan_data is allocated internally if this function returns the
586 ssize_t
ustcomm_recv_channel_from_sessiond(int sock
,
587 void **_chan_data
, uint64_t var_len
,
594 if (var_len
> LTTNG_UST_CHANNEL_DATA_MAX_LEN
) {
598 /* Receive variable length data */
599 chan_data
= zmalloc(var_len
);
604 len
= ustcomm_recv_unix_sock(sock
, chan_data
, var_len
);
605 if (len
!= var_len
) {
609 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
619 *_wakeup_fd
= wakeup_fd
;
620 *_chan_data
= chan_data
;
630 int ustcomm_recv_stream_from_sessiond(int sock
,
631 uint64_t *memory_map_size
,
632 int *shm_fd
, int *wakeup_fd
)
638 /* recv shm fd and wakeup fd */
639 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
658 * Returns 0 on success, negative error value on error.
660 int ustcomm_send_reg_msg(int sock
,
661 enum ustctl_socket_type type
,
662 uint32_t bits_per_long
,
663 uint32_t uint8_t_alignment
,
664 uint32_t uint16_t_alignment
,
665 uint32_t uint32_t_alignment
,
666 uint32_t uint64_t_alignment
,
667 uint32_t long_alignment
)
670 struct ustctl_reg_msg reg_msg
;
672 reg_msg
.magic
= LTTNG_UST_COMM_MAGIC
;
673 reg_msg
.major
= LTTNG_UST_ABI_MAJOR_VERSION
;
674 reg_msg
.minor
= LTTNG_UST_ABI_MINOR_VERSION
;
675 reg_msg
.pid
= getpid();
676 reg_msg
.ppid
= getppid();
677 reg_msg
.uid
= getuid();
678 reg_msg
.gid
= getgid();
679 reg_msg
.bits_per_long
= bits_per_long
;
680 reg_msg
.uint8_t_alignment
= uint8_t_alignment
;
681 reg_msg
.uint16_t_alignment
= uint16_t_alignment
;
682 reg_msg
.uint32_t_alignment
= uint32_t_alignment
;
683 reg_msg
.uint64_t_alignment
= uint64_t_alignment
;
684 reg_msg
.long_alignment
= long_alignment
;
685 reg_msg
.socket_type
= type
;
686 lttng_ust_getprocname(reg_msg
.name
);
687 memset(reg_msg
.padding
, 0, sizeof(reg_msg
.padding
));
689 len
= ustcomm_send_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
690 if (len
> 0 && len
!= sizeof(reg_msg
))
698 ssize_t
count_one_type(const struct lttng_type
*lt
)
709 //TODO: implement non-empty struct.
713 const struct lttng_event_field
*choices
;
717 ret
= lttng_ust_dynamic_type_choices(&nr_choices
,
722 * One field for enum, one field for variant, and
723 * one field per choice.
725 return count_fields_recursive(nr_choices
, choices
) + 2;
734 ssize_t
count_fields_recursive(size_t nr_fields
,
735 const struct lttng_event_field
*lttng_fields
)
738 ssize_t ret
, count
= 0;
740 for (i
= 0; i
< nr_fields
; i
++) {
741 const struct lttng_event_field
*lf
;
743 lf
= <tng_fields
[i
];
744 /* skip 'nowrite' fields */
747 ret
= count_one_type(&lf
->type
);
749 return ret
; /* error */
756 ssize_t
count_ctx_fields_recursive(size_t nr_fields
,
757 const struct lttng_ctx_field
*lttng_fields
)
760 ssize_t ret
, count
= 0;
762 for (i
= 0; i
< nr_fields
; i
++) {
763 const struct lttng_event_field
*lf
;
765 lf
= <tng_fields
[i
].event_field
;
766 /* skip 'nowrite' fields */
769 ret
= count_one_type(&lf
->type
);
771 return ret
; /* error */
778 int serialize_string_encoding(enum ustctl_string_encodings
*ue
,
779 enum lttng_string_encodings le
)
782 case lttng_encode_none
:
783 *ue
= ustctl_encode_none
;
785 case lttng_encode_UTF8
:
786 *ue
= ustctl_encode_UTF8
;
788 case lttng_encode_ASCII
:
789 *ue
= ustctl_encode_ASCII
;
798 int serialize_integer_type(struct ustctl_integer_type
*uit
,
799 const struct lttng_integer_type
*lit
)
801 uit
->size
= lit
->size
;
802 uit
->signedness
= lit
->signedness
;
803 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
804 uit
->base
= lit
->base
;
805 if (serialize_string_encoding(&uit
->encoding
, lit
->encoding
))
807 uit
->alignment
= lit
->alignment
;
812 int serialize_basic_type(struct lttng_session
*session
,
813 enum ustctl_abstract_types
*uatype
,
814 enum lttng_abstract_types atype
,
815 union _ustctl_basic_type
*ubt
,
816 const union _lttng_basic_type
*lbt
)
821 if (serialize_integer_type(&ubt
->integer
, &lbt
->integer
))
823 *uatype
= ustctl_atype_integer
;
828 if (serialize_string_encoding(&ubt
->string
.encoding
,
829 lbt
->string
.encoding
))
831 *uatype
= ustctl_atype_string
;
836 struct ustctl_float_type
*uft
;
837 const struct lttng_float_type
*lft
;
841 uft
->exp_dig
= lft
->exp_dig
;
842 uft
->mant_dig
= lft
->mant_dig
;
843 uft
->alignment
= lft
->alignment
;
844 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
845 *uatype
= ustctl_atype_float
;
850 strncpy(ubt
->enumeration
.name
, lbt
->enumeration
.desc
->name
,
851 LTTNG_UST_SYM_NAME_LEN
);
852 ubt
->enumeration
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
853 if (serialize_integer_type(&ubt
->enumeration
.container_type
,
854 &lbt
->enumeration
.container_type
))
857 const struct lttng_enum
*_enum
;
859 _enum
= lttng_ust_enum_get(session
,
860 lbt
->enumeration
.desc
->name
);
863 ubt
->enumeration
.id
= _enum
->id
;
865 ubt
->enumeration
.id
= -1ULL;
867 *uatype
= ustctl_atype_enum
;
879 int serialize_dynamic_type(struct lttng_session
*session
,
880 struct ustctl_field
*fields
, size_t *iter_output
,
881 const struct lttng_event_field
*lf
)
883 const struct lttng_event_field
*choices
;
884 char tag_field_name
[LTTNG_UST_SYM_NAME_LEN
];
885 const struct lttng_type
*tag_type
;
886 const struct lttng_event_field
*tag_field_generic
;
887 struct lttng_event_field tag_field
= {
888 .name
= tag_field_name
,
891 struct ustctl_field
*uf
;
892 size_t nr_choices
, i
;
895 tag_field_generic
= lttng_ust_dynamic_type_tag_field();
896 tag_type
= &tag_field_generic
->type
;
898 /* Serialize enum field. */
899 strncpy(tag_field_name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
900 tag_field_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
901 strncat(tag_field_name
,
903 LTTNG_UST_SYM_NAME_LEN
- strlen(tag_field_name
) - 1);
904 tag_field
.type
= *tag_type
;
905 ret
= serialize_one_field(session
, fields
, iter_output
,
910 /* Serialize variant field. */
911 uf
= &fields
[*iter_output
];
912 ret
= lttng_ust_dynamic_type_choices(&nr_choices
, &choices
);
916 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
917 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
918 uf
->type
.atype
= ustctl_atype_variant
;
919 uf
->type
.u
.variant
.nr_choices
= nr_choices
;
920 strncpy(uf
->type
.u
.variant
.tag_name
,
922 LTTNG_UST_SYM_NAME_LEN
);
923 uf
->type
.u
.variant
.tag_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
926 /* Serialize choice fields after variant. */
927 for (i
= 0; i
< nr_choices
; i
++) {
928 ret
= serialize_one_field(session
, fields
,
929 iter_output
, &choices
[i
]);
937 int serialize_one_field(struct lttng_session
*session
,
938 struct ustctl_field
*fields
, size_t *iter_output
,
939 const struct lttng_event_field
*lf
)
941 const struct lttng_type
*lt
= &lf
->type
;
944 /* skip 'nowrite' fields */
954 struct ustctl_field
*uf
= &fields
[*iter_output
];
955 struct ustctl_type
*ut
= &uf
->type
;
957 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
958 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
959 ret
= serialize_basic_type(session
, &ut
->atype
, lt
->atype
,
960 &ut
->u
.basic
, <
->u
.basic
);
968 struct ustctl_field
*uf
= &fields
[*iter_output
];
969 struct ustctl_type
*ut
= &uf
->type
;
970 struct ustctl_basic_type
*ubt
;
971 const struct lttng_basic_type
*lbt
;
973 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
974 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
975 uf
->type
.atype
= ustctl_atype_array
;
976 ubt
= &ut
->u
.array
.elem_type
;
977 lbt
= <
->u
.array
.elem_type
;
978 ut
->u
.array
.length
= lt
->u
.array
.length
;
979 ret
= serialize_basic_type(session
, &ubt
->atype
, lbt
->atype
,
980 &ubt
->u
.basic
, &lbt
->u
.basic
);
983 ut
->atype
= ustctl_atype_array
;
989 struct ustctl_field
*uf
= &fields
[*iter_output
];
990 struct ustctl_type
*ut
= &uf
->type
;
991 struct ustctl_basic_type
*ubt
;
992 const struct lttng_basic_type
*lbt
;
995 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
996 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
997 uf
->type
.atype
= ustctl_atype_sequence
;
998 ubt
= &ut
->u
.sequence
.length_type
;
999 lbt
= <
->u
.sequence
.length_type
;
1000 ret
= serialize_basic_type(session
, &ubt
->atype
, lbt
->atype
,
1001 &ubt
->u
.basic
, &lbt
->u
.basic
);
1004 ubt
= &ut
->u
.sequence
.elem_type
;
1005 lbt
= <
->u
.sequence
.elem_type
;
1006 ret
= serialize_basic_type(session
, &ubt
->atype
, lbt
->atype
,
1007 &ubt
->u
.basic
, &lbt
->u
.basic
);
1010 ut
->atype
= ustctl_atype_sequence
;
1016 ret
= serialize_dynamic_type(session
, fields
, iter_output
, lf
);
1023 struct ustctl_field
*uf
= &fields
[*iter_output
];
1026 * TODO: add support for non-empty struct.
1028 if (lf
->type
.u
._struct
.nr_fields
!= 0) {
1031 strncpy(uf
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
1032 uf
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1033 uf
->type
.atype
= ustctl_atype_struct
;
1034 uf
->type
.u
._struct
.nr_fields
= 0;
1045 int serialize_fields(struct lttng_session
*session
,
1046 size_t *_nr_write_fields
,
1047 struct ustctl_field
**ustctl_fields
,
1049 const struct lttng_event_field
*lttng_fields
)
1051 struct ustctl_field
*fields
;
1053 size_t i
, iter_output
= 0;
1054 ssize_t nr_write_fields
;
1056 nr_write_fields
= count_fields_recursive(nr_fields
, lttng_fields
);
1057 if (nr_write_fields
< 0) {
1058 return (int) nr_write_fields
;
1061 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1065 for (i
= 0; i
< nr_fields
; i
++) {
1066 ret
= serialize_one_field(session
, fields
, &iter_output
,
1072 *_nr_write_fields
= nr_write_fields
;
1073 *ustctl_fields
= fields
;
1082 int serialize_entries(struct ustctl_enum_entry
**_entries
,
1084 const struct lttng_enum_entry
*lttng_entries
)
1086 struct ustctl_enum_entry
*entries
;
1089 /* Serialize the entries */
1090 entries
= zmalloc(nr_entries
* sizeof(*entries
));
1093 for (i
= 0; i
< nr_entries
; i
++) {
1094 struct ustctl_enum_entry
*uentry
;
1095 const struct lttng_enum_entry
*lentry
;
1097 uentry
= &entries
[i
];
1098 lentry
= <tng_entries
[i
];
1100 uentry
->start
.value
= lentry
->start
.value
;
1101 uentry
->start
.signedness
= lentry
->start
.signedness
;
1102 uentry
->end
.value
= lentry
->end
.value
;
1103 uentry
->end
.signedness
= lentry
->end
.signedness
;
1104 strncpy(uentry
->string
, lentry
->string
, LTTNG_UST_SYM_NAME_LEN
);
1105 uentry
->string
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1107 *_entries
= entries
;
1112 int serialize_ctx_fields(struct lttng_session
*session
,
1113 size_t *_nr_write_fields
,
1114 struct ustctl_field
**ustctl_fields
,
1116 const struct lttng_ctx_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_ctx_fields_recursive(nr_fields
,
1125 if (nr_write_fields
< 0) {
1126 return (int) nr_write_fields
;
1129 fields
= zmalloc(nr_write_fields
* sizeof(*fields
));
1133 for (i
= 0; i
< nr_fields
; i
++) {
1134 ret
= serialize_one_field(session
, fields
, &iter_output
,
1135 <tng_fields
[i
].event_field
);
1140 *_nr_write_fields
= nr_write_fields
;
1141 *ustctl_fields
= fields
;
1150 * Returns 0 on success, negative error value on error.
1152 int ustcomm_register_event(int sock
,
1153 struct lttng_session
*session
,
1154 int session_objd
, /* session descriptor */
1155 int channel_objd
, /* channel descriptor */
1156 const char *event_name
, /* event name (input) */
1158 const char *signature
, /* event signature (input) */
1159 size_t nr_fields
, /* fields */
1160 const struct lttng_event_field
*lttng_fields
,
1161 const char *model_emf_uri
,
1162 uint32_t *id
) /* event id (output) */
1166 struct ustcomm_notify_hdr header
;
1167 struct ustcomm_notify_event_msg m
;
1170 struct ustcomm_notify_hdr header
;
1171 struct ustcomm_notify_event_reply r
;
1173 size_t signature_len
, fields_len
, model_emf_uri_len
;
1174 struct ustctl_field
*fields
= NULL
;
1175 size_t nr_write_fields
= 0;
1178 memset(&msg
, 0, sizeof(msg
));
1179 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
1180 msg
.m
.session_objd
= session_objd
;
1181 msg
.m
.channel_objd
= channel_objd
;
1182 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_SYM_NAME_LEN
);
1183 msg
.m
.event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1184 msg
.m
.loglevel
= loglevel
;
1185 signature_len
= strlen(signature
) + 1;
1186 msg
.m
.signature_len
= signature_len
;
1188 /* Calculate fields len, serialize fields. */
1189 if (nr_fields
> 0) {
1190 ret
= serialize_fields(session
, &nr_write_fields
, &fields
,
1191 nr_fields
, lttng_fields
);
1196 fields_len
= sizeof(*fields
) * nr_write_fields
;
1197 msg
.m
.fields_len
= fields_len
;
1198 if (model_emf_uri
) {
1199 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
1201 model_emf_uri_len
= 0;
1203 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
1205 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1206 if (len
> 0 && len
!= sizeof(msg
)) {
1215 /* send signature */
1216 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
1217 if (len
> 0 && len
!= signature_len
) {
1227 if (fields_len
> 0) {
1228 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1229 if (len
> 0 && len
!= fields_len
) {
1240 if (model_emf_uri_len
) {
1241 /* send model_emf_uri */
1242 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
1244 if (len
> 0 && len
!= model_emf_uri_len
) {
1253 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1255 case 0: /* orderly shutdown */
1258 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1259 ERR("Unexpected result message command "
1260 "expected: %u vs received: %u\n",
1261 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1264 if (reply
.r
.ret_code
> 0)
1266 if (reply
.r
.ret_code
< 0)
1267 return reply
.r
.ret_code
;
1268 *id
= reply
.r
.event_id
;
1269 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1270 event_name
, reply
.r
.ret_code
, reply
.r
.event_id
);
1274 /* Transport level error */
1275 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1279 ERR("incorrect message size: %zd\n", len
);
1285 /* Error path only. */
1292 * Returns 0 on success, negative error value on error.
1293 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1295 int ustcomm_register_enum(int sock
,
1296 int session_objd
, /* session descriptor */
1297 const char *enum_name
, /* enum name (input) */
1298 size_t nr_entries
, /* entries */
1299 const struct lttng_enum_entry
*lttng_entries
,
1304 struct ustcomm_notify_hdr header
;
1305 struct ustcomm_notify_enum_msg m
;
1308 struct ustcomm_notify_hdr header
;
1309 struct ustcomm_notify_enum_reply r
;
1312 struct ustctl_enum_entry
*entries
= NULL
;
1315 memset(&msg
, 0, sizeof(msg
));
1316 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1317 msg
.m
.session_objd
= session_objd
;
1318 strncpy(msg
.m
.enum_name
, enum_name
, LTTNG_UST_SYM_NAME_LEN
);
1319 msg
.m
.enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1321 /* Calculate entries len, serialize entries. */
1322 if (nr_entries
> 0) {
1323 ret
= serialize_entries(&entries
,
1324 nr_entries
, lttng_entries
);
1329 entries_len
= sizeof(*entries
) * nr_entries
;
1330 msg
.m
.entries_len
= entries_len
;
1332 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1333 if (len
> 0 && len
!= sizeof(msg
)) {
1343 if (entries_len
> 0) {
1344 len
= ustcomm_send_unix_sock(sock
, entries
, entries_len
);
1345 if (len
> 0 && len
!= entries_len
) {
1358 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1360 case 0: /* orderly shutdown */
1363 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1364 ERR("Unexpected result message command "
1365 "expected: %u vs received: %u\n",
1366 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1369 if (reply
.r
.ret_code
> 0)
1371 if (reply
.r
.ret_code
< 0)
1372 return reply
.r
.ret_code
;
1373 *id
= reply
.r
.enum_id
;
1374 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1375 enum_name
, reply
.r
.ret_code
);
1379 /* Transport level error */
1380 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1384 ERR("incorrect message size: %zd\n", len
);
1396 * Returns 0 on success, negative error value on error.
1397 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1399 int ustcomm_register_channel(int sock
,
1400 struct lttng_session
*session
,
1401 int session_objd
, /* session descriptor */
1402 int channel_objd
, /* channel descriptor */
1403 size_t nr_ctx_fields
,
1404 const struct lttng_ctx_field
*ctx_fields
,
1405 uint32_t *chan_id
, /* channel id (output) */
1406 int *header_type
) /* header type (output) */
1410 struct ustcomm_notify_hdr header
;
1411 struct ustcomm_notify_channel_msg m
;
1414 struct ustcomm_notify_hdr header
;
1415 struct ustcomm_notify_channel_reply r
;
1418 struct ustctl_field
*fields
= NULL
;
1420 size_t nr_write_fields
= 0;
1422 memset(&msg
, 0, sizeof(msg
));
1423 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1424 msg
.m
.session_objd
= session_objd
;
1425 msg
.m
.channel_objd
= channel_objd
;
1427 /* Calculate fields len, serialize fields. */
1428 if (nr_ctx_fields
> 0) {
1429 ret
= serialize_ctx_fields(session
, &nr_write_fields
, &fields
,
1430 nr_ctx_fields
, ctx_fields
);
1435 fields_len
= sizeof(*fields
) * nr_write_fields
;
1436 msg
.m
.ctx_fields_len
= fields_len
;
1437 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1438 if (len
> 0 && len
!= sizeof(msg
)) {
1448 if (fields_len
> 0) {
1449 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1451 if (len
> 0 && len
!= fields_len
) {
1461 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1463 case 0: /* orderly shutdown */
1466 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1467 ERR("Unexpected result message command "
1468 "expected: %u vs received: %u\n",
1469 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1472 if (reply
.r
.ret_code
> 0)
1474 if (reply
.r
.ret_code
< 0)
1475 return reply
.r
.ret_code
;
1476 *chan_id
= reply
.r
.chan_id
;
1477 switch (reply
.r
.header_type
) {
1480 *header_type
= reply
.r
.header_type
;
1483 ERR("Unexpected channel header type %u\n",
1484 reply
.r
.header_type
);
1487 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1488 reply
.r
.chan_id
, reply
.r
.header_type
);
1492 /* Transport level error */
1493 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1497 ERR("incorrect message size: %zd\n", len
);
1504 * Set socket reciving timeout.
1506 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1511 tv
.tv_sec
= msec
/ 1000;
1512 tv
.tv_usec
= (msec
* 1000 % 1000000);
1514 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1516 PERROR("setsockopt SO_RCVTIMEO");
1524 * Set socket sending timeout.
1526 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1531 tv
.tv_sec
= msec
/ 1000;
1532 tv
.tv_usec
= (msec
* 1000) % 1000000;
1534 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1536 PERROR("setsockopt SO_SNDTIMEO");