1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
package shutdown
import (
"os"
"os/signal"
"sync"
"syscall"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
var (
stopped bool
sigChan chan os.Signal
cancelChan chan bool
handlers map[string]func() error
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 {
// Already running, do nothing.
return nil
}
sigChan = make(chan os.Signal, 1)
cancelChan = make(chan bool, 1)
stopped = false
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
select {
case <-cancelChan:
signal.Stop(sigChan)
close(sigChan)
close(cancelChan)
stopped = true
return
case sig := <-sigChan:
logrus.Infof("Received shutdown signal %v, terminating!", sig)
shutdownInhibit.Lock()
for name, handler := range handlers {
logrus.Infof("Invoking shutdown handler %s", name)
if err := handler(); err != nil {
logrus.Errorf("Error running shutdown handler %s: %v", name, err)
}
}
shutdownInhibit.Unlock()
return
}
}()
return nil
}
// Stop the shutdown signal handler.
func Stop() error {
if cancelChan == nil {
return errors.New("shutdown signal handler has not yet been started")
}
if stopped {
return nil
}
cancelChan <- true
return nil
}
// Temporarily inhibit signals from shutting down Libpod.
func Inhibit() {
shutdownInhibit.RLock()
}
// Stop inhibiting signals from shutting down Libpod.
func Uninhibit() {
shutdownInhibit.RUnlock()
}
// Register registers a function that will be executed when Podman is terminated
// by a signal.
func Register(name string, handler func() error) error {
if _, ok := handlers[name]; ok {
return errors.Errorf("handler with name %s already exists", name)
}
handlers[name] = handler
return nil
}
// Unregister un-registers a given shutdown handler.
func Unregister(name string) error {
if _, ok := handlers[name]; !ok {
return errors.Errorf("no handler with name %s found", name)
}
delete(handlers, name)
return nil
}
|