github.com/prattmic/llgo-embedded@v0.0.0-20150820070356-41cfecea0e1e/third_party/gofrontend/libgo/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 netbsd openbsd solaris 6 7 #include <sys/time.h> 8 9 #include "runtime.h" 10 #include "defs.h" 11 #include "signal_unix.h" 12 13 extern SigTab runtime_sigtab[]; 14 15 void 16 runtime_initsig(void) 17 { 18 int32 i; 19 SigTab *t; 20 21 // First call: basic setup. 22 for(i = 0; runtime_sigtab[i].sig != -1; i++) { 23 t = &runtime_sigtab[i]; 24 if((t->flags == 0) || (t->flags & SigDefault)) 25 continue; 26 27 // For some signals, we respect an inherited SIG_IGN handler 28 // rather than insist on installing our own default handler. 29 // Even these signals can be fetched using the os/signal package. 30 switch(t->sig) { 31 case SIGHUP: 32 case SIGINT: 33 if(runtime_getsig(i) == GO_SIG_IGN) { 34 t->flags = SigNotify | SigIgnored; 35 continue; 36 } 37 } 38 39 t->flags |= SigHandling; 40 runtime_setsig(i, runtime_sighandler, true); 41 } 42 } 43 44 void 45 runtime_sigenable(uint32 sig) 46 { 47 int32 i; 48 SigTab *t; 49 50 t = nil; 51 for(i = 0; runtime_sigtab[i].sig != -1; i++) { 52 if(runtime_sigtab[i].sig == (int32)sig) { 53 t = &runtime_sigtab[i]; 54 break; 55 } 56 } 57 58 if(t == nil) 59 return; 60 61 if((t->flags & SigNotify) && !(t->flags & SigHandling)) { 62 t->flags |= SigHandling; 63 if(runtime_getsig(i) == GO_SIG_IGN) 64 t->flags |= SigIgnored; 65 runtime_setsig(i, runtime_sighandler, true); 66 } 67 } 68 69 void 70 runtime_sigdisable(uint32 sig) 71 { 72 int32 i; 73 SigTab *t; 74 75 t = nil; 76 for(i = 0; runtime_sigtab[i].sig != -1; i++) { 77 if(runtime_sigtab[i].sig == (int32)sig) { 78 t = &runtime_sigtab[i]; 79 break; 80 } 81 } 82 83 if(t == nil) 84 return; 85 86 if((t->flags & SigNotify) && (t->flags & SigHandling)) { 87 t->flags &= ~SigHandling; 88 if(t->flags & SigIgnored) 89 runtime_setsig(i, GO_SIG_IGN, true); 90 else 91 runtime_setsig(i, GO_SIG_DFL, true); 92 } 93 } 94 95 void 96 runtime_resetcpuprofiler(int32 hz) 97 { 98 struct itimerval it; 99 100 runtime_memclr((byte*)&it, sizeof it); 101 if(hz == 0) { 102 runtime_setitimer(ITIMER_PROF, &it, nil); 103 } else { 104 it.it_interval.tv_sec = 0; 105 it.it_interval.tv_usec = 1000000 / hz; 106 it.it_value = it.it_interval; 107 runtime_setitimer(ITIMER_PROF, &it, nil); 108 } 109 runtime_m()->profilehz = hz; 110 } 111 112 void 113 os_sigpipe(void) 114 { 115 int32 i; 116 117 for(i = 0; runtime_sigtab[i].sig != -1; i++) 118 if(runtime_sigtab[i].sig == SIGPIPE) 119 break; 120 runtime_setsig(i, GO_SIG_DFL, false); 121 runtime_raise(SIGPIPE); 122 } 123 124 void 125 runtime_unblocksignals(void) 126 { 127 sigset_t sigset_none; 128 sigemptyset(&sigset_none); 129 pthread_sigmask(SIG_SETMASK, &sigset_none, nil); 130 } 131 132 void 133 runtime_crash(void) 134 { 135 int32 i; 136 137 #ifdef GOOS_darwin 138 // OS X core dumps are linear dumps of the mapped memory, 139 // from the first virtual byte to the last, with zeros in the gaps. 140 // Because of the way we arrange the address space on 64-bit systems, 141 // this means the OS X core file will be >128 GB and even on a zippy 142 // workstation can take OS X well over an hour to write (uninterruptible). 143 // Save users from making that mistake. 144 if(sizeof(void*) == 8) 145 return; 146 #endif 147 148 runtime_unblocksignals(); 149 for(i = 0; runtime_sigtab[i].sig != -1; i++) 150 if(runtime_sigtab[i].sig == SIGABRT) 151 break; 152 runtime_setsig(i, GO_SIG_DFL, false); 153 runtime_raise(SIGABRT); 154 } 155 156 void 157 runtime_raise(int32 sig) 158 { 159 raise(sig); 160 }