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>
53 #ifndef CXX_GLOBAL_CSTD
63 class Murder : public boost::signals::trackable {
66 static void killItDead(int secs, pid_t pid)
69 new Murder(secs, pid);
70 } else if (pid != 0) {
71 lyx::support::kill(pid, SIGKILL);
79 lyx::support::kill(pid_, SIGKILL);
81 lyxerr << "Killed " << pid_ << std::endl;
87 Murder(int secs, pid_t pid)
88 : timeout_(0), pid_(pid)
90 timeout_ = new Timeout(1000*secs, Timeout::ONETIME);
91 timeout_->timeout.connect(boost::bind(&Murder::kill, this));
109 ForkedProcess::ForkedProcess()
110 : pid_(0), retval_(0)
114 void ForkedProcess::emitSignal()
117 signal_->operator()(pid_, retval_);
122 // Spawn the child process
123 int ForkedProcess::run(Starttype type)
126 pid_ = generateChild();
127 if (pid_ <= 0) { // child or fork failed.
134 retval_ = waitForChild();
137 // Integrate into the Controller
138 ForkedcallsController & contr = ForkedcallsController::get();
139 contr.addCall(*this);
148 bool ForkedProcess::running() const
153 // Un-UNIX like, but we don't have much use for
154 // knowing if a zombie exists, so just reap it first.
156 waitpid(pid(), &waitstatus, WNOHANG);
158 // Racy of course, but it will do.
159 if (::kill(pid(), 0) && errno == ESRCH)
165 void ForkedProcess::kill(int tol)
167 lyxerr << "ForkedProcess::kill(" << tol << ')' << endl;
169 lyxerr << "Can't kill non-existent process!" << endl;
173 int const tolerance = std::max(0, tol);
174 if (tolerance == 0) {
176 Murder::killItDead(0, pid());
179 int ret = lyx::support::kill(pid(), SIGHUP);
181 // The process is already dead if wait_for_death is false
182 bool const wait_for_death = (ret == 0 && errno != ESRCH);
184 if (wait_for_death) {
185 Murder::killItDead(tolerance, pid());
191 // Wait for child process to finish. Returns returncode from child.
192 int ForkedProcess::waitForChild()
194 // We'll pretend that the child returns 1 on all error conditions.
199 pid_t waitrpid = waitpid(pid_, &status, WUNTRACED);
200 if (waitrpid == -1) {
201 lyxerr << "LyX: Error waiting for child:"
202 << strerror(errno) << endl;
204 } else if (WIFEXITED(status)) {
205 // Child exited normally. Update return value.
206 retval_ = WEXITSTATUS(status);
208 } else if (WIFSIGNALED(status)) {
209 lyxerr << "LyX: Child didn't catch signal "
211 << "and died. Too bad." << endl;
213 } else if (WIFSTOPPED(status)) {
214 lyxerr << "LyX: Child (pid: " << pid_
215 << ") stopped on signal "
217 << ". Waiting for child to finish." << endl;
219 lyxerr << "LyX: Something rotten happened while "
220 "waiting for child " << pid_ << endl;
228 int Forkedcall::startscript(Starttype wait, string const & what)
231 retval_ = startscript(what, SignalTypePtr());
241 int Forkedcall::startscript(string const & what, SignalTypePtr signal)
246 return run(DontWait);
250 // generate child in background
251 int Forkedcall::generateChild()
253 string line = trim(command_);
257 // Split the input command up into an array of words stored
258 // in a contiguous block of memory.
259 char const * const c_str = line.c_str();
260 // Don't forget the terminating `\0' character.
261 vector<char> vec(c_str, c_str + line.size() + 1);
262 // Turn the string into an array of words, each terminated with '\0'.
263 std::replace(vec.begin(), vec.end(), ' ', '\0');
265 // Build an array of pointers to each word.
266 vector<char>::iterator vit = vec.begin();
267 vector<char>::iterator vend = vec.end();
270 for (; vit != vend; ++vit) {
271 if (*vit != '\0' && prev == '\0')
272 argv.push_back(&*vit);
275 // Strip quotes. Does so naively, assuming that the word begins
276 // and ends in quotes.
277 vector<char *>::iterator ait = argv.begin();
278 vector<char *>::iterator const aend = argv.end();
279 for (; ait != aend; ++ait) {
281 std::size_t const len = strlen(word);
283 char & first = word[0];
284 char & last = word[len-1];
287 (first == '\'' || first == '"')) {
296 for (; ait != aend; ++ait)
297 std::cout << *ait << std::endl;
301 pid_t const cpid = ::fork();
304 execvp(argv[0], &*argv.begin());
306 // If something goes wrong, we end up here
307 lyxerr << "execvp of \"" << command_ << "\" failed: "
308 << strerror(errno) << endl;
312 pid_t const cpid = spawnvp(P_SESSION|P_DEFAULT|P_MINIMIZE|P_BACKGROUND,
313 argv[0], &*argv.begin());
318 lyxerr << "Could not fork: " << strerror(errno) << endl;
324 } // namespace support