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