2021-08-23 09:14:53 -04:00
|
|
|
//go:build darwin || linux
|
2017-11-01 19:37:53 -04:00
|
|
|
// +build darwin linux
|
2017-05-31 12:40:41 -04:00
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package signal // import "github.com/docker/docker/pkg/signal"
|
2017-05-31 12:40:41 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
2021-05-19 13:42:18 -04:00
|
|
|
"time"
|
2017-05-31 12:40:41 -04:00
|
|
|
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-05-31 12:40:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCatchAll(t *testing.T) {
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
CatchAll(sigs)
|
|
|
|
defer StopCatch(sigs)
|
|
|
|
|
|
|
|
listOfSignals := map[string]string{
|
|
|
|
"CONT": syscall.SIGCONT.String(),
|
|
|
|
"HUP": syscall.SIGHUP.String(),
|
|
|
|
"CHLD": syscall.SIGCHLD.String(),
|
|
|
|
"ILL": syscall.SIGILL.String(),
|
|
|
|
"FPE": syscall.SIGFPE.String(),
|
|
|
|
"CLD": syscall.SIGCLD.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for sigStr := range listOfSignals {
|
2020-02-10 14:25:42 -05:00
|
|
|
if signal, ok := SignalMap[sigStr]; ok {
|
|
|
|
syscall.Kill(syscall.Getpid(), signal)
|
2017-05-31 12:40:41 -04:00
|
|
|
s := <-sigs
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(s.String(), signal.String()))
|
2017-05-31 12:40:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 13:42:18 -04:00
|
|
|
func TestCatchAllIgnoreSigUrg(t *testing.T) {
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
CatchAll(sigs)
|
|
|
|
defer StopCatch(sigs)
|
|
|
|
|
|
|
|
err := syscall.Kill(syscall.Getpid(), syscall.SIGURG)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
timer := time.NewTimer(1 * time.Second)
|
|
|
|
defer timer.Stop()
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
case s := <-sigs:
|
|
|
|
t.Fatalf("expected no signals to be handled, but received %q", s.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 12:40:41 -04:00
|
|
|
func TestStopCatch(t *testing.T) {
|
2017-09-11 14:55:05 -04:00
|
|
|
signal := SignalMap["HUP"]
|
2017-05-31 12:40:41 -04:00
|
|
|
channel := make(chan os.Signal, 1)
|
|
|
|
CatchAll(channel)
|
2020-02-10 14:25:42 -05:00
|
|
|
syscall.Kill(syscall.Getpid(), signal)
|
2017-05-31 12:40:41 -04:00
|
|
|
signalString := <-channel
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(signalString.String(), signal.String()))
|
2017-05-31 12:40:41 -04:00
|
|
|
|
|
|
|
StopCatch(channel)
|
|
|
|
_, ok := <-channel
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(ok, false))
|
2017-05-31 12:40:41 -04:00
|
|
|
}
|