diff options
author | Alan Conway <aconway@apache.org> | 2008-11-19 20:41:38 +0000 |
---|---|---|
committer | Alan Conway <aconway@apache.org> | 2008-11-19 20:41:38 +0000 |
commit | d8dd90ae7856bfe014eda251cf537f0e77594b39 (patch) | |
tree | cd0a63aa10e964035f2b09c894aa0c80d1d8c32a /cpp/src | |
parent | 66442ebf857fb8a6981ee4b0c45eb2f74143c7d0 (diff) | |
download | qpid-python-d8dd90ae7856bfe014eda251cf537f0e77594b39.tar.gz |
File descriptor leak in ForkedBroker test utility.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@719062 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src')
-rw-r--r-- | cpp/src/tests/ForkedBroker.h | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/cpp/src/tests/ForkedBroker.h b/cpp/src/tests/ForkedBroker.h index 98235b9690..fed203ebb1 100644 --- a/cpp/src/tests/ForkedBroker.h +++ b/cpp/src/tests/ForkedBroker.h @@ -61,9 +61,7 @@ class ForkedBroker { void kill(int sig=SIGINT) { if (pid == 0) return; int savePid = pid; - pid = 0; // Always reset pid, even in case of an exception below. - ::close(pipeFds[1]); - + pid = 0; // Reset pid here in case of an exception. using qpid::ErrnoException; if (::kill(savePid, sig) < 0) throw ErrnoException("kill failed"); @@ -79,10 +77,16 @@ class ForkedBroker { private: + template <class F> struct OnExit { + F fn; + OnExit(F f) : fn(f) {} + ~OnExit() { fn(); } + }; + void init(const std::vector<const char*>& args) { using qpid::ErrnoException; - pid = 0; port = 0; + int pipeFds[2]; if(::pipe(pipeFds) < 0) throw ErrnoException("Can't create pipe"); pid = ::fork(); if (pid < 0) throw ErrnoException("Fork failed"); @@ -94,10 +98,11 @@ class ForkedBroker { if (ferror(f)) throw ErrnoException("Error reading port number from child."); else throw qpid::Exception("EOF reading port number from child."); } + ::close(pipeFds[0]); } else { // child ::close(pipeFds[0]); - int fd = ::dup2(pipeFds[1], 1); + int fd = ::dup2(pipeFds[1], 1); // pipe stdout to the parent. if (fd < 0) throw ErrnoException("dup2 failed"); const char* prog = "../qpidd"; std::vector<const char*> args2(args); @@ -109,7 +114,6 @@ class ForkedBroker { } } - int pipeFds[2]; pid_t pid; int port; }; |