restic/cmd/restic/cleanup.go

42 lines
1010 B
Go
Raw Permalink Normal View History

2015-07-19 17:50:55 +02:00
package main
import (
2024-03-29 23:58:48 +01:00
"context"
2015-07-19 17:50:55 +02:00
"os"
"os/signal"
"syscall"
2017-07-23 14:21:03 +02:00
"github.com/restic/restic/internal/debug"
2015-07-19 17:50:55 +02:00
)
2024-03-29 23:58:48 +01:00
func createGlobalContext() context.Context {
ctx, cancel := context.WithCancel(context.Background())
2015-07-19 17:50:55 +02:00
2024-03-29 23:58:48 +01:00
ch := make(chan os.Signal, 1)
go cleanupHandler(ch, cancel)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
2015-07-19 17:50:55 +02:00
2024-03-29 23:58:48 +01:00
return ctx
2015-07-19 17:50:55 +02:00
}
2024-03-29 23:58:48 +01:00
// cleanupHandler handles the SIGINT and SIGTERM signals.
func cleanupHandler(c <-chan os.Signal, cancel context.CancelFunc) {
s := <-c
debug.Log("signal %v received, cleaning up", s)
Warnf("%ssignal %v received, cleaning up\n", clearLine(0), s)
2015-07-19 17:57:18 +02:00
2024-03-29 23:58:48 +01: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")
2015-07-19 17:50:55 +02:00
}
2024-03-29 23:58:48 +01:00
cancel()
2015-07-19 17:50:55 +02:00
}
2024-03-29 23:58:48 +01:00
// Exit terminates the process with the given exit code.
func Exit(code int) {
debug.Log("exiting with status code %d", code)
os.Exit(code)
}