summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anytun.org>2010-01-12 21:53:59 +0000
committerChristian Pointner <equinox@anytun.org>2010-01-12 21:53:59 +0000
commitb25f4dc131b9cd2b0575995d88f969627ce98ef6 (patch)
tree0c5f4c9c939c851cd2fc987f4ce0b4920ac25e50
parentcleanup (diff)
improved new SysExec cleanup
-rw-r--r--src/anytun.cpp4
-rw-r--r--src/bsd/tunDevice.cpp6
-rw-r--r--src/linux/tunDevice.cpp6
-rw-r--r--src/sysExec.cpp37
-rw-r--r--src/sysExec.h9
-rw-r--r--src/tunDevice.h2
-rw-r--r--src/win32/tunDevice.cpp3
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 <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;
}
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<u_int16_t>(mtu);
}
-void TunDevice::waitForPostUpScript()
+void TunDevice::waitUntilReady()
{
+// nothing to be done here
}