restic/cmd/restic/cleanup.go

87 lines
1.9 KiB
Go
Raw Normal View History

2015-07-19 17:50:55 +02:00
package main
import (
"fmt"
"os"
"os/signal"
"sync"
"syscall"
2017-07-23 14:21:03 +02:00
"github.com/restic/restic/internal/debug"
2015-07-19 17:50:55 +02:00
)
var cleanupHandlers struct {
sync.Mutex
list []func() error
done bool
ch chan os.Signal
2015-07-19 17:50:55 +02:00
}
var stderr = os.Stderr
func init() {
cleanupHandlers.ch = make(chan os.Signal)
go CleanupHandler(cleanupHandlers.ch)
2017-09-23 11:12:44 +02:00
InstallSignalHandler()
}
// InstallSignalHandler listens for SIGINT and SIGPIPE, and triggers the cleanup handlers.
2017-09-23 11:12:44 +02:00
func InstallSignalHandler() {
signal.Notify(cleanupHandlers.ch, syscall.SIGINT)
signal.Notify(cleanupHandlers.ch, syscall.SIGPIPE)
2017-09-23 11:12:44 +02:00
}
2015-07-19 17:50:55 +02:00
// SuspendSignalHandler removes the signal handler for SIGINT and SIGPIPE.
2017-09-23 11:12:44 +02:00
func SuspendSignalHandler() {
signal.Reset(syscall.SIGINT)
signal.Reset(syscall.SIGPIPE)
2015-07-19 17:50:55 +02: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.
func AddCleanupHandler(f func() error) {
cleanupHandlers.Lock()
defer cleanupHandlers.Unlock()
2016-09-15 21:17:20 +02:00
// reset the done flag for integration tests
cleanupHandlers.done = false
2015-07-19 17:50:55 +02:00
cleanupHandlers.list = append(cleanupHandlers.list, f)
}
// RunCleanupHandlers runs all registered cleanup handlers
func RunCleanupHandlers() {
cleanupHandlers.Lock()
defer cleanupHandlers.Unlock()
2015-07-19 17:57:18 +02:00
if cleanupHandlers.done {
return
}
cleanupHandlers.done = true
2015-07-19 17:50:55 +02:00
for _, f := range cleanupHandlers.list {
err := f()
if err != nil {
fmt.Fprintf(stderr, "error in cleanup handler: %v\n", err)
}
}
2016-09-15 21:17:20 +02:00
cleanupHandlers.list = nil
2015-07-19 17:50:55 +02:00
}
// CleanupHandler handles the SIGINT and SIGPIPE signals.
2015-07-19 17:50:55 +02:00
func CleanupHandler(c <-chan os.Signal) {
for s := range c {
2016-09-27 22:35:08 +02:00
debug.Log("signal %v received, cleaning up", s)
fmt.Printf("%sInterrupt received, cleaning up\n", ClearLine())
Exit(0)
2015-07-19 17:50:55 +02:00
}
}
// Exit runs the cleanup handlers and then terminates the process with the
// given exit code.
func Exit(code int) {
RunCleanupHandlers()
os.Exit(code)
}