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 <david.calavera@gmail.com>
This commit is contained in:
parent
48e15d83d2
commit
cc9407ba0d
3 changed files with 31 additions and 5 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -9,8 +9,11 @@ import (
|
|||
// Signals used in api/client (no windows equivalent, use
|
||||
// invalid signals so they don't get handled)
|
||||
|
||||
const (
|
||||
// SIGCHLD is a signal sent to a process when a child process terminates, is interrupted, or resumes after being interrupted.
|
||||
const SIGCHLD = syscall.SIGCHLD
|
||||
|
||||
SIGCHLD = syscall.SIGCHLD
|
||||
// SIGWINCH is a signal sent to a process when its controlling terminal changes its size
|
||||
const SIGWINCH = syscall.SIGWINCH
|
||||
SIGWINCH = syscall.SIGWINCH
|
||||
// DefaultStopSignal is the syscall signal used to stop a container in unix systems.
|
||||
DefaultStopSignal = "SIGTERM"
|
||||
)
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue