From 1f7962830e0a72a6d7a85ae61ab95341ad0b8fdd Mon Sep 17 00:00:00 2001 From: "Evgeny Grin (Karlson2k)" Date: Sun, 2 Jun 2019 01:52:11 +0300 Subject: Added support for SOCK_NOSIGPIPE from Solaris 11.4 and NetBSD 7+ --- src/microhttpd/daemon.c | 2 +- src/microhttpd/mhd_sockets.c | 25 ++++++++++++++++++++----- src/microhttpd/mhd_sockets.h | 8 +++++++- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c index a5051c01..0764f286 100644 --- a/src/microhttpd/daemon.c +++ b/src/microhttpd/daemon.c @@ -3136,7 +3136,7 @@ MHD_accept_connection (struct MHD_Daemon *daemon) s = accept4 (fd, addr, &addrlen, - MAYBE_SOCK_CLOEXEC | MAYBE_SOCK_NONBLOCK); + MAYBE_SOCK_CLOEXEC | MAYBE_SOCK_NONBLOCK | MAYBE_SOCK_NOSIGPIPE); sk_nonbl = (MAYBE_SOCK_NONBLOCK != 0); #else /* ! USE_ACCEPT4 */ s = accept (fd, diff --git a/src/microhttpd/mhd_sockets.c b/src/microhttpd/mhd_sockets.c index 3504dc26..04405945 100644 --- a/src/microhttpd/mhd_sockets.c +++ b/src/microhttpd/mhd_sockets.c @@ -507,12 +507,18 @@ MHD_socket_create_listen_ (int pf) { MHD_socket fd; int cloexec_set; +#if defined(SOCK_NOSIGPIPE) || defined(MHD_socket_nosignal_) + int nosigpipe_set; +#endif /* SOCK_NOSIGPIPE || MHD_socket_nosignal_ */ -#if defined(MHD_POSIX_SOCKETS) && defined(SOCK_CLOEXEC) +#if defined(MHD_POSIX_SOCKETS) && ( defined(SOCK_CLOEXEC) || defined(SOCK_NOSIGPIPE) ) fd = socket (pf, - SOCK_STREAM | SOCK_CLOEXEC, + SOCK_STREAM | SOCK_CLOEXEC | MAYBE_SOCK_NOSIGPIPE, 0); - cloexec_set = !0; + cloexec_set = (MAYBE_SOCK_CLOEXEC != 0); +#if defined(SOCK_NOSIGPIPE) || defined(MHD_socket_nosignal_) + nosigpipe_set = (MAYBE_SOCK_NOSIGPIPE != 0); +#endif /* SOCK_NOSIGPIPE || MHD_socket_nosignal_ */ #elif defined(MHD_WINSOCK_SOCKETS) && defined (WSA_FLAG_NO_HANDLE_INHERIT) fd = WSASocketW (pf, SOCK_STREAM, @@ -533,15 +539,24 @@ MHD_socket_create_listen_ (int pf) } if (MHD_INVALID_SOCKET == fd) return MHD_INVALID_SOCKET; + +#if defined(SOCK_NOSIGPIPE) || defined(MHD_socket_nosignal_) + if ( ( (! nosigpipe_set) #ifdef MHD_socket_nosignal_ - if(! MHD_socket_nosignal_(fd)) + || (! MHD_socket_nosignal_(fd)) +#endif /* MHD_socket_nosignal_ */ + ) && (0 == MAYBE_MSG_NOSIGNAL) ) { + /* SIGPIPE disable is possible on this platform + * (so application expect that it will be disabled), + * but failed to be disabled here and it is not + * possible to disable SIGPIPE by MSG_NOSIGNAL. */ const int err = MHD_socket_get_error_ (); (void) MHD_socket_close_ (fd); MHD_socket_fset_error_ (err); return MHD_INVALID_SOCKET; } -#endif /* MHD_socket_nosignal_ */ +#endif /* SOCK_NOSIGPIPE || MHD_socket_nosignal_ */ if (! cloexec_set) (void) MHD_socket_noninheritable_ (fd); diff --git a/src/microhttpd/mhd_sockets.h b/src/microhttpd/mhd_sockets.h index 8663edd3..62ea46d5 100644 --- a/src/microhttpd/mhd_sockets.h +++ b/src/microhttpd/mhd_sockets.h @@ -162,6 +162,12 @@ # define MAYBE_SOCK_NONBLOCK 0 #endif /* ! HAVE_SOCK_NONBLOCK */ +#ifdef SOCK_NOSIGPIPE +# define MAYBE_SOCK_NOSIGPIPE SOCK_NOSIGPIPE +#else /* ! HAVE_SOCK_NONBLOCK */ +# define MAYBE_SOCK_NOSIGPIPE 0 +#endif /* ! HAVE_SOCK_NONBLOCK */ + #ifdef MSG_NOSIGNAL # define MAYBE_MSG_NOSIGNAL MSG_NOSIGNAL #else /* ! MSG_NOSIGNAL */ @@ -178,7 +184,7 @@ # define SHUT_RDWR SD_BOTH #endif -#if HAVE_ACCEPT4+0 != 0 && (defined(HAVE_SOCK_NONBLOCK) || defined(SOCK_CLOEXEC)) +#if HAVE_ACCEPT4+0 != 0 && (defined(HAVE_SOCK_NONBLOCK) || defined(SOCK_CLOEXEC) || defined(SOCK_NOSIGPIPE)) # define USE_ACCEPT4 1 #endif -- cgit v1.2.3