summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-08-23 20:13:29 +0000
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-08-23 20:13:29 +0000
commit4cf082ba053867a7f9749f785627a1a80b2743a0 (patch)
tree27a681ac5a94d09e278478260280d5710c9d0808
parent76a4a10d96c8efb424bfac6cc16b1d14ec5a94dc (diff)
Moved make_noninheritable to mhd_sockets.c, improved error reporting.
-rw-r--r--src/microhttpd/daemon.c101
-rw-r--r--src/microhttpd/mhd_sockets.c28
-rw-r--r--src/microhttpd/mhd_sockets.h11
3 files changed, 74 insertions, 66 deletions
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c
index cf607da5..cdd6aae6 100644
--- a/src/microhttpd/daemon.c
+++ b/src/microhttpd/daemon.c
@@ -1753,62 +1753,6 @@ resume_suspended_connections (struct MHD_Daemon *daemon)
/**
- * Change socket options to be non-inheritable.
- *
- * @param daemon daemon context
- * @param sock socket to manipulate
- * @return #MHD_YES if succeeded, #MHD_NO otherwise
- */
-static int
-make_noninheritable (struct MHD_Daemon *daemon,
- MHD_socket sock)
-{
-#ifdef MHD_WINSOCK_SOCKETS
- if (!SetHandleInformation ((HANDLE)sock, HANDLE_FLAG_INHERIT, 0))
- {
-#ifdef HAVE_MESSAGES
- MHD_DLOG (daemon,
- "Failed to make socket non-inheritable: %u\n",
- (unsigned int)GetLastError ());
-#endif
- return MHD_NO;
- }
-#else /* MHD_POSIX_SOCKETS */
- int flags;
-
- flags = fcntl (sock, F_GETFD);
- if ( ( (-1 == flags) ||
- ( (flags != (flags | FD_CLOEXEC)) &&
- (0 != fcntl (sock, F_SETFD, flags | FD_CLOEXEC)) ) ) )
- {
-#ifdef HAVE_MESSAGES
- MHD_DLOG (daemon,
- "Failed to make socket non-inheritable: %s\n",
- MHD_socket_last_strerr_ ());
-#endif
- return MHD_NO;
- }
-#endif /* MHD_POSIX_SOCKETS */
- return MHD_YES;
-}
-
-
-/**
- * Change socket options to be non-blocking, non-inheritable.
- *
- * @param daemon daemon context
- * @param sock socket to manipulate
- */
-static void
-make_nonblocking_noninheritable (struct MHD_Daemon *daemon,
- MHD_socket sock)
-{
- (void)MHD_socket_nonblocking_(sock);
- (void)make_noninheritable (daemon, sock);
-}
-
-
-/**
* Add another client connection to the set of connections managed by
* MHD. This API is usually not needed (since MHD will accept inbound
* connections on the server socket). Use this API in special cases,
@@ -1846,8 +1790,23 @@ MHD_add_connection (struct MHD_Daemon *daemon,
/* internal_add_connection() assume that non-blocking is
already set in MHD_USE_EPOLL_TURBO mode */
if (0 != (daemon->options & MHD_USE_EPOLL_TURBO))
- make_nonblocking_noninheritable (daemon,
- client_socket);
+ {
+ if (!MHD_socket_nonblocking_ (client_socket))
+ {
+#ifdef HAVE_MESSAGES
+ MHD_DLOG (daemon,
+ "Failed to set nonblocking mode on new client socket: %s\n",
+ MHD_socket_last_strerr_());
+#endif
+ }
+ if (!MHD_socket_noninheritable_ (client_socket))
+ {
+#ifdef HAVE_MESSAGES
+ MHD_DLOG (daemon,
+ "Failed to set noninheritable mode on new client socket.\n");
+#endif
+ }
+ }
return internal_add_connection (daemon,
client_socket,
addr, addrlen,
@@ -1935,9 +1894,7 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
}
return MHD_NO;
}
-#if !defined(USE_ACCEPT4)
- make_nonblocking_noninheritable (daemon, s);
-#elif !defined(HAVE_SOCK_NONBLOCK)
+#if !defined(USE_ACCEPT4) || !defined(HAVE_SOCK_NONBLOCK)
if (!MHD_socket_nonblocking_ (s))
{
#ifdef HAVE_MESSAGES
@@ -1946,9 +1903,16 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
MHD_socket_last_strerr_());
#endif
}
-#elif !defined(SOCK_CLOEXEC)
- make_noninheritable (daemon, s);
+#endif /* !USE_ACCEPT4 || !HAVE_SOCK_NONBLOCK */
+#if !defined(USE_ACCEPT4) || !defined(SOCK_CLOEXEC)
+ if (!MHD_socket_noninheritable_ (s))
+ {
+#ifdef HAVE_MESSAGES
+ MHD_DLOG (daemon,
+ "Failed to set noninheritable mode on incoming connection socket.\n");
#endif
+ }
+#endif /* !USE_ACCEPT4 || !SOCK_CLOEXEC */
#ifdef HAVE_MESSAGES
#if DEBUG_CONNECT
MHD_DLOG (daemon,
@@ -3491,7 +3455,7 @@ create_listen_socket (struct MHD_Daemon *daemon,
setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &on_val, sizeof(on_val));
#endif
if (MHD_NO == cloexec_set)
- make_noninheritable (daemon, fd);
+ MHD_socket_noninheritable_ (fd);
return fd;
}
@@ -3524,8 +3488,13 @@ setup_epoll_to_listen (struct MHD_Daemon *daemon)
return MHD_NO;
}
#if !defined(USE_EPOLL_CREATE1)
- make_noninheritable (daemon,
- daemon->epoll_fd);
+ if (!MHD_socket_noninheritable_ (daemon->epoll_fd))
+ {
+#ifdef HAVE_MESSAGES
+ MHD_DLOG (daemon,
+ "Failed to set noninheritable mode on epoll FD.\n");
+#endif
+ }
#endif /* ! USE_EPOLL_CREATE1 */
if (MHD_INVALID_SOCKET == daemon->socket_fd)
return MHD_YES; /* non-listening daemon */
diff --git a/src/microhttpd/mhd_sockets.c b/src/microhttpd/mhd_sockets.c
index 21e265f6..3fda6138 100644
--- a/src/microhttpd/mhd_sockets.c
+++ b/src/microhttpd/mhd_sockets.c
@@ -297,3 +297,31 @@ MHD_socket_nonblocking_ (MHD_socket sock)
#endif /* MHD_WINSOCK_SOCKETS */
return !0;
}
+
+
+/**
+ * Change socket options to be non-inheritable.
+ *
+ * @param sock socket to manipulate
+ * @return non-zero if succeeded, zero otherwise
+ * @warning Does not set socket error on W32.
+ */
+int
+MHD_socket_noninheritable_ (MHD_socket sock)
+{
+#if defined(MHD_POSIX_SOCKETS)
+ int flags;
+
+ flags = fcntl (sock, F_GETFD);
+ if (-1 == flags)
+ return 0;
+
+ if ( ((flags | FD_CLOEXEC) != flags) &&
+ (0 != fcntl (sock, F_SETFD, flags | FD_CLOEXEC)) )
+ return 0;
+#elif defined(MHD_WINSOCK_SOCKETS)
+ if (!SetHandleInformation ((HANDLE)sock, HANDLE_FLAG_INHERIT, 0))
+ return 0;
+#endif /* MHD_WINSOCK_SOCKETS */
+ return !0;
+}
diff --git a/src/microhttpd/mhd_sockets.h b/src/microhttpd/mhd_sockets.h
index c2817d0f..e22ff7e2 100644
--- a/src/microhttpd/mhd_sockets.h
+++ b/src/microhttpd/mhd_sockets.h
@@ -597,4 +597,15 @@ MHD_add_to_fd_set_ (MHD_socket fd,
int
MHD_socket_nonblocking_ (MHD_socket sock);
+
+/**
+ * Change socket options to be non-inheritable.
+ *
+ * @param sock socket to manipulate
+ * @return non-zero if succeeded, zero otherwise
+ * @warning Does not set socket error on W32.
+ */
+int
+MHD_socket_noninheritable_ (MHD_socket sock);
+
#endif /* ! MHD_SOCKETS_H */