summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--configure.ac20
-rw-r--r--src/TcpSocket.cpp19
2 files changed, 37 insertions, 2 deletions
diff --git a/configure.ac b/configure.ac
index 2c84f8f..1dd7d66 100644
--- a/configure.ac
+++ b/configure.ac
@@ -159,6 +159,26 @@ AX_CHECK_COMPILE_FLAG([-Wlogical-op], [CXXFLAGS="$CXXFLAGS -Wlogical-op"])
AX_CHECK_COMPILE_FLAG([-Wrestrict], [CXXFLAGS="$CXXFLAGS -Wrestrict"])
AX_CHECK_COMPILE_FLAG([-Wdouble-promotion], [CXXFLAGS="$CXXFLAGS -Wdouble-promotion"])
AX_CHECK_COMPILE_FLAG(["-Wformat=2"], [CXXFLAGS="$CXXFLAGS -Wformat=2"])
+
+# Linux defines MSG_NOSIGNAL, some other systems have SO_NOSIGPIPE instead
+AC_MSG_CHECKING(for MSG_NOSIGNAL)
+AC_COMPILE_IFELSE([ AC_LANG_PROGRAM([[
+ #include <sys/socket.h>
+ int f = MSG_NOSIGNAL;
+ ]])],
+ [ AC_MSG_RESULT(yes)
+ AC_DEFINE(HAVE_MSG_NOSIGNAL, 1, [Define this symbol if you have MSG_NOSIGNAL]) ],
+ [ AC_MSG_RESULT(no) ])
+
+AC_MSG_CHECKING(for SO_NOSIGPIPE)
+AC_COMPILE_IFELSE([ AC_LANG_PROGRAM([[
+ #include <sys/socket.h>
+ int f = SO_NOSIGPIPE;
+ ]])],
+ [ AC_MSG_RESULT(yes)
+ AC_DEFINE(HAVE_SO_NOSIGPIPE, 1, [Define this symbol if you have SO_NOSIGPIPE]) ],
+ [ AC_MSG_RESULT(no) ])
+
AC_LANG_POP([C++])
# ZeroMQ
diff --git a/src/TcpSocket.cpp b/src/TcpSocket.cpp
index 40c1c1a..69b6df5 100644
--- a/src/TcpSocket.cpp
+++ b/src/TcpSocket.cpp
@@ -55,6 +55,14 @@ TcpSocket::TcpSocket(int port, const string& name) :
throw std::runtime_error("Can't reuse address");
}
+#if defined(HAVE_SO_NOSIGPIPE)
+ int val = 1;
+ if (setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof(val))
+ == SOCKET_ERROR) {
+ throw std::runtime_error("Can't set SO_NOSIGPIPE");
+ }
+#endif
+
m_own_address.setAddress(name);
m_own_address.setPort(port);
@@ -152,8 +160,15 @@ ssize_t TcpSocket::send(const void* data, size_t size, int timeout_ms)
}
}
- /* Without MSG_NOSIGNAL the process would receive a SIGPIPE and die */
- ssize_t ret = ::send(m_sock, (const char*)data, size, MSG_NOSIGNAL);
+ /* On Linux, the MSG_NOSIGNAL flag ensures that the process would not
+ * receive a SIGPIPE and die.
+ * Other systems have SO_NOSIGPIPE set on the socket for the same effect. */
+#if defined(HAVE_MSG_NOSIGNAL)
+ const int flags = MSG_NOSIGNAL;
+#else
+ const int flags = 0;
+#endif
+ ssize_t ret = ::send(m_sock, (const char*)data, size, flags);
if (ret == SOCKET_ERROR) {
stringstream ss;