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 error, because connect
120 * is used in normal execution to detect if sessiond is
123 if (errno
!= ECONNREFUSED
&& errno
!= ECONNRESET
)
126 if (ret
== -ECONNREFUSED
|| ret
== -ECONNRESET
)
138 closeret
= close(fd
);
147 * ustcomm_accept_unix_sock
149 * Do an accept(2) on the sock and return the
150 * new file descriptor. The socket MUST be bind(2) before.
152 int ustcomm_accept_unix_sock(int sock
)
155 struct sockaddr_un sun
;
159 new_fd
= accept(sock
, (struct sockaddr
*) &sun
, &len
);
161 if (errno
!= ECONNABORTED
)
164 if (new_fd
== -ECONNABORTED
)
171 * ustcomm_create_unix_sock
173 * Creates a AF_UNIX local socket using pathname
174 * bind the socket upon creation and return the fd.
176 int ustcomm_create_unix_sock(const char *pathname
)
178 struct sockaddr_un sun
;
181 /* Create server socket */
182 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
188 memset(&sun
, 0, sizeof(sun
));
189 sun
.sun_family
= AF_UNIX
;
190 strncpy(sun
.sun_path
, pathname
, sizeof(sun
.sun_path
));
191 sun
.sun_path
[sizeof(sun
.sun_path
) - 1] = '\0';
193 /* Unlink the old file if present */
194 (void) unlink(pathname
);
195 ret
= bind(fd
, (struct sockaddr
*) &sun
, sizeof(sun
));
208 closeret
= close(fd
);
218 * ustcomm_listen_unix_sock
220 * Make the socket listen using LTTNG_UST_COMM_MAX_LISTEN.
222 int ustcomm_listen_unix_sock(int sock
)
226 ret
= listen(sock
, LTTNG_UST_COMM_MAX_LISTEN
);
236 * ustcomm_close_unix_sock
238 * Shutdown cleanly a unix socket.
240 int ustcomm_close_unix_sock(int sock
)
254 * ustcomm_recv_unix_sock
256 * Receive data of size len in put that data into
257 * the buf param. Using recvmsg API.
258 * Return the size of received data.
259 * Return 0 on orderly shutdown.
261 ssize_t
ustcomm_recv_unix_sock(int sock
, void *buf
, size_t len
)
267 memset(&msg
, 0, sizeof(msg
));
269 iov
[0].iov_base
= buf
;
270 iov
[0].iov_len
= len
;
275 ret
= recvmsg(sock
, &msg
, 0);
276 } while (ret
< 0 && errno
== EINTR
);
281 if (errno
!= EPIPE
&& errno
!= ECONNRESET
&& errno
!= ECONNREFUSED
)
284 if (ret
== -ECONNRESET
|| ret
== -ECONNREFUSED
)
287 shutret
= shutdown(sock
, SHUT_RDWR
);
289 ERR("Socket shutdown error");
296 * ustcomm_send_unix_sock
298 * Send buf data of size len. Using sendmsg API.
299 * Return the size of sent data.
301 ssize_t
ustcomm_send_unix_sock(int sock
, const void *buf
, size_t len
)
307 memset(&msg
, 0, sizeof(msg
));
309 iov
[0].iov_base
= (void *) buf
;
310 iov
[0].iov_len
= len
;
315 * Using the MSG_NOSIGNAL when sending data from sessiond to
316 * libust, so libust does not receive an unhandled SIGPIPE or
317 * SIGURG. The sessiond receiver side can be made more resilient
318 * by ignoring SIGPIPE, but we don't have this luxury on the
322 ret
= sendmsg(sock
, &msg
, MSG_NOSIGNAL
);
323 } while (ret
< 0 && errno
== EINTR
);
328 if (errno
!= EPIPE
&& errno
!= ECONNRESET
)
331 if (ret
== -ECONNRESET
)
334 shutret
= shutdown(sock
, SHUT_RDWR
);
336 ERR("Socket shutdown error");
343 * Send a message accompanied by fd(s) over a unix socket.
345 * Returns the size of data sent, or negative error value.
347 ssize_t
ustcomm_send_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
350 struct cmsghdr
*cmptr
;
353 unsigned int sizeof_fds
= nb_fd
* sizeof(int);
354 char tmp
[CMSG_SPACE(sizeof_fds
)];
357 memset(&msg
, 0, sizeof(msg
));
358 memset(tmp
, 0, CMSG_SPACE(sizeof_fds
) * sizeof(char));
360 if (nb_fd
> USTCOMM_MAX_SEND_FDS
)
363 msg
.msg_control
= (caddr_t
)tmp
;
364 msg
.msg_controllen
= CMSG_LEN(sizeof_fds
);
366 cmptr
= CMSG_FIRSTHDR(&msg
);
367 cmptr
->cmsg_level
= SOL_SOCKET
;
368 cmptr
->cmsg_type
= SCM_RIGHTS
;
369 cmptr
->cmsg_len
= CMSG_LEN(sizeof_fds
);
370 memcpy(CMSG_DATA(cmptr
), fds
, sizeof_fds
);
371 /* Sum of the length of all control messages in the buffer: */
372 msg
.msg_controllen
= cmptr
->cmsg_len
;
374 iov
[0].iov_base
= &dummy
;
380 ret
= sendmsg(sock
, &msg
, 0);
381 } while (ret
< 0 && errno
== EINTR
);
384 * We consider EPIPE and ECONNRESET as expected.
386 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
390 if (ret
== -ECONNRESET
)
397 * Recv a message accompanied by fd(s) from a unix socket.
399 * Returns the size of received data, or negative error value.
401 * Expect at most "nb_fd" file descriptors. Returns the number of fd
402 * actually received in nb_fd.
403 * Returns -EPIPE on orderly shutdown.
405 ssize_t
ustcomm_recv_fds_unix_sock(int sock
, int *fds
, size_t nb_fd
)
409 struct cmsghdr
*cmsg
;
410 size_t sizeof_fds
= nb_fd
* sizeof(int);
411 char recv_fd
[CMSG_SPACE(sizeof_fds
)];
415 memset(&msg
, 0, sizeof(msg
));
417 /* Prepare to receive the structures */
418 iov
[0].iov_base
= &dummy
;
422 msg
.msg_control
= recv_fd
;
423 msg
.msg_controllen
= sizeof(recv_fd
);
426 ret
= recvmsg(sock
, &msg
, 0);
427 } while (ret
< 0 && errno
== EINTR
);
429 if (errno
!= EPIPE
&& errno
!= ECONNRESET
) {
430 PERROR("recvmsg fds");
433 if (ret
== -ECONNRESET
)
438 /* orderly shutdown */
443 ERR("Error: Received %zd bytes, expected %d\n",
447 if (msg
.msg_flags
& MSG_CTRUNC
) {
448 ERR("Error: Control message truncated.\n");
452 cmsg
= CMSG_FIRSTHDR(&msg
);
454 ERR("Error: Invalid control message header\n");
458 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
459 ERR("Didn't received any fd\n");
463 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof_fds
)) {
464 ERR("Error: Received %zu bytes of ancillary data, expected %zu\n",
465 (size_t) cmsg
->cmsg_len
, (size_t) CMSG_LEN(sizeof_fds
));
469 memcpy(fds
, CMSG_DATA(cmsg
), sizeof_fds
);
475 int ustcomm_send_app_msg(int sock
, struct ustcomm_ust_msg
*lum
)
479 len
= ustcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
487 ERR("incorrect message size: %zd\n", len
);
494 int ustcomm_recv_app_reply(int sock
, struct ustcomm_ust_reply
*lur
,
495 uint32_t expected_handle
, uint32_t expected_cmd
)
499 memset(lur
, 0, sizeof(*lur
));
500 len
= ustcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
502 case 0: /* orderly shutdown */
505 if (lur
->handle
!= expected_handle
) {
506 ERR("Unexpected result message handle: "
507 "expected: %u vs received: %u\n",
508 expected_handle
, lur
->handle
);
511 if (lur
->cmd
!= expected_cmd
) {
512 ERR("Unexpected result message command "
513 "expected: %u vs received: %u\n",
514 expected_cmd
, lur
->cmd
);
517 return lur
->ret_code
;
520 ERR("incorrect message size: %zd\n", len
);
526 int ustcomm_send_app_cmd(int sock
,
527 struct ustcomm_ust_msg
*lum
,
528 struct ustcomm_ust_reply
*lur
)
532 ret
= ustcomm_send_app_msg(sock
, lum
);
535 ret
= ustcomm_recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
542 * chan_data is allocated internally if this function returns the
545 ssize_t
ustcomm_recv_channel_from_sessiond(int sock
,
546 void **_chan_data
, uint64_t var_len
)
551 if (var_len
> LTTNG_UST_CHANNEL_DATA_MAX_LEN
) {
555 /* Receive variable length data */
556 chan_data
= zmalloc(var_len
);
561 len
= ustcomm_recv_unix_sock(sock
, chan_data
, var_len
);
562 if (len
!= var_len
) {
565 *_chan_data
= chan_data
;
575 int ustcomm_recv_stream_from_sessiond(int sock
,
576 uint64_t *memory_map_size
,
577 int *shm_fd
, int *wakeup_fd
)
583 /* recv shm fd and wakeup fd */
584 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
603 * Returns 0 on success, negative error value on error.
605 int ustcomm_send_reg_msg(int sock
,
606 enum ustctl_socket_type type
,
607 uint32_t bits_per_long
,
608 uint32_t uint8_t_alignment
,
609 uint32_t uint16_t_alignment
,
610 uint32_t uint32_t_alignment
,
611 uint32_t uint64_t_alignment
,
612 uint32_t long_alignment
)
615 struct ustctl_reg_msg reg_msg
;
617 reg_msg
.magic
= LTTNG_UST_COMM_MAGIC
;
618 reg_msg
.major
= LTTNG_UST_ABI_MAJOR_VERSION
;
619 reg_msg
.minor
= LTTNG_UST_ABI_MINOR_VERSION
;
620 reg_msg
.pid
= getpid();
621 reg_msg
.ppid
= getppid();
622 reg_msg
.uid
= getuid();
623 reg_msg
.gid
= getgid();
624 reg_msg
.bits_per_long
= bits_per_long
;
625 reg_msg
.uint8_t_alignment
= uint8_t_alignment
;
626 reg_msg
.uint16_t_alignment
= uint16_t_alignment
;
627 reg_msg
.uint32_t_alignment
= uint32_t_alignment
;
628 reg_msg
.uint64_t_alignment
= uint64_t_alignment
;
629 reg_msg
.long_alignment
= long_alignment
;
630 reg_msg
.socket_type
= type
;
631 lttng_ust_getprocname(reg_msg
.name
);
632 memset(reg_msg
.padding
, 0, sizeof(reg_msg
.padding
));
634 len
= ustcomm_send_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
635 if (len
> 0 && len
!= sizeof(reg_msg
))
643 int serialize_basic_type(enum ustctl_abstract_types
*uatype
,
644 enum lttng_abstract_types atype
,
645 union _ustctl_basic_type
*ubt
,
646 const union _lttng_basic_type
*lbt
)
651 struct ustctl_integer_type
*uit
;
652 const struct lttng_integer_type
*lit
;
656 uit
->size
= lit
->size
;
657 uit
->signedness
= lit
->signedness
;
658 uit
->reverse_byte_order
= lit
->reverse_byte_order
;
659 uit
->base
= lit
->base
;
660 uit
->encoding
= lit
->encoding
;
661 uit
->alignment
= lit
->alignment
;
662 *uatype
= ustctl_atype_integer
;
667 ubt
->string
.encoding
= lbt
->string
.encoding
;
668 *uatype
= ustctl_atype_string
;
673 struct ustctl_float_type
*uft
;
674 const struct lttng_float_type
*lft
;
678 uft
->exp_dig
= lft
->exp_dig
;
679 uft
->mant_dig
= lft
->mant_dig
;
680 uft
->alignment
= lft
->alignment
;
681 uft
->reverse_byte_order
= lft
->reverse_byte_order
;
682 *uatype
= ustctl_atype_float
;
695 int serialize_one_type(struct ustctl_type
*ut
, const struct lttng_type
*lt
)
703 ret
= serialize_basic_type(&ut
->atype
, lt
->atype
,
704 &ut
->u
.basic
, <
->u
.basic
);
710 struct ustctl_basic_type
*ubt
;
711 const struct lttng_basic_type
*lbt
;
714 ubt
= &ut
->u
.array
.elem_type
;
715 lbt
= <
->u
.array
.elem_type
;
716 ut
->u
.array
.length
= lt
->u
.array
.length
;
717 ret
= serialize_basic_type(&ubt
->atype
, lbt
->atype
,
718 &ubt
->u
.basic
, &lbt
->u
.basic
);
721 ut
->atype
= ustctl_atype_array
;
726 struct ustctl_basic_type
*ubt
;
727 const struct lttng_basic_type
*lbt
;
730 ubt
= &ut
->u
.sequence
.length_type
;
731 lbt
= <
->u
.sequence
.length_type
;
732 ret
= serialize_basic_type(&ubt
->atype
, lbt
->atype
,
733 &ubt
->u
.basic
, &lbt
->u
.basic
);
736 ubt
= &ut
->u
.sequence
.elem_type
;
737 lbt
= <
->u
.sequence
.elem_type
;
738 ret
= serialize_basic_type(&ubt
->atype
, lbt
->atype
,
739 &ubt
->u
.basic
, &lbt
->u
.basic
);
742 ut
->atype
= ustctl_atype_sequence
;
753 int serialize_fields(size_t *_nr_write_fields
,
754 struct ustctl_field
**ustctl_fields
,
756 const struct lttng_event_field
*lttng_fields
)
758 struct ustctl_field
*fields
;
760 size_t nr_write_fields
= 0;
762 fields
= zmalloc(nr_fields
* sizeof(*fields
));
766 for (i
= 0; i
< nr_fields
; i
++) {
767 struct ustctl_field
*f
;
768 const struct lttng_event_field
*lf
;
770 f
= &fields
[nr_write_fields
];
771 lf
= <tng_fields
[i
];
773 /* skip 'nowrite' fields */
776 strncpy(f
->name
, lf
->name
, LTTNG_UST_SYM_NAME_LEN
);
777 f
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
778 ret
= serialize_one_type(&f
->type
, &lf
->type
);
784 *_nr_write_fields
= nr_write_fields
;
785 *ustctl_fields
= fields
;
794 * Returns 0 on success, negative error value on error.
796 int ustcomm_register_event(int sock
,
797 int session_objd
, /* session descriptor */
798 int channel_objd
, /* channel descriptor */
799 const char *event_name
, /* event name (input) */
801 const char *signature
, /* event signature (input) */
802 size_t nr_fields
, /* fields */
803 const struct lttng_event_field
*lttng_fields
,
804 const char *model_emf_uri
,
805 uint32_t *id
) /* event id (output) */
809 struct ustcomm_notify_hdr header
;
810 struct ustcomm_notify_event_msg m
;
813 struct ustcomm_notify_hdr header
;
814 struct ustcomm_notify_event_reply r
;
816 size_t signature_len
, fields_len
, model_emf_uri_len
;
817 struct ustctl_field
*fields
;
818 size_t nr_write_fields
= 0;
821 memset(&msg
, 0, sizeof(msg
));
822 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
823 msg
.m
.session_objd
= session_objd
;
824 msg
.m
.channel_objd
= channel_objd
;
825 strncpy(msg
.m
.event_name
, event_name
, LTTNG_UST_SYM_NAME_LEN
);
826 msg
.m
.event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
827 msg
.m
.loglevel
= loglevel
;
828 signature_len
= strlen(signature
) + 1;
829 msg
.m
.signature_len
= signature_len
;
831 /* Calculate fields len, serialize fields. */
833 ret
= serialize_fields(&nr_write_fields
, &fields
,
834 nr_fields
, lttng_fields
);
839 fields_len
= sizeof(*fields
) * nr_write_fields
;
840 msg
.m
.fields_len
= fields_len
;
842 model_emf_uri_len
= strlen(model_emf_uri
) + 1;
844 model_emf_uri_len
= 0;
846 msg
.m
.model_emf_uri_len
= model_emf_uri_len
;
847 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
848 if (len
> 0 && len
!= sizeof(msg
)) {
858 len
= ustcomm_send_unix_sock(sock
, signature
, signature_len
);
859 if (len
> 0 && len
!= signature_len
) {
869 if (fields_len
> 0) {
870 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
872 if (len
> 0 && len
!= fields_len
) {
880 if (model_emf_uri_len
) {
881 /* send model_emf_uri */
882 len
= ustcomm_send_unix_sock(sock
, model_emf_uri
,
884 if (len
> 0 && len
!= model_emf_uri_len
)
891 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
893 case 0: /* orderly shutdown */
896 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
897 ERR("Unexpected result message command "
898 "expected: %u vs received: %u\n",
899 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
902 if (reply
.r
.ret_code
> 0)
904 if (reply
.r
.ret_code
< 0)
905 return reply
.r
.ret_code
;
906 *id
= reply
.r
.event_id
;
907 DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
908 event_name
, reply
.r
.ret_code
, reply
.r
.event_id
);
912 /* Transport level error */
913 if (errno
== EPIPE
|| errno
== ECONNRESET
)
917 ERR("incorrect message size: %zd\n", len
);
924 * Returns 0 on success, negative error value on error.
925 * Returns -EPIPE or -ECONNRESET if other end has hung up.
927 int ustcomm_register_channel(int sock
,
928 int session_objd
, /* session descriptor */
929 int channel_objd
, /* channel descriptor */
930 size_t nr_ctx_fields
,
931 const struct lttng_event_field
*ctx_fields
,
932 uint32_t *chan_id
, /* channel id (output) */
933 int *header_type
) /* header type (output) */
937 struct ustcomm_notify_hdr header
;
938 struct ustcomm_notify_channel_msg m
;
941 struct ustcomm_notify_hdr header
;
942 struct ustcomm_notify_channel_reply r
;
945 struct ustctl_field
*fields
;
947 size_t nr_write_fields
= 0;
949 memset(&msg
, 0, sizeof(msg
));
950 msg
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
951 msg
.m
.session_objd
= session_objd
;
952 msg
.m
.channel_objd
= channel_objd
;
954 /* Calculate fields len, serialize fields. */
955 if (nr_ctx_fields
> 0) {
956 ret
= serialize_fields(&nr_write_fields
, &fields
,
957 nr_ctx_fields
, ctx_fields
);
962 fields_len
= sizeof(*fields
) * nr_write_fields
;
963 msg
.m
.ctx_fields_len
= fields_len
;
964 len
= ustcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
965 if (len
> 0 && len
!= sizeof(msg
)) {
975 if (fields_len
> 0) {
976 len
= ustcomm_send_unix_sock(sock
, fields
, fields_len
);
978 if (len
> 0 && len
!= fields_len
) {
986 len
= ustcomm_recv_unix_sock(sock
, &reply
, sizeof(reply
));
988 case 0: /* orderly shutdown */
991 if (reply
.header
.notify_cmd
!= msg
.header
.notify_cmd
) {
992 ERR("Unexpected result message command "
993 "expected: %u vs received: %u\n",
994 msg
.header
.notify_cmd
, reply
.header
.notify_cmd
);
997 if (reply
.r
.ret_code
> 0)
999 if (reply
.r
.ret_code
< 0)
1000 return reply
.r
.ret_code
;
1001 *chan_id
= reply
.r
.chan_id
;
1002 switch (reply
.r
.header_type
) {
1005 *header_type
= reply
.r
.header_type
;
1008 ERR("Unexpected channel header type %u\n",
1009 reply
.r
.header_type
);
1012 DBG("Sent register channel notification: chan_id %d, header_type %d\n",
1013 reply
.r
.chan_id
, reply
.r
.header_type
);
1017 /* Transport level error */
1018 if (errno
== EPIPE
|| errno
== ECONNRESET
)
1022 ERR("incorrect message size: %zd\n", len
);
1029 * Set socket reciving timeout.
1031 int ustcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
1036 tv
.tv_sec
= msec
/ 1000;
1037 tv
.tv_usec
= (msec
* 1000 % 1000000);
1039 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
1041 PERROR("setsockopt SO_RCVTIMEO");
1049 * Set socket sending timeout.
1051 int ustcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
1056 tv
.tv_sec
= msec
/ 1000;
1057 tv
.tv_usec
= (msec
* 1000) % 1000000;
1059 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
1061 PERROR("setsockopt SO_SNDTIMEO");