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 "support/forkedcall.h"
28 #include "support/forkedcontr.h"
29 #include "support/lstrings.h"
30 #include "support/lyxlib.h"
31 #include "support/filetools.h"
32 #include "support/os.h"
36 #include "frontends/Timeout.h"
38 #include <boost/bind.hpp>
43 #include <sys/types.h>
55 #ifndef CXX_GLOBAL_CSTD
65 class Murder : public boost::signals::trackable {
68 static void killItDead(int secs, pid_t pid)
71 new Murder(secs, pid);
72 } else if (pid != 0) {
73 lyx::support::kill(pid, SIGKILL);
81 lyx::support::kill(pid_, SIGKILL);
83 lyxerr << "Killed " << pid_ << std::endl;
89 Murder(int secs, pid_t pid)
90 : timeout_(0), pid_(pid)
92 timeout_ = new Timeout(1000*secs, Timeout::ONETIME);
93 timeout_->timeout.connect(boost::bind(&Murder::kill, this));
111 ForkedProcess::ForkedProcess()
112 : pid_(0), retval_(0)
116 void ForkedProcess::emitSignal()
119 signal_->operator()(pid_, retval_);
124 // Spawn the child process
125 int ForkedProcess::run(Starttype type)
128 pid_ = generateChild();
129 if (pid_ <= 0) { // child or fork failed.
136 retval_ = waitForChild();
139 // Integrate into the Controller
140 ForkedcallsController & contr = ForkedcallsController::get();
141 contr.addCall(*this);
150 bool ForkedProcess::running() const
155 // Un-UNIX like, but we don't have much use for
156 // knowing if a zombie exists, so just reap it first.
158 waitpid(pid(), &waitstatus, WNOHANG);
160 // Racy of course, but it will do.
161 if (lyx::support::kill(pid(), 0) && errno == ESRCH)
167 void ForkedProcess::kill(int tol)
169 lyxerr << "ForkedProcess::kill(" << tol << ')' << endl;
171 lyxerr << "Can't kill non-existent process!" << endl;
175 int const tolerance = std::max(0, tol);
176 if (tolerance == 0) {
178 Murder::killItDead(0, pid());
181 int ret = lyx::support::kill(pid(), SIGHUP);
183 // The process is already dead if wait_for_death is false
184 bool const wait_for_death = (ret == 0 && errno != ESRCH);
186 if (wait_for_death) {
187 Murder::killItDead(tolerance, pid());
193 // Wait for child process to finish. Returns returncode from child.
194 int ForkedProcess::waitForChild()
196 // We'll pretend that the child returns 1 on all error conditions.
201 pid_t waitrpid = waitpid(pid_, &status, WUNTRACED);
202 if (waitrpid == -1) {
203 lyxerr << "LyX: Error waiting for child:"
204 << strerror(errno) << endl;
206 } else if (WIFEXITED(status)) {
207 // Child exited normally. Update return value.
208 retval_ = WEXITSTATUS(status);
210 } else if (WIFSIGNALED(status)) {
211 lyxerr << "LyX: Child didn't catch signal "
213 << "and died. Too bad." << endl;
215 } else if (WIFSTOPPED(status)) {
216 lyxerr << "LyX: Child (pid: " << pid_
217 << ") stopped on signal "
219 << ". Waiting for child to finish." << endl;
221 lyxerr << "LyX: Something rotten happened while "
222 "waiting for child " << pid_ << endl;
230 int Forkedcall::startscript(Starttype wait, string const & what)
233 retval_ = startscript(what, SignalTypePtr());
243 int Forkedcall::startscript(string const & what, SignalTypePtr signal)
248 return run(DontWait);
252 // generate child in background
253 int Forkedcall::generateChild()
255 string line = trim(command_);
259 // Split the input command up into an array of words stored
260 // in a contiguous block of memory. The array contains pointers
262 // Don't forget the terminating `\0' character.
263 char const * const c_str = line.c_str();
264 vector<char> vec(c_str, c_str + line.size() + 1);
266 // Splitting the command up into an array of words means replacing
267 // the whitespace between words with '\0'. Life is complicated
268 // however, because words protected by quotes can contain whitespace.
270 // The strategy we adopt is:
271 // 1. If we're not inside quotes, then replace white space with '\0'.
272 // 2. If we are inside quotes, then don't replace the white space
273 // but do remove the quotes themselves. We do this naively by
274 // replacing the quote with '\0' which is fine if quotes
275 // delimit the entire word.
276 char inside_quote = 0;
277 vector<char>::iterator it = vec.begin();
278 vector<char>::iterator const end = vec.end();
279 for (; it != end; ++it) {
284 else if (c == '\'' || c == '"') {
287 // spawnvp *requires* the quotes or it will
288 // split the arg at the internal whitespace!
289 // Make shure the quote is a DOS-style one.
296 } else if (c == inside_quote) {
306 // Build an array of pointers to each word.
310 for (; it != end; ++it) {
311 if (*it != '\0' && prev == '\0')
312 argv.push_back(&*it);
318 if (lyxerr.debugging(Debug::FILES)) {
319 vector<char *>::iterator ait = argv.begin();
320 vector<char *>::iterator const aend = argv.end();
321 lyxerr << "<command>\n\t" << line
322 << "\n\tInterpretted as:\n\n";
323 for (; ait != aend; ++ait)
325 lyxerr << '\t'<< *ait << '\n';
326 lyxerr << "</command>" << std::endl;
330 pid_t const cpid = ::fork();
333 execvp(argv[0], &*argv.begin());
335 // If something goes wrong, we end up here
336 lyxerr << "execvp of \"" << command_ << "\" failed: "
337 << strerror(errno) << endl;
341 pid_t const cpid = spawnvp(P_SESSION|P_DEFAULT|P_MINIMIZE|P_BACKGROUND,
342 argv[0], &*argv.begin());
347 lyxerr << "Could not fork: " << strerror(errno) << endl;
353 } // namespace support