diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2021-08-24 16:03:55 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2021-08-24 16:06:11 +0300 |
commit | 8361a3a7b73680bfe5dbc368be875368541ff20c (patch) | |
tree | 1237194c947955c529888fed81887efeb041bf05 | |
parent | c556824313ee9d038935645b8926debc5306ad43 (diff) | |
download | libmicrohttpd-8361a3a7b73680bfe5dbc368be875368541ff20c.tar.gz libmicrohttpd-8361a3a7b73680bfe5dbc368be875368541ff20c.zip |
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.
-rw-r--r-- | src/microhttpd/daemon.c | 12 | ||||
-rw-r--r-- | src/microhttpd/mhd_sockets.h | 6 |
2 files changed, 15 insertions, 3 deletions
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c index 231ac57f..c0278246 100644 --- a/src/microhttpd/daemon.c +++ b/src/microhttpd/daemon.c | |||
@@ -3550,7 +3550,11 @@ MHD_accept_connection (struct MHD_Daemon *daemon) | |||
3550 | s = accept (fd, | 3550 | s = accept (fd, |
3551 | addr, | 3551 | addr, |
3552 | &addrlen); | 3552 | &addrlen); |
3553 | #ifdef MHD_ACCEPT_INHERIT_NONBLOCK | ||
3554 | sk_nonbl = daemon->listen_nonblk; | ||
3555 | #else /* ! MHD_ACCEPT_INHERIT_NONBLOCK */ | ||
3553 | sk_nonbl = false; | 3556 | sk_nonbl = false; |
3557 | #endif /* ! MHD_ACCEPT_INHERIT_NONBLOCK */ | ||
3554 | #ifndef MHD_WINSOCK_SOCKETS | 3558 | #ifndef MHD_WINSOCK_SOCKETS |
3555 | sk_spipe_supprs = false; | 3559 | sk_spipe_supprs = false; |
3556 | #else /* MHD_WINSOCK_SOCKETS */ | 3560 | #else /* MHD_WINSOCK_SOCKETS */ |
@@ -3615,15 +3619,17 @@ MHD_accept_connection (struct MHD_Daemon *daemon) | |||
3615 | } | 3619 | } |
3616 | return MHD_NO; | 3620 | return MHD_NO; |
3617 | } | 3621 | } |
3618 | #if ! defined(USE_ACCEPT4) || ! defined(HAVE_SOCK_NONBLOCK) | 3622 | |
3619 | if (! MHD_socket_nonblocking_ (s)) | 3623 | if (! sk_nonbl && ! MHD_socket_nonblocking_ (s)) |
3620 | { | 3624 | { |
3621 | #ifdef HAVE_MESSAGES | 3625 | #ifdef HAVE_MESSAGES |
3622 | MHD_DLOG (daemon, | 3626 | MHD_DLOG (daemon, |
3623 | _ ("Failed to set nonblocking mode on incoming connection " \ | 3627 | _ ("Failed to set nonblocking mode on incoming connection " \ |
3624 | "socket: %s\n"), | 3628 | "socket: %s\n"), |
3625 | MHD_socket_last_strerr_ ()); | 3629 | MHD_socket_last_strerr_ ()); |
3626 | #endif | 3630 | #else /* ! HAVE_MESSAGES */ |
3631 | (void) 0; /* Mute compiler warning */ | ||
3632 | #endif /* ! HAVE_MESSAGES */ | ||
3627 | } | 3633 | } |
3628 | else | 3634 | else |
3629 | sk_nonbl = true; | 3635 | sk_nonbl = true; |
diff --git a/src/microhttpd/mhd_sockets.h b/src/microhttpd/mhd_sockets.h index 56ea64db..18f831a9 100644 --- a/src/microhttpd/mhd_sockets.h +++ b/src/microhttpd/mhd_sockets.h | |||
@@ -199,6 +199,12 @@ typedef SOCKET MHD_socket; | |||
199 | # define USE_ACCEPT4 1 | 199 | # define USE_ACCEPT4 1 |
200 | #endif | 200 | #endif |
201 | 201 | ||
202 | #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \ | ||
203 | defined(MHD_WINSOCK_SOCKETS) || defined(__MACH__) || defined(__sun) | ||
204 | /* Most of OSes inherit nonblocking setting from the listen socket */ | ||
205 | #define MHD_ACCEPT_INHERIT_NONBLOCK 1 | ||
206 | #endif | ||
207 | |||
202 | #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC) | 208 | #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC) |
203 | # define USE_EPOLL_CREATE1 1 | 209 | # define USE_EPOLL_CREATE1 1 |
204 | #endif /* HAVE_EPOLL_CREATE1 && EPOLL_CLOEXEC */ | 210 | #endif /* HAVE_EPOLL_CREATE1 && EPOLL_CLOEXEC */ |