Commit e24b2445 authored by Ian Lance Taylor's avatar Ian Lance Taylor

os/signal: Stop restores original signal handling

Since Stop was introduced, it would revert to the system default for the
signal, rather than to the default Go behavior.  Change it to revert to
the default Go behavior.

Change-Id: I345467ece0e49e31b2806d6fce2f1937b17905a6
Reviewed-on: https://go-review.googlesource.com/18229Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
parent 91f997be
...@@ -182,13 +182,14 @@ func TestStop(t *testing.T) { ...@@ -182,13 +182,14 @@ func TestStop(t *testing.T) {
sigs := []syscall.Signal{ sigs := []syscall.Signal{
syscall.SIGWINCH, syscall.SIGWINCH,
syscall.SIGHUP, syscall.SIGHUP,
syscall.SIGUSR1,
} }
for _, sig := range sigs { for _, sig := range sigs {
// Send the signal. // Send the signal.
// If it's SIGWINCH, we should not see it. // If it's SIGWINCH, we should not see it.
// If it's SIGHUP, maybe we'll die. Let the flag tell us what to do. // If it's SIGHUP, maybe we'll die. Let the flag tell us what to do.
if sig != syscall.SIGHUP || *sendUncaughtSighup == 1 { if sig == syscall.SIGWINCH || (sig == syscall.SIGHUP && *sendUncaughtSighup == 1) {
syscall.Kill(syscall.Getpid(), sig) syscall.Kill(syscall.Getpid(), sig)
} }
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
......
...@@ -50,26 +50,12 @@ func initsig() { ...@@ -50,26 +50,12 @@ func initsig() {
} }
fwdSig[i] = getsig(i) fwdSig[i] = getsig(i)
// For some signals, we respect an inherited SIG_IGN handler if !sigInstallGoHandler(i) {
// rather than insist on installing our own default handler. // Even if we are not installing a signal handler,
// Even these signals can be fetched using the os/signal package. // set SA_ONSTACK if necessary.
switch i { if fwdSig[i] != _SIG_DFL && fwdSig[i] != _SIG_IGN {
case _SIGHUP, _SIGINT: setsigstack(i)
if fwdSig[i] == _SIG_IGN {
continue
} }
}
if t.flags&_SigSetStack != 0 {
setsigstack(i)
continue
}
// When built using c-archive or c-shared, only
// install signal handlers for synchronous signals.
// Set SA_ONSTACK for other signals if necessary.
if (isarchive || islibrary) && t.flags&_SigPanic == 0 {
setsigstack(i)
continue continue
} }
...@@ -78,6 +64,31 @@ func initsig() { ...@@ -78,6 +64,31 @@ func initsig() {
} }
} }
func sigInstallGoHandler(sig int32) bool {
// 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 sig {
case _SIGHUP, _SIGINT:
if fwdSig[sig] == _SIG_IGN {
return false
}
}
t := &sigtable[sig]
if t.flags&_SigSetStack != 0 {
return false
}
// When built using c-archive or c-shared, only install signal
// handlers for synchronous signals.
if (isarchive || islibrary) && t.flags&_SigPanic == 0 {
return false
}
return true
}
func sigenable(sig uint32) { func sigenable(sig uint32) {
if sig >= uint32(len(sigtable)) { if sig >= uint32(len(sigtable)) {
return return
...@@ -105,7 +116,11 @@ func sigdisable(sig uint32) { ...@@ -105,7 +116,11 @@ func sigdisable(sig uint32) {
ensureSigM() ensureSigM()
disableSigChan <- sig disableSigChan <- sig
<-maskUpdatedChan <-maskUpdatedChan
if t.flags&_SigHandling != 0 {
// If initsig does not install a signal handler for a
// signal, then to go back to the state before Notify
// we should remove the one we installed.
if !sigInstallGoHandler(int32(sig)) {
t.flags &^= _SigHandling t.flags &^= _SigHandling
setsig(int32(sig), fwdSig[sig], true) setsig(int32(sig), fwdSig[sig], true)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment