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
, long timeout
)
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);
105 /* Give at least 10ms. */
108 ret
= ustcomm_setsockopt_snd_timeout(fd
, timeout
);
110 WARN("Error setting connect socket send timeout");
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 int serialize_string_encoding(enum ustctl_string_encodings
*ue
,
690 enum lttng_string_encodings le
)
693 case lttng_encode_none
:
694 *ue
= ustctl_encode_none
;
696 case lttng_encode_UTF8
:
697 *ue
= ustctl_encode_UTF8
;
699 case lttng_encode_ASCII
:
700 *ue
= ustctl_encode_ASCII
;
709 int serialize_basic_type(enum ustctl_abstract_types
*uatype
,
710 enum lttng_abstract_types atype
,
711 union _ustctl_basic_type
*ubt
,
712 const union _lttng_basic_type
*lbt
)
717 struct ustctl_integer_type
*uit
;
718 const struct lttng_integer_type
*lit
;
722 uit
->size
= lit
->size
;
723 uit
->signedness
= lit
->signedness
;
724 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
725 uit
->base
= lit
->base
;
726 if (serialize_string_encoding(&uit
->encoding
, lit
->encoding
))
728 uit
->alignment
= lit
->alignment
;
729 *uatype
= ustctl_atype_integer
;
734 if (serialize_string_encoding(&ubt
->string
.encoding
,
735 lbt
->string
.encoding
))
737 *uatype
= ustctl_atype_string
;
742 struct ustctl_float_type
*uft
;
743 const struct lttng_float_type
*lft
;
747 uft
->exp_dig
= lft
->exp_dig
;
748 uft
->mant_dig
= lft
->mant_dig
;
749 uft
->alignment
= lft
->alignment
;
750 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
751 *uatype
= ustctl_atype_float
;
764 int serialize_one_type(struct ustctl_type
*ut
, const struct lttng_type
*lt
)
772 ret
= serialize_basic_type(&ut
->atype
, lt
->atype
,
773 &ut
->u
.basic
, <
->u
.basic
);
779 struct ustctl_basic_type
*ubt
;
780 const struct lttng_basic_type
*lbt
;
783 ubt
= &ut
->u
.array
.elem_type
;
784 lbt
= <
->u
.array
.elem_type
;
785 ut
->u
.array
.length
= lt
->u
.array
.length
;
786 ret
= serialize_basic_type(&ubt
->atype
, lbt
->atype
,
787 &ubt
->u
.basic
, &lbt
->u
.basic
);
790 ut
->atype
= ustctl_atype_array
;
795 struct ustctl_basic_type
*ubt
;
796 const struct lttng_basic_type
*lbt
;
799 ubt
= &ut
->u
.sequence
.length_type
;
800 lbt
= <
->u
.sequence
.length_type
;
801 ret
= serialize_basic_type(&ubt
->atype
, lbt
->atype
,
802 &ubt
->u
.basic
, &lbt
->u
.basic
);
805 ubt
= &ut
->u
.sequence
.elem_type
;
806 lbt
= <
->u
.sequence
.elem_type
;
807 ret
= serialize_basic_type(&ubt
->atype
, lbt
->atype
,
808 &ubt
->u
.basic
, &lbt
->u
.basic
);
811 ut
->atype
= ustctl_atype_sequence
;
822 int serialize_fields(size_t *_nr_write_fields
,
823 struct ustctl_field
**ustctl_fields
,
825 const struct lttng_event_field
*lttng_fields
)
827 struct ustctl_field
*fields
;
829 size_t nr_write_fields
= 0;
831 fields
= zmalloc(nr_fields
* sizeof(*fields
));
835 for (i
= 0; i
< nr_fields
; i
++) {
836 struct ustctl_field
*f
;
837 const struct lttng_event_field
*lf
;
839 f
= &fields
[nr_write_fields
];
840 lf
= <tng_fields
[i
];
842 /* skip 'nowrite' fields */
845 strncpy(f
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
846 f
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
847 ret
= serialize_one_type(&f
->type
, &lf
->type
);
853 *_nr_write_fields
= nr_write_fields
;
854 *ustctl_fields
= fields
;
863 int serialize_ctx_fields(size_t *_nr_write_fields
,
864 struct ustctl_field
**ustctl_fields
,
866 const struct lttng_ctx_field
*lttng_fields
)
868 struct ustctl_field
*fields
;
870 size_t nr_write_fields
= 0;
872 fields
= zmalloc(nr_fields
* sizeof(*fields
));
876 for (i
= 0; i
< nr_fields
; i
++) {
877 struct ustctl_field
*f
;
878 const struct lttng_event_field
*lf
;
880 f
= &fields
[nr_write_fields
];
881 lf
= <tng_fields
[i
].event_field
;
883 /* skip 'nowrite' fields */
886 strncpy(f
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
887 f
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
888 ret
= serialize_one_type(&f
->type
, &lf
->type
);
894 *_nr_write_fields
= nr_write_fields
;
895 *ustctl_fields
= fields
;
904 * Returns 0 on success, negative error value on error.
906 int ustcomm_register_event(int sock
,
907 int session_objd
, /* session descriptor */
908 int channel_objd
, /* channel descriptor */
909 const char *event_name
, /* event name (input) */
911 const char *signature
, /* event signature (input) */
912 size_t nr_fields
, /* fields */
913 const struct lttng_event_field
*lttng_fields
,
914 const char *model_emf_uri
,
915 uint32_t *id
) /* event id (output) */
919 struct ustcomm_notify_hdr header
;
920 struct ustcomm_notify_event_msg m
;
923 struct ustcomm_notify_hdr header
;
924 struct ustcomm_notify_event_reply r
;
926 size_t signature_len
, fields_len
, model_emf_uri_len
;
927 struct ustctl_field
*fields
= NULL
;
928 size_t nr_write_fields
= 0;
931 memset(&msg
, 0, sizeof(msg
));
932 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
933 msg
.m
.session_objd
= session_objd
;
934 msg
.m
.channel_objd
= channel_objd
;
935 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_SYM_NAME_LEN
);
936 msg
.m
.event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
937 msg
.m
.loglevel
= loglevel
;
938 signature_len
= strlen(signature
) + 1;
939 msg
.m
.signature_len
= signature_len
;
941 /* Calculate fields len, serialize fields. */
943 ret
= serialize_fields(&nr_write_fields
, &fields
,
944 nr_fields
, lttng_fields
);
949 fields_len
= sizeof(*fields
) * nr_write_fields
;
950 msg
.m
.fields_len
= fields_len
;
952 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
954 model_emf_uri_len
= 0;
956 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
957 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
958 if (len
> 0 && len
!= sizeof(msg
)) {
968 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
969 if (len
> 0 && len
!= signature_len
) {
979 if (fields_len
> 0) {
980 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
981 if (len
> 0 && len
!= fields_len
) {
992 if (model_emf_uri_len
) {
993 /* send model_emf_uri */
994 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
996 if (len
> 0 && len
!= model_emf_uri_len
) {
1005 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1007 case 0: /* orderly shutdown */
1010 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1011 ERR("Unexpected result message command "
1012 "expected: %u vs received: %u\n",
1013 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1016 if (reply
.r
.ret_code
> 0)
1018 if (reply
.r
.ret_code
< 0)
1019 return reply
.r
.ret_code
;
1020 *id
= reply
.r
.event_id
;
1021 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
1022 event_name
, reply
.r
.ret_code
, reply
.r
.event_id
);
1026 /* Transport level error */
1027 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1031 ERR("incorrect message size: %zd\n", len
);
1037 /* Error path only. */
1044 * Returns 0 on success, negative error value on error.
1045 * Returns -EPIPE or -ECONNRESET if other end has hung up.
1047 int ustcomm_register_channel(int sock
,
1048 int session_objd
, /* session descriptor */
1049 int channel_objd
, /* channel descriptor */
1050 size_t nr_ctx_fields
,
1051 const struct lttng_ctx_field
*ctx_fields
,
1052 uint32_t *chan_id
, /* channel id (output) */
1053 int *header_type
) /* header type (output) */
1057 struct ustcomm_notify_hdr header
;
1058 struct ustcomm_notify_channel_msg m
;
1061 struct ustcomm_notify_hdr header
;
1062 struct ustcomm_notify_channel_reply r
;
1065 struct ustctl_field
*fields
= NULL
;
1067 size_t nr_write_fields
= 0;
1069 memset(&msg
, 0, sizeof(msg
));
1070 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
1071 msg
.m
.session_objd
= session_objd
;
1072 msg
.m
.channel_objd
= channel_objd
;
1074 /* Calculate fields len, serialize fields. */
1075 if (nr_ctx_fields
> 0) {
1076 ret
= serialize_ctx_fields(&nr_write_fields
, &fields
,
1077 nr_ctx_fields
, ctx_fields
);
1082 fields_len
= sizeof(*fields
) * nr_write_fields
;
1083 msg
.m
.ctx_fields_len
= fields_len
;
1084 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
1085 if (len
> 0 && len
!= sizeof(msg
)) {
1095 if (fields_len
> 0) {
1096 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
1098 if (len
> 0 && len
!= fields_len
) {
1108 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
1110 case 0: /* orderly shutdown */
1113 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
1114 ERR("Unexpected result message command "
1115 "expected: %u vs received: %u\n",
1116 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
1119 if (reply
.r
.ret_code
> 0)
1121 if (reply
.r
.ret_code
< 0)
1122 return reply
.r
.ret_code
;
1123 *chan_id
= reply
.r
.chan_id
;
1124 switch (reply
.r
.header_type
) {
1127 *header_type
= reply
.r
.header_type
;
1130 ERR("Unexpected channel header type %u\n",
1131 reply
.r
.header_type
);
1134 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1135 reply
.r
.chan_id
, reply
.r
.header_type
);
1139 /* Transport level error */
1140 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1144 ERR("incorrect message size: %zd\n", len
);
1151 * Set socket reciving timeout.
1153 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1158 tv
.tv_sec
= msec
/ 1000;
1159 tv
.tv_usec
= (msec
* 1000 % 1000000);
1161 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1163 PERROR("setsockopt SO_RCVTIMEO");
1171 * Set socket sending timeout.
1173 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1178 tv
.tv_sec
= msec
/ 1000;
1179 tv
.tv_usec
= (msec
* 1000) % 1000000;
1181 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1183 PERROR("setsockopt SO_SNDTIMEO");