2002-02-27 09:59:52 +00:00
|
|
|
/**
|
2003-06-18 09:56:10 +00:00
|
|
|
* \file forkedcall.C
|
2002-09-25 10:03:41 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-02-27 09:59:52 +00:00
|
|
|
*
|
|
|
|
* \author Asger Alstrup
|
|
|
|
*
|
|
|
|
* Interface cleaned up by
|
2002-09-25 10:03:41 +00:00
|
|
|
* \author Angus Leeming
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-02-27 09:59:52 +00:00
|
|
|
*
|
|
|
|
* An instance of Class Forkedcall represents a single child process.
|
|
|
|
*
|
|
|
|
* Class Forkedcall uses fork() and execvp() to lauch the child process.
|
|
|
|
*
|
|
|
|
* Once launched, control is returned immediately to the parent process
|
|
|
|
* but a Signal can be emitted upon completion of the child.
|
|
|
|
*
|
|
|
|
* The child process is not killed when the Forkedcall instance goes out of
|
|
|
|
* scope, but it can be killed by an explicit invocation of the kill() member
|
|
|
|
* function.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2004-11-07 13:22:51 +00:00
|
|
|
#include "support/forkedcall.h"
|
|
|
|
#include "support/forkedcontr.h"
|
|
|
|
#include "support/lstrings.h"
|
|
|
|
#include "support/lyxlib.h"
|
|
|
|
#include "support/filetools.h"
|
|
|
|
#include "support/os.h"
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
#include "debug.h"
|
2004-11-07 13:22:51 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
#include "frontends/Timeout.h"
|
|
|
|
|
2002-05-29 16:21:03 +00:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <csignal>
|
|
|
|
#include <cstdlib>
|
2004-03-26 23:55:33 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2002-02-27 09:59:52 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2004-03-26 23:55:33 +00:00
|
|
|
#include <vector>
|
2003-10-06 15:43:21 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
using std::endl;
|
2004-03-26 23:55:33 +00:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
#ifndef CXX_GLOBAL_CSTD
|
|
|
|
using std::strerror;
|
|
|
|
#endif
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace support {
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2002-05-29 16:21:03 +00:00
|
|
|
class Murder : public boost::signals::trackable {
|
2002-02-27 09:59:52 +00:00
|
|
|
public:
|
|
|
|
//
|
|
|
|
static void killItDead(int secs, pid_t pid)
|
|
|
|
{
|
|
|
|
if (secs > 0) {
|
|
|
|
new Murder(secs, pid);
|
|
|
|
} else if (pid != 0) {
|
2003-06-30 23:56:22 +00:00
|
|
|
lyx::support::kill(pid, SIGKILL);
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
void kill()
|
|
|
|
{
|
|
|
|
if (pid_ != 0) {
|
2003-06-30 23:56:22 +00:00
|
|
|
lyx::support::kill(pid_, SIGKILL);
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
lyxerr << "Killed " << pid_ << std::endl;
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
//
|
|
|
|
Murder(int secs, pid_t pid)
|
|
|
|
: timeout_(0), pid_(pid)
|
|
|
|
{
|
|
|
|
timeout_ = new Timeout(1000*secs, Timeout::ONETIME);
|
2002-05-29 16:21:03 +00:00
|
|
|
timeout_->timeout.connect(boost::bind(&Murder::kill, this));
|
2002-02-27 09:59:52 +00:00
|
|
|
timeout_->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
~Murder()
|
|
|
|
{
|
|
|
|
delete timeout_;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
Timeout * timeout_;
|
|
|
|
//
|
|
|
|
pid_t pid_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
2002-10-31 12:42:26 +00:00
|
|
|
ForkedProcess::ForkedProcess()
|
|
|
|
: pid_(0), retval_(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
void ForkedProcess::emitSignal()
|
|
|
|
{
|
|
|
|
if (signal_.get()) {
|
|
|
|
signal_->operator()(pid_, retval_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-26 23:55:33 +00:00
|
|
|
// Spawn the child process
|
|
|
|
int ForkedProcess::run(Starttype type)
|
2002-10-31 12:42:26 +00:00
|
|
|
{
|
|
|
|
retval_ = 0;
|
|
|
|
pid_ = generateChild();
|
|
|
|
if (pid_ <= 0) { // child or fork failed.
|
|
|
|
retval_ = 1;
|
|
|
|
return retval_;
|
|
|
|
}
|
|
|
|
|
2004-03-26 23:55:33 +00:00
|
|
|
switch (type) {
|
|
|
|
case Wait:
|
|
|
|
retval_ = waitForChild();
|
|
|
|
break;
|
|
|
|
case DontWait: {
|
|
|
|
// Integrate into the Controller
|
|
|
|
ForkedcallsController & contr = ForkedcallsController::get();
|
|
|
|
contr.addCall(*this);
|
|
|
|
break;
|
|
|
|
}
|
2002-10-31 12:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return retval_;
|
|
|
|
}
|
|
|
|
|
2003-02-17 18:40:04 +00:00
|
|
|
|
|
|
|
bool ForkedProcess::running() const
|
|
|
|
{
|
|
|
|
if (!pid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Un-UNIX like, but we don't have much use for
|
|
|
|
// knowing if a zombie exists, so just reap it first.
|
|
|
|
int waitstatus;
|
|
|
|
waitpid(pid(), &waitstatus, WNOHANG);
|
|
|
|
|
|
|
|
// Racy of course, but it will do.
|
|
|
|
if (::kill(pid(), 0) && errno == ESRCH)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-31 12:42:26 +00:00
|
|
|
void ForkedProcess::kill(int tol)
|
|
|
|
{
|
2002-11-27 10:30:28 +00:00
|
|
|
lyxerr << "ForkedProcess::kill(" << tol << ')' << endl;
|
2002-02-27 09:59:52 +00:00
|
|
|
if (pid() == 0) {
|
|
|
|
lyxerr << "Can't kill non-existent process!" << endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int const tolerance = std::max(0, tol);
|
|
|
|
if (tolerance == 0) {
|
|
|
|
// Kill it dead NOW!
|
|
|
|
Murder::killItDead(0, pid());
|
|
|
|
|
|
|
|
} else {
|
2003-06-30 23:56:22 +00:00
|
|
|
int ret = lyx::support::kill(pid(), SIGHUP);
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
// The process is already dead if wait_for_death is false
|
|
|
|
bool const wait_for_death = (ret == 0 && errno != ESRCH);
|
|
|
|
|
|
|
|
if (wait_for_death) {
|
|
|
|
Murder::killItDead(tolerance, pid());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Wait for child process to finish. Returns returncode from child.
|
2002-10-31 12:42:26 +00:00
|
|
|
int ForkedProcess::waitForChild()
|
|
|
|
{
|
2002-02-27 09:59:52 +00:00
|
|
|
// We'll pretend that the child returns 1 on all error conditions.
|
|
|
|
retval_ = 1;
|
|
|
|
int status;
|
|
|
|
bool wait = true;
|
|
|
|
while (wait) {
|
|
|
|
pid_t waitrpid = waitpid(pid_, &status, WUNTRACED);
|
|
|
|
if (waitrpid == -1) {
|
|
|
|
lyxerr << "LyX: Error waiting for child:"
|
|
|
|
<< strerror(errno) << endl;
|
|
|
|
wait = false;
|
|
|
|
} else if (WIFEXITED(status)) {
|
|
|
|
// Child exited normally. Update return value.
|
|
|
|
retval_ = WEXITSTATUS(status);
|
|
|
|
wait = false;
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
|
|
|
lyxerr << "LyX: Child didn't catch signal "
|
2002-03-21 17:09:55 +00:00
|
|
|
<< WTERMSIG(status)
|
2002-02-27 09:59:52 +00:00
|
|
|
<< "and died. Too bad." << endl;
|
|
|
|
wait = false;
|
|
|
|
} else if (WIFSTOPPED(status)) {
|
|
|
|
lyxerr << "LyX: Child (pid: " << pid_
|
|
|
|
<< ") stopped on signal "
|
2002-03-21 17:09:55 +00:00
|
|
|
<< WSTOPSIG(status)
|
2002-02-27 09:59:52 +00:00
|
|
|
<< ". Waiting for child to finish." << endl;
|
|
|
|
} else {
|
|
|
|
lyxerr << "LyX: Something rotten happened while "
|
|
|
|
"waiting for child " << pid_ << endl;
|
|
|
|
wait = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retval_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-31 12:42:26 +00:00
|
|
|
int Forkedcall::startscript(Starttype wait, string const & what)
|
|
|
|
{
|
|
|
|
if (wait != Wait) {
|
|
|
|
retval_ = startscript(what, SignalTypePtr());
|
|
|
|
return retval_;
|
|
|
|
}
|
|
|
|
|
|
|
|
command_ = what;
|
|
|
|
signal_.reset();
|
2004-03-26 23:55:33 +00:00
|
|
|
return run(Wait);
|
2002-10-31 12:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Forkedcall::startscript(string const & what, SignalTypePtr signal)
|
|
|
|
{
|
|
|
|
command_ = what;
|
|
|
|
signal_ = signal;
|
|
|
|
|
2004-03-26 23:55:33 +00:00
|
|
|
return run(DontWait);
|
2002-10-31 12:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
// generate child in background
|
2002-10-31 12:42:26 +00:00
|
|
|
int Forkedcall::generateChild()
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
2004-03-26 23:55:33 +00:00
|
|
|
string line = trim(command_);
|
|
|
|
if (line.empty())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// Split the input command up into an array of words stored
|
|
|
|
// in a contiguous block of memory.
|
|
|
|
char const * const c_str = line.c_str();
|
|
|
|
// Don't forget the terminating `\0' character.
|
|
|
|
vector<char> vec(c_str, c_str + line.size() + 1);
|
|
|
|
// Turn the string into an array of words, each terminated with '\0'.
|
|
|
|
std::replace(vec.begin(), vec.end(), ' ', '\0');
|
|
|
|
|
|
|
|
// Build an array of pointers to each word.
|
|
|
|
vector<char>::iterator vit = vec.begin();
|
|
|
|
vector<char>::iterator vend = vec.end();
|
|
|
|
vector<char *> argv;
|
|
|
|
char prev = '\0';
|
|
|
|
for (; vit != vend; ++vit) {
|
|
|
|
if (*vit != '\0' && prev == '\0')
|
|
|
|
argv.push_back(&*vit);
|
|
|
|
prev = *vit;
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
2004-10-25 14:21:42 +00:00
|
|
|
// Strip quotes. Does so naively, assuming that the word begins
|
|
|
|
// and ends in quotes.
|
|
|
|
vector<char *>::iterator ait = argv.begin();
|
|
|
|
vector<char *>::iterator const aend = argv.end();
|
|
|
|
for (; ait != aend; ++ait) {
|
|
|
|
char * word = *ait;
|
|
|
|
std::size_t const len = strlen(word);
|
|
|
|
if (len >= 2) {
|
|
|
|
char & first = word[0];
|
|
|
|
char & last = word[len-1];
|
|
|
|
|
|
|
|
if (first == last &&
|
|
|
|
(first == '\'' || first == '"')) {
|
|
|
|
first = '\0';
|
|
|
|
last = '\0';
|
|
|
|
*ait += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ait = argv.begin();
|
|
|
|
for (; ait != aend; ++ait)
|
|
|
|
std::cout << *ait << std::endl;
|
2004-03-26 23:55:33 +00:00
|
|
|
argv.push_back(0);
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
#ifndef __EMX__
|
2002-10-31 13:53:26 +00:00
|
|
|
pid_t const cpid = ::fork();
|
2002-10-31 12:42:26 +00:00
|
|
|
if (cpid == 0) {
|
|
|
|
// Child
|
2004-03-26 23:55:33 +00:00
|
|
|
execvp(argv[0], &*argv.begin());
|
2002-10-31 13:53:26 +00:00
|
|
|
|
2002-08-01 01:41:41 +00:00
|
|
|
// If something goes wrong, we end up here
|
2002-10-31 13:53:26 +00:00
|
|
|
lyxerr << "execvp of \"" << command_ << "\" failed: "
|
2002-02-27 09:59:52 +00:00
|
|
|
<< strerror(errno) << endl;
|
2002-10-31 12:42:26 +00:00
|
|
|
_exit(1);
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
#else
|
2002-10-31 13:53:26 +00:00
|
|
|
pid_t const cpid = spawnvp(P_SESSION|P_DEFAULT|P_MINIMIZE|P_BACKGROUND,
|
2004-03-26 23:55:33 +00:00
|
|
|
argv[0], &*argv.begin());
|
2002-02-27 09:59:52 +00:00
|
|
|
#endif
|
|
|
|
|
2002-10-31 12:42:26 +00:00
|
|
|
if (cpid < 0) {
|
|
|
|
// Error.
|
|
|
|
lyxerr << "Could not fork: " << strerror(errno) << endl;
|
|
|
|
}
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
return cpid;
|
|
|
|
}
|
2003-06-30 23:56:22 +00:00
|
|
|
|
|
|
|
} // namespace support
|
|
|
|
} // namespace lyx
|