From: Remi Gacogne Date: Fri, 18 Mar 2022 09:32:42 +0000 (+0100) Subject: dnsdist: Wrap the pipe descriptors earlier, do not leak on send errors X-Git-Tag: rec-5.0.0-alpha1~161^2~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=52f6247b5a24ad58af41d085dfb049475b8a2ca5;p=thirdparty%2Fpdns.git dnsdist: Wrap the pipe descriptors earlier, do not leak on send errors --- diff --git a/pdns/dnsdistdist/channel.cc b/pdns/dnsdistdist/channel.cc index 75cca8a706..dd25c6d34e 100644 --- a/pdns/dnsdistdist/channel.cc +++ b/pdns/dnsdistdist/channel.cc @@ -82,27 +82,23 @@ namespace channel throw std::runtime_error("Error creating notification channel pipe: " + stringerror()); } - if (nonBlocking && !setNonBlocking(fds[0])) { + FDWrapper sender(fds[1]); + FDWrapper receiver(fds[0]); + + if (nonBlocking && !setNonBlocking(receiver.getHandle())) { int err = errno; - close(fds[0]); - close(fds[1]); throw std::runtime_error("Error making notification channel pipe non-blocking: " + stringerror(err)); } - if (nonBlocking && !setNonBlocking(fds[1])) { + if (nonBlocking && !setNonBlocking(sender.getHandle())) { int err = errno; - close(fds[0]); - close(fds[1]); throw std::runtime_error("Error making notification channel pipe non-blocking: " + stringerror(err)); } - if (pipeBufferSize > 0 && getPipeBufferSize(fds[0]) < pipeBufferSize) { - setPipeBufferSize(fds[0], pipeBufferSize); + if (pipeBufferSize > 0 && getPipeBufferSize(receiver.getHandle()) < pipeBufferSize) { + setPipeBufferSize(receiver.getHandle(), pipeBufferSize); } - FDWrapper sender(fds[1]); - FDWrapper receiver(fds[0]); - return std::pair(Notifier(std::move(sender)), Waiter(std::move(receiver))); } } diff --git a/pdns/dnsdistdist/channel.hh b/pdns/dnsdistdist/channel.hh index 55ee5f956d..5698fb7ba3 100644 --- a/pdns/dnsdistdist/channel.hh +++ b/pdns/dnsdistdist/channel.hh @@ -191,13 +191,13 @@ namespace channel ssize_t sent = write(d_fd.getHandle(), &ptr, sizeof(ptr)); if (sent != sizeof(ptr)) { + delete ptr; if (errno == EAGAIN || errno == EWOULDBLOCK) { return false; } else { throw std::runtime_error("Unable to write to channel:" + stringerror()); } - delete ptr; } return true; @@ -234,27 +234,23 @@ namespace channel throw std::runtime_error("Error creating channel pipe: " + stringerror()); } - if (nonBlocking && !setNonBlocking(fds[0])) { + FDWrapper sender(fds[1]); + FDWrapper receiver(fds[0]); + + if (nonBlocking && !setNonBlocking(receiver.getHandle())) { int err = errno; - close(fds[0]); - close(fds[1]); throw std::runtime_error("Error making channel pipe non-blocking: " + stringerror(err)); } - if (nonBlocking && !setNonBlocking(fds[1])) { + if (nonBlocking && !setNonBlocking(sender.getHandle())) { int err = errno; - close(fds[0]); - close(fds[1]); throw std::runtime_error("Error making channel pipe non-blocking: " + stringerror(err)); } - if (pipeBufferSize > 0 && getPipeBufferSize(fds[0]) < pipeBufferSize) { - setPipeBufferSize(fds[0], pipeBufferSize); + if (pipeBufferSize > 0 && getPipeBufferSize(receiver.getHandle()) < pipeBufferSize) { + setPipeBufferSize(receiver.getHandle(), pipeBufferSize); } - FDWrapper sender(fds[1]); - FDWrapper receiver(fds[0]); - return std::pair(Sender(std::move(sender)), Receiver(std::move(receiver))); } }