From cc9407ba0d2fe7fab03de3db5ec697fa46f035e6 Mon Sep 17 00:00:00 2001 From: David Calavera Date: Tue, 4 Aug 2015 13:51:48 -0700 Subject: [PATCH] Signal to stop a container. Allow to set the signal to stop a container in `docker run`: - Use `--stop-signal` with docker-run to set the default signal the container will use to exit. Signed-off-by: David Calavera --- signal/signal.go | 21 +++++++++++++++++++++ signal/signal_unix.go | 13 ++++++++----- signal/signal_windows.go | 2 ++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/signal/signal.go b/signal/signal.go index db60bf2..106fe20 100644 --- a/signal/signal.go +++ b/signal/signal.go @@ -3,8 +3,12 @@ package signal import ( + "fmt" "os" "os/signal" + "strconv" + "strings" + "syscall" ) // CatchAll catches all signals and relays them to the specified channel. @@ -21,3 +25,20 @@ func StopCatch(sigc chan os.Signal) { signal.Stop(sigc) close(sigc) } + +// 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 +} diff --git a/signal/signal_unix.go b/signal/signal_unix.go index 5c1ad5f..d4fea93 100644 --- a/signal/signal_unix.go +++ b/signal/signal_unix.go @@ -9,8 +9,11 @@ import ( // Signals used in api/client (no windows equivalent, use // invalid signals so they don't get handled) -// SIGCHLD is a signal sent to a process when a child process terminates, is interrupted, or resumes after being interrupted. -const SIGCHLD = syscall.SIGCHLD - -// SIGWINCH is a signal sent to a process when its controlling terminal changes its size -const SIGWINCH = syscall.SIGWINCH +const ( + // SIGCHLD is a signal sent to a process when a child process terminates, is interrupted, or resumes after being interrupted. + SIGCHLD = syscall.SIGCHLD + // SIGWINCH is a signal sent to a process when its controlling terminal changes its size + SIGWINCH = syscall.SIGWINCH + // DefaultStopSignal is the syscall signal used to stop a container in unix systems. + DefaultStopSignal = "SIGTERM" +) diff --git a/signal/signal_windows.go b/signal/signal_windows.go index 1f1a6ed..b0585b0 100644 --- a/signal/signal_windows.go +++ b/signal/signal_windows.go @@ -11,4 +11,6 @@ import ( const ( SIGCHLD = syscall.Signal(0xff) SIGWINCH = syscall.Signal(0xff) + // DefaultStopSignal is the syscall signal used to stop a container in windows systems. + DefaultStopSignal = "15" )