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
|
|
|
|
*
|
2002-09-25 10:03:41 +00:00
|
|
|
* Full author contact details are available in file CREDITS
|
|
|
|
*
|
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>
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
using std::endl;
|
|
|
|
using std::find_if;
|
|
|
|
|
|
|
|
#ifndef CXX_GLOBAL_CSTD
|
|
|
|
using std::strerror;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// 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
|
2002-05-29 16:21:03 +00:00
|
|
|
.connect(boost::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()
|
|
|
|
{
|
|
|
|
for (ListType::iterator it = forkedCalls.begin();
|
|
|
|
it != forkedCalls.end(); ++it) {
|
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2002-10-31 12:42:26 +00:00
|
|
|
forkedCalls.push_back(newcall.clone());
|
2002-05-29 16:21:03 +00:00
|
|
|
childrenChanged();
|
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()
|
|
|
|
{
|
|
|
|
ListType::size_type start_size = forkedCalls.size();
|
|
|
|
|
|
|
|
for (ListType::iterator it = forkedCalls.begin();
|
|
|
|
it != forkedCalls.end(); ++it) {
|
2002-10-31 12:42:26 +00:00
|
|
|
ForkedProcess * actCall = *it;
|
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.
|
|
|
|
continue;
|
|
|
|
|
|
|
|
} 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) {
|
|
|
|
// Emit signal and remove the item from the list
|
|
|
|
actCall->emitSignal();
|
|
|
|
delete actCall;
|
|
|
|
// erase returns the next iterator, so decrement it
|
|
|
|
// to continue the loop.
|
|
|
|
ListType::iterator prev = it;
|
|
|
|
--prev;
|
|
|
|
forkedCalls.erase(it);
|
|
|
|
it = prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (start_size != forkedCalls.size())
|
2002-05-29 16:21:03 +00:00
|
|
|
childrenChanged();
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Return a vector of the pids of all the controlled processes.
|
|
|
|
vector<pid_t> const ForkedcallsController::getPIDs() const
|
|
|
|
{
|
|
|
|
vector<pid_t> pids;
|
|
|
|
|
|
|
|
if (forkedCalls.empty())
|
|
|
|
return pids;
|
|
|
|
|
|
|
|
pids.resize(forkedCalls.size());
|
|
|
|
|
|
|
|
vector<pid_t>::iterator vit = pids.begin();
|
|
|
|
for (ListType::const_iterator lit = forkedCalls.begin();
|
|
|
|
lit != forkedCalls.end(); ++lit, ++vit) {
|
|
|
|
*vit = (*lit)->pid();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(pids.begin(), pids.end());
|
|
|
|
return pids;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get the command string of the process.
|
|
|
|
string const ForkedcallsController::getCommand(pid_t pid) const
|
|
|
|
{
|
|
|
|
ListType::const_iterator it =
|
|
|
|
find_if(forkedCalls.begin(), forkedCalls.end(),
|
|
|
|
lyx::compare_memfun(&Forkedcall::pid, pid));
|
|
|
|
|
|
|
|
if (it == forkedCalls.end())
|
|
|
|
return string();
|
|
|
|
|
|
|
|
return (*it)->command();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Kill the process prematurely and remove it from the list
|
|
|
|
// within tolerance secs
|
|
|
|
void ForkedcallsController::kill(pid_t pid, int tolerance)
|
|
|
|
{
|
|
|
|
ListType::iterator it =
|
|
|
|
find_if(forkedCalls.begin(), forkedCalls.end(),
|
|
|
|
lyx::compare_memfun(&Forkedcall::pid, pid));
|
|
|
|
|
|
|
|
if (it == forkedCalls.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
(*it)->kill(tolerance);
|
|
|
|
forkedCalls.erase(it);
|
|
|
|
|
|
|
|
if (forkedCalls.empty()) {
|
|
|
|
timeout_->stop();
|
|
|
|
}
|
|
|
|
}
|