2015-07-25 04:35:07 -04:00
|
|
|
// Package signal provides helper functions for dealing with signals across
|
|
|
|
// various operating systems.
|
2018-02-05 16:05:59 -05:00
|
|
|
package signal // import "github.com/docker/docker/pkg/signal"
|
2013-10-29 14:59:08 -04:00
|
|
|
|
|
|
|
import (
|
2015-08-04 16:51:48 -04:00
|
|
|
"fmt"
|
2013-10-29 14:59:08 -04:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2015-08-04 16:51:48 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
2013-10-29 14:59:08 -04:00
|
|
|
)
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// CatchAll catches all signals and relays them to the specified channel.
|
2021-05-25 06:07:53 -04:00
|
|
|
// SIGURG is not handled, as it's used by the Go runtime to support
|
2021-05-19 13:42:18 -04:00
|
|
|
// preemptable system calls.
|
2014-03-10 16:50:16 -04:00
|
|
|
func CatchAll(sigc chan os.Signal) {
|
2018-05-19 07:38:54 -04:00
|
|
|
var handledSigs []os.Signal
|
2021-05-25 06:07:53 -04:00
|
|
|
for n, s := range SignalMap {
|
|
|
|
if n == "URG" {
|
|
|
|
// Do not handle SIGURG, as in go1.14+, the go runtime issues
|
2021-05-19 13:42:18 -04:00
|
|
|
// SIGURG as an interrupt to support preemptable system calls on Linux.
|
|
|
|
continue
|
|
|
|
}
|
2014-03-10 16:50:16 -04:00
|
|
|
handledSigs = append(handledSigs, s)
|
|
|
|
}
|
|
|
|
signal.Notify(sigc, handledSigs...)
|
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// StopCatch stops catching the signals and closes the specified channel.
|
2013-10-29 14:59:08 -04:00
|
|
|
func StopCatch(sigc chan os.Signal) {
|
|
|
|
signal.Stop(sigc)
|
|
|
|
close(sigc)
|
|
|
|
}
|
2015-08-04 16:51:48 -04:00
|
|
|
|
|
|
|
// ParseSignal translates a string to a valid syscall signal.
|
|
|
|
// It returns an error if the signal map doesn't include the given signal.
|
|
|
|
func ParseSignal(rawSignal string) (syscall.Signal, error) {
|
|
|
|
s, err := strconv.Atoi(rawSignal)
|
|
|
|
if err == nil {
|
|
|
|
if s == 0 {
|
|
|
|
return -1, fmt.Errorf("Invalid signal: %s", rawSignal)
|
|
|
|
}
|
|
|
|
return syscall.Signal(s), nil
|
|
|
|
}
|
|
|
|
signal, ok := SignalMap[strings.TrimPrefix(strings.ToUpper(rawSignal), "SIG")]
|
|
|
|
if !ok {
|
|
|
|
return -1, fmt.Errorf("Invalid signal: %s", rawSignal)
|
|
|
|
}
|
|
|
|
return signal, nil
|
|
|
|
}
|
2015-10-12 19:34:03 -04:00
|
|
|
|
|
|
|
// ValidSignalForPlatform returns true if a signal is valid on the platform
|
|
|
|
func ValidSignalForPlatform(sig syscall.Signal) bool {
|
|
|
|
for _, v := range SignalMap {
|
|
|
|
if v == sig {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|