| 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_MACOS) | 6 #if defined(TARGET_OS_MACOS) |
| 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 <signal.h> // NOLINT | 13 #include <signal.h> // NOLINT |
| 14 #include <stdio.h> // NOLINT | 14 #include <stdio.h> // NOLINT |
| 15 #include <stdlib.h> // NOLINT | 15 #include <stdlib.h> // NOLINT |
| 16 #include <string.h> // NOLINT | 16 #include <string.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" | |
| 22 #include "bin/thread.h" | 21 #include "bin/thread.h" |
| 23 | 22 |
| 24 extern char **environ; | 23 extern char **environ; |
| 25 | 24 |
| 26 | 25 |
| 27 namespace dart { | 26 namespace dart { |
| 28 namespace bin { | 27 namespace bin { |
| 29 | 28 |
| 30 // ProcessInfo is used to map a process id to the file descriptor for | 29 // ProcessInfo is used to map a process id to the file descriptor for |
| 31 // the pipe used to communicate the exit code of the process to Dart. | 30 // the pipe used to communicate the exit code of the process to Dart. |
| (...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 630 | 629 |
| 631 void Process::TerminateExitCodeHandler() { | 630 void Process::TerminateExitCodeHandler() { |
| 632 ExitCodeHandler::TerminateExitCodeThread(); | 631 ExitCodeHandler::TerminateExitCodeThread(); |
| 633 } | 632 } |
| 634 | 633 |
| 635 | 634 |
| 636 intptr_t Process::CurrentProcessId() { | 635 intptr_t Process::CurrentProcessId() { |
| 637 return static_cast<intptr_t>(getpid()); | 636 return static_cast<intptr_t>(getpid()); |
| 638 } | 637 } |
| 639 | 638 |
| 640 | |
| 641 static Mutex* signal_mutex = new Mutex(); | |
| 642 static SignalInfo* signal_handlers = NULL; | |
| 643 static const int kSignalsCount = 5; | |
| 644 static const int kSignals[kSignalsCount] = { | |
| 645 SIGINT, | |
| 646 SIGWINCH, | |
| 647 SIGTERM, | |
| 648 SIGUSR1, | |
| 649 SIGUSR2 | |
| 650 }; | |
| 651 | |
| 652 | |
| 653 SignalInfo::~SignalInfo() { | |
| 654 VOID_TEMP_FAILURE_RETRY(close(fd_)); | |
| 655 } | |
| 656 | |
| 657 | |
| 658 static void SignalHandler(int signal) { | |
| 659 MutexLocker lock(signal_mutex); | |
| 660 const SignalInfo* handler = signal_handlers; | |
| 661 while (handler != NULL) { | |
| 662 if (handler->signal() == signal) { | |
| 663 int value = 0; | |
| 664 VOID_TEMP_FAILURE_RETRY(write(handler->fd(), &value, 1)); | |
| 665 } | |
| 666 handler = handler->next(); | |
| 667 } | |
| 668 } | |
| 669 | |
| 670 | |
| 671 intptr_t Process::SetSignalHandler(intptr_t signal) { | |
| 672 bool found = false; | |
| 673 for (int i = 0; i < kSignalsCount; i++) { | |
| 674 if (kSignals[i] == signal) { | |
| 675 found = true; | |
| 676 break; | |
| 677 } | |
| 678 } | |
| 679 if (!found) return -1; | |
| 680 int fds[2]; | |
| 681 if (TEMP_FAILURE_RETRY_BLOCK_SIGNALS(pipe(fds)) != 0) { | |
| 682 return -1; | |
| 683 } | |
| 684 if (!FDUtils::SetNonBlocking(fds[0])) { | |
| 685 VOID_TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(fds[0])); | |
| 686 VOID_TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(fds[1])); | |
| 687 return -1; | |
| 688 } | |
| 689 ThreadSignalBlocker blocker(kSignalsCount, kSignals); | |
| 690 MutexLocker lock(signal_mutex); | |
| 691 SignalInfo* handler = signal_handlers; | |
| 692 bool listen = true; | |
| 693 while (handler != NULL) { | |
| 694 if (handler->signal() == signal) { | |
| 695 listen = false; | |
| 696 break; | |
| 697 } | |
| 698 handler = handler->next(); | |
| 699 } | |
| 700 if (listen) { | |
| 701 struct sigaction act; | |
| 702 bzero(&act, sizeof(act)); | |
| 703 act.sa_handler = SignalHandler; | |
| 704 int status = TEMP_FAILURE_RETRY_BLOCK_SIGNALS( | |
| 705 sigaction(signal, &act, NULL)); | |
| 706 if (status < 0) { | |
| 707 VOID_TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(fds[0])); | |
| 708 VOID_TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(fds[1])); | |
| 709 return -1; | |
| 710 } | |
| 711 } | |
| 712 if (signal_handlers == NULL) { | |
| 713 signal_handlers = new SignalInfo(fds[1], signal); | |
| 714 } else { | |
| 715 new SignalInfo(fds[1], signal, signal_handlers); | |
| 716 } | |
| 717 return fds[0]; | |
| 718 } | |
| 719 | |
| 720 | |
| 721 void Process::ClearSignalHandler(intptr_t signal) { | |
| 722 ThreadSignalBlocker blocker(kSignalsCount, kSignals); | |
| 723 MutexLocker lock(signal_mutex); | |
| 724 SignalInfo* handler = signal_handlers; | |
| 725 bool unlisten = true; | |
| 726 while (handler != NULL) { | |
| 727 bool remove = false; | |
| 728 if (handler->signal() == signal) { | |
| 729 if (handler->port() == Dart_GetMainPortId()) { | |
| 730 if (signal_handlers == handler) signal_handlers = handler->next(); | |
| 731 handler->Unlink(); | |
| 732 remove = true; | |
| 733 } else { | |
| 734 unlisten = false; | |
| 735 } | |
| 736 } | |
| 737 SignalInfo* next = handler->next(); | |
| 738 if (remove) delete handler; | |
| 739 handler = next; | |
| 740 } | |
| 741 if (unlisten) { | |
| 742 struct sigaction act; | |
| 743 bzero(&act, sizeof(act)); | |
| 744 act.sa_handler = SIG_DFL; | |
| 745 VOID_TEMP_FAILURE_RETRY_BLOCK_SIGNALS(sigaction(signal, &act, NULL)); | |
| 746 } | |
| 747 } | |
| 748 | |
| 749 } // namespace bin | 639 } // namespace bin |
| 750 } // namespace dart | 640 } // namespace dart |
| 751 | 641 |
| 752 #endif // defined(TARGET_OS_MACOS) | 642 #endif // defined(TARGET_OS_MACOS) |
| OLD | NEW |