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
28 #pragma implementation
31 #include "forkedcall.h"
32 #include "forkedcontr.h"
35 #include "filetools.h"
38 #include "frontends/Timeout.h"
40 #include <boost/bind.hpp>
43 #include <sys/types.h>
51 #ifndef CXX_GLOBAL_CSTD
56 Forkedcall::Forkedcall()
61 int Forkedcall::startscript(Starttype wait, string const & what)
67 pid_ = generateChild();
68 if (pid_ <= 0) { // child or fork failed.
71 retval_ = waitForChild();
78 retval_ = startscript(what, SignalTypePtr());
83 int Forkedcall::startscript(string const & what, SignalTypePtr signal)
89 pid_ = generateChild();
90 if (pid_ <= 0) { // child or fork failed.
95 // Non-blocking execution.
96 // Integrate into the Controller
97 ForkedcallsController & contr = ForkedcallsController::get();
104 void Forkedcall::emitSignal()
107 signal_->operator()(command_, pid_, retval_);
114 class Murder : public boost::signals::trackable {
117 static void killItDead(int secs, pid_t pid)
120 new Murder(secs, pid);
121 } else if (pid != 0) {
122 lyx::kill(pid, SIGKILL);
130 lyx::kill(pid_, SIGKILL);
132 lyxerr << "Killed " << pid_ << std::endl;
138 Murder(int secs, pid_t pid)
139 : timeout_(0), pid_(pid)
141 timeout_ = new Timeout(1000*secs, Timeout::ONETIME);
142 timeout_->timeout.connect(boost::bind(&Murder::kill, this));
160 void Forkedcall::kill(int tol)
162 lyxerr << "Forkedcall::kill(" << tol << ")" << std::endl;
164 lyxerr << "Can't kill non-existent process!" << endl;
168 int const tolerance = std::max(0, tol);
169 if (tolerance == 0) {
171 Murder::killItDead(0, pid());
174 int ret = lyx::kill(pid(), SIGHUP);
176 // The process is already dead if wait_for_death is false
177 bool const wait_for_death = (ret == 0 && errno != ESRCH);
179 if (wait_for_death) {
180 Murder::killItDead(tolerance, pid());
186 // Wait for child process to finish. Returns returncode from child.
187 int Forkedcall::waitForChild() {
188 // We'll pretend that the child returns 1 on all error conditions.
193 pid_t waitrpid = waitpid(pid_, &status, WUNTRACED);
194 if (waitrpid == -1) {
195 lyxerr << "LyX: Error waiting for child:"
196 << strerror(errno) << endl;
198 } else if (WIFEXITED(status)) {
199 // Child exited normally. Update return value.
200 retval_ = WEXITSTATUS(status);
202 } else if (WIFSIGNALED(status)) {
203 lyxerr << "LyX: Child didn't catch signal "
205 << "and died. Too bad." << endl;
207 } else if (WIFSTOPPED(status)) {
208 lyxerr << "LyX: Child (pid: " << pid_
209 << ") stopped on signal "
211 << ". Waiting for child to finish." << endl;
213 lyxerr << "LyX: Something rotten happened while "
214 "waiting for child " << pid_ << endl;
222 // generate child in background
223 pid_t Forkedcall::generateChild()
225 const int MAX_ARGV = 255;
227 char *argv[MAX_ARGV];
229 string childcommand(command_); // copy
231 string rest = split(command_, childcommand, ' ');
235 childcommand = ltrim(childcommand);
237 syscmd = new char[childcommand.length() + 1];
238 childcommand.copy(syscmd, childcommand.length());
239 syscmd[childcommand.length()] = '\0';
241 if (!childcommand.empty()) {
242 char * tmp = new char[childcommand.length() + 1];
243 childcommand.copy(tmp, childcommand.length());
244 tmp[childcommand.length()] = '\0';
249 more = !rest.empty();
251 rest = split(rest, childcommand, ' ');
256 pid_t cpid = ::fork();
257 if (cpid == 0) { // child
258 execvp(syscmd, argv);
259 // If something goes wrong, we end up here
263 args += string(" ") + argv[i++];
264 lyxerr << "execvp of \"" << syscmd << args << "\" failed: "
265 << strerror(errno) << endl;
268 pid_t cpid = spawnvp(P_SESSION|P_DEFAULT|P_MINIMIZE|P_BACKGROUND,
272 if (cpid < 0) { // error
273 lyxerr << "Could not fork: "
274 << strerror(errno) << endl;