3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
8 * Interface cleaned up by
9 * \author Angus Leeming
11 * Full author contact details are available in file CREDITS
13 * An instance of Class Forkedcall represents a single child process.
15 * Class Forkedcall uses fork() and execvp() to lauch the child process.
17 * Once launched, control is returned immediately to the parent process
18 * but a Signal can be emitted upon completion of the child.
20 * The child process is not killed when the Forkedcall instance goes out of
21 * scope, but it can be killed by an explicit invocation of the kill() member
27 #include "forkedcall.h"
28 #include "forkedcontr.h"
31 #include "filetools.h"
34 #include "frontends/Timeout.h"
36 #include <boost/bind.hpp>
39 #include <sys/types.h>
47 #ifndef CXX_GLOBAL_CSTD
54 class Murder : public boost::signals::trackable {
57 static void killItDead(int secs, pid_t pid)
60 new Murder(secs, pid);
61 } else if (pid != 0) {
62 lyx::kill(pid, SIGKILL);
70 lyx::kill(pid_, SIGKILL);
72 lyxerr << "Killed " << pid_ << std::endl;
78 Murder(int secs, pid_t pid)
79 : timeout_(0), pid_(pid)
81 timeout_ = new Timeout(1000*secs, Timeout::ONETIME);
82 timeout_->timeout.connect(boost::bind(&Murder::kill, this));
100 ForkedProcess::ForkedProcess()
101 : pid_(0), retval_(0)
105 void ForkedProcess::emitSignal()
108 signal_->operator()(pid_, retval_);
113 // Wait for child process to finish.
114 int ForkedProcess::runBlocking()
117 pid_ = generateChild();
118 if (pid_ <= 0) { // child or fork failed.
123 retval_ = waitForChild();
128 // Do not wait for child process to finish.
129 int ForkedProcess::runNonBlocking()
132 pid_ = generateChild();
133 if (pid_ <= 0) { // child or fork failed.
138 // Non-blocking execution.
139 // Integrate into the Controller
140 ForkedcallsController & contr = ForkedcallsController::get();
141 contr.addCall(*this);
147 bool ForkedProcess::running() const
152 // Un-UNIX like, but we don't have much use for
153 // knowing if a zombie exists, so just reap it first.
155 waitpid(pid(), &waitstatus, WNOHANG);
157 // Racy of course, but it will do.
158 if (::kill(pid(), 0) && errno == ESRCH)
164 void ForkedProcess::kill(int tol)
166 lyxerr << "ForkedProcess::kill(" << tol << ')' << endl;
168 lyxerr << "Can't kill non-existent process!" << endl;
172 int const tolerance = std::max(0, tol);
173 if (tolerance == 0) {
175 Murder::killItDead(0, pid());
178 int ret = lyx::kill(pid(), SIGHUP);
180 // The process is already dead if wait_for_death is false
181 bool const wait_for_death = (ret == 0 && errno != ESRCH);
183 if (wait_for_death) {
184 Murder::killItDead(tolerance, pid());
190 // Wait for child process to finish. Returns returncode from child.
191 int ForkedProcess::waitForChild()
193 // We'll pretend that the child returns 1 on all error conditions.
198 pid_t waitrpid = waitpid(pid_, &status, WUNTRACED);
199 if (waitrpid == -1) {
200 lyxerr << "LyX: Error waiting for child:"
201 << strerror(errno) << endl;
203 } else if (WIFEXITED(status)) {
204 // Child exited normally. Update return value.
205 retval_ = WEXITSTATUS(status);
207 } else if (WIFSIGNALED(status)) {
208 lyxerr << "LyX: Child didn't catch signal "
210 << "and died. Too bad." << endl;
212 } else if (WIFSTOPPED(status)) {
213 lyxerr << "LyX: Child (pid: " << pid_
214 << ") stopped on signal "
216 << ". Waiting for child to finish." << endl;
218 lyxerr << "LyX: Something rotten happened while "
219 "waiting for child " << pid_ << endl;
227 int Forkedcall::startscript(Starttype wait, string const & what)
230 retval_ = startscript(what, SignalTypePtr());
236 return runBlocking();
240 int Forkedcall::startscript(string const & what, SignalTypePtr signal)
245 return runNonBlocking();
249 // generate child in background
250 int Forkedcall::generateChild()
252 // Split command_ up into a char * array
253 int const MAX_ARGV = 255;
254 char *argv[MAX_ARGV];
256 string line = command_;
258 for (; index < MAX_ARGV-1; ++index) {
260 line = split(line, word, ' ');
264 char * tmp = new char[word.length() + 1];
265 word.copy(tmp, word.length());
266 tmp[word.length()] = '\0';
273 pid_t const cpid = ::fork();
276 execvp(argv[0], argv);
278 // If something goes wrong, we end up here
279 lyxerr << "execvp of \"" << command_ << "\" failed: "
280 << strerror(errno) << endl;
284 pid_t const cpid = spawnvp(P_SESSION|P_DEFAULT|P_MINIMIZE|P_BACKGROUND,
290 lyxerr << "Could not fork: " << strerror(errno) << endl;
294 for (int i = 0; i < MAX_ARGV; ++i) {