From b25f4dc131b9cd2b0575995d88f969627ce98ef6 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 12 Jan 2010 21:53:59 +0000 Subject: improved new SysExec cleanup --- src/anytun.cpp | 4 ++-- src/bsd/tunDevice.cpp | 6 +++--- src/linux/tunDevice.cpp | 6 +++--- src/sysExec.cpp | 37 ++++++++++++++++++++++++++++--------- src/sysExec.h | 9 ++++++++- src/tunDevice.h | 2 +- src/win32/tunDevice.cpp | 3 ++- 7 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/anytun.cpp b/src/anytun.cpp index 4fa0b25..50aa255 100644 --- a/src/anytun.cpp +++ b/src/anytun.cpp @@ -430,9 +430,9 @@ int main(int argc, char* argv[]) #endif gResolver.init(); #ifndef NO_EXEC - boost::thread(boost::bind(&TunDevice::waitForPostUpScript,&dev)); + boost::thread(boost::bind(&TunDevice::waitUntilReady,&dev)); if (postup_script) - boost::thread(boost::bind(&SysExec::waitForScript,postup_script)); + boost::thread(boost::bind(&SysExec::waitAndDestroy,&postup_script)); #endif initCrypto(); diff --git a/src/bsd/tunDevice.cpp b/src/bsd/tunDevice.cpp index 40c3001..84fa727 100644 --- a/src/bsd/tunDevice.cpp +++ b/src/bsd/tunDevice.cpp @@ -275,9 +275,9 @@ void TunDevice::do_ifconfig() #endif } -void TunDevice::waitForPostUpScript() +void TunDevice::waitUntilReady() { - if (sys_exec_) - sys_exec_->waitForScript(); + if(sys_exec_) + SysExec::waitAndDestroy(&sys_exec_); } diff --git a/src/linux/tunDevice.cpp b/src/linux/tunDevice.cpp index c351683..bb3e722 100644 --- a/src/linux/tunDevice.cpp +++ b/src/linux/tunDevice.cpp @@ -166,8 +166,8 @@ void TunDevice::do_ifconfig() #endif } -void TunDevice::waitForPostUpScript() +void TunDevice::waitUntilReady() { - if (sys_exec_) - sys_exec_->waitForScript(); + if(sys_exec_) + SysExec::waitAndDestroy(&sys_exec_); } diff --git a/src/sysExec.cpp b/src/sysExec.cpp index 8e94c89..b6f47d8 100644 --- a/src/sysExec.cpp +++ b/src/sysExec.cpp @@ -48,26 +48,32 @@ #include #include -SysExec::SysExec(std::string const& script) : script_(script),closed_(false) +SysExec::SysExec(std::string const& script) : script_(script),closed_(false),return_code_(0) { doExec(script, StringVector(), StringList()); } -SysExec::SysExec(std::string const& script, StringVector const& args) : script_(script),closed_(false) +SysExec::SysExec(std::string const& script, StringVector const& args) : script_(script),closed_(false),return_code_(0) { doExec(script, args, StringList()); } -SysExec::SysExec(std::string const& script, StringList const& env) : script_(script),closed_(false) +SysExec::SysExec(std::string const& script, StringList const& env) : script_(script),closed_(false),return_code_(0) { doExec( script, StringVector(), env); } -SysExec::SysExec(std::string const& script, StringVector const& args, StringList const& env) : script_(script),closed_(false) +SysExec::SysExec(std::string const& script, StringVector const& args, StringList const& env) : script_(script),closed_(false),return_code_(0) { doExec( script, args, env); } +SysExec::~SysExec() +{ + if(!closed_) + close(pipefd_); +} + void SysExec::doExec(std::string const& script, StringVector const& args, StringList const& env) { int pipefd[2]; @@ -133,7 +139,7 @@ void SysExec::doExec(std::string const& script, StringVector const& args, String exit(-1); } -void SysExec::waitForScript() +int SysExec::waitForScript() { int status = 0; waitpid(pid_, &status, 0); @@ -147,7 +153,7 @@ void SysExec::waitForScript() if(read(pipefd_, (void*)(&err), sizeof(err)) >= static_cast(sizeof(err))) { cLog.msg(Log::PRIO_NOTICE) << "script '" << script_ << "' exec() error: " << AnytunErrno(err); close(pipefd_); - return; + return -1; } } if(WIFEXITED(status)) @@ -159,10 +165,23 @@ void SysExec::waitForScript() close(pipefd_); closed_=true; + + return_code_ = status; + + return status; } -SysExec::~SysExec() +int SysExec::getReturnCode() const { - if(!closed_) - close(pipefd_); + return return_code_; +} + +void SysExec::waitAndDestroy(SysExec** s) +{ + if(!s && !(*s)) + return; + + (*s)->waitForScript(); + delete(*s); + *s = NULL; } diff --git a/src/sysExec.h b/src/sysExec.h index d5315e4..7aada56 100644 --- a/src/sysExec.h +++ b/src/sysExec.h @@ -49,14 +49,21 @@ class SysExec SysExec(std::string const& script, StringVector const& args); SysExec(std::string const& script, StringList const& env); SysExec(std::string const& script, StringVector const& args, StringList const& env); - void waitForScript(); ~SysExec(); + + int waitForScript(); + int getReturnCode() const; + + static void waitAndDestroy(SysExec** s); + private: void doExec(std::string const& script, StringVector const& args, StringList const& env); + std::string script_; pid_t pid_; int pipefd_; bool closed_; + int return_code_; }; #endif diff --git a/src/tunDevice.h b/src/tunDevice.h index 834837e..8b400a9 100644 --- a/src/tunDevice.h +++ b/src/tunDevice.h @@ -54,7 +54,7 @@ public: const char* getActualName() const { return actual_name_.c_str(); } const char* getActualNode() const { return actual_node_.c_str(); } device_type_t getType() const { return conf_.type_; } - void waitForPostUpScript(); + void waitUntilReady(); const char* getTypeString() const { #ifndef _MSC_VER diff --git a/src/win32/tunDevice.cpp b/src/win32/tunDevice.cpp index c5b378f..6e6c83d 100644 --- a/src/win32/tunDevice.cpp +++ b/src/win32/tunDevice.cpp @@ -271,6 +271,7 @@ void TunDevice::do_ifconfig() conf_.mtu_ = static_cast(mtu); } -void TunDevice::waitForPostUpScript() +void TunDevice::waitUntilReady() { +// nothing to be done here } -- cgit v1.2.3