From 4d1a6ad2f267e3b83f7c04122af8d8e7ee9a2113 Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Mon, 25 Apr 2016 08:28:34 +0200 Subject: [PATCH] Issue #138: fixed MSG_NOSIGNAL on Mac OS. --- CMakeLists.txt | 3 +++ cmake/config.hpp.cmake | 2 ++ src/vmime/platforms/posix/posixSocket.cpp | 20 ++++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index aaec1d15..c157e4b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -977,6 +977,9 @@ CHECK_FUNCTION_EXISTS(syscall VMIME_HAVE_SYSCALL) CHECK_SYMBOL_EXISTS(SYS_gettid sys/syscall.h VMIME_HAVE_SYSCALL_GETTID) CHECK_SYMBOL_EXISTS(SO_KEEPALIVE sys/socket.h VMIME_HAVE_SO_KEEPALIVE) +CHECK_SYMBOL_EXISTS(SO_NOSIGPIPE sys/socket.h VMIME_HAVE_SO_NOSIGPIPE) + +CHECK_SYMBOL_EXISTS(MSG_NOSIGNAL sys/socket.h VMIME_HAVE_MSG_NOSIGNAL) CHECK_SYMBOL_EXISTS(strerror_r string.h VMIME_HAVE_STRERROR_R) diff --git a/cmake/config.hpp.cmake b/cmake/config.hpp.cmake index bb24239d..3e32dd92 100644 --- a/cmake/config.hpp.cmake +++ b/cmake/config.hpp.cmake @@ -87,6 +87,8 @@ typedef unsigned @VMIME_64BIT_TYPE@ vmime_uint64; #cmakedefine01 VMIME_HAVE_STRERROR_R #cmakedefine01 VMIME_HAVE_MLANG #cmakedefine01 VMIME_HAVE_SO_KEEPALIVE +#cmakedefine01 VMIME_HAVE_SO_NOSIGPIPE +#cmakedefine01 VMIME_HAVE_MSG_NOSIGNAL #cmakedefine01 VMIME_SHARED_PTR_USE_CXX #cmakedefine01 VMIME_SHARED_PTR_USE_BOOST diff --git a/src/vmime/platforms/posix/posixSocket.cpp b/src/vmime/platforms/posix/posixSocket.cpp index e27cb170..13b2d85d 100644 --- a/src/vmime/platforms/posix/posixSocket.cpp +++ b/src/vmime/platforms/posix/posixSocket.cpp @@ -138,6 +138,17 @@ void posixSocket::connect(const vmime::string& address, const vmime::port_t port #endif // VMIME_HAVE_SO_KEEPALIVE +#if VMIME_HAVE_SO_NOSIGPIPE + + // Return EPIPE instead of generating SIGPIPE + int nosigpipe_optval = 1; + socklen_t nosigpipe_optlen = sizeof(nosigpipe_optval); + + ::setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe_optval, nosigpipe_optlen); + +#endif // VMIME_HAVE_SO_NOSIGPIPE + + if (m_timeoutHandler != NULL) { ::fcntl(sock, F_SETFL, ::fcntl(sock, F_GETFL) | O_NONBLOCK); @@ -750,7 +761,12 @@ void posixSocket::sendRaw(const byte_t* buffer, const size_t count) while (size > 0) { + +#if VMIME_HAVE_MSG_NOSIGNAL const ssize_t ret = ::send(m_desc, buffer, size, MSG_NOSIGNAL); +#else + const ssize_t ret = ::send(m_desc, buffer, size, 0); +#endif if (ret <= 0) { @@ -776,7 +792,11 @@ size_t posixSocket::sendRawNonBlocking(const byte_t* buffer, const size_t count) { m_status &= ~STATUS_WOULDBLOCK; +#if VMIME_HAVE_MSG_NOSIGNAL const ssize_t ret = ::send(m_desc, buffer, count, MSG_NOSIGNAL); +#else + const ssize_t ret = ::send(m_desc, buffer, count, 0); +#endif if (ret <= 0) {