summaryrefslogtreecommitdiff
path: root/src/sysExec.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/sysExec.cpp')
-rw-r--r--src/sysExec.cpp37
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;
}