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