From d566e3ce9bc083dae60ea7bdd3cf2da270e682da Mon Sep 17 00:00:00 2001 From: Mark Andrews Date: Fri, 14 Dec 2007 04:00:53 +0000 Subject: [PATCH] 2279. [bug] Use setsockopt(SO_NOSIGPIPE), when available, to protect applications from receiving spurious SIGPIPE signals when using the resolver. --- CHANGES | 4 ++++ lib/bind/resolv/res_send.c | 16 +++++++++++++++- lib/isc/unix/socket.c | 16 +++++++++++++++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 5d43b45015..982884a8ac 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,7 @@ +2279. [bug] Use setsockopt(SO_NOSIGPIPE), when available, + to protect applications from receiving spurious + SIGPIPE signals when using the resolver. + 2278. [bug] win32: handle the case where Windows returns no searchlist or DNS suffix. [RT #17354] diff --git a/lib/bind/resolv/res_send.c b/lib/bind/resolv/res_send.c index 39dc998da7..ec83d2712c 100644 --- a/lib/bind/resolv/res_send.c +++ b/lib/bind/resolv/res_send.c @@ -70,7 +70,7 @@ #if defined(LIBC_SCCS) && !defined(lint) static const char sccsid[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93"; -static const char rcsid[] = "$Id: res_send.c,v 1.9.18.8 2006/10/16 23:00:58 marka Exp $"; +static const char rcsid[] = "$Id: res_send.c,v 1.9.18.9 2007/12/14 04:00:53 marka Exp $"; #endif /* LIBC_SCCS and not lint */ /*! \file @@ -601,6 +601,9 @@ send_vc(res_state statp, u_short len; u_char *cp; void *tmp; +#ifdef SO_NOSIGPIPE + int on = 1; +#endif nsap = get_nsaddr(statp, ns); nsaplen = get_salen(nsap); @@ -646,6 +649,17 @@ send_vc(res_state statp, return (-1); } } +#ifdef SO_NOSIGPIPE + /* + * Disable generation of SIGPIPE when writing to a closed + * socket. Write should return -1 and set errno to EPIPE + * instead. + * + * Push on even if setsockopt(SO_NOSIGPIPE) fails. + */ + (void)setsockopt(statp->_vcsock, SOL_SOCKET, SO_NOSIGPIPE, &on, + sizeof(on)); +#endif errno = 0; if (connect(statp->_vcsock, nsap, nsaplen) < 0) { *terrno = errno; diff --git a/lib/isc/unix/socket.c b/lib/isc/unix/socket.c index e0b902167e..0e15ab8674 100644 --- a/lib/isc/unix/socket.c +++ b/lib/isc/unix/socket.c @@ -15,7 +15,7 @@ * PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: socket.c,v 1.237.18.29 2007/08/28 07:20:06 tbox Exp $ */ +/* $Id: socket.c,v 1.237.18.30 2007/12/14 04:00:53 marka Exp $ */ /*! \file */ @@ -1557,6 +1557,20 @@ isc_socket_create(isc_socketmgr_t *manager, int pf, isc_sockettype_t type, } #endif +#ifdef SO_NOSIGPIPE + if (setsockopt(sock->fd, SOL_SOCKET, SO_NOSIGPIPE, + (void *)&on, sizeof(on)) < 0) { + isc__strerror(errno, strbuf, sizeof(strbuf)); + UNEXPECTED_ERROR(__FILE__, __LINE__, + "setsockopt(%d, SO_NOSIGPIPE) %s: %s", + sock->fd, + isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL, + ISC_MSG_FAILED, "failed"), + strbuf); + /* Press on... */ + } +#endif + #if defined(USE_CMSG) || defined(SO_RCVBUF) if (type == isc_sockettype_udp) {