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"
39 #include <sys/types.h>
47 #ifndef CXX_GLOBAL_CSTD
52 Forkedcall::Forkedcall()
57 int Forkedcall::startscript(Starttype wait, string const & what)
63 pid_ = generateChild();
64 if (pid_ <= 0) { // child or fork failed.
67 retval_ = waitForChild();
74 retval_ = startscript(what, SignalTypePtr());
79 int Forkedcall::startscript(string const & what, SignalTypePtr signal)
85 pid_ = generateChild();
86 if (pid_ <= 0) { // child or fork failed.
91 // Non-blocking execution.
92 // Integrate into the Controller
93 ForkedcallsController & contr = ForkedcallsController::get();
100 void Forkedcall::emitSignal()
103 signal_->emit(command_, pid_, retval_);
110 class Murder : public SigC::Object {
113 static void killItDead(int secs, pid_t pid)
116 new Murder(secs, pid);
117 } else if (pid != 0) {
118 lyx::kill(pid, SIGKILL);
126 lyx::kill(pid_, SIGKILL);
128 lyxerr << "Killed " << pid_ << std::endl;
134 Murder(int secs, pid_t pid)
135 : timeout_(0), pid_(pid)
137 timeout_ = new Timeout(1000*secs, Timeout::ONETIME);
138 timeout_->timeout.connect(SigC::slot(this, &Murder::kill));
156 void Forkedcall::kill(int tol)
158 lyxerr << "Forkedcall::kill(" << tol << ")" << std::endl;
160 lyxerr << "Can't kill non-existent process!" << endl;
164 int const tolerance = std::max(0, tol);
165 if (tolerance == 0) {
167 Murder::killItDead(0, pid());
170 int ret = lyx::kill(pid(), SIGHUP);
172 // The process is already dead if wait_for_death is false
173 bool const wait_for_death = (ret == 0 && errno != ESRCH);
175 if (wait_for_death) {
176 Murder::killItDead(tolerance, pid());
182 // Wait for child process to finish. Returns returncode from child.
183 int Forkedcall::waitForChild() {
184 // We'll pretend that the child returns 1 on all error conditions.
189 pid_t waitrpid = waitpid(pid_, &status, WUNTRACED);
190 if (waitrpid == -1) {
191 lyxerr << "LyX: Error waiting for child:"
192 << strerror(errno) << endl;
194 } else if (WIFEXITED(status)) {
195 // Child exited normally. Update return value.
196 retval_ = WEXITSTATUS(status);
198 } else if (WIFSIGNALED(status)) {
199 lyxerr << "LyX: Child didn't catch signal "
201 << "and died. Too bad." << endl;
203 } else if (WIFSTOPPED(status)) {
204 lyxerr << "LyX: Child (pid: " << pid_
205 << ") stopped on signal "
207 << ". Waiting for child to finish." << endl;
209 lyxerr << "LyX: Something rotten happened while "
210 "waiting for child " << pid_ << endl;
218 // generate child in background
219 pid_t Forkedcall::generateChild()
221 const int MAX_ARGV = 255;
223 char *argv[MAX_ARGV];
225 string childcommand(command_); // copy
227 string rest = split(command_, childcommand, ' ');
231 childcommand = frontStrip(childcommand);
233 syscmd = new char[childcommand.length() + 1];
234 childcommand.copy(syscmd, childcommand.length());
235 syscmd[childcommand.length()] = '\0';
237 if (!childcommand.empty()) {
238 char * tmp = new char[childcommand.length() + 1];
239 childcommand.copy(tmp, childcommand.length());
240 tmp[childcommand.length()] = '\0';
245 more = !rest.empty();
247 rest = split(rest, childcommand, ' ');
252 pid_t cpid = ::fork();
253 if (cpid == 0) { // child
254 execvp(syscmd, argv);
255 // If something goes wrong, we end up here:
256 lyxerr << "execvp failed: "
257 << strerror(errno) << endl;
260 pid_t cpid = spawnvp(P_SESSION|P_DEFAULT|P_MINIMIZE|P_BACKGROUND,
264 if (cpid < 0) { // error
265 lyxerr << "Could not fork: "
266 << strerror(errno) << endl;