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 <usterr-signal-safe.h>
41 #include "../liblttng-ust/compat.h"
43 #define USTCOMM_CODE_OFFSET(code) \
44 (code == LTTNG_UST_OK ? 0 : (code - LTTNG_UST_ERR + 1))
46 #define USTCOMM_MAX_SEND_FDS 4
49 * Human readable error message.
51 static const char *ustcomm_readable_code
[] = {
52 [ USTCOMM_CODE_OFFSET(LTTNG_UST_OK
) ] = "Success",
53 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR
) ] = "Unknown error",
54 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOENT
) ] = "No entry",
55 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXIST
) ] = "Object already exists",
56 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL
) ] = "Invalid argument",
57 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PERM
) ] = "Permission denied",
58 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_NOSYS
) ] = "Not implemented",
59 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_EXITING
) ] = "Process is exiting",
61 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_MAGIC
) ] = "Invalid magic number",
62 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_SOCKET_TYPE
) ] = "Invalid socket type",
63 [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_UNSUP_MAJOR
) ] = "Unsupported major version",
69 * Receives positive error value.
70 * Return ptr to string representing a human readable
71 * error code from the ustcomm_return_code enum.
73 const char *lttng_ust_strerror(int code
)
75 if (code
== LTTNG_UST_OK
)
76 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
77 if (code
< LTTNG_UST_ERR
)
78 return strerror(code
);
79 if (code
>= LTTNG_UST_ERR_NR
)
81 return ustcomm_readable_code
[USTCOMM_CODE_OFFSET(code
)];
85 * ustcomm_connect_unix_sock
87 * Connect to unix socket using the path name.
89 int ustcomm_connect_unix_sock(const char *pathname
)
91 struct sockaddr_un sun
;
95 * libust threads require the close-on-exec flag for all
96 * resources so it does not leak file descriptors upon exec.
98 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
104 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
111 memset(&sun
, 0, sizeof(sun
));
112 sun
.sun_family
= AF_UNIX
;
113 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
114 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
116 ret
= connect(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
119 * Don't print message on connect ENOENT error, because
120 * connect is used in normal execution to detect if
121 * sessiond is alive. ENOENT is when the unix socket
122 * file does not exist, and ECONNREFUSED is when the
123 * file exists but no sessiond is listening.
125 if (errno
!= ECONNREFUSED
&& errno
!= ECONNRESET
126 && errno
!= ENOENT
&& errno
!= EACCES
)
129 if (ret
== -ECONNREFUSED
|| ret
== -ECONNRESET
)
141 closeret
= close(fd
);
150 * ustcomm_accept_unix_sock
152 * Do an accept(2) on the sock and return the
153 * new file descriptor. The socket MUST be bind(2) before.
155 int ustcomm_accept_unix_sock(int sock
)
158 struct sockaddr_un sun
;
162 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
164 if (errno
!= ECONNABORTED
)
167 if (new_fd
== -ECONNABORTED
)
174 * ustcomm_create_unix_sock
176 * Creates a AF_UNIX local socket using pathname
177 * bind the socket upon creation and return the fd.
179 int ustcomm_create_unix_sock(const char *pathname
)
181 struct sockaddr_un sun
;
184 /* Create server socket */
185 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
191 memset(&sun
, 0, sizeof(sun
));
192 sun
.sun_family
= AF_UNIX
;
193 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
194 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
196 /* Unlink the old file if present */
197 (void) unlink(pathname
);
198 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
211 closeret
= close(fd
);
221 * ustcomm_listen_unix_sock
223 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
225 int ustcomm_listen_unix_sock(int sock
)
229 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
239 * ustcomm_close_unix_sock
241 * Shutdown cleanly a unix socket.
243 int ustcomm_close_unix_sock(int sock
)
257 * ustcomm_recv_unix_sock
259 * Receive data of size len in put that data into
260 * the buf param. Using recvmsg API.
261 * Return the size of received data.
262 * Return 0 on orderly shutdown.
264 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
271 memset(&msg
, 0, sizeof(msg
));
273 iov
[0].iov_base
= buf
;
274 iov
[0].iov_len
= len
;
279 len_last
= iov
[0].iov_len
;
280 ret
= recvmsg(sock
, &msg
, 0);
282 iov
[0].iov_base
+= ret
;
283 iov
[0].iov_len
-= ret
;
284 assert(ret
<= len_last
);
286 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
291 if (errno
!= EPIPE
&& errno
!= ECONNRESET
&& errno
!= ECONNREFUSED
)
294 if (ret
== -ECONNRESET
|| ret
== -ECONNREFUSED
)
297 shutret
= shutdown(sock
, SHUT_RDWR
);
299 ERR("Socket shutdown error");
300 } else if (ret
> 0) {
303 /* ret = 0 means an orderly shutdown. */
309 * ustcomm_send_unix_sock
311 * Send buf data of size len. Using sendmsg API.
312 * Return the size of sent data.
314 ssize_t
ustcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
320 memset(&msg
, 0, sizeof(msg
));
322 iov
[0].iov_base
= (void *) buf
;
323 iov
[0].iov_len
= len
;
328 * Using the MSG_NOSIGNAL when sending data from sessiond to
329 * libust, so libust does not receive an unhandled SIGPIPE or
330 * SIGURG. The sessiond receiver side can be made more resilient
331 * by ignoring SIGPIPE, but we don't have this luxury on the
335 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
336 } while (ret
< 0 && errno
== EINTR
);
341 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
344 if (ret
== -ECONNRESET
)
347 shutret
= shutdown(sock
, SHUT_RDWR
);
349 ERR("Socket shutdown error");
356 * Send a message accompanied by fd(s) over a unix socket.
358 * Returns the size of data sent, or negative error value.
360 ssize_t
ustcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
363 struct cmsghdr
*cmptr
;
366 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
367 char tmp
[CMSG_SPACE(sizeof_fds
)];
370 memset(&msg
, 0, sizeof(msg
));
371 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
373 if (nb_fd
> USTCOMM_MAX_SEND_FDS
)
376 msg
.msg_control
= (caddr_t
)tmp
;
377 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
379 cmptr
= CMSG_FIRSTHDR(&msg
);
382 cmptr
->cmsg_level
= SOL_SOCKET
;
383 cmptr
->cmsg_type
= SCM_RIGHTS
;
384 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
385 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
386 /* Sum of the length of all control messages in the buffer: */
387 msg
.msg_controllen
= cmptr
->cmsg_len
;
389 iov
[0].iov_base
= &dummy
;
395 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
396 } while (ret
< 0 && errno
== EINTR
);
399 * We consider EPIPE and ECONNRESET as expected.
401 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
405 if (ret
== -ECONNRESET
)
412 * Recv a message accompanied by fd(s) from a unix socket.
414 * Returns the size of received data, or negative error value.
416 * Expect at most "nb_fd" file descriptors. Returns the number of fd
417 * actually received in nb_fd.
418 * Returns -EPIPE on orderly shutdown.
420 ssize_t
ustcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
424 struct cmsghdr
*cmsg
;
425 size_t sizeof_fds
= nb_fd
* sizeof(int);
426 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
430 memset(&msg
, 0, sizeof(msg
));
432 /* Prepare to receive the structures */
433 iov
[0].iov_base
= &dummy
;
437 msg
.msg_control
= recv_fd
;
438 msg
.msg_controllen
= sizeof(recv_fd
);
441 ret
= recvmsg(sock
, &msg
, 0);
442 } while (ret
< 0 && errno
== EINTR
);
444 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
445 PERROR("recvmsg fds");
448 if (ret
== -ECONNRESET
)
453 /* orderly shutdown */
458 ERR("Error: Received %zd bytes, expected %d\n",
462 if (msg
.msg_flags
& MSG_CTRUNC
) {
463 ERR("Error: Control message truncated.\n");
467 cmsg
= CMSG_FIRSTHDR(&msg
);
469 ERR("Error: Invalid control message header\n");
473 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
474 ERR("Didn't received any fd\n");
478 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
479 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
480 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
484 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
490 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
494 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
502 ERR("incorrect message size: %zd\n", len
);
509 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
510 uint32_t expected_handle
, uint32_t expected_cmd
)
514 memset(lur
, 0, sizeof(*lur
));
515 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
517 case 0: /* orderly shutdown */
523 if (lur
->handle
!= expected_handle
) {
524 ERR("Unexpected result message handle: "
525 "expected: %u vs received: %u\n",
526 expected_handle
, lur
->handle
);
529 if (lur
->cmd
!= expected_cmd
) {
530 ERR("Unexpected result message command "
531 "expected: %u vs received: %u\n",
532 expected_cmd
, lur
->cmd
);
538 return lur
->ret_code
;
543 ERR("incorrect message size: %zd\n", len
);
549 int ustcomm_send_app_cmd(int sock
,
550 struct ustcomm_ust_msg
*lum
,
551 struct ustcomm_ust_reply
*lur
)
555 ret
= ustcomm_send_app_msg(sock
, lum
);
558 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
565 * chan_data is allocated internally if this function returns the
568 ssize_t
ustcomm_recv_channel_from_sessiond(int sock
,
569 void **_chan_data
, uint64_t var_len
,
576 if (var_len
> LTTNG_UST_CHANNEL_DATA_MAX_LEN
) {
580 /* Receive variable length data */
581 chan_data
= zmalloc(var_len
);
586 len
= ustcomm_recv_unix_sock(sock
, chan_data
, var_len
);
587 if (len
!= var_len
) {
591 nr_fd
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
601 *_wakeup_fd
= wakeup_fd
;
602 *_chan_data
= chan_data
;
612 int ustcomm_recv_stream_from_sessiond(int sock
,
613 uint64_t *memory_map_size
,
614 int *shm_fd
, int *wakeup_fd
)
620 /* recv shm fd and wakeup fd */
621 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
640 * Returns 0 on success, negative error value on error.
642 int ustcomm_send_reg_msg(int sock
,
643 enum ustctl_socket_type type
,
644 uint32_t bits_per_long
,
645 uint32_t uint8_t_alignment
,
646 uint32_t uint16_t_alignment
,
647 uint32_t uint32_t_alignment
,
648 uint32_t uint64_t_alignment
,
649 uint32_t long_alignment
)
652 struct ustctl_reg_msg reg_msg
;
654 reg_msg
.magic
= LTTNG_UST_COMM_MAGIC
;
655 reg_msg
.major
= LTTNG_UST_ABI_MAJOR_VERSION
;
656 reg_msg
.minor
= LTTNG_UST_ABI_MINOR_VERSION
;
657 reg_msg
.pid
= getpid();
658 reg_msg
.ppid
= getppid();
659 reg_msg
.uid
= getuid();
660 reg_msg
.gid
= getgid();
661 reg_msg
.bits_per_long
= bits_per_long
;
662 reg_msg
.uint8_t_alignment
= uint8_t_alignment
;
663 reg_msg
.uint16_t_alignment
= uint16_t_alignment
;
664 reg_msg
.uint32_t_alignment
= uint32_t_alignment
;
665 reg_msg
.uint64_t_alignment
= uint64_t_alignment
;
666 reg_msg
.long_alignment
= long_alignment
;
667 reg_msg
.socket_type
= type
;
668 lttng_ust_getprocname(reg_msg
.name
);
669 memset(reg_msg
.padding
, 0, sizeof(reg_msg
.padding
));
671 len
= ustcomm_send_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
672 if (len
> 0 && len
!= sizeof(reg_msg
))
680 int serialize_string_encoding(enum ustctl_string_encodings
*ue
,
681 enum lttng_string_encodings le
)
684 case lttng_encode_none
:
685 *ue
= ustctl_encode_none
;
687 case lttng_encode_UTF8
:
688 *ue
= ustctl_encode_UTF8
;
690 case lttng_encode_ASCII
:
691 *ue
= ustctl_encode_ASCII
;
700 int serialize_integer_type(struct ustctl_integer_type
*uit
,
701 const struct lttng_integer_type
*lit
)
703 uit
->size
= lit
->size
;
704 uit
->signedness
= lit
->signedness
;
705 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
706 uit
->base
= lit
->base
;
707 if (serialize_string_encoding(&uit
->encoding
, lit
->encoding
))
709 uit
->alignment
= lit
->alignment
;
714 int serialize_basic_type(struct lttng_session
*session
,
715 enum ustctl_abstract_types
*uatype
,
716 enum lttng_abstract_types atype
,
717 union _ustctl_basic_type
*ubt
,
718 const union _lttng_basic_type
*lbt
)
723 if (serialize_integer_type(&ubt
->integer
, &lbt
->integer
))
725 *uatype
= ustctl_atype_integer
;
730 if (serialize_string_encoding(&ubt
->string
.encoding
,
731 lbt
->string
.encoding
))
733 *uatype
= ustctl_atype_string
;
738 struct ustctl_float_type
*uft
;
739 const struct lttng_float_type
*lft
;
743 uft
->exp_dig
= lft
->exp_dig
;
744 uft
->mant_dig
= lft
->mant_dig
;
745 uft
->alignment
= lft
->alignment
;
746 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
747 *uatype
= ustctl_atype_float
;
752 strncpy(ubt
->enumeration
.name
, lbt
->enumeration
.desc
->name
,
753 LTTNG_UST_SYM_NAME_LEN
);
754 ubt
->enumeration
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
755 if (serialize_integer_type(&ubt
->enumeration
.container_type
,
756 &lbt
->enumeration
.container_type
))
759 const struct lttng_enum
*_enum
;
761 _enum
= lttng_ust_enum_get(session
,
762 lbt
->enumeration
.desc
->name
);
765 ubt
->enumeration
.id
= _enum
->id
;
767 ubt
->enumeration
.id
= -1ULL;
769 *uatype
= ustctl_atype_enum
;
781 int serialize_one_type(struct lttng_session
*session
,
782 struct ustctl_type
*ut
, const struct lttng_type
*lt
)
791 ret
= serialize_basic_type(session
, &ut
->atype
, lt
->atype
,
792 &ut
->u
.basic
, <
->u
.basic
);
798 struct ustctl_basic_type
*ubt
;
799 const struct lttng_basic_type
*lbt
;
802 ubt
= &ut
->u
.array
.elem_type
;
803 lbt
= <
->u
.array
.elem_type
;
804 ut
->u
.array
.length
= lt
->u
.array
.length
;
805 ret
= serialize_basic_type(session
, &ubt
->atype
, lbt
->atype
,
806 &ubt
->u
.basic
, &lbt
->u
.basic
);
809 ut
->atype
= ustctl_atype_array
;
814 struct ustctl_basic_type
*ubt
;
815 const struct lttng_basic_type
*lbt
;
818 ubt
= &ut
->u
.sequence
.length_type
;
819 lbt
= <
->u
.sequence
.length_type
;
820 ret
= serialize_basic_type(session
, &ubt
->atype
, lbt
->atype
,
821 &ubt
->u
.basic
, &lbt
->u
.basic
);
824 ubt
= &ut
->u
.sequence
.elem_type
;
825 lbt
= <
->u
.sequence
.elem_type
;
826 ret
= serialize_basic_type(session
, &ubt
->atype
, lbt
->atype
,
827 &ubt
->u
.basic
, &lbt
->u
.basic
);
830 ut
->atype
= ustctl_atype_sequence
;
840 int serialize_fields(struct lttng_session
*session
,
841 size_t *_nr_write_fields
,
842 struct ustctl_field
**ustctl_fields
,
844 const struct lttng_event_field
*lttng_fields
)
846 struct ustctl_field
*fields
;
848 size_t nr_write_fields
= 0;
850 fields
= zmalloc(nr_fields
* sizeof(*fields
));
854 for (i
= 0; i
< nr_fields
; i
++) {
855 struct ustctl_field
*f
;
856 const struct lttng_event_field
*lf
;
858 f
= &fields
[nr_write_fields
];
859 lf
= <tng_fields
[i
];
861 /* skip 'nowrite' fields */
864 strncpy(f
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
865 f
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
866 ret
= serialize_one_type(session
, &f
->type
, &lf
->type
);
872 *_nr_write_fields
= nr_write_fields
;
873 *ustctl_fields
= fields
;
882 int serialize_entries(struct ustctl_enum_entry
**_entries
,
884 const struct lttng_enum_entry
*lttng_entries
)
886 struct ustctl_enum_entry
*entries
;
889 /* Serialize the entries */
890 entries
= zmalloc(nr_entries
* sizeof(*entries
));
893 for (i
= 0; i
< nr_entries
; i
++) {
894 struct ustctl_enum_entry
*uentry
;
895 const struct lttng_enum_entry
*lentry
;
897 uentry
= &entries
[i
];
898 lentry
= <tng_entries
[i
];
900 uentry
->start
= lentry
->start
;
901 uentry
->end
= lentry
->end
;
902 strncpy(uentry
->string
, lentry
->string
, LTTNG_UST_SYM_NAME_LEN
);
903 uentry
->string
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
910 int serialize_ctx_fields(size_t *_nr_write_fields
,
911 struct ustctl_field
**ustctl_fields
,
913 const struct lttng_ctx_field
*lttng_fields
)
915 struct ustctl_field
*fields
;
917 size_t nr_write_fields
= 0;
919 fields
= zmalloc(nr_fields
* sizeof(*fields
));
923 for (i
= 0; i
< nr_fields
; i
++) {
924 struct ustctl_field
*f
;
925 const struct lttng_event_field
*lf
;
927 f
= &fields
[nr_write_fields
];
928 lf
= <tng_fields
[i
].event_field
;
930 /* skip 'nowrite' fields */
933 strncpy(f
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
934 f
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
935 ret
= serialize_one_type(NULL
, &f
->type
, &lf
->type
);
941 *_nr_write_fields
= nr_write_fields
;
942 *ustctl_fields
= fields
;
951 * Returns 0 on success, negative error value on error.
953 int ustcomm_register_event(int sock
,
954 struct lttng_session
*session
,
955 int session_objd
, /* session descriptor */
956 int channel_objd
, /* channel descriptor */
957 const char *event_name
, /* event name (input) */
959 const char *signature
, /* event signature (input) */
960 size_t nr_fields
, /* fields */
961 const struct lttng_event_field
*lttng_fields
,
962 const char *model_emf_uri
,
963 uint32_t *id
) /* event id (output) */
967 struct ustcomm_notify_hdr header
;
968 struct ustcomm_notify_event_msg m
;
971 struct ustcomm_notify_hdr header
;
972 struct ustcomm_notify_event_reply r
;
974 size_t signature_len
, fields_len
, model_emf_uri_len
;
975 struct ustctl_field
*fields
= NULL
;
976 size_t nr_write_fields
= 0;
979 memset(&msg
, 0, sizeof(msg
));
980 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
981 msg
.m
.session_objd
= session_objd
;
982 msg
.m
.channel_objd
= channel_objd
;
983 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_SYM_NAME_LEN
);
984 msg
.m
.event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
985 msg
.m
.loglevel
= loglevel
;
986 signature_len
= strlen(signature
) + 1;
987 msg
.m
.signature_len
= signature_len
;
989 /* Calculate fields len, serialize fields. */
991 ret
= serialize_fields(session
, &nr_write_fields
, &fields
,
992 nr_fields
, lttng_fields
);
997 fields_len
= sizeof(*fields
) * nr_write_fields
;
998 msg
.m
.fields_len
= fields_len
;
1000 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
1002 model_emf_uri_len
= 0;
1004 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
1006 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1007 if (len
> 0 && len
!= sizeof(msg
)) {
1016 /* send signature */
1017 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
1018 if (len
> 0 && len
!= signature_len
) {
1028 if (fields_len
> 0) {
1029 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1030 if (len
> 0 && len
!= fields_len
) {
1041 if (model_emf_uri_len
) {
1042 /* send model_emf_uri */
1043 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
1045 if (len
> 0 && len
!= model_emf_uri_len
) {
1054 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1056 case 0: /* orderly shutdown */
1059 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1060 ERR("Unexpected result message command "
1061 "expected: %u vs received: %u\n",
1062 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1065 if (reply
.r
.ret_code
> 0)
1067 if (reply
.r
.ret_code
< 0)
1068 return reply
.r
.ret_code
;
1069 *id
= reply
.r
.event_id
;
1070 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1071 event_name
, reply
.r
.ret_code
, reply
.r
.event_id
);
1075 /* Transport level error */
1076 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1080 ERR("incorrect message size: %zd\n", len
);
1086 /* Error path only. */
1093 * Returns 0 on success, negative error value on error.
1094 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1096 int ustcomm_register_enum(int sock
,
1097 int session_objd
, /* session descriptor */
1098 const char *enum_name
, /* enum name (input) */
1099 size_t nr_entries
, /* entries */
1100 const struct lttng_enum_entry
*lttng_entries
,
1105 struct ustcomm_notify_hdr header
;
1106 struct ustcomm_notify_enum_msg m
;
1109 struct ustcomm_notify_hdr header
;
1110 struct ustcomm_notify_enum_reply r
;
1113 struct ustctl_enum_entry
*entries
= NULL
;
1116 memset(&msg
, 0, sizeof(msg
));
1117 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
1118 msg
.m
.session_objd
= session_objd
;
1119 strncpy(msg
.m
.enum_name
, enum_name
, LTTNG_UST_SYM_NAME_LEN
);
1120 msg
.m
.enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
1122 /* Calculate entries len, serialize entries. */
1123 if (nr_entries
> 0) {
1124 ret
= serialize_entries(&entries
,
1125 nr_entries
, lttng_entries
);
1130 entries_len
= sizeof(*entries
) * nr_entries
;
1131 msg
.m
.entries_len
= entries_len
;
1133 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1134 if (len
> 0 && len
!= sizeof(msg
)) {
1144 if (entries_len
> 0) {
1145 len
= ustcomm_send_unix_sock(sock
, entries
, entries_len
);
1146 if (len
> 0 && len
!= entries_len
) {
1159 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1161 case 0: /* orderly shutdown */
1164 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1165 ERR("Unexpected result message command "
1166 "expected: %u vs received: %u\n",
1167 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1170 if (reply
.r
.ret_code
> 0)
1172 if (reply
.r
.ret_code
< 0)
1173 return reply
.r
.ret_code
;
1174 *id
= reply
.r
.enum_id
;
1175 DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
1176 enum_name
, reply
.r
.ret_code
);
1180 /* Transport level error */
1181 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1185 ERR("incorrect message size: %zd\n", len
);
1197 * Returns 0 on success, negative error value on error.
1198 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1200 int ustcomm_register_channel(int sock
,
1201 int session_objd
, /* session descriptor */
1202 int channel_objd
, /* channel descriptor */
1203 size_t nr_ctx_fields
,
1204 const struct lttng_ctx_field
*ctx_fields
,
1205 uint32_t *chan_id
, /* channel id (output) */
1206 int *header_type
) /* header type (output) */
1210 struct ustcomm_notify_hdr header
;
1211 struct ustcomm_notify_channel_msg m
;
1214 struct ustcomm_notify_hdr header
;
1215 struct ustcomm_notify_channel_reply r
;
1218 struct ustctl_field
*fields
= NULL
;
1220 size_t nr_write_fields
= 0;
1222 memset(&msg
, 0, sizeof(msg
));
1223 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1224 msg
.m
.session_objd
= session_objd
;
1225 msg
.m
.channel_objd
= channel_objd
;
1227 /* Calculate fields len, serialize fields. */
1228 if (nr_ctx_fields
> 0) {
1229 ret
= serialize_ctx_fields(&nr_write_fields
, &fields
,
1230 nr_ctx_fields
, ctx_fields
);
1235 fields_len
= sizeof(*fields
) * nr_write_fields
;
1236 msg
.m
.ctx_fields_len
= fields_len
;
1237 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1238 if (len
> 0 && len
!= sizeof(msg
)) {
1248 if (fields_len
> 0) {
1249 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1251 if (len
> 0 && len
!= fields_len
) {
1261 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1263 case 0: /* orderly shutdown */
1266 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1267 ERR("Unexpected result message command "
1268 "expected: %u vs received: %u\n",
1269 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1272 if (reply
.r
.ret_code
> 0)
1274 if (reply
.r
.ret_code
< 0)
1275 return reply
.r
.ret_code
;
1276 *chan_id
= reply
.r
.chan_id
;
1277 switch (reply
.r
.header_type
) {
1280 *header_type
= reply
.r
.header_type
;
1283 ERR("Unexpected channel header type %u\n",
1284 reply
.r
.header_type
);
1287 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1288 reply
.r
.chan_id
, reply
.r
.header_type
);
1292 /* Transport level error */
1293 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1297 ERR("incorrect message size: %zd\n", len
);
1304 * Set socket reciving timeout.
1306 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1311 tv
.tv_sec
= msec
/ 1000;
1312 tv
.tv_usec
= (msec
* 1000 % 1000000);
1314 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1316 PERROR("setsockopt SO_RCVTIMEO");
1324 * Set socket sending timeout.
1326 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1331 tv
.tv_sec
= msec
/ 1000;
1332 tv
.tv_usec
= (msec
* 1000) % 1000000;
1334 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1336 PERROR("setsockopt SO_SNDTIMEO");