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