aboutsummaryrefslogtreecommitdiff
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)
downloadlibmicrohttpd-4cf082ba053867a7f9749f785627a1a80b2743a0.tar.gz
libmicrohttpd-4cf082ba053867a7f9749f785627a1a80b2743a0.zip
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)
1753 1753
1754 1754
1755/** 1755/**
1756 * Change socket options to be non-inheritable.
1757 *
1758 * @param daemon daemon context
1759 * @param sock socket to manipulate
1760 * @return #MHD_YES if succeeded, #MHD_NO otherwise
1761 */
1762static int
1763make_noninheritable (struct MHD_Daemon *daemon,
1764 MHD_socket sock)
1765{
1766#ifdef MHD_WINSOCK_SOCKETS
1767 if (!SetHandleInformation ((HANDLE)sock, HANDLE_FLAG_INHERIT, 0))
1768 {
1769#ifdef HAVE_MESSAGES
1770 MHD_DLOG (daemon,
1771 "Failed to make socket non-inheritable: %u\n",
1772 (unsigned int)GetLastError ());
1773#endif
1774 return MHD_NO;
1775 }
1776#else /* MHD_POSIX_SOCKETS */
1777 int flags;
1778
1779 flags = fcntl (sock, F_GETFD);
1780 if ( ( (-1 == flags) ||
1781 ( (flags != (flags | FD_CLOEXEC)) &&
1782 (0 != fcntl (sock, F_SETFD, flags | FD_CLOEXEC)) ) ) )
1783 {
1784#ifdef HAVE_MESSAGES
1785 MHD_DLOG (daemon,
1786 "Failed to make socket non-inheritable: %s\n",
1787 MHD_socket_last_strerr_ ());
1788#endif
1789 return MHD_NO;
1790 }
1791#endif /* MHD_POSIX_SOCKETS */
1792 return MHD_YES;
1793}
1794
1795
1796/**
1797 * Change socket options to be non-blocking, non-inheritable.
1798 *
1799 * @param daemon daemon context
1800 * @param sock socket to manipulate
1801 */
1802static void
1803make_nonblocking_noninheritable (struct MHD_Daemon *daemon,
1804 MHD_socket sock)
1805{
1806 (void)MHD_socket_nonblocking_(sock);
1807 (void)make_noninheritable (daemon, sock);
1808}
1809
1810
1811/**
1812 * Add another client connection to the set of connections managed by 1756 * Add another client connection to the set of connections managed by
1813 * MHD. This API is usually not needed (since MHD will accept inbound 1757 * MHD. This API is usually not needed (since MHD will accept inbound
1814 * connections on the server socket). Use this API in special cases, 1758 * connections on the server socket). Use this API in special cases,
@@ -1846,8 +1790,23 @@ MHD_add_connection (struct MHD_Daemon *daemon,
1846 /* internal_add_connection() assume that non-blocking is 1790 /* internal_add_connection() assume that non-blocking is
1847 already set in MHD_USE_EPOLL_TURBO mode */ 1791 already set in MHD_USE_EPOLL_TURBO mode */
1848 if (0 != (daemon->options & MHD_USE_EPOLL_TURBO)) 1792 if (0 != (daemon->options & MHD_USE_EPOLL_TURBO))
1849 make_nonblocking_noninheritable (daemon, 1793 {
1850 client_socket); 1794 if (!MHD_socket_nonblocking_ (client_socket))
1795 {
1796#ifdef HAVE_MESSAGES
1797 MHD_DLOG (daemon,
1798 "Failed to set nonblocking mode on new client socket: %s\n",
1799 MHD_socket_last_strerr_());
1800#endif
1801 }
1802 if (!MHD_socket_noninheritable_ (client_socket))
1803 {
1804#ifdef HAVE_MESSAGES
1805 MHD_DLOG (daemon,
1806 "Failed to set noninheritable mode on new client socket.\n");
1807#endif
1808 }
1809 }
1851 return internal_add_connection (daemon, 1810 return internal_add_connection (daemon,
1852 client_socket, 1811 client_socket,
1853 addr, addrlen, 1812 addr, addrlen,
@@ -1935,9 +1894,7 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
1935 } 1894 }
1936 return MHD_NO; 1895 return MHD_NO;
1937 } 1896 }
1938#if !defined(USE_ACCEPT4) 1897#if !defined(USE_ACCEPT4) || !defined(HAVE_SOCK_NONBLOCK)
1939 make_nonblocking_noninheritable (daemon, s);
1940#elif !defined(HAVE_SOCK_NONBLOCK)
1941 if (!MHD_socket_nonblocking_ (s)) 1898 if (!MHD_socket_nonblocking_ (s))
1942 { 1899 {
1943#ifdef HAVE_MESSAGES 1900#ifdef HAVE_MESSAGES
@@ -1946,9 +1903,16 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
1946 MHD_socket_last_strerr_()); 1903 MHD_socket_last_strerr_());
1947#endif 1904#endif
1948 } 1905 }
1949#elif !defined(SOCK_CLOEXEC) 1906#endif /* !USE_ACCEPT4 || !HAVE_SOCK_NONBLOCK */
1950 make_noninheritable (daemon, s); 1907#if !defined(USE_ACCEPT4) || !defined(SOCK_CLOEXEC)
1908 if (!MHD_socket_noninheritable_ (s))
1909 {
1910#ifdef HAVE_MESSAGES
1911 MHD_DLOG (daemon,
1912 "Failed to set noninheritable mode on incoming connection socket.\n");
1951#endif 1913#endif
1914 }
1915#endif /* !USE_ACCEPT4 || !SOCK_CLOEXEC */
1952#ifdef HAVE_MESSAGES 1916#ifdef HAVE_MESSAGES
1953#if DEBUG_CONNECT 1917#if DEBUG_CONNECT
1954 MHD_DLOG (daemon, 1918 MHD_DLOG (daemon,
@@ -3491,7 +3455,7 @@ create_listen_socket (struct MHD_Daemon *daemon,
3491 setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &on_val, sizeof(on_val)); 3455 setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &on_val, sizeof(on_val));
3492#endif 3456#endif
3493 if (MHD_NO == cloexec_set) 3457 if (MHD_NO == cloexec_set)
3494 make_noninheritable (daemon, fd); 3458 MHD_socket_noninheritable_ (fd);
3495 return fd; 3459 return fd;
3496} 3460}
3497 3461
@@ -3524,8 +3488,13 @@ setup_epoll_to_listen (struct MHD_Daemon *daemon)
3524 return MHD_NO; 3488 return MHD_NO;
3525 } 3489 }
3526#if !defined(USE_EPOLL_CREATE1) 3490#if !defined(USE_EPOLL_CREATE1)
3527 make_noninheritable (daemon, 3491 if (!MHD_socket_noninheritable_ (daemon->epoll_fd))
3528 daemon->epoll_fd); 3492 {
3493#ifdef HAVE_MESSAGES
3494 MHD_DLOG (daemon,
3495 "Failed to set noninheritable mode on epoll FD.\n");
3496#endif
3497 }
3529#endif /* ! USE_EPOLL_CREATE1 */ 3498#endif /* ! USE_EPOLL_CREATE1 */
3530 if (MHD_INVALID_SOCKET == daemon->socket_fd) 3499 if (MHD_INVALID_SOCKET == daemon->socket_fd)
3531 return MHD_YES; /* non-listening daemon */ 3500 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)
297#endif /* MHD_WINSOCK_SOCKETS */ 297#endif /* MHD_WINSOCK_SOCKETS */
298 return !0; 298 return !0;
299} 299}
300
301
302/**
303 * Change socket options to be non-inheritable.
304 *
305 * @param sock socket to manipulate
306 * @return non-zero if succeeded, zero otherwise
307 * @warning Does not set socket error on W32.
308 */
309int
310MHD_socket_noninheritable_ (MHD_socket sock)
311{
312#if defined(MHD_POSIX_SOCKETS)
313 int flags;
314
315 flags = fcntl (sock, F_GETFD);
316 if (-1 == flags)
317 return 0;
318
319 if ( ((flags | FD_CLOEXEC) != flags) &&
320 (0 != fcntl (sock, F_SETFD, flags | FD_CLOEXEC)) )
321 return 0;
322#elif defined(MHD_WINSOCK_SOCKETS)
323 if (!SetHandleInformation ((HANDLE)sock, HANDLE_FLAG_INHERIT, 0))
324 return 0;
325#endif /* MHD_WINSOCK_SOCKETS */
326 return !0;
327}
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,
597int 597int
598MHD_socket_nonblocking_ (MHD_socket sock); 598MHD_socket_nonblocking_ (MHD_socket sock);
599 599
600
601/**
602 * Change socket options to be non-inheritable.
603 *
604 * @param sock socket to manipulate
605 * @return non-zero if succeeded, zero otherwise
606 * @warning Does not set socket error on W32.
607 */
608int
609MHD_socket_noninheritable_ (MHD_socket sock);
610
600#endif /* ! MHD_SOCKETS_H */ 611#endif /* ! MHD_SOCKETS_H */