2018-01-25 10:10:21 +00:00
|
|
|
// Package atexit provides handling for functions you want called when
|
|
|
|
// the program exits unexpectedly due to a signal.
|
|
|
|
//
|
|
|
|
// You should also make sure you call Run in the normal exit path.
|
|
|
|
package atexit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
2020-11-27 10:49:24 +00:00
|
|
|
"sync/atomic"
|
2018-01-25 10:10:21 +00:00
|
|
|
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs"
|
2018-01-25 10:10:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-02-23 07:17:01 +00:00
|
|
|
fns = make(map[FnHandle]bool)
|
|
|
|
fnsMutex sync.Mutex
|
2018-05-12 09:40:44 +00:00
|
|
|
exitChan chan os.Signal
|
2018-01-25 10:10:21 +00:00
|
|
|
exitOnce sync.Once
|
|
|
|
registerOnce sync.Once
|
2020-11-27 10:49:24 +00:00
|
|
|
signalled int32
|
2018-01-25 10:10:21 +00:00
|
|
|
)
|
|
|
|
|
2019-02-23 07:17:01 +00:00
|
|
|
// FnHandle is the type of the handle returned by function `Register`
|
|
|
|
// that can be used to unregister an at-exit function
|
|
|
|
type FnHandle *func()
|
|
|
|
|
|
|
|
// Register a function to be called on exit.
|
|
|
|
// Returns a handle which can be used to unregister the function with `Unregister`.
|
|
|
|
func Register(fn func()) FnHandle {
|
|
|
|
fnsMutex.Lock()
|
|
|
|
fns[&fn] = true
|
|
|
|
fnsMutex.Unlock()
|
|
|
|
|
2019-02-27 22:49:22 +00:00
|
|
|
// Run AtExit handlers on exitSignals so everything gets tidied up properly
|
2018-01-25 10:10:21 +00:00
|
|
|
registerOnce.Do(func() {
|
2018-05-12 09:40:44 +00:00
|
|
|
exitChan = make(chan os.Signal, 1)
|
2019-02-27 22:49:22 +00:00
|
|
|
signal.Notify(exitChan, exitSignals...)
|
2018-01-25 10:10:21 +00:00
|
|
|
go func() {
|
2019-02-23 07:17:01 +00:00
|
|
|
sig := <-exitChan
|
|
|
|
if sig == nil {
|
2018-05-12 09:40:44 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-27 10:49:24 +00:00
|
|
|
atomic.StoreInt32(&signalled, 1)
|
2018-01-25 10:10:21 +00:00
|
|
|
fs.Infof(nil, "Signal received: %s", sig)
|
|
|
|
Run()
|
|
|
|
fs.Infof(nil, "Exiting...")
|
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
})
|
2019-02-23 07:17:01 +00:00
|
|
|
|
|
|
|
return &fn
|
|
|
|
}
|
|
|
|
|
2020-11-27 10:49:24 +00:00
|
|
|
// Signalled returns true if an exit signal has been received
|
|
|
|
func Signalled() bool {
|
|
|
|
return atomic.LoadInt32(&signalled) != 0
|
|
|
|
}
|
|
|
|
|
2019-02-23 07:17:01 +00:00
|
|
|
// Unregister a function using the handle returned by `Register`
|
|
|
|
func Unregister(handle FnHandle) {
|
|
|
|
fnsMutex.Lock()
|
|
|
|
defer fnsMutex.Unlock()
|
|
|
|
delete(fns, handle)
|
2018-01-25 10:10:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 10:39:20 +00:00
|
|
|
// IgnoreSignals disables the signal handler and prevents Run from being executed automatically
|
2018-05-12 09:40:44 +00:00
|
|
|
func IgnoreSignals() {
|
|
|
|
registerOnce.Do(func() {})
|
|
|
|
if exitChan != nil {
|
|
|
|
signal.Stop(exitChan)
|
|
|
|
close(exitChan)
|
|
|
|
exitChan = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 10:10:21 +00:00
|
|
|
// Run all the at exit functions if they haven't been run already
|
|
|
|
func Run() {
|
|
|
|
exitOnce.Do(func() {
|
2019-02-23 07:17:01 +00:00
|
|
|
fnsMutex.Lock()
|
|
|
|
defer fnsMutex.Unlock()
|
|
|
|
for fnHandle := range fns {
|
|
|
|
(*fnHandle)()
|
2018-01-25 10:10:21 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-06-04 11:02:48 +00:00
|
|
|
|
|
|
|
// OnError registers fn with atexit and returns a function which
|
|
|
|
// runs fn() if *perr != nil and deregisters fn
|
|
|
|
//
|
|
|
|
// It should be used in a defer statement normally so
|
|
|
|
//
|
|
|
|
// defer OnError(&err, cancelFunc)()
|
|
|
|
//
|
|
|
|
// So cancelFunc will be run if the function exits with an error or
|
|
|
|
// at exit.
|
|
|
|
func OnError(perr *error, fn func()) func() {
|
|
|
|
handle := Register(fn)
|
|
|
|
return func() {
|
|
|
|
defer Unregister(handle)
|
|
|
|
if *perr != nil {
|
|
|
|
fn()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|