aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-08-23 20:13:26 +0000
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-08-23 20:13:26 +0000
commit76a4a10d96c8efb424bfac6cc16b1d14ec5a94dc (patch)
treeceb25c44466bfa53cb87e42dd46ca7d7827cfde9
parentb6d29e0cb35aeb2df21f2e959a93ca0552951009 (diff)
downloadlibmicrohttpd-76a4a10d96c8efb424bfac6cc16b1d14ec5a94dc.tar.gz
libmicrohttpd-76a4a10d96c8efb424bfac6cc16b1d14ec5a94dc.zip
Moved make_nonblocking() to mhd_sockets.c, added MHD_itc_nonblocking() for pipes.
-rw-r--r--configure.ac2
-rw-r--r--src/microhttpd/daemon.c98
-rw-r--r--src/microhttpd/mhd_itc.c33
-rw-r--r--src/microhttpd/mhd_itc.h12
-rw-r--r--src/microhttpd/mhd_sockets.c33
-rw-r--r--src/microhttpd/mhd_sockets.h11
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
547# Check for optional headers 547# Check for optional headers
548AC_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 \ 548AC_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 \
549 endian.h machine/endian.h sys/endian.h sys/param.h sys/machine.h sys/byteorder.h machine/param.h sys/isa_defs.h \ 549 endian.h machine/endian.h sys/endian.h sys/param.h sys/machine.h sys/byteorder.h machine/param.h sys/isa_defs.h \
550 inttypes.h stddef.h \ 550 inttypes.h stddef.h unistd.h \
551 sockLib.h inetLib.h net/if.h]) 551 sockLib.h inetLib.h net/if.h])
552AM_CONDITIONAL([HAVE_TSEARCH], [test "x$ac_cv_header_search_h" = "xyes"]) 552AM_CONDITIONAL([HAVE_TSEARCH], [test "x$ac_cv_header_search_h" = "xyes"])
553 553
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;
136 136
137 137
138/** 138/**
139 * Change socket options to be non-blocking.
140 *
141 * @param daemon daemon context
142 * @param sock socket to manipulate
143 * @return #MHD_YES if succeeded, #MHD_NO otherwise
144 */
145static int
146make_nonblocking (struct MHD_Daemon *daemon,
147 MHD_socket sock)
148{
149#ifdef MHD_WINSOCK_SOCKETS
150 unsigned long flags = 1;
151
152 if (0 != ioctlsocket (sock, FIONBIO, &flags))
153 {
154 #ifdef HAVE_MESSAGES
155 MHD_DLOG (daemon,
156 "Failed to make socket non-blocking: %s\n",
157 MHD_socket_last_strerr_ ());
158 #endif
159 return MHD_NO;
160 }
161#else /* MHD_POSIX_SOCKETS */
162 int flags;
163
164 flags = fcntl (sock, F_GETFL);
165 if ( ( (-1 == flags) ||
166 ( (flags != (flags | O_NONBLOCK)) &&
167 (0 != fcntl (sock, F_SETFL, flags | O_NONBLOCK)) ) ) )
168 {
169#ifdef HAVE_MESSAGES
170 MHD_DLOG (daemon,
171 "Failed to make socket non-blocking: %s\n",
172 MHD_socket_last_strerr_ ());
173#endif
174 return MHD_NO;
175 }
176#endif /* MHD_POSIX_SOCKETS */
177 return MHD_YES;
178}
179
180
181/**
182 * Trace up to and return master daemon. If the supplied daemon 139 * Trace up to and return master daemon. If the supplied daemon
183 * is a master, then return the daemon itself. 140 * is a master, then return the daemon itself.
184 * 141 *
@@ -1420,7 +1377,14 @@ internal_add_connection (struct MHD_Daemon *daemon,
1420 { 1377 {
1421 /* in turbo mode, we assume that non-blocking was already set 1378 /* in turbo mode, we assume that non-blocking was already set
1422 by 'accept4' or whoever calls 'MHD_add_connection' */ 1379 by 'accept4' or whoever calls 'MHD_add_connection' */
1423 make_nonblocking (daemon, connection->socket_fd); 1380 if (!MHD_socket_nonblocking_ (connection->socket_fd))
1381 {
1382#ifdef HAVE_MESSAGES
1383 MHD_DLOG (connection->daemon,
1384 "Failed to set nonblocking mode on connection socket: %s\n",
1385 MHD_socket_last_strerr_());
1386#endif
1387 }
1424 } 1388 }
1425 1389
1426#if HTTPS_SUPPORT 1390#if HTTPS_SUPPORT
@@ -1839,7 +1803,7 @@ static void
1839make_nonblocking_noninheritable (struct MHD_Daemon *daemon, 1803make_nonblocking_noninheritable (struct MHD_Daemon *daemon,
1840 MHD_socket sock) 1804 MHD_socket sock)
1841{ 1805{
1842 (void)make_nonblocking (daemon, sock); 1806 (void)MHD_socket_nonblocking_(sock);
1843 (void)make_noninheritable (daemon, sock); 1807 (void)make_noninheritable (daemon, sock);
1844} 1808}
1845 1809
@@ -1974,7 +1938,14 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
1974#if !defined(USE_ACCEPT4) 1938#if !defined(USE_ACCEPT4)
1975 make_nonblocking_noninheritable (daemon, s); 1939 make_nonblocking_noninheritable (daemon, s);
1976#elif !defined(HAVE_SOCK_NONBLOCK) 1940#elif !defined(HAVE_SOCK_NONBLOCK)
1977 make_nonblocking (daemon, s); 1941 if (!MHD_socket_nonblocking_ (s))
1942 {
1943#ifdef HAVE_MESSAGES
1944 MHD_DLOG (daemon,
1945 "Failed to set nonblocking mode on incoming connection socket: %s\n",
1946 MHD_socket_last_strerr_());
1947#endif
1948 }
1978#elif !defined(SOCK_CLOEXEC) 1949#elif !defined(SOCK_CLOEXEC)
1979 make_noninheritable (daemon, s); 1950 make_noninheritable (daemon, s);
1980#endif 1951#endif
@@ -3711,11 +3682,11 @@ MHD_start_daemon_va (unsigned int flags,
3711 free (daemon); 3682 free (daemon);
3712 return NULL; 3683 return NULL;
3713 } 3684 }
3714 if (MHD_NO == make_nonblocking (daemon, daemon->wpipe[0])) 3685 if (!MHD_itc_nonblocking_(daemon->wpipe[0]))
3715 { 3686 {
3716#ifdef HAVE_MESSAGES 3687#ifdef HAVE_MESSAGES
3717 MHD_DLOG (daemon, 3688 MHD_DLOG (daemon,
3718 "Failed to make control pipe non-blocking: %s\n", 3689 "Failed to make read side of inter-thread control channel non-blocking: %s\n",
3719 MHD_pipe_last_strerror_ ()); 3690 MHD_pipe_last_strerror_ ());
3720#endif 3691#endif
3721 if (0 != MHD_pipe_close_ (daemon->wpipe[0])) 3692 if (0 != MHD_pipe_close_ (daemon->wpipe[0]))
@@ -3725,7 +3696,14 @@ MHD_start_daemon_va (unsigned int flags,
3725 free (daemon); 3696 free (daemon);
3726 return NULL; 3697 return NULL;
3727 } 3698 }
3728 make_nonblocking (daemon, daemon->wpipe[1]); 3699 if (!MHD_itc_nonblocking_(daemon->wpipe[1]))
3700 {
3701#ifdef HAVE_MESSAGES
3702 MHD_DLOG (daemon,
3703 "Failed to make write side of inter-thread control channel non-blocking: %s\n",
3704 MHD_pipe_last_strerror_ ());
3705#endif
3706 }
3729 } 3707 }
3730 if ( (0 == (flags & (MHD_USE_POLL | MHD_USE_EPOLL_LINUX_ONLY))) && 3708 if ( (0 == (flags & (MHD_USE_POLL | MHD_USE_EPOLL_LINUX_ONLY))) &&
3731 (1 == use_pipe) && 3709 (1 == use_pipe) &&
@@ -4071,8 +4049,13 @@ MHD_start_daemon_va (unsigned int flags,
4071 socket_fd = daemon->socket_fd; 4049 socket_fd = daemon->socket_fd;
4072 } 4050 }
4073 4051
4074 if (MHD_NO == make_nonblocking (daemon, socket_fd)) 4052 if (!MHD_socket_nonblocking_ (socket_fd))
4075 { 4053 {
4054#ifdef HAVE_MESSAGES
4055 MHD_DLOG (daemon,
4056 "Failed to set nonblocking mode on listening socket: %s\n",
4057 MHD_socket_last_strerr_());
4058#endif
4076 if (0 != (flags & MHD_USE_EPOLL_LINUX_ONLY) || 4059 if (0 != (flags & MHD_USE_EPOLL_LINUX_ONLY) ||
4077 daemon->worker_pool_size > 0) 4060 daemon->worker_pool_size > 0)
4078 { 4061 {
@@ -4233,16 +4216,23 @@ MHD_start_daemon_va (unsigned int flags,
4233#endif 4216#endif
4234 goto thread_failed; 4217 goto thread_failed;
4235 } 4218 }
4236 if (MHD_NO == make_nonblocking (d, d->wpipe[0])) 4219 if (!MHD_itc_nonblocking_(d->wpipe[0]))
4237 { 4220 {
4238#ifdef HAVE_MESSAGES 4221#ifdef HAVE_MESSAGES
4239 MHD_DLOG (daemon, 4222 MHD_DLOG (daemon,
4240 "Failed to make worker control pipe non_blocking: %s\n", 4223 "Failed to make read side of worker inter-thread control channel non-blocking: %s\n",
4241 MHD_pipe_last_strerror_() ); 4224 MHD_pipe_last_strerror_ ());
4242#endif 4225#endif
4243 goto thread_failed; 4226 goto thread_failed;
4244 } 4227 }
4245 make_nonblocking (d, d->wpipe[1]); 4228 if (!MHD_itc_nonblocking_(d->wpipe[1]))
4229 {
4230#ifdef HAVE_MESSAGES
4231 MHD_DLOG (daemon,
4232 "Failed to make write side of worker inter-thread control channel non-blocking: %s\n",
4233 MHD_pipe_last_strerror_ ());
4234#endif
4235 }
4246 } 4236 }
4247 if ( (0 == (flags & (MHD_USE_POLL | MHD_USE_EPOLL_LINUX_ONLY))) && 4237 if ( (0 == (flags & (MHD_USE_POLL | MHD_USE_EPOLL_LINUX_ONLY))) &&
4248 (!MHD_SCKT_FD_FITS_FDSET_(d->wpipe[0], NULL)) ) 4238 (!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 @@
26 26
27#include "mhd_itc.h" 27#include "mhd_itc.h"
28 28
29#ifdef HAVE_UNISTD_H
30#include <unistd.h>
31#endif /* HAVE_UNISTD_H */
32#include <fcntl.h>
33
29#if defined(_WIN32) && !defined(__CYGWIN__) 34#if defined(_WIN32) && !defined(__CYGWIN__)
30/** 35/**
31 * Create pair of mutually connected TCP/IP sockets on loopback address 36 * 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])
106} 111}
107 112
108#endif /* _WIN32 && ! __CYGWIN__ */ 113#endif /* _WIN32 && ! __CYGWIN__ */
114
115#ifndef MHD_DONT_USE_PIPES
116#if !defined(_WIN32) || defined(__CYGWIN__)
117
118
119/**
120 * Change itc FD options to be non-blocking.
121 *
122 * @param fd the FD to manipulate
123 * @return non-zero if succeeded, zero otherwise
124 */
125int
126MHD_itc_nonblocking_ (MHD_pipe fd)
127{
128 int flags;
129
130 flags = fcntl (fd, F_GETFL);
131 if (-1 == flags)
132 return 0;
133
134 if ( ((flags | O_NONBLOCK) != flags) &&
135 (0 != fcntl (fd, F_SETFL, flags | O_NONBLOCK)) )
136 return 0;
137
138 return !0;
139}
140#endif /* _WIN32 && ! __CYGWIN__ */
141#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 @@
114int MHD_W32_pair_of_sockets_(SOCKET sockets_pair[2]); 114int MHD_W32_pair_of_sockets_(SOCKET sockets_pair[2]);
115#endif /* _WIN32 && ! __CYGWIN__ */ 115#endif /* _WIN32 && ! __CYGWIN__ */
116 116
117#ifndef MHD_DONT_USE_PIPES
118/**
119 * Change itc FD options to be non-blocking.
120 *
121 * @param fd the FD to manipulate
122 * @return non-zero if succeeded, zero otherwise
123 */
124 int
125 MHD_itc_nonblocking_ (MHD_pipe fd);
126#else
127# define MHD_itc_nonblocking_(f) MHD_socket_nonblocking_((f))
128#endif
117 129
118#endif /* MHD_ITC_H */ 130#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 @@
25 */ 25 */
26 26
27#include "mhd_sockets.h" 27#include "mhd_sockets.h"
28#ifdef HAVE_UNISTD_H
29#include <unistd.h>
30#endif /* HAVE_UNISTD_H */
31#include <fcntl.h>
28 32
29#ifdef MHD_WINSOCK_SOCKETS 33#ifdef MHD_WINSOCK_SOCKETS
30 34
@@ -264,3 +268,32 @@ MHD_add_to_fd_set_ (MHD_socket fd,
264 268
265 return !0; 269 return !0;
266} 270}
271
272
273/**
274 * Change socket options to be non-blocking.
275 *
276 * @param sock socket to manipulate
277 * @return non-zero if succeeded, zero otherwise
278 */
279int
280MHD_socket_nonblocking_ (MHD_socket sock)
281{
282#if defined(MHD_POSIX_SOCKETS)
283 int flags;
284
285 flags = fcntl (sock, F_GETFL);
286 if (-1 == flags)
287 return 0;
288
289 if ( ((flags | O_NONBLOCK) != flags) &&
290 (0 != fcntl (sock, F_SETFL, flags | O_NONBLOCK)) )
291 return 0;
292#elif defined(MHD_WINSOCK_SOCKETS)
293 unsigned long flags = 1;
294
295 if (0 != ioctlsocket (sock, FIONBIO, &flags))
296 return 0;
297#endif /* MHD_WINSOCK_SOCKETS */
298 return !0;
299}
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,
586 fd_set *set, 586 fd_set *set,
587 MHD_socket *max_fd, 587 MHD_socket *max_fd,
588 unsigned int fd_setsize); 588 unsigned int fd_setsize);
589
590
591/**
592 * Change socket options to be non-blocking.
593 *
594 * @param sock socket to manipulate
595 * @return non-zero if succeeded, zero otherwise
596 */
597int
598MHD_socket_nonblocking_ (MHD_socket sock);
599
589#endif /* ! MHD_SOCKETS_H */ 600#endif /* ! MHD_SOCKETS_H */