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
42 // Ensure, that only one controller exists inside process
43 ForkedcallsController & ForkedcallsController::get()
45 static ForkedcallsController singleton;
50 ForkedcallsController::ForkedcallsController()
52 timeout_ = new Timeout(100, Timeout::ONETIME);
55 .connect(boost::bind(&ForkedcallsController::timer, this));
59 // open question: should we stop childs here?
60 // Asger says no: I like to have my xdvi open after closing LyX. Maybe
61 // I want to print or something.
62 ForkedcallsController::~ForkedcallsController()
64 for (ListType::iterator it = forkedCalls.begin();
65 it != forkedCalls.end(); ++it) {
73 void ForkedcallsController::addCall(ForkedProcess const & newcall)
75 if (!timeout_->running())
78 forkedCalls.push_back(newcall.clone());
84 // Check the list and, if there is a stopped child, emit the signal.
85 void ForkedcallsController::timer()
87 ListType::size_type start_size = forkedCalls.size();
89 ListType::iterator it = forkedCalls.begin();
90 ListType::iterator end = forkedCalls.end();
92 ForkedProcess * actCall = *it;
94 pid_t pid = actCall->pid();
96 pid_t const waitrpid = waitpid(pid, &stat_loc, WNOHANG);
97 bool remove_it = false;
100 lyxerr << "LyX: Error waiting for child: "
101 << strerror(errno) << endl;
103 // Child died, so pretend it returned 1
104 actCall->setRetValue(1);
107 } else if (waitrpid == 0) {
108 // Still running. Move on to the next child.
110 } else if (WIFEXITED(stat_loc)) {
111 // Ok, the return value goes into retval.
112 actCall->setRetValue(WEXITSTATUS(stat_loc));
115 } else if (WIFSIGNALED(stat_loc)) {
116 // Child died, so pretend it returned 1
117 actCall->setRetValue(1);
120 } else if (WIFSTOPPED(stat_loc)) {
121 lyxerr << "LyX: Child (pid: " << pid
122 << ") stopped on signal "
123 << WSTOPSIG(stat_loc)
124 << ". Waiting for child to finish." << endl;
127 lyxerr << "LyX: Something rotten happened while "
128 "waiting for child " << pid << endl;
130 // Child died, so pretend it returned 1
131 actCall->setRetValue(1);
136 forkedCalls.erase(it);
138 actCall->emitSignal();
141 /* start all over: emiting the signal can result
142 * in changing the list (Ab)
144 it = forkedCalls.begin();
150 if (!forkedCalls.empty() && !timeout_->running()) {
154 if (start_size != forkedCalls.size())
159 // Return a vector of the pids of all the controlled processes.
160 vector<pid_t> const ForkedcallsController::getPIDs() const
164 if (forkedCalls.empty())
167 pids.resize(forkedCalls.size());
169 vector<pid_t>::iterator vit = pids.begin();
170 for (ListType::const_iterator lit = forkedCalls.begin();
171 lit != forkedCalls.end(); ++lit, ++vit) {
172 *vit = (*lit)->pid();
175 std::sort(pids.begin(), pids.end());
180 // Get the command string of the process.
181 string const ForkedcallsController::getCommand(pid_t pid) const
183 ListType::const_iterator it =
184 find_if(forkedCalls.begin(), forkedCalls.end(),
185 lyx::compare_memfun(&Forkedcall::pid, pid));
187 if (it == forkedCalls.end())
190 return (*it)->command();
194 // Kill the process prematurely and remove it from the list
195 // within tolerance secs
196 void ForkedcallsController::kill(pid_t pid, int tolerance)
198 ListType::iterator it =
199 find_if(forkedCalls.begin(), forkedCalls.end(),
200 lyx::compare_memfun(&Forkedcall::pid, pid));
202 if (it == forkedCalls.end())
205 (*it)->kill(tolerance);
206 forkedCalls.erase(it);
208 if (forkedCalls.empty()) {
213 } // namespace support