Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(254)

Side by Side Diff: runtime/bin/process_macos.cc

Issue 119093007: Signal handling, take 2. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/process_linux.cc ('k') | runtime/bin/process_patch.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
21 #include "bin/thread.h" 22 #include "bin/thread.h"
22 23
23 extern char **environ; 24 extern char **environ;
24 25
25 26
26 namespace dart { 27 namespace dart {
27 namespace bin { 28 namespace bin {
28 29
29 // ProcessInfo is used to map a process id to the file descriptor for 30 // ProcessInfo is used to map a process id to the file descriptor for
30 // the pipe used to communicate the exit code of the process to Dart. 31 // the pipe used to communicate the exit code of the process to Dart.
(...skipping 584 matching lines...) Expand 10 before | Expand all | Expand 10 after
615 // Calculate the exit code. 616 // Calculate the exit code.
616 intptr_t exit_code = exit_code_data.ints[0]; 617 intptr_t exit_code = exit_code_data.ints[0];
617 intptr_t negative = exit_code_data.ints[1]; 618 intptr_t negative = exit_code_data.ints[1];
618 if (negative) exit_code = -exit_code; 619 if (negative) exit_code = -exit_code;
619 result->set_exit_code(exit_code); 620 result->set_exit_code(exit_code);
620 621
621 return true; 622 return true;
622 } 623 }
623 624
624 625
626 static int SignalMap(intptr_t id) {
627 switch (static_cast<ProcessSignals>(id)) {
628 case kSighup: return SIGHUP;
629 case kSigint: return SIGINT;
630 case kSigquit: return SIGQUIT;
631 case kSigill: return SIGILL;
632 case kSigtrap: return SIGTRAP;
633 case kSigabrt: return SIGABRT;
634 case kSigbus: return SIGBUS;
635 case kSigfpe: return SIGFPE;
636 case kSigkill: return SIGKILL;
637 case kSigusr1: return SIGUSR1;
638 case kSigsegv: return SIGSEGV;
639 case kSigusr2: return SIGUSR2;
640 case kSigpipe: return SIGPIPE;
641 case kSigalrm: return SIGALRM;
642 case kSigterm: return SIGTERM;
643 case kSigchld: return SIGCHLD;
644 case kSigcont: return SIGCONT;
645 case kSigstop: return SIGSTOP;
646 case kSigtstp: return SIGTSTP;
647 case kSigttin: return SIGTTIN;
648 case kSigttou: return SIGTTOU;
649 case kSigurg: return SIGURG;
650 case kSigxcpu: return SIGXCPU;
651 case kSigxfsz: return SIGXFSZ;
652 case kSigvtalrm: return SIGVTALRM;
653 case kSigprof: return SIGPROF;
654 case kSigwinch: return SIGWINCH;
655 case kSigpoll: return -1;
656 case kSigsys: return SIGSYS;
657 }
658 return -1;
659 }
660
661
625 bool Process::Kill(intptr_t id, int signal) { 662 bool Process::Kill(intptr_t id, int signal) {
626 return (TEMP_FAILURE_RETRY(kill(id, signal)) != -1); 663 return (TEMP_FAILURE_RETRY(kill(id, SignalMap(signal))) != -1);
627 } 664 }
628 665
629 666
630 void Process::TerminateExitCodeHandler() { 667 void Process::TerminateExitCodeHandler() {
631 ExitCodeHandler::TerminateExitCodeThread(); 668 ExitCodeHandler::TerminateExitCodeThread();
632 } 669 }
633 670
634 671
635 intptr_t Process::CurrentProcessId() { 672 intptr_t Process::CurrentProcessId() {
636 return static_cast<intptr_t>(getpid()); 673 return static_cast<intptr_t>(getpid());
637 } 674 }
638 675
676
677 static Mutex* signal_mutex = new Mutex();
678 static SignalInfo* signal_handlers = NULL;
679 static const int kSignalsCount = 5;
680 static const int kSignals[kSignalsCount] = {
681 SIGINT,
682 SIGWINCH,
683 SIGTERM,
684 SIGUSR1,
685 SIGUSR2
686 };
687
688
689 SignalInfo::~SignalInfo() {
690 VOID_TEMP_FAILURE_RETRY(close(fd_));
691 }
692
693
694 static void SignalHandler(int signal) {
695 MutexLocker lock(signal_mutex);
696 const SignalInfo* handler = signal_handlers;
697 while (handler != NULL) {
698 if (handler->signal() == signal) {
699 int value = 0;
700 VOID_TEMP_FAILURE_RETRY(write(handler->fd(), &value, 1));
701 }
702 handler = handler->next();
703 }
704 }
705
706
707 intptr_t Process::SetSignalHandler(intptr_t signal) {
708 signal = SignalMap(signal);
709 if (signal == -1) return -1;
710 bool found = false;
711 for (int i = 0; i < kSignalsCount; i++) {
712 if (kSignals[i] == signal) {
713 found = true;
714 break;
715 }
716 }
717 if (!found) return -1;
718 int fds[2];
719 if (TEMP_FAILURE_RETRY_BLOCK_SIGNALS(pipe(fds)) != 0) {
720 return -1;
721 }
722 if (!FDUtils::SetNonBlocking(fds[0])) {
723 VOID_TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(fds[0]));
724 VOID_TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(fds[1]));
725 return -1;
726 }
727 ThreadSignalBlocker blocker(kSignalsCount, kSignals);
728 MutexLocker lock(signal_mutex);
729 SignalInfo* handler = signal_handlers;
730 bool listen = true;
731 while (handler != NULL) {
732 if (handler->signal() == signal) {
733 listen = false;
734 break;
735 }
736 handler = handler->next();
737 }
738 if (listen) {
739 struct sigaction act;
740 bzero(&act, sizeof(act));
741 act.sa_handler = SignalHandler;
742 int status = TEMP_FAILURE_RETRY_BLOCK_SIGNALS(
743 sigaction(signal, &act, NULL));
744 if (status < 0) {
745 VOID_TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(fds[0]));
746 VOID_TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(fds[1]));
747 return -1;
748 }
749 }
750 if (signal_handlers == NULL) {
751 signal_handlers = new SignalInfo(fds[1], signal);
752 } else {
753 new SignalInfo(fds[1], signal, signal_handlers);
754 }
755 return fds[0];
756 }
757
758
759 void Process::ClearSignalHandler(intptr_t signal) {
760 signal = SignalMap(signal);
761 if (signal == -1) return;
762 ThreadSignalBlocker blocker(kSignalsCount, kSignals);
763 MutexLocker lock(signal_mutex);
764 SignalInfo* handler = signal_handlers;
765 bool unlisten = true;
766 while (handler != NULL) {
767 bool remove = false;
768 if (handler->signal() == signal) {
769 if (handler->port() == Dart_GetMainPortId()) {
770 if (signal_handlers == handler) signal_handlers = handler->next();
771 handler->Unlink();
772 remove = true;
773 } else {
774 unlisten = false;
775 }
776 }
777 SignalInfo* next = handler->next();
778 if (remove) delete handler;
779 handler = next;
780 }
781 if (unlisten) {
782 struct sigaction act;
783 bzero(&act, sizeof(act));
784 act.sa_handler = SIG_DFL;
785 VOID_TEMP_FAILURE_RETRY_BLOCK_SIGNALS(sigaction(signal, &act, NULL));
786 }
787 }
788
639 } // namespace bin 789 } // namespace bin
640 } // namespace dart 790 } // namespace dart
641 791
642 #endif // defined(TARGET_OS_MACOS) 792 #endif // defined(TARGET_OS_MACOS)
OLDNEW
« no previous file with comments | « runtime/bin/process_linux.cc ('k') | runtime/bin/process_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698