2015-07-19 15:50:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
2015-07-19 15:50:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var cleanupHandlers struct {
|
|
|
|
sync.Mutex
|
2022-08-26 21:04:59 +00:00
|
|
|
list []func(code int) (int, error)
|
2017-11-05 12:14:27 +00:00
|
|
|
done bool
|
|
|
|
ch chan os.Signal
|
2015-07-19 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2020-03-12 18:55:46 +00:00
|
|
|
cleanupHandlers.ch = make(chan os.Signal, 1)
|
2017-11-05 12:14:27 +00:00
|
|
|
go CleanupHandler(cleanupHandlers.ch)
|
2024-02-19 10:09:25 +00:00
|
|
|
signal.Notify(cleanupHandlers.ch, syscall.SIGINT, syscall.SIGTERM)
|
2017-09-23 09:12:44 +00:00
|
|
|
}
|
2015-07-19 15:50:55 +00:00
|
|
|
|
|
|
|
// AddCleanupHandler adds the function f to the list of cleanup handlers so
|
|
|
|
// that it is executed when all the cleanup handlers are run, e.g. when SIGINT
|
|
|
|
// is received.
|
2022-08-26 21:04:59 +00:00
|
|
|
func AddCleanupHandler(f func(code int) (int, error)) {
|
2015-07-19 15:50:55 +00:00
|
|
|
cleanupHandlers.Lock()
|
|
|
|
defer cleanupHandlers.Unlock()
|
|
|
|
|
2016-09-15 19:17:20 +00:00
|
|
|
// reset the done flag for integration tests
|
|
|
|
cleanupHandlers.done = false
|
|
|
|
|
2015-07-19 15:50:55 +00:00
|
|
|
cleanupHandlers.list = append(cleanupHandlers.list, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunCleanupHandlers runs all registered cleanup handlers
|
2022-08-26 21:04:59 +00:00
|
|
|
func RunCleanupHandlers(code int) int {
|
2015-07-19 15:50:55 +00:00
|
|
|
cleanupHandlers.Lock()
|
|
|
|
defer cleanupHandlers.Unlock()
|
|
|
|
|
2015-07-19 15:57:18 +00:00
|
|
|
if cleanupHandlers.done {
|
2022-08-26 21:04:59 +00:00
|
|
|
return code
|
2015-07-19 15:57:18 +00:00
|
|
|
}
|
|
|
|
cleanupHandlers.done = true
|
|
|
|
|
2015-07-19 15:50:55 +00:00
|
|
|
for _, f := range cleanupHandlers.list {
|
2022-08-26 21:04:59 +00:00
|
|
|
var err error
|
|
|
|
code, err = f(code)
|
2015-07-19 15:50:55 +00:00
|
|
|
if err != nil {
|
2020-04-04 17:41:24 +00:00
|
|
|
Warnf("error in cleanup handler: %v\n", err)
|
2015-07-19 15:50:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-15 19:17:20 +00:00
|
|
|
cleanupHandlers.list = nil
|
2022-08-26 21:04:59 +00:00
|
|
|
return code
|
2015-07-19 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
2024-02-22 21:00:42 +00:00
|
|
|
// CleanupHandler handles the SIGINT and SIGTERM signals.
|
2015-07-19 15:50:55 +00:00
|
|
|
func CleanupHandler(c <-chan os.Signal) {
|
|
|
|
for s := range c {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("signal %v received, cleaning up", s)
|
2021-08-29 12:55:33 +00:00
|
|
|
Warnf("%ssignal %v received, cleaning up\n", clearLine(0), s)
|
2017-11-09 12:16:01 +00:00
|
|
|
|
2023-04-23 12:56:36 +00:00
|
|
|
if val, _ := os.LookupEnv("RESTIC_DEBUG_STACKTRACE_SIGINT"); val != "" {
|
|
|
|
_, _ = os.Stderr.WriteString("\n--- STACKTRACE START ---\n\n")
|
|
|
|
_, _ = os.Stderr.WriteString(debug.DumpStacktrace())
|
|
|
|
_, _ = os.Stderr.WriteString("\n--- STACKTRACE END ---\n")
|
|
|
|
}
|
|
|
|
|
2017-11-09 12:16:01 +00:00
|
|
|
code := 0
|
2018-04-16 15:29:09 +00:00
|
|
|
|
2024-02-19 10:09:25 +00:00
|
|
|
if s == syscall.SIGINT || s == syscall.SIGTERM {
|
2018-04-16 15:29:09 +00:00
|
|
|
code = 130
|
|
|
|
} else {
|
2017-11-09 12:16:01 +00:00
|
|
|
code = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
Exit(code)
|
2015-07-19 15:50:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-28 09:53:31 +00:00
|
|
|
|
|
|
|
// Exit runs the cleanup handlers and then terminates the process with the
|
|
|
|
// given exit code.
|
|
|
|
func Exit(code int) {
|
2022-08-26 21:04:59 +00:00
|
|
|
code = RunCleanupHandlers(code)
|
2023-04-12 19:58:29 +00:00
|
|
|
debug.Log("exiting with status code %d", code)
|
2016-12-28 09:53:31 +00:00
|
|
|
os.Exit(code)
|
|
|
|
}
|