commit a41dc5dce6553e422959def98e6787909a31bdc5
parent 634cb0abd867bee5668d4501a4b63b950e5b481c
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date: Sun, 23 Oct 2016 22:03:34 +0300
mhd_sockets.h: added MHD_socket_nosignal_() macro for Darwin and *BSD
Diffstat:
3 files changed, 34 insertions(+), 26 deletions(-)
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c
@@ -1789,9 +1789,6 @@ internal_add_connection (struct MHD_Daemon *daemon,
unsigned int i;
int eno;
struct MHD_Daemon *worker;
-#if OSX
- static int on = 1;
-#endif
if (NULL != daemon->worker_pool)
{
@@ -1833,6 +1830,24 @@ internal_add_connection (struct MHD_Daemon *daemon,
return MHD_NO;
}
+#ifdef MHD_socket_nosignal_
+ if (! MHD_socket_nosignal_ (client_socket))
+ {
+#ifdef HAVE_MESSAGES
+ MHD_DLOG (daemon,
+ _("Failed to set SO_NOSIGPIPE on accepted socket: %s\n"),
+ MHD_socket_last_strerr_());
+#endif
+#ifndef MSG_NOSIGNAL
+ /* Cannot use socket as it can produce SIGPIPE. */
+#ifdef ENOTSOCK
+ errno = ENOTSOCK;
+#endif /* ENOTSOCK */
+ return MHD_NO;
+#endif /* ! MSG_NOSIGNAL */
+ }
+#endif /* MHD_socket_nosignal_ */
+
#ifdef HAVE_MESSAGES
#if DEBUG_CONNECT
@@ -1880,18 +1895,6 @@ internal_add_connection (struct MHD_Daemon *daemon,
return MHD_NO;
}
-#if OSX
-#ifdef SOL_SOCKET
-#ifdef SO_NOSIGPIPE
- setsockopt (client_socket,
- SOL_SOCKET,
- SO_NOSIGPIPE,
- &on,
- sizeof (on));
-#endif
-#endif
-#endif
-
if (NULL == (connection = malloc (sizeof (struct MHD_Connection))))
{
eno = errno;
diff --git a/src/microhttpd/mhd_sockets.c b/src/microhttpd/mhd_sockets.c
@@ -473,9 +473,6 @@ MHD_socket_create_listen_ (int use_ipv6)
int domain;
MHD_socket fd;
int cloexec_set;
-#if defined(OSX) && defined(SOL_SOCKET) && defined(SO_NOSIGPIPE)
- static const int on_val = 1;
-#endif
#ifdef HAVE_INET6
domain = (use_ipv6) ? PF_INET6 : PF_INET;
@@ -510,19 +507,15 @@ MHD_socket_create_listen_ (int use_ipv6)
}
if (MHD_INVALID_SOCKET == fd)
return MHD_INVALID_SOCKET;
-#if defined(OSX) && defined(SOL_SOCKET) && defined(SO_NOSIGPIPE)
- if(0 != setsockopt(fd,
- SOL_SOCKET,
- SO_NOSIGPIPE,
- &on_val,
- sizeof (on_val)))
+#ifdef MHD_socket_nosignal_
+ if(! MHD_socket_nosignal_(fd))
{
- int err = MHD_socket_get_error_ ();
+ const int err = MHD_socket_get_error_ ();
MHD_socket_close_ (fd);
MHD_socket_fset_error_ (err);
return MHD_INVALID_SOCKET;
}
-#endif
+#endif /* MHD_socket_nosignal_ */
if (! cloexec_set)
(void) MHD_socket_noninheritable_ (fd);
diff --git a/src/microhttpd/mhd_sockets.h b/src/microhttpd/mhd_sockets.h
@@ -702,6 +702,18 @@ int
MHD_socket_noninheritable_ (MHD_socket sock);
+#if defined(SOL_SOCKET) && defined(SO_NOSIGPIPE)
+ static const int _MHD_socket_int_one = 1;
+/**
+ * Change socket options to no signal on remote disconnect.
+ *
+ * @param sock socket to manipulate
+ * @return non-zero if succeeded, zero otherwise
+ */
+# define MHD_socket_nosignal_(sock) \
+ (!setsockopt((sock),SOL_SOCKET,SO_NOSIGPIPE,&_MHD_socket_int_one,sizeof(_MHD_socket_int_one)))
+#endif /* SOL_SOCKET && SO_NOSIGPIPE */
+
/**
* Create a listen socket, with noninheritable flag if possible.
*