Daily bump.
[official-gcc.git] / libgo / runtime / signal_unix.c
blobea0a58f2ea2c693fc7db8fc43e50cf91b63e9b25
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 openbsd netbsd
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 // 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;
39 t->flags |= SigHandling;
40 runtime_setsig(i, runtime_sighandler, true);
44 void
45 runtime_sigenable(uint32 sig)
47 int32 i;
48 SigTab *t;
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;
58 if(t == nil)
59 return;
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);
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((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);
95 void
96 runtime_resetcpuprofiler(int32 hz)
98 struct itimerval it;
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);
109 runtime_m()->profilehz = hz;
112 void
113 os_sigpipe(void)
115 int32 i;
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);
124 void
125 runtime_crash(void)
127 int32 i;
129 #ifdef GOOS_darwin
130 // OS X core dumps are linear dumps of the mapped memory,
131 // from the first virtual byte to the last, with zeros in the gaps.
132 // Because of the way we arrange the address space on 64-bit systems,
133 // this means the OS X core file will be >128 GB and even on a zippy
134 // workstation can take OS X well over an hour to write (uninterruptible).
135 // Save users from making that mistake.
136 if(sizeof(void*) == 8)
137 return;
138 #endif
140 for(i = 0; runtime_sigtab[i].sig != -1; i++)
141 if(runtime_sigtab[i].sig == SIGABRT)
142 break;
143 runtime_setsig(i, GO_SIG_DFL, false);
144 runtime_raise(SIGABRT);
147 void
148 runtime_raise(int32 sig)
150 raise(sig);