aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-10-23 22:03:34 +0300
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-10-24 15:51:29 +0300
commita41dc5dce6553e422959def98e6787909a31bdc5 (patch)
tree3029d4785b1bd45474c10dc1f7b467149efb5be3
parent634cb0abd867bee5668d4501a4b63b950e5b481c (diff)
downloadlibmicrohttpd-a41dc5dce6553e422959def98e6787909a31bdc5.tar.gz
libmicrohttpd-a41dc5dce6553e422959def98e6787909a31bdc5.zip
mhd_sockets.h: added MHD_socket_nosignal_() macro for Darwin and *BSD
-rw-r--r--src/microhttpd/daemon.c33
-rw-r--r--src/microhttpd/mhd_sockets.c15
-rw-r--r--src/microhttpd/mhd_sockets.h12
3 files changed, 34 insertions, 26 deletions
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c
index 916542f4..03bc3a17 100644
--- a/src/microhttpd/daemon.c
+++ b/src/microhttpd/daemon.c
@@ -1789,9 +1789,6 @@ internal_add_connection (struct MHD_Daemon *daemon,
1789 unsigned int i; 1789 unsigned int i;
1790 int eno; 1790 int eno;
1791 struct MHD_Daemon *worker; 1791 struct MHD_Daemon *worker;
1792#if OSX
1793 static int on = 1;
1794#endif
1795 1792
1796 if (NULL != daemon->worker_pool) 1793 if (NULL != daemon->worker_pool)
1797 { 1794 {
@@ -1833,6 +1830,24 @@ internal_add_connection (struct MHD_Daemon *daemon,
1833 return MHD_NO; 1830 return MHD_NO;
1834 } 1831 }
1835 1832
1833#ifdef MHD_socket_nosignal_
1834 if (! MHD_socket_nosignal_ (client_socket))
1835 {
1836#ifdef HAVE_MESSAGES
1837 MHD_DLOG (daemon,
1838 _("Failed to set SO_NOSIGPIPE on accepted socket: %s\n"),
1839 MHD_socket_last_strerr_());
1840#endif
1841#ifndef MSG_NOSIGNAL
1842 /* Cannot use socket as it can produce SIGPIPE. */
1843#ifdef ENOTSOCK
1844 errno = ENOTSOCK;
1845#endif /* ENOTSOCK */
1846 return MHD_NO;
1847#endif /* ! MSG_NOSIGNAL */
1848 }
1849#endif /* MHD_socket_nosignal_ */
1850
1836 1851
1837#ifdef HAVE_MESSAGES 1852#ifdef HAVE_MESSAGES
1838#if DEBUG_CONNECT 1853#if DEBUG_CONNECT
@@ -1880,18 +1895,6 @@ internal_add_connection (struct MHD_Daemon *daemon,
1880 return MHD_NO; 1895 return MHD_NO;
1881 } 1896 }
1882 1897
1883#if OSX
1884#ifdef SOL_SOCKET
1885#ifdef SO_NOSIGPIPE
1886 setsockopt (client_socket,
1887 SOL_SOCKET,
1888 SO_NOSIGPIPE,
1889 &on,
1890 sizeof (on));
1891#endif
1892#endif
1893#endif
1894
1895 if (NULL == (connection = malloc (sizeof (struct MHD_Connection)))) 1898 if (NULL == (connection = malloc (sizeof (struct MHD_Connection))))
1896 { 1899 {
1897 eno = errno; 1900 eno = errno;
diff --git a/src/microhttpd/mhd_sockets.c b/src/microhttpd/mhd_sockets.c
index 3bd48f58..9b60e224 100644
--- a/src/microhttpd/mhd_sockets.c
+++ b/src/microhttpd/mhd_sockets.c
@@ -473,9 +473,6 @@ MHD_socket_create_listen_ (int use_ipv6)
473 int domain; 473 int domain;
474 MHD_socket fd; 474 MHD_socket fd;
475 int cloexec_set; 475 int cloexec_set;
476#if defined(OSX) && defined(SOL_SOCKET) && defined(SO_NOSIGPIPE)
477 static const int on_val = 1;
478#endif
479 476
480#ifdef HAVE_INET6 477#ifdef HAVE_INET6
481 domain = (use_ipv6) ? PF_INET6 : PF_INET; 478 domain = (use_ipv6) ? PF_INET6 : PF_INET;
@@ -510,19 +507,15 @@ MHD_socket_create_listen_ (int use_ipv6)
510 } 507 }
511 if (MHD_INVALID_SOCKET == fd) 508 if (MHD_INVALID_SOCKET == fd)
512 return MHD_INVALID_SOCKET; 509 return MHD_INVALID_SOCKET;
513#if defined(OSX) && defined(SOL_SOCKET) && defined(SO_NOSIGPIPE) 510#ifdef MHD_socket_nosignal_
514 if(0 != setsockopt(fd, 511 if(! MHD_socket_nosignal_(fd))
515 SOL_SOCKET,
516 SO_NOSIGPIPE,
517 &on_val,
518 sizeof (on_val)))
519 { 512 {
520 int err = MHD_socket_get_error_ (); 513 const int err = MHD_socket_get_error_ ();
521 MHD_socket_close_ (fd); 514 MHD_socket_close_ (fd);
522 MHD_socket_fset_error_ (err); 515 MHD_socket_fset_error_ (err);
523 return MHD_INVALID_SOCKET; 516 return MHD_INVALID_SOCKET;
524 } 517 }
525#endif 518#endif /* MHD_socket_nosignal_ */
526 if (! cloexec_set) 519 if (! cloexec_set)
527 (void) MHD_socket_noninheritable_ (fd); 520 (void) MHD_socket_noninheritable_ (fd);
528 521
diff --git a/src/microhttpd/mhd_sockets.h b/src/microhttpd/mhd_sockets.h
index 6fefd7d4..d160dbcd 100644
--- a/src/microhttpd/mhd_sockets.h
+++ b/src/microhttpd/mhd_sockets.h
@@ -702,6 +702,18 @@ int
702MHD_socket_noninheritable_ (MHD_socket sock); 702MHD_socket_noninheritable_ (MHD_socket sock);
703 703
704 704
705#if defined(SOL_SOCKET) && defined(SO_NOSIGPIPE)
706 static const int _MHD_socket_int_one = 1;
707/**
708 * Change socket options to no signal on remote disconnect.
709 *
710 * @param sock socket to manipulate
711 * @return non-zero if succeeded, zero otherwise
712 */
713# define MHD_socket_nosignal_(sock) \
714 (!setsockopt((sock),SOL_SOCKET,SO_NOSIGPIPE,&_MHD_socket_int_one,sizeof(_MHD_socket_int_one)))
715#endif /* SOL_SOCKET && SO_NOSIGPIPE */
716
705/** 717/**
706 * Create a listen socket, with noninheritable flag if possible. 718 * Create a listen socket, with noninheritable flag if possible.
707 * 719 *