diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2017-12-25 06:46:07 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2017-12-25 06:46:07 +0100 |
commit | bc5175023f3f0587391699d736071d1ccc4eff05 (patch) | |
tree | 7f46b665906641f1a11f5ad7cb9f6b8ee69cda26 /src/Socket.h | |
parent | 92bd6722d4dfc0526d5f2f12e99440b0e18d7479 (diff) | |
download | dabmod-bc5175023f3f0587391699d736071d1ccc4eff05.tar.gz dabmod-bc5175023f3f0587391699d736071d1ccc4eff05.tar.bz2 dabmod-bc5175023f3f0587391699d736071d1ccc4eff05.zip |
Portability: check for SO_NOSIGPIPE
Diffstat (limited to 'src/Socket.h')
-rw-r--r-- | src/Socket.h | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/src/Socket.h b/src/Socket.h index 39554ca..5145103 100644 --- a/src/Socket.h +++ b/src/Socket.h @@ -49,6 +49,14 @@ class TCPSocket { if ((m_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) { throw std::runtime_error("Can't create TCP socket"); } + +#if defined(HAVE_SO_NOSIGPIPE) + int val = 1; + if (setsockopt(m_sock, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof(val)) + == SOCKET_ERROR) { + throw std::runtime_error("Can't set SO_NOSIGPIPE"); + } +#endif } ~TCPSocket() { @@ -93,7 +101,7 @@ class TCPSocket { throw std::runtime_error("Can't reuse address for TCP socket"); } - if (bind(m_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { + if (::bind(m_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { close(); throw std::runtime_error("Can't bind TCP socket"); } @@ -138,8 +146,16 @@ class TCPSocket { { uint8_t *buf = (uint8_t*)buffer; while (buflen > 0) { - // Set MSG_NOSIGNAL to avoid that this thread gets a SIGPIPE - ssize_t sent = send(m_sock, buf, buflen, 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 sent = ::send(m_sock, buf, buflen, flags); if (sent < 0) { return -1; } |