commit 8361a3a7b73680bfe5dbc368be875368541ff20c
parent c556824313ee9d038935645b8926debc5306ad43
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date: Tue, 24 Aug 2021 16:03:55 +0300
Inherit non-blocking status when accepting on most platforms
When accept() is used, socket non-blocking flag is inherited from listen
socket almost on all platforms (except Linux kernel).
Actually modern platforms use accept4() so this change is mostly for W32.
Diffstat:
2 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c
@@ -3550,7 +3550,11 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
s = accept (fd,
addr,
&addrlen);
+#ifdef MHD_ACCEPT_INHERIT_NONBLOCK
+ sk_nonbl = daemon->listen_nonblk;
+#else /* ! MHD_ACCEPT_INHERIT_NONBLOCK */
sk_nonbl = false;
+#endif /* ! MHD_ACCEPT_INHERIT_NONBLOCK */
#ifndef MHD_WINSOCK_SOCKETS
sk_spipe_supprs = false;
#else /* MHD_WINSOCK_SOCKETS */
@@ -3615,15 +3619,17 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
}
return MHD_NO;
}
-#if ! defined(USE_ACCEPT4) || ! defined(HAVE_SOCK_NONBLOCK)
- if (! MHD_socket_nonblocking_ (s))
+
+ if (! sk_nonbl && ! MHD_socket_nonblocking_ (s))
{
#ifdef HAVE_MESSAGES
MHD_DLOG (daemon,
_ ("Failed to set nonblocking mode on incoming connection " \
"socket: %s\n"),
MHD_socket_last_strerr_ ());
-#endif
+#else /* ! HAVE_MESSAGES */
+ (void) 0; /* Mute compiler warning */
+#endif /* ! HAVE_MESSAGES */
}
else
sk_nonbl = true;
diff --git a/src/microhttpd/mhd_sockets.h b/src/microhttpd/mhd_sockets.h
@@ -199,6 +199,12 @@ typedef SOCKET MHD_socket;
# define USE_ACCEPT4 1
#endif
+#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
+ defined(MHD_WINSOCK_SOCKETS) || defined(__MACH__) || defined(__sun)
+/* Most of OSes inherit nonblocking setting from the listen socket */
+#define MHD_ACCEPT_INHERIT_NONBLOCK 1
+#endif
+
#if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
# define USE_EPOLL_CREATE1 1
#endif /* HAVE_EPOLL_CREATE1 && EPOLL_CLOEXEC */