diff options
-rw-r--r-- | src/posix/sysExec.hpp | 62 | ||||
-rw-r--r-- | src/sysExec.cpp | 6 | ||||
-rw-r--r-- | src/sysExec.h | 8 | ||||
-rw-r--r-- | src/win32/sysExec.hpp | 2 |
4 files changed, 49 insertions, 29 deletions
diff --git a/src/posix/sysExec.hpp b/src/posix/sysExec.hpp index 48cd8f2..18fde97 100644 --- a/src/posix/sysExec.hpp +++ b/src/posix/sysExec.hpp @@ -49,21 +49,42 @@ SysExec::~SysExec() close(pipefd_); } -char** dupEnv(StringList const& env) + +template<class T> +char** dupSysStringArray(T const& array) { - char** evp; - evp = new char*[env.size() + 1]; + char** new_array; + new_array = static_cast<char**>(malloc((array.size() + 1)*sizeof(char*))); + if(!new_array) + return NULL; + unsigned int i = 0; - for(StringList::const_iterator it = env.begin(); it != env.end(); ++it) { - evp[i] = new char[it->size() + 1]; - std::strcpy(evp[i], it->c_str()); + for(typename T::const_iterator it = array.begin(); it != array.end(); ++it) { + new_array[i] = strdup(it->c_str()); + if(!new_array) { + while(i--) + free(new_array[i]); + free(new_array); + return NULL; + } ++i; } - evp[env.size()] = NULL; - return evp; + new_array[array.size()] = NULL; + return new_array; } -void SysExec::doExec(StringVector const& args, StringList const& env) +void freeSysStringArray(char** array) +{ + if(!array) + return; + + for(int i=0; array[i] ; ++i) + free(array[i]); + + free(array); +} + +void SysExec::doExec(StringVector args, StringList env) { int pipefd[2]; if(pipe(pipefd) == -1) { @@ -98,21 +119,18 @@ void SysExec::doExec(StringVector const& args, StringList const& env) cLog.msg(Log::PRIO_WARNING) << "can't open stderr"; } - char** argv = new char*[args.size() + 2]; - argv[0] = new char[script_.size() + 1]; - std::strcpy(argv[0], script_.c_str()); - for(unsigned int i=0; i<args.size(); ++i) { - argv[i+1] = new char[args[i].size() + 1]; - std::strcpy(argv[i+1], args[i].c_str()); - } - argv[args.size() + 1] = NULL; - - char** evp = dupEnv(env); + args.insert(args.begin(), script_); + char** argv = dupSysStringArray(args); + char** evp = dupSysStringArray(env); execve(script_.c_str(), argv, evp); // if execve returns, an error occurred, but logging doesn't work // because we closed all file descriptors, so just write errno to // pipe and call exit + + freeSysStringArray(argv); + freeSysStringArray(evp); + int err = errno; int ret = write(pipefd[1], (void*)(&err), sizeof(err)); if(ret != sizeof(errno)) @@ -156,8 +174,10 @@ void SysExec::waitAndDestroy(SysExec*& s) cLog.msg(Log::PRIO_NOTICE) << "script '" << s->script_ << "' returned " << WEXITSTATUS(s->return_code_); else if(WIFSIGNALED(s->return_code_)) cLog.msg(Log::PRIO_NOTICE) << "script '" << s->script_ << "' terminated after signal " << WTERMSIG(s->return_code_); - else - cLog.msg(Log::PRIO_ERROR) << "executing script '" << s->script_ << "': unknown error"; + else if(WIFSTOPPED(s->return_code_)) + cLog.msg(Log::PRIO_NOTICE) << "script '" << s->script_ << "' stopped after signal " << WSTOPSIG(s->return_code_); + else if(WIFCONTINUED(s->return_code_)) + cLog.msg(Log::PRIO_NOTICE) << "script '" << s->script_ << "' continued after SIGCONT"; delete(s); s = NULL; diff --git a/src/sysExec.cpp b/src/sysExec.cpp index 9fc6e10..a69349f 100644 --- a/src/sysExec.cpp +++ b/src/sysExec.cpp @@ -51,17 +51,17 @@ SysExec::SysExec(std::string const& script) : script_(script),closed_(false),ret doExec(StringVector(), StringList()); } -SysExec::SysExec(std::string const& script, StringVector const& args) : script_(script),closed_(false),return_code_(0) +SysExec::SysExec(std::string const& script, StringVector args) : script_(script),closed_(false),return_code_(0) { doExec(args, StringList()); } -SysExec::SysExec(std::string const& script, StringList const& env) : script_(script),closed_(false),return_code_(0) +SysExec::SysExec(std::string const& script, StringList env) : script_(script),closed_(false),return_code_(0) { doExec(StringVector(), env); } -SysExec::SysExec(std::string const& script, StringVector const& args, StringList const& env) : script_(script),closed_(false),return_code_(0) +SysExec::SysExec(std::string const& script, StringVector args, StringList env) : script_(script),closed_(false),return_code_(0) { doExec(args, env); } diff --git a/src/sysExec.h b/src/sysExec.h index b6efb45..b5400b9 100644 --- a/src/sysExec.h +++ b/src/sysExec.h @@ -44,9 +44,9 @@ class SysExec { public: SysExec(std::string const& script); - 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); + SysExec(std::string const& script, StringVector args); + SysExec(std::string const& script, StringList env); + SysExec(std::string const& script, StringVector args, StringList env); ~SysExec(); int waitForScript(); @@ -55,7 +55,7 @@ class SysExec static void waitAndDestroy(SysExec*& s); private: - void doExec(StringVector const& args, StringList const& env); + void doExec(StringVector args, StringList env); std::string script_; bool closed_; diff --git a/src/win32/sysExec.hpp b/src/win32/sysExec.hpp index eba26f4..ed5be01 100644 --- a/src/win32/sysExec.hpp +++ b/src/win32/sysExec.hpp @@ -67,7 +67,7 @@ bool endsWith(std::string const& string, std::string const& suffix) { return string.find(suffix, string.size() - suffix.size()) != std::string::npos;
}
-void SysExec::doExec(StringVector const& args, StringList const& env)
+void SysExec::doExec(StringVector args, StringList env)
{
std::vector<char> arguments;
|