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);
146 void ForkedProcess::kill(int tol)
148 lyxerr << "ForkedProcess::kill(" << tol << ')' << endl;
150 lyxerr << "Can't kill non-existent process!" << endl;
154 int const tolerance = std::max(0, tol);
155 if (tolerance == 0) {
157 Murder::killItDead(0, pid());
160 int ret = lyx::kill(pid(), SIGHUP);
162 // The process is already dead if wait_for_death is false
163 bool const wait_for_death = (ret == 0 && errno != ESRCH);
165 if (wait_for_death) {
166 Murder::killItDead(tolerance, pid());
172 // Wait for child process to finish. Returns returncode from child.
173 int ForkedProcess::waitForChild()
175 // We'll pretend that the child returns 1 on all error conditions.
180 pid_t waitrpid = waitpid(pid_, &status, WUNTRACED);
181 if (waitrpid == -1) {
182 lyxerr << "LyX: Error waiting for child:"
183 << strerror(errno) << endl;
185 } else if (WIFEXITED(status)) {
186 // Child exited normally. Update return value.
187 retval_ = WEXITSTATUS(status);
189 } else if (WIFSIGNALED(status)) {
190 lyxerr << "LyX: Child didn't catch signal "
192 << "and died. Too bad." << endl;
194 } else if (WIFSTOPPED(status)) {
195 lyxerr << "LyX: Child (pid: " << pid_
196 << ") stopped on signal "
198 << ". Waiting for child to finish." << endl;
200 lyxerr << "LyX: Something rotten happened while "
201 "waiting for child " << pid_ << endl;
209 int Forkedcall::startscript(Starttype wait, string const & what)
212 retval_ = startscript(what, SignalTypePtr());
218 return runBlocking();
222 int Forkedcall::startscript(string const & what, SignalTypePtr signal)
227 return runNonBlocking();
231 // generate child in background
232 int Forkedcall::generateChild()
234 // Split command_ up into a char * array
235 int const MAX_ARGV = 255;
236 char *argv[MAX_ARGV];
238 string line = command_;
240 for (; index < MAX_ARGV-1; ++index) {
242 line = split(line, word, ' ');
246 char * tmp = new char[word.length() + 1];
247 word.copy(tmp, word.length());
248 tmp[word.length()] = '\0';
255 pid_t const cpid = ::fork();
258 execvp(argv[0], argv);
260 // If something goes wrong, we end up here
261 lyxerr << "execvp of \"" << command_ << "\" failed: "
262 << strerror(errno) << endl;
266 pid_t const cpid = spawnvp(P_SESSION|P_DEFAULT|P_MINIMIZE|P_BACKGROUND,
272 lyxerr << "Could not fork: " << strerror(errno) << endl;
276 for (int i = 0; i < MAX_ARGV; ++i) {