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>
49 #ifndef CXX_GLOBAL_CSTD
59 class Murder : public boost::signals::trackable {
62 static void killItDead(int secs, pid_t pid)
65 new Murder(secs, pid);
66 } else if (pid != 0) {
67 lyx::support::kill(pid, SIGKILL);
75 lyx::support::kill(pid_, SIGKILL);
77 lyxerr << "Killed " << pid_ << std::endl;
83 Murder(int secs, pid_t pid)
84 : timeout_(0), pid_(pid)
86 timeout_ = new Timeout(1000*secs, Timeout::ONETIME);
87 timeout_->timeout.connect(boost::bind(&Murder::kill, this));
105 ForkedProcess::ForkedProcess()
106 : pid_(0), retval_(0)
110 void ForkedProcess::emitSignal()
113 signal_->operator()(pid_, retval_);
118 // Wait for child process to finish.
119 int ForkedProcess::runBlocking()
122 pid_ = generateChild();
123 if (pid_ <= 0) { // child or fork failed.
128 retval_ = waitForChild();
133 // Do not wait for child process to finish.
134 int ForkedProcess::runNonBlocking()
137 pid_ = generateChild();
138 if (pid_ <= 0) { // child or fork failed.
143 // Non-blocking execution.
144 // Integrate into the Controller
145 ForkedcallsController & contr = ForkedcallsController::get();
146 contr.addCall(*this);
152 bool ForkedProcess::running() const
157 // Un-UNIX like, but we don't have much use for
158 // knowing if a zombie exists, so just reap it first.
160 waitpid(pid(), &waitstatus, WNOHANG);
162 // Racy of course, but it will do.
163 if (::kill(pid(), 0) && errno == ESRCH)
169 void ForkedProcess::kill(int tol)
171 lyxerr << "ForkedProcess::kill(" << tol << ')' << endl;
173 lyxerr << "Can't kill non-existent process!" << endl;
177 int const tolerance = std::max(0, tol);
178 if (tolerance == 0) {
180 Murder::killItDead(0, pid());
183 int ret = lyx::support::kill(pid(), SIGHUP);
185 // The process is already dead if wait_for_death is false
186 bool const wait_for_death = (ret == 0 && errno != ESRCH);
188 if (wait_for_death) {
189 Murder::killItDead(tolerance, pid());
195 // Wait for child process to finish. Returns returncode from child.
196 int ForkedProcess::waitForChild()
198 // We'll pretend that the child returns 1 on all error conditions.
203 pid_t waitrpid = waitpid(pid_, &status, WUNTRACED);
204 if (waitrpid == -1) {
205 lyxerr << "LyX: Error waiting for child:"
206 << strerror(errno) << endl;
208 } else if (WIFEXITED(status)) {
209 // Child exited normally. Update return value.
210 retval_ = WEXITSTATUS(status);
212 } else if (WIFSIGNALED(status)) {
213 lyxerr << "LyX: Child didn't catch signal "
215 << "and died. Too bad." << endl;
217 } else if (WIFSTOPPED(status)) {
218 lyxerr << "LyX: Child (pid: " << pid_
219 << ") stopped on signal "
221 << ". Waiting for child to finish." << endl;
223 lyxerr << "LyX: Something rotten happened while "
224 "waiting for child " << pid_ << endl;
232 int Forkedcall::startscript(Starttype wait, string const & what)
235 retval_ = startscript(what, SignalTypePtr());
241 return runBlocking();
245 int Forkedcall::startscript(string const & what, SignalTypePtr signal)
250 return runNonBlocking();
254 // generate child in background
255 int Forkedcall::generateChild()
257 // Split command_ up into a char * array
258 int const MAX_ARGV = 255;
259 char *argv[MAX_ARGV];
261 string line = command_;
263 for (; index < MAX_ARGV-1; ++index) {
265 line = split(line, word, ' ');
269 char * tmp = new char[word.length() + 1];
270 word.copy(tmp, word.length());
271 tmp[word.length()] = '\0';
278 pid_t const cpid = ::fork();
281 execvp(argv[0], argv);
283 // If something goes wrong, we end up here
284 lyxerr << "execvp of \"" << command_ << "\" failed: "
285 << strerror(errno) << endl;
289 pid_t const cpid = spawnvp(P_SESSION|P_DEFAULT|P_MINIMIZE|P_BACKGROUND,
295 lyxerr << "Could not fork: " << strerror(errno) << endl;
299 for (int i = 0; i < MAX_ARGV; ++i) {
308 } // namespace support