aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/daemon.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/microhttpd/daemon.c')
-rw-r--r--src/microhttpd/daemon.c98
1 files changed, 44 insertions, 54 deletions
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)) )