github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/runtime/signal_unix.c (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build darwin dragonfly freebsd linux openbsd netbsd 6 7 #include "runtime.h" 8 #include "defs_GOOS_GOARCH.h" 9 #include "os_GOOS.h" 10 #include "signal_unix.h" 11 12 extern SigTab runtime·sigtab[]; 13 14 void 15 runtime·initsig(void) 16 { 17 int32 i; 18 SigTab *t; 19 20 // First call: basic setup. 21 for(i = 0; i<NSIG; i++) { 22 t = &runtime·sigtab[i]; 23 if((t->flags == 0) || (t->flags & SigDefault)) 24 continue; 25 26 // For some signals, we respect an inherited SIG_IGN handler 27 // rather than insist on installing our own default handler. 28 // Even these signals can be fetched using the os/signal package. 29 switch(i) { 30 case SIGHUP: 31 case SIGINT: 32 if(runtime·getsig(i) == SIG_IGN) { 33 t->flags = SigNotify | SigIgnored; 34 continue; 35 } 36 } 37 38 t->flags |= SigHandling; 39 runtime·setsig(i, runtime·sighandler, true); 40 } 41 } 42 43 void 44 runtime·sigenable(uint32 sig) 45 { 46 SigTab *t; 47 48 if(sig >= NSIG) 49 return; 50 51 t = &runtime·sigtab[sig]; 52 if((t->flags & SigNotify) && !(t->flags & SigHandling)) { 53 t->flags |= SigHandling; 54 if(runtime·getsig(sig) == SIG_IGN) 55 t->flags |= SigIgnored; 56 runtime·setsig(sig, runtime·sighandler, true); 57 } 58 } 59 60 void 61 runtime·sigdisable(uint32 sig) 62 { 63 SigTab *t; 64 65 if(sig >= NSIG) 66 return; 67 68 t = &runtime·sigtab[sig]; 69 if((t->flags & SigNotify) && (t->flags & SigHandling)) { 70 t->flags &= ~SigHandling; 71 if(t->flags & SigIgnored) 72 runtime·setsig(sig, SIG_IGN, true); 73 else 74 runtime·setsig(sig, SIG_DFL, true); 75 } 76 } 77 78 void 79 runtime·resetcpuprofiler(int32 hz) 80 { 81 Itimerval it; 82 83 runtime·memclr((byte*)&it, sizeof it); 84 if(hz == 0) { 85 runtime·setitimer(ITIMER_PROF, &it, nil); 86 } else { 87 it.it_interval.tv_sec = 0; 88 it.it_interval.tv_usec = 1000000 / hz; 89 it.it_value = it.it_interval; 90 runtime·setitimer(ITIMER_PROF, &it, nil); 91 } 92 m->profilehz = hz; 93 } 94 95 void 96 os·sigpipe(void) 97 { 98 runtime·setsig(SIGPIPE, SIG_DFL, false); 99 runtime·raise(SIGPIPE); 100 } 101 102 void 103 runtime·crash(void) 104 { 105 #ifdef GOOS_darwin 106 // OS X core dumps are linear dumps of the mapped memory, 107 // from the first virtual byte to the last, with zeros in the gaps. 108 // Because of the way we arrange the address space on 64-bit systems, 109 // this means the OS X core file will be >128 GB and even on a zippy 110 // workstation can take OS X well over an hour to write (uninterruptible). 111 // Save users from making that mistake. 112 if(sizeof(void*) == 8) 113 return; 114 #endif 115 116 runtime·setsig(SIGABRT, SIG_DFL, false); 117 runtime·raise(SIGABRT); 118 }