2002-02-27 09:59:52 +00:00
|
|
|
|
/**
|
2007-11-29 19:19:39 +00:00
|
|
|
|
* \file ForkedCalls.cpp
|
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
|
2002-09-25 10:03:41 +00:00
|
|
|
|
* \author Angus Leeming
|
2007-11-29 19:19:39 +00:00
|
|
|
|
* \author Alfredo Braunstein
|
2002-09-25 10:03:41 +00:00
|
|
|
|
*
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
2007-11-29 19:19:39 +00:00
|
|
|
|
#include "support/ForkedCalls.h"
|
2007-11-29 07:04:28 +00:00
|
|
|
|
|
|
|
|
|
#include "support/debug.h"
|
|
|
|
|
#include "support/filetools.h"
|
2004-11-07 13:22:51 +00:00
|
|
|
|
#include "support/lstrings.h"
|
|
|
|
|
#include "support/lyxlib.h"
|
|
|
|
|
#include "support/os.h"
|
2007-08-11 22:37:09 +00:00
|
|
|
|
#include "support/Timeout.h"
|
2004-11-07 13:22:51 +00:00
|
|
|
|
|
2002-05-29 16:21:03 +00:00
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
|
2006-05-29 14:56:08 +00:00
|
|
|
|
#include <cerrno>
|
2007-11-29 19:56:25 +00:00
|
|
|
|
#include <queue>
|
2007-11-29 19:19:39 +00:00
|
|
|
|
#include <sstream>
|
2007-11-29 19:56:25 +00:00
|
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
2005-04-20 17:35:47 +00:00
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
# define SIGHUP 1
|
|
|
|
|
# define SIGKILL 9
|
|
|
|
|
# include <windows.h>
|
2007-11-29 19:19:39 +00:00
|
|
|
|
# include <process.h>
|
2007-11-29 20:02:50 +00:00
|
|
|
|
# undef max
|
2005-04-20 17:35:47 +00:00
|
|
|
|
#else
|
|
|
|
|
# include <csignal>
|
|
|
|
|
# include <cstdlib>
|
2005-04-26 10:30:24 +00:00
|
|
|
|
# ifdef HAVE_UNISTD_H
|
|
|
|
|
# include <unistd.h>
|
|
|
|
|
# endif
|
2005-04-20 17:35:47 +00:00
|
|
|
|
# include <sys/wait.h>
|
2005-01-20 15:38:14 +00:00
|
|
|
|
#endif
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
|
using namespace std;
|
|
|
|
|
|
2007-11-29 19:19:39 +00:00
|
|
|
|
using boost::bind;
|
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
|
namespace lyx {
|
|
|
|
|
namespace support {
|
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
|
namespace {
|
|
|
|
|
|
2007-11-29 19:19:39 +00:00
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// Murder
|
|
|
|
|
//
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2007-11-29 20:02:50 +00:00
|
|
|
|
if (secs > 0)
|
2002-02-27 09:59:52 +00:00
|
|
|
|
new Murder(secs, pid);
|
2007-11-29 20:02:50 +00:00
|
|
|
|
else if (pid != 0)
|
2007-11-10 13:44:50 +00:00
|
|
|
|
support::kill(pid, SIGKILL);
|
2002-02-27 09:59:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
void kill()
|
|
|
|
|
{
|
2007-11-29 19:19:39 +00:00
|
|
|
|
if (pid_ != 0)
|
2007-11-10 13:44:50 +00:00
|
|
|
|
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)
|
2007-11-29 20:02:50 +00:00
|
|
|
|
: timeout_(1000*secs, Timeout::ONETIME), pid_(pid)
|
2002-02-27 09:59:52 +00:00
|
|
|
|
{
|
2007-11-29 20:02:50 +00:00
|
|
|
|
timeout_.timeout.connect(boost::bind(&Murder::kill, this));
|
|
|
|
|
timeout_.start();
|
2002-02-27 09:59:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2007-11-29 20:02:50 +00:00
|
|
|
|
Timeout timeout_;
|
2002-02-27 09:59:52 +00:00
|
|
|
|
//
|
|
|
|
|
pid_t pid_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
|
|
2007-11-29 19:19:39 +00:00
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// ForkedProcess
|
|
|
|
|
//
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
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
|
|
|
|
{
|
2007-11-29 20:02:50 +00:00
|
|
|
|
retval_ = 0;
|
2002-10-31 12:42:26 +00:00
|
|
|
|
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
|
2007-11-29 19:56:25 +00:00
|
|
|
|
ForkedCallsController::addCall(*this);
|
2004-03-26 23:55:33 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2005-04-20 17:35:47 +00:00
|
|
|
|
#if !defined (_WIN32)
|
2003-02-17 18:40:04 +00:00
|
|
|
|
// 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);
|
2005-04-20 17:35:47 +00:00
|
|
|
|
#endif
|
2003-02-17 18:40:04 +00:00
|
|
|
|
|
|
|
|
|
// Racy of course, but it will do.
|
2007-11-10 13:44:50 +00:00
|
|
|
|
if (support::kill(pid(), 0) && errno == ESRCH)
|
2003-02-17 18:40:04 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-29 20:02:50 +00:00
|
|
|
|
int const tolerance = std::max(0, tol);
|
2002-02-27 09:59:52 +00:00
|
|
|
|
if (tolerance == 0) {
|
|
|
|
|
// Kill it dead NOW!
|
|
|
|
|
Murder::killItDead(0, pid());
|
|
|
|
|
} else {
|
2007-11-10 13:44:50 +00:00
|
|
|
|
int ret = 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);
|
|
|
|
|
|
2007-11-29 20:02:50 +00:00
|
|
|
|
if (wait_for_death)
|
2002-02-27 09:59:52 +00:00
|
|
|
|
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;
|
2005-04-20 17:35:47 +00:00
|
|
|
|
|
|
|
|
|
#if defined (_WIN32)
|
|
|
|
|
HANDLE const hProcess = HANDLE(pid_);
|
|
|
|
|
|
|
|
|
|
DWORD const wait_status = ::WaitForSingleObject(hProcess, INFINITE);
|
|
|
|
|
|
|
|
|
|
switch (wait_status) {
|
|
|
|
|
case WAIT_OBJECT_0: {
|
|
|
|
|
DWORD exit_code = 0;
|
|
|
|
|
if (!GetExitCodeProcess(hProcess, &exit_code)) {
|
|
|
|
|
lyxerr << "GetExitCodeProcess failed waiting for child\n"
|
|
|
|
|
<< getChildErrorMessage() << std::endl;
|
|
|
|
|
} else
|
|
|
|
|
retval_ = exit_code;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case WAIT_FAILED:
|
|
|
|
|
lyxerr << "WaitForSingleObject failed waiting for child\n"
|
|
|
|
|
<< getChildErrorMessage() << std::endl;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
2002-02-27 09:59:52 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-04-20 17:35:47 +00:00
|
|
|
|
#endif
|
2002-02-27 09:59:52 +00:00
|
|
|
|
return retval_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-11-29 19:19:39 +00:00
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// ForkedCall
|
|
|
|
|
//
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int ForkedCall::startScript(Starttype wait, string const & what)
|
2002-10-31 12:42:26 +00:00
|
|
|
|
{
|
|
|
|
|
if (wait != Wait) {
|
2007-11-29 19:19:39 +00:00
|
|
|
|
retval_ = startScript(what, SignalTypePtr());
|
2002-10-31 12:42:26 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-11-29 19:19:39 +00:00
|
|
|
|
int ForkedCall::startScript(string const & what, SignalTypePtr signal)
|
2002-10-31 12:42:26 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
2007-11-29 19:19:39 +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
|
2005-02-02 12:57:20 +00:00
|
|
|
|
// in a contiguous block of memory. The array contains pointers
|
|
|
|
|
// to each word.
|
2004-03-26 23:55:33 +00:00
|
|
|
|
// Don't forget the terminating `\0' character.
|
2005-02-02 12:57:20 +00:00
|
|
|
|
char const * const c_str = line.c_str();
|
2004-03-26 23:55:33 +00:00
|
|
|
|
vector<char> vec(c_str, c_str + line.size() + 1);
|
2005-02-02 12:57:20 +00:00
|
|
|
|
|
|
|
|
|
// Splitting the command up into an array of words means replacing
|
|
|
|
|
// the whitespace between words with '\0'. Life is complicated
|
|
|
|
|
// however, because words protected by quotes can contain whitespace.
|
|
|
|
|
//
|
|
|
|
|
// The strategy we adopt is:
|
|
|
|
|
// 1. If we're not inside quotes, then replace white space with '\0'.
|
|
|
|
|
// 2. If we are inside quotes, then don't replace the white space
|
|
|
|
|
// but do remove the quotes themselves. We do this naively by
|
|
|
|
|
// replacing the quote with '\0' which is fine if quotes
|
|
|
|
|
// delimit the entire word.
|
|
|
|
|
char inside_quote = 0;
|
|
|
|
|
vector<char>::iterator it = vec.begin();
|
|
|
|
|
vector<char>::iterator const end = vec.end();
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
|
char const c = *it;
|
|
|
|
|
if (!inside_quote) {
|
|
|
|
|
if (c == ' ')
|
|
|
|
|
*it = '\0';
|
|
|
|
|
else if (c == '\'' || c == '"') {
|
2005-04-17 18:41:14 +00:00
|
|
|
|
#if defined (_WIN32)
|
|
|
|
|
// How perverse!
|
|
|
|
|
// spawnvp *requires* the quotes or it will
|
|
|
|
|
// split the arg at the internal whitespace!
|
|
|
|
|
// Make shure the quote is a DOS-style one.
|
|
|
|
|
*it = '"';
|
|
|
|
|
#else
|
2005-02-02 12:57:20 +00:00
|
|
|
|
*it = '\0';
|
2005-04-17 18:41:14 +00:00
|
|
|
|
#endif
|
2005-02-02 12:57:20 +00:00
|
|
|
|
inside_quote = c;
|
|
|
|
|
}
|
|
|
|
|
} else if (c == inside_quote) {
|
2005-04-17 18:41:14 +00:00
|
|
|
|
#if defined (_WIN32)
|
|
|
|
|
*it = '"';
|
|
|
|
|
#else
|
2005-02-02 12:57:20 +00:00
|
|
|
|
*it = '\0';
|
2005-04-17 18:41:14 +00:00
|
|
|
|
#endif
|
2005-02-02 12:57:20 +00:00
|
|
|
|
inside_quote = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-03-26 23:55:33 +00:00
|
|
|
|
|
|
|
|
|
// Build an array of pointers to each word.
|
2005-02-02 12:57:20 +00:00
|
|
|
|
it = vec.begin();
|
2004-03-26 23:55:33 +00:00
|
|
|
|
vector<char *> argv;
|
|
|
|
|
char prev = '\0';
|
2005-02-02 12:57:20 +00:00
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
|
if (*it != '\0' && prev == '\0')
|
|
|
|
|
argv.push_back(&*it);
|
|
|
|
|
prev = *it;
|
2002-02-27 09:59:52 +00:00
|
|
|
|
}
|
2005-02-02 12:57:20 +00:00
|
|
|
|
argv.push_back(0);
|
|
|
|
|
|
|
|
|
|
// Debug output.
|
2005-04-17 18:41:14 +00:00
|
|
|
|
if (lyxerr.debugging(Debug::FILES)) {
|
|
|
|
|
vector<char *>::iterator ait = argv.begin();
|
|
|
|
|
vector<char *>::iterator const aend = argv.end();
|
|
|
|
|
lyxerr << "<command>\n\t" << line
|
|
|
|
|
<< "\n\tInterpretted as:\n\n";
|
|
|
|
|
for (; ait != aend; ++ait)
|
|
|
|
|
if (*ait)
|
|
|
|
|
lyxerr << '\t'<< *ait << '\n';
|
|
|
|
|
lyxerr << "</command>" << std::endl;
|
|
|
|
|
}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
2006-05-14 17:00:53 +00:00
|
|
|
|
#ifdef _WIN32
|
2006-04-05 23:39:11 +00:00
|
|
|
|
pid_t const cpid = spawnvp(_P_NOWAIT, argv[0], &*argv.begin());
|
2005-04-20 17:35:47 +00:00
|
|
|
|
#else // POSIX
|
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
|
|
|
|
}
|
|
|
|
|
#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
|
|
|
|
|
2007-11-29 19:19:39 +00:00
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// ForkedCallQueue
|
|
|
|
|
//
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2007-11-29 19:56:25 +00:00
|
|
|
|
namespace ForkedCallQueue {
|
2007-11-29 19:19:39 +00:00
|
|
|
|
|
2007-11-29 19:56:25 +00:00
|
|
|
|
/// A process in the queue
|
|
|
|
|
typedef std::pair<std::string, ForkedCall::SignalTypePtr> Process;
|
|
|
|
|
/** Add a process to the queue. Processes are forked sequentially
|
|
|
|
|
* only one is running at a time.
|
|
|
|
|
* Connect to the returned signal and you'll be informed when
|
|
|
|
|
* the process has ended.
|
|
|
|
|
*/
|
|
|
|
|
ForkedCall::SignalTypePtr add(std::string const & process);
|
|
|
|
|
|
|
|
|
|
/// in-progress queue
|
|
|
|
|
static std::queue<Process> callQueue_;
|
2007-11-29 19:19:39 +00:00
|
|
|
|
|
2007-11-29 19:56:25 +00:00
|
|
|
|
/// flag whether queue is running
|
|
|
|
|
static bool running_ = 0;
|
2007-11-29 19:19:39 +00:00
|
|
|
|
|
2007-11-29 19:56:25 +00:00
|
|
|
|
///
|
|
|
|
|
void startCaller();
|
|
|
|
|
///
|
|
|
|
|
void stopCaller();
|
|
|
|
|
///
|
|
|
|
|
void callback(pid_t, int);
|
|
|
|
|
|
|
|
|
|
ForkedCall::SignalTypePtr add(string const & process)
|
2007-11-29 19:19:39 +00:00
|
|
|
|
{
|
|
|
|
|
ForkedCall::SignalTypePtr ptr;
|
|
|
|
|
ptr.reset(new ForkedCall::SignalType);
|
|
|
|
|
callQueue_.push(Process(process, ptr));
|
|
|
|
|
if (!running_)
|
|
|
|
|
startCaller();
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-11-29 19:56:25 +00:00
|
|
|
|
void callNext()
|
2007-11-29 19:19:39 +00:00
|
|
|
|
{
|
|
|
|
|
if (callQueue_.empty())
|
|
|
|
|
return;
|
|
|
|
|
Process pro = callQueue_.front();
|
|
|
|
|
callQueue_.pop();
|
|
|
|
|
// Bind our chain caller
|
2007-11-29 19:56:25 +00:00
|
|
|
|
pro.second->connect(boost::bind(&ForkedCallQueue::callback, _1, _2));
|
2007-11-29 19:19:39 +00:00
|
|
|
|
ForkedCall call;
|
|
|
|
|
//<2F>If<49>we<77>fail<69>to<74>fork<72>the<68>process,<2C>then<65>emit<69>the<68>signal
|
|
|
|
|
//<2F>to<74>tell<6C>the<68>outside<64>world<6C>that<61>it<69>failed.
|
|
|
|
|
if (call.startScript(pro.first, pro.second) > 0)
|
|
|
|
|
pro.second->operator()(0,1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-11-29 19:56:25 +00:00
|
|
|
|
void callback(pid_t, int)
|
2007-11-29 19:19:39 +00:00
|
|
|
|
{
|
|
|
|
|
if (callQueue_.empty())
|
|
|
|
|
stopCaller();
|
|
|
|
|
else
|
|
|
|
|
callNext();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-11-29 19:56:25 +00:00
|
|
|
|
void startCaller()
|
2007-11-29 19:19:39 +00:00
|
|
|
|
{
|
|
|
|
|
LYXERR(Debug::GRAPHICS, "ForkedCallQueue: waking up");
|
|
|
|
|
running_ = true ;
|
|
|
|
|
callNext();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-11-29 19:56:25 +00:00
|
|
|
|
void stopCaller()
|
2007-11-29 19:19:39 +00:00
|
|
|
|
{
|
|
|
|
|
running_ = false ;
|
|
|
|
|
LYXERR(Debug::GRAPHICS, "ForkedCallQueue: I'm going to sleep");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-11-29 19:56:25 +00:00
|
|
|
|
bool running()
|
2007-11-29 19:19:39 +00:00
|
|
|
|
{
|
2007-11-29 19:56:25 +00:00
|
|
|
|
return running_;
|
2007-11-29 19:19:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-11-29 19:56:25 +00:00
|
|
|
|
} // namespace ForkedCallsQueue
|
|
|
|
|
|
|
|
|
|
|
2007-11-29 19:19:39 +00:00
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// ForkedCallsController
|
|
|
|
|
//
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
string const getChildErrorMessage()
|
|
|
|
|
{
|
|
|
|
|
DWORD const error_code = ::GetLastError();
|
|
|
|
|
|
|
|
|
|
HLOCAL t_message = 0;
|
|
|
|
|
bool const ok = ::FormatMessage(
|
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM,
|
|
|
|
|
0, error_code,
|
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
|
(LPTSTR) &t_message, 0, 0
|
|
|
|
|
) != 0;
|
|
|
|
|
|
|
|
|
|
std::ostringstream ss;
|
|
|
|
|
ss << "LyX: Error waiting for child: " << error_code;
|
|
|
|
|
|
|
|
|
|
if (ok) {
|
|
|
|
|
ss << ": " << (LPTSTR)t_message;
|
|
|
|
|
::LocalFree(t_message);
|
|
|
|
|
} else
|
|
|
|
|
ss << ": Error unknown.";
|
|
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2007-11-29 19:56:25 +00:00
|
|
|
|
namespace ForkedCallsController {
|
2007-11-29 19:19:39 +00:00
|
|
|
|
|
2007-11-29 19:56:25 +00:00
|
|
|
|
typedef boost::shared_ptr<ForkedProcess> ForkedProcessPtr;
|
|
|
|
|
typedef std::list<ForkedProcessPtr> ListType;
|
|
|
|
|
typedef ListType::iterator iterator;
|
2007-11-29 19:19:39 +00:00
|
|
|
|
|
|
|
|
|
|
2007-11-29 19:56:25 +00:00
|
|
|
|
/// The child processes
|
|
|
|
|
static ListType forkedCalls;
|
2007-11-29 19:19:39 +00:00
|
|
|
|
|
2007-11-29 19:56:25 +00:00
|
|
|
|
iterator find_pid(pid_t pid)
|
|
|
|
|
{
|
|
|
|
|
return find_if(forkedCalls.begin(), forkedCalls.end(),
|
|
|
|
|
bind(equal_to<pid_t>(),
|
|
|
|
|
bind(&ForkedCall::pid, _1),
|
|
|
|
|
pid));
|
|
|
|
|
}
|
2007-11-29 19:19:39 +00:00
|
|
|
|
|
|
|
|
|
|
2007-11-29 19:56:25 +00:00
|
|
|
|
void addCall(ForkedProcess const & newcall)
|
2007-11-29 19:19:39 +00:00
|
|
|
|
{
|
|
|
|
|
forkedCalls.push_back(newcall.clone());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check the list of dead children and emit any associated signals.
|
2007-11-29 19:56:25 +00:00
|
|
|
|
void handleCompletedProcesses()
|
2007-11-29 19:19:39 +00:00
|
|
|
|
{
|
|
|
|
|
ListType::iterator it = forkedCalls.begin();
|
|
|
|
|
ListType::iterator end = forkedCalls.end();
|
|
|
|
|
while (it != end) {
|
|
|
|
|
ForkedProcessPtr actCall = *it;
|
|
|
|
|
bool remove_it = false;
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
HANDLE const hProcess = HANDLE(actCall->pid());
|
|
|
|
|
|
|
|
|
|
DWORD const wait_status = ::WaitForSingleObject(hProcess, 0);
|
|
|
|
|
|
|
|
|
|
switch (wait_status) {
|
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
|
// Still running
|
|
|
|
|
break;
|
|
|
|
|
case WAIT_OBJECT_0: {
|
|
|
|
|
DWORD exit_code = 0;
|
|
|
|
|
if (!GetExitCodeProcess(hProcess, &exit_code)) {
|
|
|
|
|
lyxerr << "GetExitCodeProcess failed waiting for child\n"
|
|
|
|
|
<< getChildErrorMessage() << std::endl;
|
|
|
|
|
// Child died, so pretend it returned 1
|
|
|
|
|
actCall->setRetValue(1);
|
|
|
|
|
} else {
|
|
|
|
|
actCall->setRetValue(exit_code);
|
|
|
|
|
}
|
|
|
|
|
remove_it = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case WAIT_FAILED:
|
|
|
|
|
lyxerr << "WaitForSingleObject failed waiting for child\n"
|
|
|
|
|
<< getChildErrorMessage() << std::endl;
|
|
|
|
|
actCall->setRetValue(1);
|
|
|
|
|
remove_it = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
pid_t pid = actCall->pid();
|
|
|
|
|
int stat_loc;
|
|
|
|
|
pid_t const waitrpid = waitpid(pid, &stat_loc, WNOHANG);
|
|
|
|
|
|
|
|
|
|
if (waitrpid == -1) {
|
|
|
|
|
lyxerr << "LyX: Error waiting for child: "
|
|
|
|
|
<< strerror(errno) << endl;
|
|
|
|
|
|
|
|
|
|
// Child died, so pretend it returned 1
|
|
|
|
|
actCall->setRetValue(1);
|
|
|
|
|
remove_it = true;
|
|
|
|
|
|
|
|
|
|
} else if (waitrpid == 0) {
|
|
|
|
|
// Still running. Move on to the next child.
|
|
|
|
|
|
|
|
|
|
} else if (WIFEXITED(stat_loc)) {
|
|
|
|
|
// Ok, the return value goes into retval.
|
|
|
|
|
actCall->setRetValue(WEXITSTATUS(stat_loc));
|
|
|
|
|
remove_it = true;
|
|
|
|
|
|
|
|
|
|
} else if (WIFSIGNALED(stat_loc)) {
|
|
|
|
|
// Child died, so pretend it returned 1
|
|
|
|
|
actCall->setRetValue(1);
|
|
|
|
|
remove_it = true;
|
|
|
|
|
|
|
|
|
|
} else if (WIFSTOPPED(stat_loc)) {
|
|
|
|
|
lyxerr << "LyX: Child (pid: " << pid
|
|
|
|
|
<< ") stopped on signal "
|
|
|
|
|
<< WSTOPSIG(stat_loc)
|
|
|
|
|
<< ". Waiting for child to finish." << endl;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
lyxerr << "LyX: Something rotten happened while "
|
|
|
|
|
"waiting for child " << pid << endl;
|
|
|
|
|
|
|
|
|
|
// Child died, so pretend it returned 1
|
|
|
|
|
actCall->setRetValue(1);
|
|
|
|
|
remove_it = true;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (remove_it) {
|
|
|
|
|
forkedCalls.erase(it);
|
|
|
|
|
actCall->emitSignal();
|
|
|
|
|
|
|
|
|
|
/* start all over: emiting the signal can result
|
|
|
|
|
* in changing the list (Ab)
|
|
|
|
|
*/
|
|
|
|
|
it = forkedCalls.begin();
|
|
|
|
|
} else {
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Kill the process prematurely and remove it from the list
|
|
|
|
|
// within tolerance secs
|
2007-11-29 19:56:25 +00:00
|
|
|
|
void kill(pid_t pid, int tolerance)
|
2007-11-29 19:19:39 +00:00
|
|
|
|
{
|
|
|
|
|
ListType::iterator it = find_pid(pid);
|
|
|
|
|
if (it == forkedCalls.end())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
(*it)->kill(tolerance);
|
|
|
|
|
forkedCalls.erase(it);
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-29 19:56:25 +00:00
|
|
|
|
} // namespace ForkedCallsController
|
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
|
} // namespace support
|
|
|
|
|
} // namespace lyx
|