2002-02-27 09:59:52 +00:00
|
|
|
/**
|
|
|
|
* \file forkedcontr.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 Nielsen
|
|
|
|
* \author Angus Leeming
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-09-25 10:03:41 +00:00
|
|
|
*
|
2002-03-21 17:09:55 +00:00
|
|
|
* A class for the control of child processes launched using
|
2002-02-27 09:59:52 +00:00
|
|
|
* fork() and execvp().
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "forkedcontr.h"
|
|
|
|
#include "forkedcall.h"
|
|
|
|
#include "lyxfunctional.h"
|
|
|
|
#include "debug.h"
|
|
|
|
|
2002-05-29 16:21:03 +00:00
|
|
|
#include "frontends/Timeout.h"
|
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
2004-03-23 14:39:41 +00:00
|
|
|
#include <boost/iterator/indirect_iterator.hpp>
|
2002-05-29 16:21:03 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
|
2004-01-31 15:30:24 +00:00
|
|
|
using boost::bind;
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
using std::endl;
|
|
|
|
using std::find_if;
|
2003-10-06 15:43:21 +00:00
|
|
|
using std::string;
|
2003-09-08 00:33:41 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
#ifndef CXX_GLOBAL_CSTD
|
|
|
|
using std::strerror;
|
|
|
|
#endif
|
|
|
|
|
2003-09-08 00:33:41 +00:00
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace support {
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
// Ensure, that only one controller exists inside process
|
|
|
|
ForkedcallsController & ForkedcallsController::get()
|
|
|
|
{
|
|
|
|
static ForkedcallsController singleton;
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ForkedcallsController::ForkedcallsController()
|
|
|
|
{
|
2002-03-14 18:01:51 +00:00
|
|
|
timeout_ = new Timeout(100, Timeout::ONETIME);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
timeout_->timeout
|
2004-01-31 15:30:24 +00:00
|
|
|
.connect(bind(&ForkedcallsController::timer, this));
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// open question: should we stop childs here?
|
|
|
|
// Asger says no: I like to have my xdvi open after closing LyX. Maybe
|
|
|
|
// I want to print or something.
|
|
|
|
ForkedcallsController::~ForkedcallsController()
|
|
|
|
{
|
|
|
|
delete timeout_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-31 12:42:26 +00:00
|
|
|
void ForkedcallsController::addCall(ForkedProcess const & newcall)
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
|
|
|
if (!timeout_->running())
|
|
|
|
timeout_->start();
|
|
|
|
|
2004-03-23 14:39:41 +00:00
|
|
|
forkedCalls.push_back(newcall.clone());
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Timer-call
|
|
|
|
// Check the list and, if there is a stopped child, emit the signal.
|
|
|
|
void ForkedcallsController::timer()
|
|
|
|
{
|
2003-02-25 12:42:28 +00:00
|
|
|
ListType::iterator it = forkedCalls.begin();
|
|
|
|
ListType::iterator end = forkedCalls.end();
|
|
|
|
while (it != end) {
|
2004-03-23 14:39:41 +00:00
|
|
|
ForkedProcess * actCall = it->get();
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
pid_t pid = actCall->pid();
|
|
|
|
int stat_loc;
|
|
|
|
pid_t const waitrpid = waitpid(pid, &stat_loc, WNOHANG);
|
|
|
|
bool remove_it = false;
|
|
|
|
|
|
|
|
if (waitrpid == -1) {
|
2002-03-21 17:09:55 +00:00
|
|
|
lyxerr << "LyX: Error waiting for child: "
|
2002-02-27 09:59:52 +00:00
|
|
|
<< 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)) {
|
2002-03-21 17:09:55 +00:00
|
|
|
lyxerr << "LyX: Child (pid: " << pid
|
2002-02-27 09:59:52 +00:00
|
|
|
<< ") stopped on signal "
|
2002-03-21 17:09:55 +00:00
|
|
|
<< WSTOPSIG(stat_loc)
|
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;
|
|
|
|
|
|
|
|
// Child died, so pretend it returned 1
|
|
|
|
actCall->setRetValue(1);
|
|
|
|
remove_it = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remove_it) {
|
|
|
|
actCall->emitSignal();
|
2004-03-23 14:39:41 +00:00
|
|
|
forkedCalls.erase(it);
|
2003-02-25 15:17:53 +00:00
|
|
|
|
|
|
|
/* start all over: emiting the signal can result
|
|
|
|
* in changing the list (Ab)
|
|
|
|
*/
|
|
|
|
it = forkedCalls.begin();
|
2003-02-25 12:42:28 +00:00
|
|
|
} else {
|
|
|
|
++it;
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-25 11:33:00 +00:00
|
|
|
if (!forkedCalls.empty() && !timeout_->running()) {
|
2002-03-14 18:01:51 +00:00
|
|
|
timeout_->start();
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Kill the process prematurely and remove it from the list
|
|
|
|
// within tolerance secs
|
|
|
|
void ForkedcallsController::kill(pid_t pid, int tolerance)
|
|
|
|
{
|
2004-03-23 14:39:41 +00:00
|
|
|
typedef boost::indirect_iterator<ListType::iterator> iterator;
|
|
|
|
|
|
|
|
iterator begin = boost::make_indirect_iterator(forkedCalls.begin());
|
|
|
|
iterator end = boost::make_indirect_iterator(forkedCalls.end());
|
|
|
|
iterator it = find_if(begin, end,
|
|
|
|
lyx::compare_memfun(&Forkedcall::pid, pid));
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2004-03-23 14:39:41 +00:00
|
|
|
if (it == end)
|
2002-02-27 09:59:52 +00:00
|
|
|
return;
|
|
|
|
|
2004-03-23 14:39:41 +00:00
|
|
|
it->kill(tolerance);
|
|
|
|
forkedCalls.erase(it.base());
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2004-03-23 14:39:41 +00:00
|
|
|
if (forkedCalls.empty())
|
2002-02-27 09:59:52 +00:00
|
|
|
timeout_->stop();
|
|
|
|
}
|
2003-06-30 23:56:22 +00:00
|
|
|
|
|
|
|
} // namespace support
|
|
|
|
} // namespace lyx
|