libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit c556824313ee9d038935645b8926debc5306ad43
parent 4371815fc76e6e9bfa3c977c687bae94300e501b
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Tue, 24 Aug 2021 15:58:46 +0300

Added flag for non-blocking listen socket

Diffstat:
Msrc/microhttpd/daemon.c | 59+++++++++++++++++++++++++++++++++--------------------------
Msrc/microhttpd/internal.h | 5+++++
2 files changed, 38 insertions(+), 26 deletions(-)

diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c @@ -6954,42 +6954,49 @@ MHD_start_daemon_va (unsigned int flags, } } #endif /* HAVE_GETSOCKNAME */ - if ( (MHD_INVALID_SOCKET != listen_fd) && - (! MHD_socket_nonblocking_ (listen_fd)) ) + + if (MHD_INVALID_SOCKET != listen_fd) { + if (! MHD_socket_nonblocking_ (listen_fd)) + { #ifdef HAVE_MESSAGES - MHD_DLOG (daemon, - _ ("Failed to set nonblocking mode on listening socket: %s\n"), - MHD_socket_last_strerr_ ()); + MHD_DLOG (daemon, + _ ("Failed to set nonblocking mode on listening socket: %s\n"), + MHD_socket_last_strerr_ ()); #endif - if (0 != (*pflags & MHD_USE_EPOLL) + if (0 != (*pflags & MHD_USE_EPOLL) #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) - || (daemon->worker_pool_size > 0) + || (daemon->worker_pool_size > 0) #endif - ) + ) + { + /* Accept must be non-blocking. Multiple children may wake up + * to handle a new connection, but only one will win the race. + * The others must immediately return. */ + MHD_socket_close_chk_ (listen_fd); + goto free_and_fail; + } + daemon->listen_nonblk = false; + } + else + daemon->listen_nonblk = true; + if ( (! MHD_SCKT_FD_FITS_FDSET_ (listen_fd, + NULL)) && + (0 == (*pflags & (MHD_USE_POLL | MHD_USE_EPOLL)) ) ) { - /* Accept must be non-blocking. Multiple children may wake up - * to handle a new connection, but only one will win the race. - * The others must immediately return. */ +#ifdef HAVE_MESSAGES + MHD_DLOG (daemon, + _ ("Listen socket descriptor (%d) is not " \ + "less than FD_SETSIZE (%d).\n"), + (int) listen_fd, + (int) FD_SETSIZE); +#endif MHD_socket_close_chk_ (listen_fd); goto free_and_fail; } } - if ( (MHD_INVALID_SOCKET != listen_fd) && - (! MHD_SCKT_FD_FITS_FDSET_ (listen_fd, - NULL)) && - (0 == (*pflags & (MHD_USE_POLL | MHD_USE_EPOLL)) ) ) - { -#ifdef HAVE_MESSAGES - MHD_DLOG (daemon, - _ ("Listen socket descriptor (%d) is not " \ - "less than FD_SETSIZE (%d).\n"), - (int) listen_fd, - (int) FD_SETSIZE); -#endif - MHD_socket_close_chk_ (listen_fd); - goto free_and_fail; - } + else + daemon->listen_nonblk = false; /* Actually listen socket does not exist */ #ifdef EPOLL_SUPPORT if ( (0 != (*pflags & MHD_USE_EPOLL)) diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h @@ -1827,6 +1827,11 @@ struct MHD_Daemon */ MHD_socket listen_fd; + /** + * Listen socket is non-blocking. + */ + bool listen_nonblk; + #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) /** * Worker daemons (one per thread)