From ee4cf6a07ccbbe41ba70baeaca1000c7fc495c3c Mon Sep 17 00:00:00 2001 From: Frediano Ziglio Date: Wed, 15 Apr 2020 13:36:13 +0100 Subject: [PATCH] Fix compatibility with MSG_NOSIGNAL and Darwin Darwin does not have MSG_NOSIGNAL but allows to set a SO_NOSIGPIPE option to disable sending SIGPIPE writing to closed socket. Note that *BSD has the SO_NOSIGPIPE option but does not affect all write calls so instead continue to use MSG_NOSIGNAL instead on that systems. Signed-off-by: Frediano Ziglio --- server/net-utils.c | 12 ++++++++++++ server/net-utils.h | 1 + server/reds.cpp | 1 + server/sys-socket.h | 4 ++++ 4 files changed, 18 insertions(+) diff --git a/server/net-utils.c b/server/net-utils.c index 144bfd8f..78b94886 100644 --- a/server/net-utils.c +++ b/server/net-utils.c @@ -150,3 +150,15 @@ int red_socket_get_no_delay(int fd) return delay_val; } + +/** + * red_socket_set_nosigpipe + * @fd: a socket file descriptor + */ +void red_socket_set_nosigpipe(int fd, bool enable) +{ +#if defined(SO_NOSIGPIPE) && defined(__APPLE__) + int val = !!enable; + setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, (const void *) &val, sizeof(val)); +#endif +} diff --git a/server/net-utils.h b/server/net-utils.h index 5b8c9163..62ff2f23 100644 --- a/server/net-utils.h +++ b/server/net-utils.h @@ -27,6 +27,7 @@ bool red_socket_set_keepalive(int fd, bool enable, int timeout); bool red_socket_set_no_delay(int fd, bool no_delay); int red_socket_get_no_delay(int fd); bool red_socket_set_non_blocking(int fd, bool non_blocking); +void red_socket_set_nosigpipe(int fd, bool enable); SPICE_END_DECLS diff --git a/server/reds.cpp b/server/reds.cpp index 23c17f3d..fb6e7795 100644 --- a/server/reds.cpp +++ b/server/reds.cpp @@ -2401,6 +2401,7 @@ static RedLinkInfo *reds_init_client_connection(RedsState *reds, int socket) } red_socket_set_keepalive(socket, TRUE, KEEPALIVE_TIMEOUT); + red_socket_set_nosigpipe(socket, true); link = g_new0(RedLinkInfo, 1); link->reds = reds; diff --git a/server/sys-socket.h b/server/sys-socket.h index b13ecdee..f8916a2f 100644 --- a/server/sys-socket.h +++ b/server/sys-socket.h @@ -146,4 +146,8 @@ SPICE_END_DECLS #endif +#if defined(SO_NOSIGPIPE) && defined(__APPLE__) +#define MSG_NOSIGNAL 0 +#endif + #endif // RED_SYS_SOCKET_H_