Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
| 6 #if defined(TARGET_OS_LINUX) | 6 #if defined(TARGET_OS_LINUX) |
| 7 | 7 |
| 8 #include "bin/process.h" | 8 #include "bin/process.h" |
| 9 | 9 |
| 10 #include <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
| 11 #include <fcntl.h> // NOLINT | 11 #include <fcntl.h> // NOLINT |
| 12 #include <poll.h> // NOLINT | 12 #include <poll.h> // NOLINT |
| 13 #include <stdio.h> // NOLINT | 13 #include <stdio.h> // NOLINT |
| 14 #include <stdlib.h> // NOLINT | 14 #include <stdlib.h> // NOLINT |
| 15 #include <string.h> // NOLINT | 15 #include <string.h> // NOLINT |
| 16 #include <sys/wait.h> // NOLINT | 16 #include <sys/wait.h> // NOLINT |
| 17 #include <unistd.h> // NOLINT | 17 #include <unistd.h> // NOLINT |
| 18 | 18 |
| 19 #include "bin/fdutils.h" | 19 #include "bin/fdutils.h" |
| 20 #include "bin/log.h" | 20 #include "bin/log.h" |
| 21 #include "bin/signal_blocker.h" | |
| 21 #include "bin/thread.h" | 22 #include "bin/thread.h" |
| 22 | 23 |
| 23 | 24 |
| 24 extern char **environ; | 25 extern char **environ; |
| 25 | 26 |
| 26 | 27 |
| 27 namespace dart { | 28 namespace dart { |
| 28 namespace bin { | 29 namespace bin { |
| 29 | 30 |
| 30 // ProcessInfo is used to map a process id to the file descriptor for | 31 // ProcessInfo is used to map a process id to the file descriptor for |
| (...skipping 592 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 623 | 624 |
| 624 void Process::TerminateExitCodeHandler() { | 625 void Process::TerminateExitCodeHandler() { |
| 625 ExitCodeHandler::TerminateExitCodeThread(); | 626 ExitCodeHandler::TerminateExitCodeThread(); |
| 626 } | 627 } |
| 627 | 628 |
| 628 | 629 |
| 629 intptr_t Process::CurrentProcessId() { | 630 intptr_t Process::CurrentProcessId() { |
| 630 return static_cast<intptr_t>(getpid()); | 631 return static_cast<intptr_t>(getpid()); |
| 631 } | 632 } |
| 632 | 633 |
| 634 | |
| 635 static Mutex* signal_mutex = new Mutex(); | |
| 636 static const int kSignalsCount = 5; | |
| 637 static const int kSignals[kSignalsCount] = { | |
| 638 SIGINT, | |
| 639 SIGWINCH, | |
| 640 SIGTERM, | |
| 641 SIGUSR1, | |
| 642 SIGUSR2 | |
| 643 }; | |
| 644 | |
| 645 | |
| 646 class SignalInfo { | |
|
Søren Gjesse
2013/12/19 08:51:24
This class could should go into the platform indep
Anders Johnsen
2013/12/19 10:54:17
Done.
| |
| 647 public: | |
| 648 SignalInfo(int fd, int signal, SignalInfo* prev = NULL) | |
| 649 : fd_(fd), | |
| 650 signal_(signal), | |
| 651 port_(Dart_GetMainPortId()), | |
| 652 next_(NULL), | |
| 653 prev_(prev) { | |
| 654 if (prev_ != NULL) { | |
| 655 prev_->next_ = this; | |
| 656 } | |
| 657 } | |
| 658 | |
| 659 ~SignalInfo() { | |
| 660 VOID_TEMP_FAILURE_RETRY(close(fd_)); | |
| 661 } | |
| 662 | |
| 663 void Unlink() { | |
| 664 if (prev_ != NULL) { | |
| 665 prev_->next_ = next_; | |
| 666 } | |
| 667 if (next_ != NULL) { | |
| 668 next_->prev_ = prev_; | |
| 669 } | |
| 670 } | |
| 671 | |
| 672 int fd() const { return fd_; } | |
| 673 int signal() const { return signal_; } | |
| 674 Dart_Port port() const { return port_; } | |
| 675 SignalInfo* next() const { return next_; } | |
| 676 | |
| 677 private: | |
| 678 int fd_; | |
| 679 int signal_; | |
| 680 Dart_Port port_; | |
|
Søren Gjesse
2013/12/19 08:51:24
Please add some documentation to the fields. Espec
Anders Johnsen
2013/12/19 10:54:17
Done.
| |
| 681 SignalInfo* next_; | |
| 682 SignalInfo* prev_; | |
| 683 }; | |
| 684 | |
| 685 | |
| 686 static SignalInfo* signal_handlers = NULL; | |
| 687 | |
| 688 | |
| 689 static void SignalHandler(int signal) { | |
| 690 MutexLocker lock(signal_mutex); | |
| 691 const SignalInfo* handler = signal_handlers; | |
| 692 while (handler != NULL) { | |
| 693 if (handler->signal() == signal) { | |
| 694 int value = 0; | |
| 695 VOID_TEMP_FAILURE_RETRY(write(handler->fd(), &value, 1)); | |
| 696 } | |
| 697 handler = handler->next(); | |
| 698 } | |
| 699 } | |
| 700 | |
| 701 | |
| 702 intptr_t Process::SetSignalHandler(intptr_t signal) { | |
| 703 bool found = false; | |
| 704 for (int i = 0; i < kSignalsCount; i++) { | |
| 705 if (kSignals[i] == signal) { | |
| 706 found = true; | |
| 707 break; | |
| 708 } | |
| 709 } | |
| 710 if (!found) return -1; | |
| 711 int fds[2]; | |
| 712 if (TEMP_FAILURE_RETRY_BLOCK_SIGNALS(pipe(fds)) != 0) { | |
| 713 return -1; | |
| 714 } | |
| 715 if (!FDUtils::SetNonBlocking(fds[0])) { | |
| 716 VOID_TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(fds[0])); | |
| 717 VOID_TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(fds[1])); | |
| 718 return -1; | |
| 719 } | |
| 720 ThreadSignalBlocker blocker(kSignalsCount, kSignals); | |
| 721 MutexLocker lock(signal_mutex); | |
| 722 SignalInfo* handler = signal_handlers; | |
| 723 bool listen = true; | |
| 724 while (handler != NULL) { | |
| 725 if (handler->signal() == signal) { | |
| 726 listen = false; | |
| 727 break; | |
| 728 } | |
| 729 handler = handler->next(); | |
| 730 } | |
| 731 if (listen) { | |
| 732 struct sigaction act; | |
| 733 bzero(&act, sizeof(act)); | |
| 734 act.sa_handler = SignalHandler; | |
| 735 int status = TEMP_FAILURE_RETRY_BLOCK_SIGNALS( | |
| 736 sigaction(signal, &act, NULL)); | |
| 737 if (status < 0) { | |
| 738 VOID_TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(fds[0])); | |
| 739 VOID_TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(fds[1])); | |
| 740 return -1; | |
| 741 } | |
| 742 } | |
| 743 if (signal_handlers == NULL) { | |
| 744 signal_handlers = new SignalInfo(fds[1], signal); | |
| 745 } else { | |
| 746 new SignalInfo(fds[1], signal, signal_handlers); | |
| 747 } | |
| 748 return fds[0]; | |
| 749 } | |
| 750 | |
| 751 | |
| 752 void Process::ClearSignalHandler(intptr_t signal) { | |
| 753 ThreadSignalBlocker blocker(kSignalsCount, kSignals); | |
| 754 MutexLocker lock(signal_mutex); | |
| 755 SignalInfo* handler = signal_handlers; | |
| 756 bool unlisten = true; | |
| 757 while (handler != NULL) { | |
| 758 bool remove = false; | |
| 759 if (handler->signal() == signal) { | |
| 760 if (handler->port() == Dart_GetMainPortId()) { | |
| 761 if (signal_handlers == handler) signal_handlers = handler->next(); | |
| 762 handler->Unlink(); | |
| 763 remove = true; | |
| 764 } else { | |
| 765 unlisten = false; | |
| 766 } | |
| 767 } | |
| 768 SignalInfo* next = handler->next(); | |
| 769 if (remove) delete handler; | |
| 770 handler = next; | |
| 771 } | |
| 772 if (unlisten) { | |
| 773 struct sigaction act; | |
| 774 bzero(&act, sizeof(act)); | |
| 775 act.sa_handler = SIG_DFL; | |
| 776 VOID_TEMP_FAILURE_RETRY_BLOCK_SIGNALS(sigaction(signal, &act, NULL)); | |
| 777 } | |
| 778 } | |
| 779 | |
| 633 } // namespace bin | 780 } // namespace bin |
| 634 } // namespace dart | 781 } // namespace dart |
| 635 | 782 |
| 636 #endif // defined(TARGET_OS_LINUX) | 783 #endif // defined(TARGET_OS_LINUX) |
| OLD | NEW |