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>
36 #ifndef CXX_GLOBAL_CSTD
44 // Ensure, that only one controller exists inside process
45 ForkedcallsController & ForkedcallsController::get()
47 static ForkedcallsController singleton;
52 ForkedcallsController::ForkedcallsController()
54 timeout_ = new Timeout(100, Timeout::ONETIME);
57 .connect(boost::bind(&ForkedcallsController::timer, this));
61 // open question: should we stop childs here?
62 // Asger says no: I like to have my xdvi open after closing LyX. Maybe
63 // I want to print or something.
64 ForkedcallsController::~ForkedcallsController()
66 for (ListType::iterator it = forkedCalls.begin();
67 it != forkedCalls.end(); ++it) {
75 void ForkedcallsController::addCall(ForkedProcess const & newcall)
77 if (!timeout_->running())
80 forkedCalls.push_back(newcall.clone());
86 // Check the list and, if there is a stopped child, emit the signal.
87 void ForkedcallsController::timer()
89 ListType::size_type start_size = forkedCalls.size();
91 ListType::iterator it = forkedCalls.begin();
92 ListType::iterator end = forkedCalls.end();
94 ForkedProcess * actCall = *it;
96 pid_t pid = actCall->pid();
98 pid_t const waitrpid = waitpid(pid, &stat_loc, WNOHANG);
99 bool remove_it = false;
101 if (waitrpid == -1) {
102 lyxerr << "LyX: Error waiting for child: "
103 << strerror(errno) << endl;
105 // Child died, so pretend it returned 1
106 actCall->setRetValue(1);
109 } else if (waitrpid == 0) {
110 // Still running. Move on to the next child.
112 } else if (WIFEXITED(stat_loc)) {
113 // Ok, the return value goes into retval.
114 actCall->setRetValue(WEXITSTATUS(stat_loc));
117 } else if (WIFSIGNALED(stat_loc)) {
118 // Child died, so pretend it returned 1
119 actCall->setRetValue(1);
122 } else if (WIFSTOPPED(stat_loc)) {
123 lyxerr << "LyX: Child (pid: " << pid
124 << ") stopped on signal "
125 << WSTOPSIG(stat_loc)
126 << ". Waiting for child to finish." << endl;
129 lyxerr << "LyX: Something rotten happened while "
130 "waiting for child " << pid << endl;
132 // Child died, so pretend it returned 1
133 actCall->setRetValue(1);
138 forkedCalls.erase(it);
140 actCall->emitSignal();
143 /* start all over: emiting the signal can result
144 * in changing the list (Ab)
146 it = forkedCalls.begin();
152 if (!forkedCalls.empty() && !timeout_->running()) {
156 if (start_size != forkedCalls.size())
161 // Return a vector of the pids of all the controlled processes.
162 vector<pid_t> const ForkedcallsController::getPIDs() const
166 if (forkedCalls.empty())
169 pids.resize(forkedCalls.size());
171 vector<pid_t>::iterator vit = pids.begin();
172 for (ListType::const_iterator lit = forkedCalls.begin();
173 lit != forkedCalls.end(); ++lit, ++vit) {
174 *vit = (*lit)->pid();
177 std::sort(pids.begin(), pids.end());
182 // Get the command string of the process.
183 string const ForkedcallsController::getCommand(pid_t pid) const
185 ListType::const_iterator it =
186 find_if(forkedCalls.begin(), forkedCalls.end(),
187 lyx::compare_memfun(&Forkedcall::pid, pid));
189 if (it == forkedCalls.end())
192 return (*it)->command();
196 // Kill the process prematurely and remove it from the list
197 // within tolerance secs
198 void ForkedcallsController::kill(pid_t pid, int tolerance)
200 ListType::iterator it =
201 find_if(forkedCalls.begin(), forkedCalls.end(),
202 lyx::compare_memfun(&Forkedcall::pid, pid));
204 if (it == forkedCalls.end())
207 (*it)->kill(tolerance);
208 forkedCalls.erase(it);
210 if (forkedCalls.empty()) {
215 } // namespace support