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
57 class Murder : public boost::signals::trackable {
60 static void killItDead(int secs, pid_t pid)
63 new Murder(secs, pid);
64 } else if (pid != 0) {
65 lyx::support::kill(pid, SIGKILL);
73 lyx::support::kill(pid_, SIGKILL);
75 lyxerr << "Killed " << pid_ << std::endl;
81 Murder(int secs, pid_t pid)
82 : timeout_(0), pid_(pid)
84 timeout_ = new Timeout(1000*secs, Timeout::ONETIME);
85 timeout_->timeout.connect(boost::bind(&Murder::kill, this));
103 ForkedProcess::ForkedProcess()
104 : pid_(0), retval_(0)
108 void ForkedProcess::emitSignal()
111 signal_->operator()(pid_, retval_);
116 // Wait for child process to finish.
117 int ForkedProcess::runBlocking()
120 pid_ = generateChild();
121 if (pid_ <= 0) { // child or fork failed.
126 retval_ = waitForChild();
131 // Do not wait for child process to finish.
132 int ForkedProcess::runNonBlocking()
135 pid_ = generateChild();
136 if (pid_ <= 0) { // child or fork failed.
141 // Non-blocking execution.
142 // Integrate into the Controller
143 ForkedcallsController & contr = ForkedcallsController::get();
144 contr.addCall(*this);
150 bool ForkedProcess::running() const
155 // Un-UNIX like, but we don't have much use for
156 // knowing if a zombie exists, so just reap it first.
158 waitpid(pid(), &waitstatus, WNOHANG);
160 // Racy of course, but it will do.
161 if (::kill(pid(), 0) && errno == ESRCH)
167 void ForkedProcess::kill(int tol)
169 lyxerr << "ForkedProcess::kill(" << tol << ')' << endl;
171 lyxerr << "Can't kill non-existent process!" << endl;
175 int const tolerance = std::max(0, tol);
176 if (tolerance == 0) {
178 Murder::killItDead(0, pid());
181 int ret = lyx::support::kill(pid(), SIGHUP);
183 // The process is already dead if wait_for_death is false
184 bool const wait_for_death = (ret == 0 && errno != ESRCH);
186 if (wait_for_death) {
187 Murder::killItDead(tolerance, pid());
193 // Wait for child process to finish. Returns returncode from child.
194 int ForkedProcess::waitForChild()
196 // We'll pretend that the child returns 1 on all error conditions.
201 pid_t waitrpid = waitpid(pid_, &status, WUNTRACED);
202 if (waitrpid == -1) {
203 lyxerr << "LyX: Error waiting for child:"
204 << strerror(errno) << endl;
206 } else if (WIFEXITED(status)) {
207 // Child exited normally. Update return value.
208 retval_ = WEXITSTATUS(status);
210 } else if (WIFSIGNALED(status)) {
211 lyxerr << "LyX: Child didn't catch signal "
213 << "and died. Too bad." << endl;
215 } else if (WIFSTOPPED(status)) {
216 lyxerr << "LyX: Child (pid: " << pid_
217 << ") stopped on signal "
219 << ". Waiting for child to finish." << endl;
221 lyxerr << "LyX: Something rotten happened while "
222 "waiting for child " << pid_ << endl;
230 int Forkedcall::startscript(Starttype wait, string const & what)
233 retval_ = startscript(what, SignalTypePtr());
239 return runBlocking();
243 int Forkedcall::startscript(string const & what, SignalTypePtr signal)
248 return runNonBlocking();
252 // generate child in background
253 int Forkedcall::generateChild()
255 // Split command_ up into a char * array
256 int const MAX_ARGV = 255;
257 char *argv[MAX_ARGV];
259 string line = command_;
261 for (; index < MAX_ARGV-1; ++index) {
263 line = split(line, word, ' ');
267 char * tmp = new char[word.length() + 1];
268 word.copy(tmp, word.length());
269 tmp[word.length()] = '\0';
276 pid_t const cpid = ::fork();
279 execvp(argv[0], argv);
281 // If something goes wrong, we end up here
282 lyxerr << "execvp of \"" << command_ << "\" failed: "
283 << strerror(errno) << endl;
287 pid_t const cpid = spawnvp(P_SESSION|P_DEFAULT|P_MINIMIZE|P_BACKGROUND,
293 lyxerr << "Could not fork: " << strerror(errno) << endl;
297 for (int i = 0; i < MAX_ARGV; ++i) {
306 } // namespace support