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>
41 #include <sys/types.h>
51 #ifndef CXX_GLOBAL_CSTD
61 class Murder : public boost::signals::trackable {
64 static void killItDead(int secs, pid_t pid)
67 new Murder(secs, pid);
68 } else if (pid != 0) {
69 lyx::support::kill(pid, SIGKILL);
77 lyx::support::kill(pid_, SIGKILL);
79 lyxerr << "Killed " << pid_ << std::endl;
85 Murder(int secs, pid_t pid)
86 : timeout_(0), pid_(pid)
88 timeout_ = new Timeout(1000*secs, Timeout::ONETIME);
89 timeout_->timeout.connect(boost::bind(&Murder::kill, this));
107 ForkedProcess::ForkedProcess()
108 : pid_(0), retval_(0)
112 void ForkedProcess::emitSignal()
115 signal_->operator()(pid_, retval_);
120 // Spawn the child process
121 int ForkedProcess::run(Starttype type)
124 pid_ = generateChild();
125 if (pid_ <= 0) { // child or fork failed.
132 retval_ = waitForChild();
135 // Integrate into the Controller
136 ForkedcallsController & contr = ForkedcallsController::get();
137 contr.addCall(*this);
146 bool ForkedProcess::running() const
151 // Un-UNIX like, but we don't have much use for
152 // knowing if a zombie exists, so just reap it first.
154 waitpid(pid(), &waitstatus, WNOHANG);
156 // Racy of course, but it will do.
157 if (::kill(pid(), 0) && errno == ESRCH)
163 void ForkedProcess::kill(int tol)
165 lyxerr << "ForkedProcess::kill(" << tol << ')' << endl;
167 lyxerr << "Can't kill non-existent process!" << endl;
171 int const tolerance = std::max(0, tol);
172 if (tolerance == 0) {
174 Murder::killItDead(0, pid());
177 int ret = lyx::support::kill(pid(), SIGHUP);
179 // The process is already dead if wait_for_death is false
180 bool const wait_for_death = (ret == 0 && errno != ESRCH);
182 if (wait_for_death) {
183 Murder::killItDead(tolerance, pid());
189 // Wait for child process to finish. Returns returncode from child.
190 int ForkedProcess::waitForChild()
192 // We'll pretend that the child returns 1 on all error conditions.
197 pid_t waitrpid = waitpid(pid_, &status, WUNTRACED);
198 if (waitrpid == -1) {
199 lyxerr << "LyX: Error waiting for child:"
200 << strerror(errno) << endl;
202 } else if (WIFEXITED(status)) {
203 // Child exited normally. Update return value.
204 retval_ = WEXITSTATUS(status);
206 } else if (WIFSIGNALED(status)) {
207 lyxerr << "LyX: Child didn't catch signal "
209 << "and died. Too bad." << endl;
211 } else if (WIFSTOPPED(status)) {
212 lyxerr << "LyX: Child (pid: " << pid_
213 << ") stopped on signal "
215 << ". Waiting for child to finish." << endl;
217 lyxerr << "LyX: Something rotten happened while "
218 "waiting for child " << pid_ << endl;
226 int Forkedcall::startscript(Starttype wait, string const & what)
229 retval_ = startscript(what, SignalTypePtr());
239 int Forkedcall::startscript(string const & what, SignalTypePtr signal)
244 return run(DontWait);
248 // generate child in background
249 int Forkedcall::generateChild()
251 string line = trim(command_);
255 // Split the input command up into an array of words stored
256 // in a contiguous block of memory.
257 char const * const c_str = line.c_str();
258 // Don't forget the terminating `\0' character.
259 vector<char> vec(c_str, c_str + line.size() + 1);
260 // Turn the string into an array of words, each terminated with '\0'.
261 std::replace(vec.begin(), vec.end(), ' ', '\0');
263 // Build an array of pointers to each word.
264 vector<char>::iterator vit = vec.begin();
265 vector<char>::iterator vend = vec.end();
268 for (; vit != vend; ++vit) {
269 if (*vit != '\0' && prev == '\0')
270 argv.push_back(&*vit);
273 // Strip quotes. Does so naively, assuming that the word begins
274 // and ends in quotes.
275 vector<char *>::iterator ait = argv.begin();
276 vector<char *>::iterator const aend = argv.end();
277 for (; ait != aend; ++ait) {
279 std::size_t const len = strlen(word);
281 char & first = word[0];
282 char & last = word[len-1];
285 (first == '\'' || first == '"')) {
294 for (; ait != aend; ++ait)
295 std::cout << *ait << std::endl;
299 pid_t const cpid = ::fork();
302 execvp(argv[0], &*argv.begin());
304 // If something goes wrong, we end up here
305 lyxerr << "execvp of \"" << command_ << "\" failed: "
306 << strerror(errno) << endl;
310 pid_t const cpid = spawnvp(P_SESSION|P_DEFAULT|P_MINIMIZE|P_BACKGROUND,
311 argv[0], &*argv.begin());
316 lyxerr << "Could not fork: " << strerror(errno) << endl;
322 } // namespace support