Fix PR #.
[official-gcc.git] / libgo / runtime / signal_unix.c
blob20289335f8dc5835a8a4a0a7810f06ab4ae9a6f5
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.
5 // +build darwin dragonfly freebsd linux netbsd openbsd solaris
7 #include <sys/time.h>
9 #include "runtime.h"
10 #include "defs.h"
11 #include "signal_unix.h"
13 extern SigTab runtime_sigtab[];
15 void
16 runtime_initsig(void)
18 int32 i;
19 SigTab *t;
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;
27 t->fwdsig = runtime_getsig(i);
29 // For some signals, we respect an inherited SIG_IGN handler
30 // rather than insist on installing our own default handler.
31 // Even these signals can be fetched using the os/signal package.
32 switch(t->sig) {
33 case SIGHUP:
34 case SIGINT:
35 if(t->fwdsig == GO_SIG_IGN) {
36 continue;
40 t->flags |= SigHandling;
41 runtime_setsig(i, runtime_sighandler, true);
45 void
46 runtime_sigenable(uint32 sig)
48 int32 i;
49 SigTab *t;
51 t = nil;
52 for(i = 0; runtime_sigtab[i].sig != -1; i++) {
53 if(runtime_sigtab[i].sig == (int32)sig) {
54 t = &runtime_sigtab[i];
55 break;
59 if(t == nil)
60 return;
62 if((t->flags & SigNotify) && !(t->flags & SigHandling)) {
63 t->flags |= SigHandling;
64 t->fwdsig = runtime_getsig(i);
65 runtime_setsig(i, runtime_sighandler, true);
69 void
70 runtime_sigdisable(uint32 sig)
72 int32 i;
73 SigTab *t;
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;
83 if(t == nil)
84 return;
86 if((sig == SIGHUP || sig == SIGINT) && t->fwdsig == GO_SIG_IGN) {
87 t->flags &= ~SigHandling;
88 runtime_setsig(i, t->fwdsig, true);
92 void
93 runtime_sigignore(uint32 sig)
95 int32 i;
96 SigTab *t;
98 t = nil;
99 for(i = 0; runtime_sigtab[i].sig != -1; i++) {
100 if(runtime_sigtab[i].sig == (int32)sig) {
101 t = &runtime_sigtab[i];
102 break;
106 if(t == nil)
107 return;
109 if((t->flags & SigNotify) != 0) {
110 t->flags &= ~SigHandling;
111 runtime_setsig(i, GO_SIG_IGN, true);
115 void
116 runtime_resetcpuprofiler(int32 hz)
118 struct itimerval it;
120 runtime_memclr((byte*)&it, sizeof it);
121 if(hz == 0) {
122 runtime_setitimer(ITIMER_PROF, &it, nil);
123 } else {
124 it.it_interval.tv_sec = 0;
125 it.it_interval.tv_usec = 1000000 / hz;
126 it.it_value = it.it_interval;
127 runtime_setitimer(ITIMER_PROF, &it, nil);
129 runtime_m()->profilehz = hz;
132 void
133 runtime_unblocksignals(void)
135 sigset_t sigset_none;
136 sigemptyset(&sigset_none);
137 pthread_sigmask(SIG_SETMASK, &sigset_none, nil);
140 void
141 runtime_crash(void)
143 int32 i;
145 #ifdef GOOS_darwin
146 // OS X core dumps are linear dumps of the mapped memory,
147 // from the first virtual byte to the last, with zeros in the gaps.
148 // Because of the way we arrange the address space on 64-bit systems,
149 // this means the OS X core file will be >128 GB and even on a zippy
150 // workstation can take OS X well over an hour to write (uninterruptible).
151 // Save users from making that mistake.
152 if(sizeof(void*) == 8)
153 return;
154 #endif
156 runtime_unblocksignals();
157 for(i = 0; runtime_sigtab[i].sig != -1; i++)
158 if(runtime_sigtab[i].sig == SIGABRT)
159 break;
160 runtime_setsig(i, GO_SIG_DFL, false);
161 runtime_raise(SIGABRT);
164 void
165 runtime_raise(int32 sig)
167 raise(sig);