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 <fziglio@redhat.com>
This commit is contained in:
Frediano Ziglio 2020-04-15 13:36:13 +01:00 committed by Frediano Ziglio
parent 2d971c6ec0
commit ee4cf6a07c
4 changed files with 18 additions and 0 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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;

View File

@ -146,4 +146,8 @@ SPICE_END_DECLS
#endif
#if defined(SO_NOSIGPIPE) && defined(__APPLE__)
#define MSG_NOSIGNAL 0
#endif
#endif // RED_SYS_SOCKET_H_