139
140
if (m_parent->m_pid > 0) {
140
141
pid_t grp = getpgid(m_parent->m_pid);
141
LOGDEB2(("ExecCmd: killpg(%d, SIGTERM)\n", grp));
142
LOGDEB(("ExecCmd: killpg(%d, SIGTERM)\n", grp));
142
143
int ret = killpg(grp, SIGTERM);
144
145
for (int i = 0; i < 3; i++) {
175
176
int ExecCmd::startExec(const string &cmd, const list<string>& args,
176
bool has_input, bool has_output)
177
bool has_input, bool has_output)
178
179
{ // Debug and logging
179
180
string command = cmd + " ";
268
269
LOGDEB2(("ExecWriter: provide m_cnt %d input length %d\n",
269
m_cnt, m_input->length()));
270
m_cnt, m_input->length()));
385
386
// Normal return: deactivate cleaner, wait() will do the cleanup
388
return ExecCmd::wait(ret);
389
int ret1 = ExecCmd::wait();
391
395
int ExecCmd::send(const string& data)
459
463
// Wait for command status and clean up all resources.
460
int ExecCmd::wait(bool haderror)
462
466
ExecCmdRsrc e(this);
464
468
if (!m_killRequest && m_pid > 0) {
465
if (waitpid(m_pid, &status, 0) < 0)
469
if (waitpid(m_pid, &status, 0) < 0) {
470
LOGERR(("ExecCmd::waitpid: returned -1 errno %d\n", errno));
467
473
LOGDEB(("ExecCmd::wait: got status 0x%x\n", status));
470
return haderror ? -1 : status;
476
// Let the ExecCmdRsrc cleanup
480
bool ExecCmd::maybereap(int *status)
486
// Already waited for ??
490
pid_t pid = waitpid(m_pid, status, WNOHANG);
492
LOGERR(("ExecCmd::maybereap: returned -1 errno %d\n", errno));
495
} else if (pid == 0) {
496
LOGDEB1(("ExecCmd::maybereap: not exited yet\n"));
500
LOGDEB(("ExecCmd::maybereap: got status 0x%x\n", status));
473
506
// In child process. Set up pipes, environment, and exec command.
610
ReExec::ReExec(int argc, char *args[])
615
void ReExec::init(int argc, char *args[])
617
for (int i = 0; i < argc; i++) {
618
m_argv.push_back(args[i]);
620
m_cfd = open(".", 0);
621
char *cd = getcwd(0, 0);
627
// Reexecute myself, as close as possible to the initial exec
628
void ReExec::reexec()
634
FILE *fp = stdout; //fopen("/tmp/exectrace", "w");
636
fprintf(fp, "reexec: pwd: [%s] args: ", cwd?cwd:"getcwd failed");
637
for (vector<string>::const_iterator it = m_argv.begin();
638
it != m_argv.end(); it++) {
639
fprintf(fp, "[%s] ", it->c_str());
645
// Execute the atexit funcs
646
while (!m_atexitfuncs.empty()) {
647
(m_atexitfuncs.top())();
651
// Try to get back to the initial working directory
652
if (m_cfd < 0 || fchdir(m_cfd) < 0) {
653
LOGINFO(("ReExec::reexec: fchdir failed, trying chdir\n"));
654
if (!m_curdir.empty() && chdir(m_curdir.c_str())) {
655
LOGERR(("ReExec::reexec: chdir failed\n"));
659
// Close all descriptors except 0,1,2
662
// Allocate arg vector (1 more for final 0)
663
typedef const char *Ccharp;
665
argv = (Ccharp *)malloc((m_argv.size()+1) * sizeof(char *));
667
LOGERR(("ExecCmd::doexec: malloc() failed. errno %d\n", errno));
673
vector<string>::const_iterator it;
674
for (it = m_argv.begin(); it != m_argv.end(); it++) {
675
argv[i++] = it->c_str();
678
execvp(m_argv[0].c_str(), (char *const*)argv);
577
681
////////////////////////////////////////////////////////////////////
579
683
#include <stdio.h>
675
785
while (argc > 0) {
676
786
l.push_back(*argv++); argc--;
679
788
DebugLog::getdbl()->setloglevel(DEBDEB1);
680
789
DebugLog::setfilename("stderr");
681
790
signal(SIGPIPE, SIG_IGN);
792
if (op_flags & OPT_r) {
794
argv[0] = strdup("");
682
799
if (op_flags & OPT_w) {
684
801
if (ExecCmd::which(cmd, path)) {