3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup Nielsen
7 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS
11 * A class for the control of child processes launched using
12 * fork() and execvp().
17 #include "forkedcontr.h"
18 #include "forkedcall.h"
19 #include "lyxfunctional.h"
22 #include "frontends/Timeout.h"
24 #include <boost/bind.hpp>
35 #ifndef CXX_GLOBAL_CSTD
39 // Ensure, that only one controller exists inside process
40 ForkedcallsController & ForkedcallsController::get()
42 static ForkedcallsController singleton;
47 ForkedcallsController::ForkedcallsController()
49 timeout_ = new Timeout(100, Timeout::ONETIME);
52 .connect(boost::bind(&ForkedcallsController::timer, this));
56 // open question: should we stop childs here?
57 // Asger says no: I like to have my xdvi open after closing LyX. Maybe
58 // I want to print or something.
59 ForkedcallsController::~ForkedcallsController()
61 for (ListType::iterator it = forkedCalls.begin();
62 it != forkedCalls.end(); ++it) {
70 void ForkedcallsController::addCall(ForkedProcess const & newcall)
72 if (!timeout_->running())
75 forkedCalls.push_back(newcall.clone());
81 // Check the list and, if there is a stopped child, emit the signal.
82 void ForkedcallsController::timer()
84 ListType::size_type start_size = forkedCalls.size();
86 ListType::iterator it = forkedCalls.begin();
87 ListType::iterator end = forkedCalls.end();
89 ForkedProcess * actCall = *it;
91 pid_t pid = actCall->pid();
93 pid_t const waitrpid = waitpid(pid, &stat_loc, WNOHANG);
94 bool remove_it = false;
97 lyxerr << "LyX: Error waiting for child: "
98 << strerror(errno) << endl;
100 // Child died, so pretend it returned 1
101 actCall->setRetValue(1);
104 } else if (waitrpid == 0) {
105 // Still running. Move on to the next child.
108 } else if (WIFEXITED(stat_loc)) {
109 // Ok, the return value goes into retval.
110 actCall->setRetValue(WEXITSTATUS(stat_loc));
113 } else if (WIFSIGNALED(stat_loc)) {
114 // Child died, so pretend it returned 1
115 actCall->setRetValue(1);
118 } else if (WIFSTOPPED(stat_loc)) {
119 lyxerr << "LyX: Child (pid: " << pid
120 << ") stopped on signal "
121 << WSTOPSIG(stat_loc)
122 << ". Waiting for child to finish." << endl;
125 lyxerr << "LyX: Something rotten happened while "
126 "waiting for child " << pid << endl;
128 // Child died, so pretend it returned 1
129 actCall->setRetValue(1);
134 forkedCalls.erase(it);
136 actCall->emitSignal();
139 /* start all over: emiting the signal can result
140 * in changing the list (Ab)
142 it = forkedCalls.begin();
148 if (!forkedCalls.empty() && !timeout_->running()) {
152 if (start_size != forkedCalls.size())
157 // Return a vector of the pids of all the controlled processes.
158 vector<pid_t> const ForkedcallsController::getPIDs() const
162 if (forkedCalls.empty())
165 pids.resize(forkedCalls.size());
167 vector<pid_t>::iterator vit = pids.begin();
168 for (ListType::const_iterator lit = forkedCalls.begin();
169 lit != forkedCalls.end(); ++lit, ++vit) {
170 *vit = (*lit)->pid();
173 std::sort(pids.begin(), pids.end());
178 // Get the command string of the process.
179 string const ForkedcallsController::getCommand(pid_t pid) const
181 ListType::const_iterator it =
182 find_if(forkedCalls.begin(), forkedCalls.end(),
183 lyx::compare_memfun(&Forkedcall::pid, pid));
185 if (it == forkedCalls.end())
188 return (*it)->command();
192 // Kill the process prematurely and remove it from the list
193 // within tolerance secs
194 void ForkedcallsController::kill(pid_t pid, int tolerance)
196 ListType::iterator it =
197 find_if(forkedCalls.begin(), forkedCalls.end(),
198 lyx::compare_memfun(&Forkedcall::pid, pid));
200 if (it == forkedCalls.end())
203 (*it)->kill(tolerance);
204 forkedCalls.erase(it);
206 if (forkedCalls.empty()) {