diff options
author | Christian Pointner <equinox@anytun.org> | 2010-01-12 21:53:59 +0000 |
---|---|---|
committer | Christian Pointner <equinox@anytun.org> | 2010-01-12 21:53:59 +0000 |
commit | b25f4dc131b9cd2b0575995d88f969627ce98ef6 (patch) | |
tree | 0c5f4c9c939c851cd2fc987f4ce0b4920ac25e50 /src/sysExec.cpp | |
parent | cleanup (diff) |
improved new SysExec cleanup
Diffstat (limited to 'src/sysExec.cpp')
-rw-r--r-- | src/sysExec.cpp | 37 |
1 files changed, 28 insertions, 9 deletions
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 <string.h> #include <cstring> -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<int>(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; } |