aboutsummaryrefslogtreecommitdiff
path: root/libpod/shutdown/handler.go
diff options
context:
space:
mode:
authorMatthew Heon <mheon@redhat.com>2020-10-13 14:00:35 -0400
committerMatthew Heon <mheon@redhat.com>2020-10-13 14:21:19 -0400
commitf58d2f5e75b4982774509847c18b39f4a50fd5be (patch)
tree53096a2bed60f6805ac0b25dcb297b7a0bd0078e /libpod/shutdown/handler.go
parent83e6e4ccdd925fa25500cff9e4b631b2c5d157cb (diff)
downloadpodman-f58d2f5e75b4982774509847c18b39f4a50fd5be.tar.gz
podman-f58d2f5e75b4982774509847c18b39f4a50fd5be.tar.bz2
podman-f58d2f5e75b4982774509847c18b39f4a50fd5be.zip
Enforce LIFO ordering for shutdown handlers
This allows us to run both the Libpod and Server handlers at the same time without unregistering one. Also, pass the signal that killed us into the handlers, in case they want to use it to determine what to do (e.g. what exit code to set). Signed-off-by: Matthew Heon <mheon@redhat.com>
Diffstat (limited to 'libpod/shutdown/handler.go')
-rw-r--r--libpod/shutdown/handler.go42
1 files changed, 30 insertions, 12 deletions
diff --git a/libpod/shutdown/handler.go b/libpod/shutdown/handler.go
index 7abaf065b..87538dec9 100644
--- a/libpod/shutdown/handler.go
+++ b/libpod/shutdown/handler.go
@@ -11,17 +11,20 @@ import (
)
var (
- stopped bool
- sigChan chan os.Signal
- cancelChan chan bool
- handlers map[string]func() error
+ stopped bool
+ sigChan chan os.Signal
+ cancelChan chan bool
+ // Definitions of all on-shutdown handlers
+ handlers map[string]func(os.Signal) error
+ // Ordering that on-shutdown handlers will be invoked.
+ handlerOrder []string
shutdownInhibit sync.RWMutex
)
// Start begins handling SIGTERM and SIGINT and will run the given on-signal
// handlers when one is called. This can be cancelled by calling Stop().
func Start() error {
- if sigChan != nil && !stopped {
+ if sigChan != nil {
// Already running, do nothing.
return nil
}
@@ -43,9 +46,14 @@ func Start() error {
case sig := <-sigChan:
logrus.Infof("Received shutdown signal %v, terminating!", sig)
shutdownInhibit.Lock()
- for name, handler := range handlers {
+ for _, name := range handlerOrder {
+ handler, ok := handlers[name]
+ if !ok {
+ logrus.Errorf("Shutdown handler %s definition not found!", name)
+ continue
+ }
logrus.Infof("Invoking shutdown handler %s", name)
- if err := handler(); err != nil {
+ if err := handler(sig); err != nil {
logrus.Errorf("Error running shutdown handler %s: %v", name, err)
}
}
@@ -82,10 +90,11 @@ func Uninhibit() {
}
// Register registers a function that will be executed when Podman is terminated
-// by a signal.
-func Register(name string, handler func() error) error {
+// by a signal. Handlers are invoked LIFO - the last handler registered is the
+// first run.
+func Register(name string, handler func(os.Signal) error) error {
if handlers == nil {
- handlers = make(map[string]func() error)
+ handlers = make(map[string]func(os.Signal) error)
}
if _, ok := handlers[name]; ok {
@@ -93,6 +102,7 @@ func Register(name string, handler func() error) error {
}
handlers[name] = handler
+ handlerOrder = append([]string{name}, handlerOrder...)
return nil
}
@@ -100,14 +110,22 @@ func Register(name string, handler func() error) error {
// Unregister un-registers a given shutdown handler.
func Unregister(name string) error {
if handlers == nil {
- handlers = make(map[string]func() error)
+ return nil
}
if _, ok := handlers[name]; !ok {
- return errors.Errorf("no handler with name %s found", name)
+ return nil
}
delete(handlers, name)
+ newOrder := []string{}
+ for _, checkName := range handlerOrder {
+ if checkName != name {
+ newOrder = append(newOrder, checkName)
+ }
+ }
+ handlerOrder = newOrder
+
return nil
}