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>
37 #ifndef CXX_GLOBAL_CSTD
45 // Ensure, that only one controller exists inside process
46 ForkedcallsController & ForkedcallsController::get()
48 static ForkedcallsController singleton;
53 ForkedcallsController::ForkedcallsController()
55 timeout_ = new Timeout(100, Timeout::ONETIME);
58 .connect(boost::bind(&ForkedcallsController::timer, this));
62 // open question: should we stop childs here?
63 // Asger says no: I like to have my xdvi open after closing LyX. Maybe
64 // I want to print or something.
65 ForkedcallsController::~ForkedcallsController()
67 for (ListType::iterator it = forkedCalls.begin();
68 it != forkedCalls.end(); ++it) {
76 void ForkedcallsController::addCall(ForkedProcess const & newcall)
78 if (!timeout_->running())
81 forkedCalls.push_back(newcall.clone());
87 // Check the list and, if there is a stopped child, emit the signal.
88 void ForkedcallsController::timer()
90 ListType::size_type start_size = forkedCalls.size();
92 ListType::iterator it = forkedCalls.begin();
93 ListType::iterator end = forkedCalls.end();
95 ForkedProcess * actCall = *it;
97 pid_t pid = actCall->pid();
99 pid_t const waitrpid = waitpid(pid, &stat_loc, WNOHANG);
100 bool remove_it = false;
102 if (waitrpid == -1) {
103 lyxerr << "LyX: Error waiting for child: "
104 << strerror(errno) << endl;
106 // Child died, so pretend it returned 1
107 actCall->setRetValue(1);
110 } else if (waitrpid == 0) {
111 // Still running. Move on to the next child.
113 } else if (WIFEXITED(stat_loc)) {
114 // Ok, the return value goes into retval.
115 actCall->setRetValue(WEXITSTATUS(stat_loc));
118 } else if (WIFSIGNALED(stat_loc)) {
119 // Child died, so pretend it returned 1
120 actCall->setRetValue(1);
123 } else if (WIFSTOPPED(stat_loc)) {
124 lyxerr << "LyX: Child (pid: " << pid
125 << ") stopped on signal "
126 << WSTOPSIG(stat_loc)
127 << ". Waiting for child to finish." << endl;
130 lyxerr << "LyX: Something rotten happened while "
131 "waiting for child " << pid << endl;
133 // Child died, so pretend it returned 1
134 actCall->setRetValue(1);
139 forkedCalls.erase(it);
141 actCall->emitSignal();
144 /* start all over: emiting the signal can result
145 * in changing the list (Ab)
147 it = forkedCalls.begin();
153 if (!forkedCalls.empty() && !timeout_->running()) {
157 if (start_size != forkedCalls.size())
162 // Return a vector of the pids of all the controlled processes.
163 vector<pid_t> const ForkedcallsController::getPIDs() const
167 if (forkedCalls.empty())
170 pids.resize(forkedCalls.size());
172 vector<pid_t>::iterator vit = pids.begin();
173 for (ListType::const_iterator lit = forkedCalls.begin();
174 lit != forkedCalls.end(); ++lit, ++vit) {
175 *vit = (*lit)->pid();
178 std::sort(pids.begin(), pids.end());
183 // Get the command string of the process.
184 string const ForkedcallsController::getCommand(pid_t pid) const
186 ListType::const_iterator it =
187 find_if(forkedCalls.begin(), forkedCalls.end(),
188 lyx::compare_memfun(&Forkedcall::pid, pid));
190 if (it == forkedCalls.end())
193 return (*it)->command();
197 // Kill the process prematurely and remove it from the list
198 // within tolerance secs
199 void ForkedcallsController::kill(pid_t pid, int tolerance)
201 ListType::iterator it =
202 find_if(forkedCalls.begin(), forkedCalls.end(),
203 lyx::compare_memfun(&Forkedcall::pid, pid));
205 if (it == forkedCalls.end())
208 (*it)->kill(tolerance);
209 forkedCalls.erase(it);
211 if (forkedCalls.empty()) {
216 } // namespace support