From 8d03dee93cc5a9598e0485344a3ec237d4ae8846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Denis-Courmont?= <remi@remlab.net> Date: Sun, 17 May 2015 10:58:38 +0300 Subject: [PATCH] Use MSG_NOSIGNAL in send()/sendto()/sendmsg() This avoids SIGPIPE firing when writing to a remote-closed connection- oriented socket (it is useless for datagram sockets or on Windows though). SIGPIPE is blocked ins VLC, but not necessarily in LibVLC. The problem remains for write() on broken pipe and sockets. --- bin/rootwrap.c | 4 ++-- include/vlc_network.h | 4 ++++ modules/lua/libs/net.c | 3 ++- src/network/httpd.c | 2 +- src/network/rootbind.c | 2 +- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/bin/rootwrap.c b/bin/rootwrap.c index 37d88396faf5..6e49ee8f2261 100644 --- a/bin/rootwrap.c +++ b/bin/rootwrap.c @@ -68,7 +68,7 @@ static inline int is_allowed_port (uint16_t port) static inline int send_err (int fd, int err) { - return send (fd, &err, sizeof (err), 0) == sizeof (err) ? 0 : -1; + return send(fd, &err, sizeof (err), MSG_NOSIGNAL) == sizeof (err) ? 0 : -1; } /** @@ -99,7 +99,7 @@ static int send_fd (int p, int fd) memcpy (CMSG_DATA (cmsg), &fd, sizeof (fd)); hdr.msg_controllen = cmsg->cmsg_len; - return sendmsg (p, &hdr, 0) == sizeof (val) ? 0 : -1; + return sendmsg(p, &hdr, MSG_NOSIGNAL) == sizeof (val) ? 0 : -1; } diff --git a/include/vlc_network.h b/include/vlc_network.h index b6bd56c8eaf3..c66e2d18b510 100644 --- a/include/vlc_network.h +++ b/include/vlc_network.h @@ -78,6 +78,10 @@ struct msghdr # undef IPV6_JOIN_GROUP #endif +#ifndef MSG_NOSIGNAL +# define MSG_NOSIGNAL 0 +#endif + VLC_API int vlc_socket (int, int, int, bool nonblock) VLC_USED; struct sockaddr; diff --git a/modules/lua/libs/net.c b/modules/lua/libs/net.c index 4d6e87b3da93..f34c0104d402 100644 --- a/modules/lua/libs/net.c +++ b/modules/lua/libs/net.c @@ -294,7 +294,8 @@ static int vlclua_net_send( lua_State *L ) const char *psz_buffer = luaL_checklstring( L, 2, &i_len ); i_len = luaL_optint( L, 3, i_len ); - lua_pushinteger( L, (fd != -1) ? send( fd, psz_buffer, i_len, 0 ) : -1 ); + lua_pushinteger( L, + (fd != -1) ? send( fd, psz_buffer, i_len, MSG_NOSIGNAL ) : -1 ); return 1; } diff --git a/src/network/httpd.c b/src/network/httpd.c index 3d31da59afe1..2af6b4e4ca9e 100644 --- a/src/network/httpd.c +++ b/src/network/httpd.c @@ -1278,7 +1278,7 @@ ssize_t httpd_NetSend (httpd_client_t *cl, const uint8_t *p, size_t i_len) p_tls = cl->p_tls; do val = p_tls ? tls_Send(p_tls, p, i_len) - : send (cl->fd, p, i_len, 0); + : send (cl->fd, p, i_len, MSG_NOSIGNAL); while (val == -1 && errno == EINTR); return val; } diff --git a/src/network/rootbind.c b/src/network/rootbind.c index 89d9328667c1..9c24c970867d 100644 --- a/src/network/rootbind.c +++ b/src/network/rootbind.c @@ -166,7 +166,7 @@ int rootwrap_bind (int family, int socktype, int protocol, memcpy (&ss, addr, (alen > sizeof (ss)) ? sizeof (ss) : alen); pthread_mutex_lock (&mutex); - if (send (sock, &ss, sizeof (ss), 0) != sizeof (ss)) + if (send (sock, &ss, sizeof (ss), MSG_NOSIGNAL) != sizeof (ss)) { pthread_mutex_unlock (&mutex); return -1; -- GitLab