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