diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2016-08-23 20:13:26 +0000 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2016-08-23 20:13:26 +0000 |
commit | 76a4a10d96c8efb424bfac6cc16b1d14ec5a94dc (patch) | |
tree | ceb25c44466bfa53cb87e42dd46ca7d7827cfde9 | |
parent | b6d29e0cb35aeb2df21f2e959a93ca0552951009 (diff) |
Moved make_nonblocking() to mhd_sockets.c, added MHD_itc_nonblocking() for pipes.
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | src/microhttpd/daemon.c | 98 | ||||
-rw-r--r-- | src/microhttpd/mhd_itc.c | 33 | ||||
-rw-r--r-- | src/microhttpd/mhd_itc.h | 12 | ||||
-rw-r--r-- | src/microhttpd/mhd_sockets.c | 33 | ||||
-rw-r--r-- | src/microhttpd/mhd_sockets.h | 11 |
6 files changed, 134 insertions, 55 deletions
diff --git a/configure.ac b/configure.ac index 2fbdc538..8414a710 100644 --- a/configure.ac +++ b/configure.ac @@ -547,7 +547,7 @@ AC_CHECK_HEADERS([fcntl.h math.h errno.h limits.h stdio.h locale.h sys/stat.h sy # Check for optional headers AC_CHECK_HEADERS([sys/types.h sys/time.h sys/msg.h netdb.h netinet/in.h netinet/tcp.h time.h sys/socket.h sys/mman.h arpa/inet.h sys/select.h search.h \ endian.h machine/endian.h sys/endian.h sys/param.h sys/machine.h sys/byteorder.h machine/param.h sys/isa_defs.h \ - inttypes.h stddef.h \ + inttypes.h stddef.h unistd.h \ sockLib.h inetLib.h net/if.h]) AM_CONDITIONAL([HAVE_TSEARCH], [test "x$ac_cv_header_search_h" = "xyes"]) diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c index 105f3083..cf607da5 100644 --- a/src/microhttpd/daemon.c +++ b/src/microhttpd/daemon.c @@ -136,49 +136,6 @@ static int mhd_winsock_inited_ = 0; /** - * Change socket options to be non-blocking. - * - * @param daemon daemon context - * @param sock socket to manipulate - * @return #MHD_YES if succeeded, #MHD_NO otherwise - */ -static int -make_nonblocking (struct MHD_Daemon *daemon, - MHD_socket sock) -{ -#ifdef MHD_WINSOCK_SOCKETS - unsigned long flags = 1; - - if (0 != ioctlsocket (sock, FIONBIO, &flags)) - { - #ifdef HAVE_MESSAGES - MHD_DLOG (daemon, - "Failed to make socket non-blocking: %s\n", - MHD_socket_last_strerr_ ()); - #endif - return MHD_NO; - } -#else /* MHD_POSIX_SOCKETS */ - int flags; - - flags = fcntl (sock, F_GETFL); - if ( ( (-1 == flags) || - ( (flags != (flags | O_NONBLOCK)) && - (0 != fcntl (sock, F_SETFL, flags | O_NONBLOCK)) ) ) ) - { -#ifdef HAVE_MESSAGES - MHD_DLOG (daemon, - "Failed to make socket non-blocking: %s\n", - MHD_socket_last_strerr_ ()); -#endif - return MHD_NO; - } -#endif /* MHD_POSIX_SOCKETS */ - return MHD_YES; -} - - -/** * Trace up to and return master daemon. If the supplied daemon * is a master, then return the daemon itself. * @@ -1420,7 +1377,14 @@ internal_add_connection (struct MHD_Daemon *daemon, { /* in turbo mode, we assume that non-blocking was already set by 'accept4' or whoever calls 'MHD_add_connection' */ - make_nonblocking (daemon, connection->socket_fd); + if (!MHD_socket_nonblocking_ (connection->socket_fd)) + { +#ifdef HAVE_MESSAGES + MHD_DLOG (connection->daemon, + "Failed to set nonblocking mode on connection socket: %s\n", + MHD_socket_last_strerr_()); +#endif + } } #if HTTPS_SUPPORT @@ -1839,7 +1803,7 @@ static void make_nonblocking_noninheritable (struct MHD_Daemon *daemon, MHD_socket sock) { - (void)make_nonblocking (daemon, sock); + (void)MHD_socket_nonblocking_(sock); (void)make_noninheritable (daemon, sock); } @@ -1974,7 +1938,14 @@ MHD_accept_connection (struct MHD_Daemon *daemon) #if !defined(USE_ACCEPT4) make_nonblocking_noninheritable (daemon, s); #elif !defined(HAVE_SOCK_NONBLOCK) - make_nonblocking (daemon, s); + if (!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 + } #elif !defined(SOCK_CLOEXEC) make_noninheritable (daemon, s); #endif @@ -3711,11 +3682,11 @@ MHD_start_daemon_va (unsigned int flags, free (daemon); return NULL; } - if (MHD_NO == make_nonblocking (daemon, daemon->wpipe[0])) + if (!MHD_itc_nonblocking_(daemon->wpipe[0])) { #ifdef HAVE_MESSAGES MHD_DLOG (daemon, - "Failed to make control pipe non-blocking: %s\n", + "Failed to make read side of inter-thread control channel non-blocking: %s\n", MHD_pipe_last_strerror_ ()); #endif if (0 != MHD_pipe_close_ (daemon->wpipe[0])) @@ -3725,7 +3696,14 @@ MHD_start_daemon_va (unsigned int flags, free (daemon); return NULL; } - make_nonblocking (daemon, daemon->wpipe[1]); + if (!MHD_itc_nonblocking_(daemon->wpipe[1])) + { +#ifdef HAVE_MESSAGES + MHD_DLOG (daemon, + "Failed to make write side of inter-thread control channel non-blocking: %s\n", + MHD_pipe_last_strerror_ ()); +#endif + } } if ( (0 == (flags & (MHD_USE_POLL | MHD_USE_EPOLL_LINUX_ONLY))) && (1 == use_pipe) && @@ -4071,8 +4049,13 @@ MHD_start_daemon_va (unsigned int flags, socket_fd = daemon->socket_fd; } - if (MHD_NO == make_nonblocking (daemon, socket_fd)) + if (!MHD_socket_nonblocking_ (socket_fd)) { +#ifdef HAVE_MESSAGES + MHD_DLOG (daemon, + "Failed to set nonblocking mode on listening socket: %s\n", + MHD_socket_last_strerr_()); +#endif if (0 != (flags & MHD_USE_EPOLL_LINUX_ONLY) || daemon->worker_pool_size > 0) { @@ -4233,16 +4216,23 @@ MHD_start_daemon_va (unsigned int flags, #endif goto thread_failed; } - if (MHD_NO == make_nonblocking (d, d->wpipe[0])) + if (!MHD_itc_nonblocking_(d->wpipe[0])) { #ifdef HAVE_MESSAGES MHD_DLOG (daemon, - "Failed to make worker control pipe non_blocking: %s\n", - MHD_pipe_last_strerror_() ); + "Failed to make read side of worker inter-thread control channel non-blocking: %s\n", + MHD_pipe_last_strerror_ ()); #endif goto thread_failed; } - make_nonblocking (d, d->wpipe[1]); + if (!MHD_itc_nonblocking_(d->wpipe[1])) + { +#ifdef HAVE_MESSAGES + MHD_DLOG (daemon, + "Failed to make write side of worker inter-thread control channel non-blocking: %s\n", + MHD_pipe_last_strerror_ ()); +#endif + } } if ( (0 == (flags & (MHD_USE_POLL | MHD_USE_EPOLL_LINUX_ONLY))) && (!MHD_SCKT_FD_FITS_FDSET_(d->wpipe[0], NULL)) ) diff --git a/src/microhttpd/mhd_itc.c b/src/microhttpd/mhd_itc.c index cd0e5371..71e54a0d 100644 --- a/src/microhttpd/mhd_itc.c +++ b/src/microhttpd/mhd_itc.c @@ -26,6 +26,11 @@ #include "mhd_itc.h" +#ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif /* HAVE_UNISTD_H */ +#include <fcntl.h> + #if defined(_WIN32) && !defined(__CYGWIN__) /** * Create pair of mutually connected TCP/IP sockets on loopback address @@ -106,3 +111,31 @@ int MHD_W32_pair_of_sockets_(SOCKET sockets_pair[2]) } #endif /* _WIN32 && ! __CYGWIN__ */ + +#ifndef MHD_DONT_USE_PIPES +#if !defined(_WIN32) || defined(__CYGWIN__) + + +/** + * Change itc FD options to be non-blocking. + * + * @param fd the FD to manipulate + * @return non-zero if succeeded, zero otherwise + */ +int +MHD_itc_nonblocking_ (MHD_pipe fd) +{ + int flags; + + flags = fcntl (fd, F_GETFL); + if (-1 == flags) + return 0; + + if ( ((flags | O_NONBLOCK) != flags) && + (0 != fcntl (fd, F_SETFL, flags | O_NONBLOCK)) ) + return 0; + + return !0; +} +#endif /* _WIN32 && ! __CYGWIN__ */ +#endif /* ! MHD_DONT_USE_PIPES */ diff --git a/src/microhttpd/mhd_itc.h b/src/microhttpd/mhd_itc.h index 169bbb03..c7fd3f0d 100644 --- a/src/microhttpd/mhd_itc.h +++ b/src/microhttpd/mhd_itc.h @@ -114,5 +114,17 @@ int MHD_W32_pair_of_sockets_(SOCKET sockets_pair[2]); #endif /* _WIN32 && ! __CYGWIN__ */ +#ifndef MHD_DONT_USE_PIPES +/** + * Change itc FD options to be non-blocking. + * + * @param fd the FD to manipulate + * @return non-zero if succeeded, zero otherwise + */ + int + MHD_itc_nonblocking_ (MHD_pipe fd); +#else +# define MHD_itc_nonblocking_(f) MHD_socket_nonblocking_((f)) +#endif #endif /* MHD_ITC_H */ diff --git a/src/microhttpd/mhd_sockets.c b/src/microhttpd/mhd_sockets.c index 640c59e3..21e265f6 100644 --- a/src/microhttpd/mhd_sockets.c +++ b/src/microhttpd/mhd_sockets.c @@ -25,6 +25,10 @@ */ #include "mhd_sockets.h" +#ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif /* HAVE_UNISTD_H */ +#include <fcntl.h> #ifdef MHD_WINSOCK_SOCKETS @@ -264,3 +268,32 @@ MHD_add_to_fd_set_ (MHD_socket fd, return !0; } + + +/** + * Change socket options to be non-blocking. + * + * @param sock socket to manipulate + * @return non-zero if succeeded, zero otherwise + */ +int +MHD_socket_nonblocking_ (MHD_socket sock) +{ +#if defined(MHD_POSIX_SOCKETS) + int flags; + + flags = fcntl (sock, F_GETFL); + if (-1 == flags) + return 0; + + if ( ((flags | O_NONBLOCK) != flags) && + (0 != fcntl (sock, F_SETFL, flags | O_NONBLOCK)) ) + return 0; +#elif defined(MHD_WINSOCK_SOCKETS) + unsigned long flags = 1; + + if (0 != ioctlsocket (sock, FIONBIO, &flags)) + return 0; +#endif /* MHD_WINSOCK_SOCKETS */ + return !0; +} diff --git a/src/microhttpd/mhd_sockets.h b/src/microhttpd/mhd_sockets.h index 4d8444ca..c2817d0f 100644 --- a/src/microhttpd/mhd_sockets.h +++ b/src/microhttpd/mhd_sockets.h @@ -586,4 +586,15 @@ MHD_add_to_fd_set_ (MHD_socket fd, fd_set *set, MHD_socket *max_fd, unsigned int fd_setsize); + + +/** + * Change socket options to be non-blocking. + * + * @param sock socket to manipulate + * @return non-zero if succeeded, zero otherwise + */ +int +MHD_socket_nonblocking_ (MHD_socket sock); + #endif /* ! MHD_SOCKETS_H */ |