rsc.io/go@v0.0.0-20150416155037-e040fd465409/src/runtime/signal1_unix.go (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  package runtime
     8  
     9  const (
    10  	_SIG_DFL uintptr = 0
    11  	_SIG_IGN uintptr = 1
    12  )
    13  
    14  func initsig() {
    15  	// _NSIG is the number of signals on this operating system.
    16  	// sigtable should describe what to do for all the possible signals.
    17  	if len(sigtable) != _NSIG {
    18  		print("runtime: len(sigtable)=", len(sigtable), " _NSIG=", _NSIG, "\n")
    19  		throw("initsig")
    20  	}
    21  
    22  	// First call: basic setup.
    23  	for i := int32(0); i < _NSIG; i++ {
    24  		t := &sigtable[i]
    25  		if t.flags == 0 || t.flags&_SigDefault != 0 {
    26  			continue
    27  		}
    28  
    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 i {
    33  		case _SIGHUP, _SIGINT:
    34  			if getsig(i) == _SIG_IGN {
    35  				t.flags = _SigNotify | _SigIgnored
    36  				continue
    37  			}
    38  		}
    39  
    40  		if t.flags&_SigSetStack != 0 {
    41  			setsigstack(i)
    42  			continue
    43  		}
    44  
    45  		t.flags |= _SigHandling
    46  		setsig(i, funcPC(sighandler), true)
    47  	}
    48  }
    49  
    50  func sigenable(sig uint32) {
    51  	if sig >= uint32(len(sigtable)) {
    52  		return
    53  	}
    54  
    55  	t := &sigtable[sig]
    56  	if t.flags&_SigNotify != 0 && t.flags&_SigHandling == 0 {
    57  		t.flags |= _SigHandling
    58  		if getsig(int32(sig)) == _SIG_IGN {
    59  			t.flags |= _SigIgnored
    60  		}
    61  		setsig(int32(sig), funcPC(sighandler), true)
    62  	}
    63  }
    64  
    65  func sigdisable(sig uint32) {
    66  	if sig >= uint32(len(sigtable)) {
    67  		return
    68  	}
    69  
    70  	t := &sigtable[sig]
    71  	if t.flags&_SigNotify != 0 && t.flags&_SigHandling != 0 {
    72  		t.flags &^= _SigHandling
    73  		if t.flags&_SigIgnored != 0 {
    74  			setsig(int32(sig), _SIG_IGN, true)
    75  		} else {
    76  			setsig(int32(sig), _SIG_DFL, true)
    77  		}
    78  	}
    79  }
    80  
    81  func sigignore(sig uint32) {
    82  	if sig >= uint32(len(sigtable)) {
    83  		return
    84  	}
    85  
    86  	t := &sigtable[sig]
    87  	if t.flags&_SigNotify != 0 {
    88  		t.flags &^= _SigHandling
    89  		setsig(int32(sig), _SIG_IGN, true)
    90  	}
    91  }
    92  
    93  func resetcpuprofiler(hz int32) {
    94  	var it itimerval
    95  	if hz == 0 {
    96  		setitimer(_ITIMER_PROF, &it, nil)
    97  	} else {
    98  		it.it_interval.tv_sec = 0
    99  		it.it_interval.set_usec(1000000 / hz)
   100  		it.it_value = it.it_interval
   101  		setitimer(_ITIMER_PROF, &it, nil)
   102  	}
   103  	_g_ := getg()
   104  	_g_.m.profilehz = hz
   105  }
   106  
   107  func sigpipe() {
   108  	setsig(_SIGPIPE, _SIG_DFL, false)
   109  	raise(_SIGPIPE)
   110  }
   111  
   112  func crash() {
   113  	if GOOS == "darwin" {
   114  		// OS X core dumps are linear dumps of the mapped memory,
   115  		// from the first virtual byte to the last, with zeros in the gaps.
   116  		// Because of the way we arrange the address space on 64-bit systems,
   117  		// this means the OS X core file will be >128 GB and even on a zippy
   118  		// workstation can take OS X well over an hour to write (uninterruptible).
   119  		// Save users from making that mistake.
   120  		if ptrSize == 8 {
   121  			return
   122  		}
   123  	}
   124  
   125  	unblocksignals()
   126  	setsig(_SIGABRT, _SIG_DFL, false)
   127  	raise(_SIGABRT)
   128  }