3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Asger Alstrup
8 * Interface cleaned up by
9 * \author Angus Leeming <a.leeming@ic.ac.uk>
11 * An instance of Class Forkedcall represents a single child process.
13 * Class Forkedcall uses fork() and execvp() to lauch the child process.
15 * Once launched, control is returned immediately to the parent process
16 * but a Signal can be emitted upon completion of the child.
18 * The child process is not killed when the Forkedcall instance goes out of
19 * scope, but it can be killed by an explicit invocation of the kill() member
26 #pragma implementation
29 #include "forkedcall.h"
30 #include "forkedcontr.h"
33 #include "filetools.h"
36 #include "frontends/Timeout.h"
38 #include <boost/bind.hpp>
41 #include <sys/types.h>
49 #ifndef CXX_GLOBAL_CSTD
54 Forkedcall::Forkedcall()
59 int Forkedcall::startscript(Starttype wait, string const & what)
65 pid_ = generateChild();
66 if (pid_ <= 0) { // child or fork failed.
69 retval_ = waitForChild();
76 retval_ = startscript(what, SignalTypePtr());
81 int Forkedcall::startscript(string const & what, SignalTypePtr signal)
87 pid_ = generateChild();
88 if (pid_ <= 0) { // child or fork failed.
93 // Non-blocking execution.
94 // Integrate into the Controller
95 ForkedcallsController & contr = ForkedcallsController::get();
102 void Forkedcall::emitSignal()
105 signal_->operator()(command_, pid_, retval_);
112 class Murder : public boost::signals::trackable {
115 static void killItDead(int secs, pid_t pid)
118 new Murder(secs, pid);
119 } else if (pid != 0) {
120 lyx::kill(pid, SIGKILL);
128 lyx::kill(pid_, SIGKILL);
130 lyxerr << "Killed " << pid_ << std::endl;
136 Murder(int secs, pid_t pid)
137 : timeout_(0), pid_(pid)
139 timeout_ = new Timeout(1000*secs, Timeout::ONETIME);
140 timeout_->timeout.connect(boost::bind(&Murder::kill, this));
158 void Forkedcall::kill(int tol)
160 lyxerr << "Forkedcall::kill(" << tol << ")" << std::endl;
162 lyxerr << "Can't kill non-existent process!" << endl;
166 int const tolerance = std::max(0, tol);
167 if (tolerance == 0) {
169 Murder::killItDead(0, pid());
172 int ret = lyx::kill(pid(), SIGHUP);
174 // The process is already dead if wait_for_death is false
175 bool const wait_for_death = (ret == 0 && errno != ESRCH);
177 if (wait_for_death) {
178 Murder::killItDead(tolerance, pid());
184 // Wait for child process to finish. Returns returncode from child.
185 int Forkedcall::waitForChild() {
186 // We'll pretend that the child returns 1 on all error conditions.
191 pid_t waitrpid = waitpid(pid_, &status, WUNTRACED);
192 if (waitrpid == -1) {
193 lyxerr << "LyX: Error waiting for child:"
194 << strerror(errno) << endl;
196 } else if (WIFEXITED(status)) {
197 // Child exited normally. Update return value.
198 retval_ = WEXITSTATUS(status);
200 } else if (WIFSIGNALED(status)) {
201 lyxerr << "LyX: Child didn't catch signal "
203 << "and died. Too bad." << endl;
205 } else if (WIFSTOPPED(status)) {
206 lyxerr << "LyX: Child (pid: " << pid_
207 << ") stopped on signal "
209 << ". Waiting for child to finish." << endl;
211 lyxerr << "LyX: Something rotten happened while "
212 "waiting for child " << pid_ << endl;
220 // generate child in background
221 pid_t Forkedcall::generateChild()
223 const int MAX_ARGV = 255;
225 char *argv[MAX_ARGV];
227 string childcommand(command_); // copy
229 string rest = split(command_, childcommand, ' ');
233 childcommand = frontStrip(childcommand);
235 syscmd = new char[childcommand.length() + 1];
236 childcommand.copy(syscmd, childcommand.length());
237 syscmd[childcommand.length()] = '\0';
239 if (!childcommand.empty()) {
240 char * tmp = new char[childcommand.length() + 1];
241 childcommand.copy(tmp, childcommand.length());
242 tmp[childcommand.length()] = '\0';
247 more = !rest.empty();
249 rest = split(rest, childcommand, ' ');
254 pid_t cpid = ::fork();
255 if (cpid == 0) { // child
256 execvp(syscmd, argv);
257 // If something goes wrong, we end up here:
258 lyxerr << "execvp failed: "
259 << strerror(errno) << endl;
262 pid_t cpid = spawnvp(P_SESSION|P_DEFAULT|P_MINIMIZE|P_BACKGROUND,
266 if (cpid < 0) { // error
267 lyxerr << "Could not fork: "
268 << strerror(errno) << endl;