diff --git a/src/utils/execmd.cpp b/src/utils/execmd.cpp index 9d093f96..9eb2b205 100644 --- a/src/utils/execmd.cpp +++ b/src/utils/execmd.cpp @@ -46,6 +46,7 @@ static char rcsid[] = "@(#$Id: execmd.cpp,v 1.27 2008-10-06 06:22:47 dockes Exp #include "debuglog.h" #include "smallut.h" #include "netcon.h" +#include "closefrom.h" #ifndef NO_NAMESPACES using namespace std; @@ -353,12 +354,15 @@ int ExecCmd::doexec(const string &cmd, const list& args, LOGDEB(("ExecCmd::doexec: selectloop returned %d\n", ret)); if (m_advise) m_advise->newData(0); - if (m_cancelRequest) { + if (m_killRequest) { LOGINFO(("ExecCmd::doexec: cancel request\n")); break; } } LOGDEB0(("ExecCmd::doexec: selectloop returned %d\n", ret)); + // Check for interrupt request: we won't want to waitpid() + if (m_advise) + m_advise->newData(0); // The netcons don't take ownership of the fds: we have to close them // (have to do it before wait, this may be the signal the child is @@ -388,7 +392,7 @@ int ExecCmd::send(const string& data) } unsigned int nwritten = 0; while (nwritten < data.length()) { - if (m_cancelRequest) + if (m_killRequest) break; int n = con->send(data.c_str() + nwritten, data.length() - nwritten); if (n < 0) { @@ -452,7 +456,7 @@ int ExecCmd::wait(bool haderror) { ExecCmdRsrc e(this); int status = -1; - if (!m_cancelRequest && m_pid > 0) { + if (!m_killRequest && m_pid > 0) { if (waitpid(m_pid, &status, 0) < 0) status = -1; m_pid = -1; @@ -508,6 +512,9 @@ void ExecCmd::dochild(const string &cmd, const list& args, } } + // Close all descriptors except 0,1,2 + libclf_closefrom(3); + // Allocate arg vector (2 more for arg0 + final 0) typedef const char *Ccharp; Ccharp *argv; diff --git a/src/utils/execmd.h b/src/utils/execmd.h index a5a38379..b3e9da24 100644 --- a/src/utils/execmd.h +++ b/src/utils/execmd.h @@ -35,7 +35,7 @@ using std::vector; * * To interrupt the command, the code using ExecCmd should either * raise an exception inside newData() (and catch it in doexec's caller), or - * call ExecCmd::setCancel() + * call ExecCmd::setKill() * */ class ExecCmdAdvise { @@ -145,7 +145,7 @@ class ExecCmd { * from the advise callback, which could also raise an exception to * accomplish the same thing */ - void setCancel() {m_cancelRequest = true;} + void setKill() {m_killRequest = true;} ExecCmd() : m_advise(0), m_provide(0), m_timeoutMs(1000) @@ -169,7 +169,7 @@ class ExecCmd { vector m_env; ExecCmdAdvise *m_advise; ExecCmdProvide *m_provide; - bool m_cancelRequest; + bool m_killRequest; int m_timeoutMs; string m_stderrFile; // Pipe for data going to the command @@ -186,7 +186,7 @@ class ExecCmd { // Reset internal state indicators. Any resources should have been // previously freed void reset() { - m_cancelRequest = false; + m_killRequest = false; m_pipein[0] = m_pipein[1] = m_pipeout[0] = m_pipeout[1] = -1; m_pid = -1; sigemptyset(&m_blkcld);