signal_unix.c 3.03 KB
Newer Older
1 2 3 4
// Copyright 2012 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

5
// +build darwin dragonfly freebsd linux openbsd netbsd
6 7 8 9 10

#include <sys/time.h>

#include "runtime.h"
#include "defs.h"
11
#include "signal_unix.h"
12 13 14 15 16 17 18 19 20 21 22 23 24 25

extern SigTab runtime_sigtab[];

void
runtime_initsig(void)
{
	int32 i;
	SigTab *t;

	// First call: basic setup.
	for(i = 0; runtime_sigtab[i].sig != -1; i++) {
		t = &runtime_sigtab[i];
		if((t->flags == 0) || (t->flags & SigDefault))
			continue;
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

		// For some signals, we respect an inherited SIG_IGN handler
		// rather than insist on installing our own default handler.
		// Even these signals can be fetched using the os/signal package.
		switch(t->sig) {
		case SIGHUP:
		case SIGINT:
			if(runtime_getsig(i) == GO_SIG_IGN) {
				t->flags = SigNotify | SigIgnored;
				continue;
			}
		}

		t->flags |= SigHandling;
		runtime_setsig(i, runtime_sighandler, true);
41 42 43 44 45 46 47 48 49
	}
}

void
runtime_sigenable(uint32 sig)
{
	int32 i;
	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;
55 56
		}
	}
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

	if(t == nil)
		return;

	if((t->flags & SigNotify) && !(t->flags & SigHandling)) {
		t->flags |= SigHandling;
		if(runtime_getsig(i) == GO_SIG_IGN)
			t->flags |= SigIgnored;
		runtime_setsig(i, runtime_sighandler, true);
	}
}

void
runtime_sigdisable(uint32 sig)
{
	int32 i;
	SigTab *t;

	t = nil;
	for(i = 0; runtime_sigtab[i].sig != -1; i++) {
		if(runtime_sigtab[i].sig == (int32)sig) {
			t = &runtime_sigtab[i];
			break;
		}
	}

	if(t == nil)
		return;

	if((t->flags & SigNotify) && (t->flags & SigHandling)) {
		t->flags &= ~SigHandling;
		if(t->flags & SigIgnored)
			runtime_setsig(i, GO_SIG_IGN, true);
		else
			runtime_setsig(i, GO_SIG_DFL, true);
	}
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
}

void
runtime_resetcpuprofiler(int32 hz)
{
	struct itimerval it;

	runtime_memclr((byte*)&it, sizeof it);
	if(hz == 0) {
		runtime_setitimer(ITIMER_PROF, &it, nil);
	} else {
		it.it_interval.tv_sec = 0;
		it.it_interval.tv_usec = 1000000 / hz;
		it.it_value = it.it_interval;
		runtime_setitimer(ITIMER_PROF, &it, nil);
	}
	runtime_m()->profilehz = hz;
}
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

void
os_sigpipe(void)
{
	int32 i;

	for(i = 0; runtime_sigtab[i].sig != -1; i++)
		if(runtime_sigtab[i].sig == SIGPIPE)
			break;
	runtime_setsig(i, GO_SIG_DFL, false);
	runtime_raise(SIGPIPE);
}

void
runtime_crash(void)
{
	int32 i;

#ifdef GOOS_darwin
	// OS X core dumps are linear dumps of the mapped memory,
	// from the first virtual byte to the last, with zeros in the gaps.
	// Because of the way we arrange the address space on 64-bit systems,
	// this means the OS X core file will be >128 GB and even on a zippy
	// workstation can take OS X well over an hour to write (uninterruptible).
	// Save users from making that mistake.
	if(sizeof(void*) == 8)
		return;
#endif

	for(i = 0; runtime_sigtab[i].sig != -1; i++)
		if(runtime_sigtab[i].sig == SIGABRT)
			break;
	runtime_setsig(i, GO_SIG_DFL, false);
	runtime_raise(SIGABRT);
}

void
runtime_raise(int32 sig)
{
	raise(sig);
}