From: Remi Gacogne Date: Mon, 14 Mar 2016 09:22:08 +0000 (+0100) Subject: dnsdist: Add more information when readn2 fails because of EAGAIN X-Git-Tag: dnsdist-1.0.0-beta1~119^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F3564%2Fhead;p=thirdparty%2Fpdns.git dnsdist: Add more information when readn2 fails because of EAGAIN --- diff --git a/pdns/dnsdist-tcp.cc b/pdns/dnsdist-tcp.cc index 2cb75474b7..d4320e7aab 100644 --- a/pdns/dnsdist-tcp.cc +++ b/pdns/dnsdist-tcp.cc @@ -160,7 +160,13 @@ void* tcpClientThread(int pipefd) for(;;) { ConnectionInfo* citmp, ci; - readn2(pipefd, &citmp, sizeof(citmp)); + try { + readn2(pipefd, &citmp, sizeof(citmp)); + } + catch(const std::runtime_error& e) { + throw std::runtime_error("Error reading from TCP acceptor pipe (" + std::to_string(pipefd) + ") in " + std::string(isNonBlocking(pipefd) ? "non-blocking" : "blocking") + " mode: " + e.what()); + } + --g_tcpclientthreads->d_queued; ci=*citmp; delete citmp; @@ -366,7 +372,7 @@ void* tcpClientThread(int pipefd) break; } - int dsock; + int dsock = -1; if(sockets.count(ds->remote) == 0) { dsock=sockets[ds->remote]=setupTCPDownstream(ds); } @@ -387,6 +393,7 @@ void* tcpClientThread(int pipefd) if (ds->retries > 0 && downstream_failures > ds->retries) { vinfolog("Downstream connection to %s failed %d times in a row, giving up.", ds->getName(), downstream_failures); close(dsock); + dsock=-1; sockets.erase(ds->remote); break; } @@ -608,6 +615,7 @@ void* tcpAcceptorThread(void* p) --g_tcpclientthreads->d_queued; close(ci->fd); delete ci; + ci=nullptr; } } catch(std::exception& e) { diff --git a/pdns/misc.cc b/pdns/misc.cc index e4eb2d58a9..1b7a545b13 100644 --- a/pdns/misc.cc +++ b/pdns/misc.cc @@ -1063,6 +1063,12 @@ bool setBlocking(int sock) return true; } +bool isNonBlocking(int sock) +{ + int flags=fcntl(sock,F_GETFL,0); + return flags & O_NONBLOCK; +} + // Closes a socket. int closesocket( int socket ) { diff --git a/pdns/misc.hh b/pdns/misc.hh index d9e6f9f8c4..7b2713fbf5 100644 --- a/pdns/misc.hh +++ b/pdns/misc.hh @@ -600,6 +600,7 @@ bool setBlocking( int sock ); //! Sets the socket into non-blocking mode. bool setNonBlocking( int sock ); +bool isNonBlocking(int sock); int closesocket(int fd); bool setCloseOnExec(int sock); uint64_t udpErrorStats(const std::string& str);