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/sysExec.cpp | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) (limited to 'src/sysExec.cpp') 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; } -- cgit v1.2.3