]> git.lyx.org Git - lyx.git/blob - src/support/ForkedCalls.cpp
cosmetics
[lyx.git] / src / support / ForkedCalls.cpp
1 /**
2  * \file ForkedCalls.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Angus Leeming
8  * \author Alfredo Braunstein
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "support/ForkedCalls.h"
16
17 #include "support/debug.h"
18 #include "support/filetools.h"
19 #include "support/lstrings.h"
20 #include "support/lyxlib.h"
21 #include "support/os.h"
22 #include "support/Timeout.h"
23
24 #include <boost/bind.hpp>
25
26 #include <cerrno>
27 #include <queue>
28 #include <sstream>
29 #include <utility>
30 #include <vector>
31
32 #ifdef _WIN32
33 # define SIGHUP 1
34 # define SIGKILL 9
35 # include <windows.h>
36 # include <process.h>
37 # undef max
38 #else
39 # include <csignal>
40 # include <cstdlib>
41 # ifdef HAVE_UNISTD_H
42 #  include <unistd.h>
43 # endif
44 # include <sys/wait.h>
45 #endif
46
47 using namespace std;
48
49 using boost::bind;
50
51 namespace lyx {
52 namespace support {
53
54 namespace {
55
56 /////////////////////////////////////////////////////////////////////
57 //
58 // Murder
59 //
60 /////////////////////////////////////////////////////////////////////
61
62 class Murder : public boost::signals::trackable {
63 public:
64         //
65         static void killItDead(int secs, pid_t pid)
66         {
67                 if (secs > 0)
68                         new Murder(secs, pid);
69                 else if (pid != 0)
70                         support::kill(pid, SIGKILL);
71         }
72
73         //
74         void kill()
75         {
76                 if (pid_ != 0)
77                         support::kill(pid_, SIGKILL);
78                 lyxerr << "Killed " << pid_ << endl;
79                 delete this;
80         }
81
82 private:
83         //
84         Murder(int secs, pid_t pid)
85                 : timeout_(1000*secs, Timeout::ONETIME), pid_(pid)
86         {
87                 timeout_.timeout.connect(boost::bind(&Murder::kill, this));
88                 timeout_.start();
89         }
90
91         //
92         Timeout timeout_;
93         //
94         pid_t pid_;
95 };
96
97 } // namespace anon
98
99
100 /////////////////////////////////////////////////////////////////////
101 //
102 // ForkedProcess
103 //
104 /////////////////////////////////////////////////////////////////////
105
106 ForkedProcess::ForkedProcess()
107         : pid_(0), retval_(0)
108 {}
109
110
111 void ForkedProcess::emitSignal()
112 {
113         if (signal_.get()) {
114                 signal_->operator()(pid_, retval_);
115         }
116 }
117
118
119 // Spawn the child process
120 int ForkedProcess::run(Starttype type)
121 {
122         retval_ = 0;
123         pid_ = generateChild();
124         if (pid_ <= 0) { // child or fork failed.
125                 retval_ = 1;
126                 return retval_;
127         }
128
129         switch (type) {
130         case Wait:
131                 retval_ = waitForChild();
132                 break;
133         case DontWait: {
134                 // Integrate into the Controller
135                 ForkedCallsController::addCall(*this);
136                 break;
137         }
138         }
139
140         return retval_;
141 }
142
143
144 bool ForkedProcess::running() const
145 {
146         if (!pid())
147                 return false;
148
149 #if !defined (_WIN32)
150         // Un-UNIX like, but we don't have much use for
151         // knowing if a zombie exists, so just reap it first.
152         int waitstatus;
153         waitpid(pid(), &waitstatus, WNOHANG);
154 #endif
155
156         // Racy of course, but it will do.
157         if (support::kill(pid(), 0) && errno == ESRCH)
158                 return false;
159         return true;
160 }
161
162
163 void ForkedProcess::kill(int tol)
164 {
165         lyxerr << "ForkedProcess::kill(" << tol << ')' << endl;
166         if (pid() == 0) {
167                 lyxerr << "Can't kill non-existent process!" << endl;
168                 return;
169         }
170
171         int const tolerance = max(0, tol);
172         if (tolerance == 0) {
173                 // Kill it dead NOW!
174                 Murder::killItDead(0, pid());
175         } else {
176                 int ret = support::kill(pid(), SIGHUP);
177
178                 // The process is already dead if wait_for_death is false
179                 bool const wait_for_death = (ret == 0 && errno != ESRCH);
180
181                 if (wait_for_death)
182                         Murder::killItDead(tolerance, pid());
183         }
184 }
185
186
187 // Wait for child process to finish. Returns returncode from child.
188 int ForkedProcess::waitForChild()
189 {
190         // We'll pretend that the child returns 1 on all error conditions.
191         retval_ = 1;
192
193 #if defined (_WIN32)
194         HANDLE const hProcess = HANDLE(pid_);
195
196         DWORD const wait_status = ::WaitForSingleObject(hProcess, INFINITE);
197
198         switch (wait_status) {
199         case WAIT_OBJECT_0: {
200                 DWORD exit_code = 0;
201                 if (!GetExitCodeProcess(hProcess, &exit_code)) {
202                         lyxerr << "GetExitCodeProcess failed waiting for child\n"
203                                << getChildErrorMessage() << endl;
204                 } else
205                         retval_ = exit_code;
206                 break;
207         }
208         case WAIT_FAILED:
209                 lyxerr << "WaitForSingleObject failed waiting for child\n"
210                        << getChildErrorMessage() << endl;
211                 break;
212         }
213
214 #else
215         int status;
216         bool wait = true;
217         while (wait) {
218                 pid_t waitrpid = waitpid(pid_, &status, WUNTRACED);
219                 if (waitrpid == -1) {
220                         lyxerr << "LyX: Error waiting for child:"
221                                << strerror(errno) << endl;
222                         wait = false;
223                 } else if (WIFEXITED(status)) {
224                         // Child exited normally. Update return value.
225                         retval_ = WEXITSTATUS(status);
226                         wait = false;
227                 } else if (WIFSIGNALED(status)) {
228                         lyxerr << "LyX: Child didn't catch signal "
229                                << WTERMSIG(status)
230                                << "and died. Too bad." << endl;
231                         wait = false;
232                 } else if (WIFSTOPPED(status)) {
233                         lyxerr << "LyX: Child (pid: " << pid_
234                                << ") stopped on signal "
235                                << WSTOPSIG(status)
236                                << ". Waiting for child to finish." << endl;
237                 } else {
238                         lyxerr << "LyX: Something rotten happened while "
239                                 "waiting for child " << pid_ << endl;
240                         wait = false;
241                 }
242         }
243 #endif
244         return retval_;
245 }
246
247
248 /////////////////////////////////////////////////////////////////////
249 //
250 // ForkedCall
251 //
252 /////////////////////////////////////////////////////////////////////
253
254
255 int ForkedCall::startScript(Starttype wait, string const & what)
256 {
257         if (wait != Wait) {
258                 retval_ = startScript(what, SignalTypePtr());
259                 return retval_;
260         }
261
262         command_ = what;
263         signal_.reset();
264         return run(Wait);
265 }
266
267
268 int ForkedCall::startScript(string const & what, SignalTypePtr signal)
269 {
270         command_ = what;
271         signal_  = signal;
272
273         return run(DontWait);
274 }
275
276
277 // generate child in background
278 int ForkedCall::generateChild()
279 {
280         string line = trim(command_);
281         if (line.empty())
282                 return 1;
283
284         // Split the input command up into an array of words stored
285         // in a contiguous block of memory. The array contains pointers
286         // to each word.
287         // Don't forget the terminating `\0' character.
288         char const * const c_str = line.c_str();
289         vector<char> vec(c_str, c_str + line.size() + 1);
290
291         // Splitting the command up into an array of words means replacing
292         // the whitespace between words with '\0'. Life is complicated
293         // however, because words protected by quotes can contain whitespace.
294         //
295         // The strategy we adopt is:
296         // 1. If we're not inside quotes, then replace white space with '\0'.
297         // 2. If we are inside quotes, then don't replace the white space
298         //    but do remove the quotes themselves. We do this naively by
299         //    replacing the quote with '\0' which is fine if quotes
300         //    delimit the entire word.
301         char inside_quote = 0;
302         vector<char>::iterator it = vec.begin();
303         vector<char>::iterator const end = vec.end();
304         for (; it != end; ++it) {
305                 char const c = *it;
306                 if (!inside_quote) {
307                         if (c == ' ')
308                                 *it = '\0';
309                         else if (c == '\'' || c == '"') {
310 #if defined (_WIN32)
311                                 // How perverse!
312                                 // spawnvp *requires* the quotes or it will
313                                 // split the arg at the internal whitespace!
314                                 // Make shure the quote is a DOS-style one.
315                                 *it = '"';
316 #else
317                                 *it = '\0';
318 #endif
319                                 inside_quote = c;
320                         }
321                 } else if (c == inside_quote) {
322 #if defined (_WIN32)
323                         *it = '"';
324 #else
325                         *it = '\0';
326 #endif
327                         inside_quote = 0;
328                 }
329         }
330
331         // Build an array of pointers to each word.
332         it = vec.begin();
333         vector<char *> argv;
334         char prev = '\0';
335         for (; it != end; ++it) {
336                 if (*it != '\0' && prev == '\0')
337                         argv.push_back(&*it);
338                 prev = *it;
339         }
340         argv.push_back(0);
341
342         // Debug output.
343         if (lyxerr.debugging(Debug::FILES)) {
344                 vector<char *>::iterator ait = argv.begin();
345                 vector<char *>::iterator const aend = argv.end();
346                 lyxerr << "<command>\n\t" << line
347                        << "\n\tInterpretted as:\n\n";
348                 for (; ait != aend; ++ait)
349                         if (*ait)
350                                 lyxerr << '\t'<< *ait << '\n';
351                 lyxerr << "</command>" << endl;
352         }
353
354 #ifdef _WIN32
355         pid_t const cpid = spawnvp(_P_NOWAIT, argv[0], &*argv.begin());
356 #else // POSIX
357         pid_t const cpid = ::fork();
358         if (cpid == 0) {
359                 // Child
360                 execvp(argv[0], &*argv.begin());
361
362                 // If something goes wrong, we end up here
363                 lyxerr << "execvp of \"" << command_ << "\" failed: "
364                        << strerror(errno) << endl;
365                 _exit(1);
366         }
367 #endif
368
369         if (cpid < 0) {
370                 // Error.
371                 lyxerr << "Could not fork: " << strerror(errno) << endl;
372         }
373
374         return cpid;
375 }
376
377
378 /////////////////////////////////////////////////////////////////////
379 //
380 // ForkedCallQueue
381 //
382 /////////////////////////////////////////////////////////////////////
383
384 namespace ForkedCallQueue {
385
386 /// A process in the queue
387 typedef pair<string, ForkedCall::SignalTypePtr> Process;
388 /** Add a process to the queue. Processes are forked sequentially
389  *  only one is running at a time.
390  *  Connect to the returned signal and you'll be informed when
391  *  the process has ended.
392  */
393 ForkedCall::SignalTypePtr add(string const & process);
394
395 /// in-progress queue
396 static queue<Process> callQueue_;
397
398 /// flag whether queue is running
399 static bool running_ = 0;
400
401 ///
402 void startCaller();
403 ///
404 void stopCaller();
405 ///
406 void callback(pid_t, int);
407
408 ForkedCall::SignalTypePtr add(string const & process)
409 {
410         ForkedCall::SignalTypePtr ptr;
411         ptr.reset(new ForkedCall::SignalType);
412         callQueue_.push(Process(process, ptr));
413         if (!running_)
414                 startCaller();
415         return ptr;
416 }
417
418
419 void callNext()
420 {
421         if (callQueue_.empty())
422                 return;
423         Process pro = callQueue_.front();
424         callQueue_.pop();
425         // Bind our chain caller
426         pro.second->connect(boost::bind(&ForkedCallQueue::callback, _1, _2));
427         ForkedCall call;
428         // If we fail to fork the process, then emit the signal
429         // to tell the outside world that it failed.
430         if (call.startScript(pro.first, pro.second) > 0)
431                 pro.second->operator()(0,1);
432 }
433
434
435 void callback(pid_t, int)
436 {
437         if (callQueue_.empty())
438                 stopCaller();
439         else
440                 callNext();
441 }
442
443
444 void startCaller()
445 {
446         LYXERR(Debug::GRAPHICS, "ForkedCallQueue: waking up");
447         running_ = true ;
448         callNext();
449 }
450
451
452 void stopCaller()
453 {
454         running_ = false ;
455         LYXERR(Debug::GRAPHICS, "ForkedCallQueue: I'm going to sleep");
456 }
457
458
459 bool running()
460 {
461         return running_;
462 }
463
464 } // namespace ForkedCallsQueue
465
466
467
468 /////////////////////////////////////////////////////////////////////
469 //
470 // ForkedCallsController
471 //
472 /////////////////////////////////////////////////////////////////////
473
474 #if defined(_WIN32)
475 string const getChildErrorMessage()
476 {
477         DWORD const error_code = ::GetLastError();
478
479         HLOCAL t_message = 0;
480         bool const ok = ::FormatMessage(
481                 FORMAT_MESSAGE_ALLOCATE_BUFFER |
482                 FORMAT_MESSAGE_FROM_SYSTEM,
483                 0, error_code,
484                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
485                 (LPTSTR) &t_message, 0, 0
486                 ) != 0;
487
488         ostringstream ss;
489         ss << "LyX: Error waiting for child: " << error_code;
490
491         if (ok) {
492                 ss << ": " << (LPTSTR)t_message;
493                 ::LocalFree(t_message);
494         } else
495                 ss << ": Error unknown.";
496
497         return ss.str();
498 }
499 #endif
500
501
502 namespace ForkedCallsController {
503
504 typedef boost::shared_ptr<ForkedProcess> ForkedProcessPtr;
505 typedef list<ForkedProcessPtr> ListType;
506 typedef ListType::iterator iterator;
507
508
509 /// The child processes
510 static ListType forkedCalls;
511
512 iterator find_pid(pid_t pid)
513 {
514         return find_if(forkedCalls.begin(), forkedCalls.end(),
515                        bind(equal_to<pid_t>(),
516                             bind(&ForkedCall::pid, _1),
517                             pid));
518 }
519
520
521 void addCall(ForkedProcess const & newcall)
522 {
523         forkedCalls.push_back(newcall.clone());
524 }
525
526
527 // Check the list of dead children and emit any associated signals.
528 void handleCompletedProcesses()
529 {
530         ListType::iterator it  = forkedCalls.begin();
531         ListType::iterator end = forkedCalls.end();
532         while (it != end) {
533                 ForkedProcessPtr actCall = *it;
534                 bool remove_it = false;
535
536 #if defined(_WIN32)
537                 HANDLE const hProcess = HANDLE(actCall->pid());
538
539                 DWORD const wait_status = ::WaitForSingleObject(hProcess, 0);
540
541                 switch (wait_status) {
542                 case WAIT_TIMEOUT:
543                         // Still running
544                         break;
545                 case WAIT_OBJECT_0: {
546                         DWORD exit_code = 0;
547                         if (!GetExitCodeProcess(hProcess, &exit_code)) {
548                                 lyxerr << "GetExitCodeProcess failed waiting for child\n"
549                                        << getChildErrorMessage() << endl;
550                                 // Child died, so pretend it returned 1
551                                 actCall->setRetValue(1);
552                         } else {
553                                 actCall->setRetValue(exit_code);
554                         }
555                         remove_it = true;
556                         break;
557                 }
558                 case WAIT_FAILED:
559                         lyxerr << "WaitForSingleObject failed waiting for child\n"
560                                << getChildErrorMessage() << endl;
561                         actCall->setRetValue(1);
562                         remove_it = true;
563                         break;
564                 }
565 #else
566                 pid_t pid = actCall->pid();
567                 int stat_loc;
568                 pid_t const waitrpid = waitpid(pid, &stat_loc, WNOHANG);
569
570                 if (waitrpid == -1) {
571                         lyxerr << "LyX: Error waiting for child: "
572                                << strerror(errno) << endl;
573
574                         // Child died, so pretend it returned 1
575                         actCall->setRetValue(1);
576                         remove_it = true;
577
578                 } else if (waitrpid == 0) {
579                         // Still running. Move on to the next child.
580
581                 } else if (WIFEXITED(stat_loc)) {
582                         // Ok, the return value goes into retval.
583                         actCall->setRetValue(WEXITSTATUS(stat_loc));
584                         remove_it = true;
585
586                 } else if (WIFSIGNALED(stat_loc)) {
587                         // Child died, so pretend it returned 1
588                         actCall->setRetValue(1);
589                         remove_it = true;
590
591                 } else if (WIFSTOPPED(stat_loc)) {
592                         lyxerr << "LyX: Child (pid: " << pid
593                                << ") stopped on signal "
594                                << WSTOPSIG(stat_loc)
595                                << ". Waiting for child to finish." << endl;
596
597                 } else {
598                         lyxerr << "LyX: Something rotten happened while "
599                                 "waiting for child " << pid << endl;
600
601                         // Child died, so pretend it returned 1
602                         actCall->setRetValue(1);
603                         remove_it = true;
604                 }
605 #endif
606
607                 if (remove_it) {
608                         forkedCalls.erase(it);
609                         actCall->emitSignal();
610
611                         /* start all over: emiting the signal can result
612                          * in changing the list (Ab)
613                          */
614                         it = forkedCalls.begin();
615                 } else {
616                         ++it;
617                 }
618         }
619 }
620
621
622 // Kill the process prematurely and remove it from the list
623 // within tolerance secs
624 void kill(pid_t pid, int tolerance)
625 {
626         ListType::iterator it = find_pid(pid);
627         if (it == forkedCalls.end())
628                 return;
629
630         (*it)->kill(tolerance);
631         forkedCalls.erase(it);
632 }
633
634 } // namespace ForkedCallsController
635
636 } // namespace support
637 } // namespace lyx