2018-01-25 11:10:21 +01: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 11:49:24 +01:00
|
|
|
"sync/atomic"
|
2018-01-25 11:10:21 +01:00
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
2018-01-25 11:10:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-02-23 08:17:01 +01:00
|
|
|
fns = make(map[FnHandle]bool)
|
|
|
|
fnsMutex sync.Mutex
|
2018-05-12 11:40:44 +02:00
|
|
|
exitChan chan os.Signal
|
2018-01-25 11:10:21 +01:00
|
|
|
exitOnce sync.Once
|
|
|
|
registerOnce sync.Once
|
2020-11-27 11:49:24 +01:00
|
|
|
signalled int32
|
2021-03-20 15:28:50 +01:00
|
|
|
runCalled int32
|
2018-01-25 11:10:21 +01:00
|
|
|
)
|
|
|
|
|
2019-02-23 08:17:01 +01: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 {
|
2021-03-20 15:28:50 +01:00
|
|
|
if running() {
|
|
|
|
return nil
|
|
|
|
}
|
2019-02-23 08:17:01 +01:00
|
|
|
fnsMutex.Lock()
|
|
|
|
fns[&fn] = true
|
|
|
|
fnsMutex.Unlock()
|
|
|
|
|
2019-02-27 23:49:22 +01:00
|
|
|
// Run AtExit handlers on exitSignals so everything gets tidied up properly
|
2018-01-25 11:10:21 +01:00
|
|
|
registerOnce.Do(func() {
|
2018-05-12 11:40:44 +02:00
|
|
|
exitChan = make(chan os.Signal, 1)
|
2019-02-27 23:49:22 +01:00
|
|
|
signal.Notify(exitChan, exitSignals...)
|
2018-01-25 11:10:21 +01:00
|
|
|
go func() {
|
2019-02-23 08:17:01 +01:00
|
|
|
sig := <-exitChan
|
|
|
|
if sig == nil {
|
2018-05-12 11:40:44 +02:00
|
|
|
return
|
|
|
|
}
|
2021-03-22 21:45:07 +01:00
|
|
|
signal.Stop(exitChan)
|
2020-11-27 11:49:24 +01:00
|
|
|
atomic.StoreInt32(&signalled, 1)
|
2018-01-25 11:10:21 +01:00
|
|
|
fs.Infof(nil, "Signal received: %s", sig)
|
|
|
|
Run()
|
|
|
|
fs.Infof(nil, "Exiting...")
|
2021-07-05 23:51:12 +02:00
|
|
|
os.Exit(exitCode(sig))
|
2018-01-25 11:10:21 +01:00
|
|
|
}()
|
|
|
|
})
|
2019-02-23 08:17:01 +01:00
|
|
|
|
|
|
|
return &fn
|
|
|
|
}
|
|
|
|
|
2020-11-27 11:49:24 +01:00
|
|
|
// Signalled returns true if an exit signal has been received
|
|
|
|
func Signalled() bool {
|
|
|
|
return atomic.LoadInt32(&signalled) != 0
|
|
|
|
}
|
|
|
|
|
2021-03-20 15:28:50 +01:00
|
|
|
// running returns true if run has been called
|
|
|
|
func running() bool {
|
|
|
|
return atomic.LoadInt32(&runCalled) != 0
|
|
|
|
}
|
|
|
|
|
2019-02-23 08:17:01 +01:00
|
|
|
// Unregister a function using the handle returned by `Register`
|
|
|
|
func Unregister(handle FnHandle) {
|
2021-03-20 15:28:50 +01:00
|
|
|
if running() {
|
|
|
|
return
|
|
|
|
}
|
2019-02-23 08:17:01 +01:00
|
|
|
fnsMutex.Lock()
|
|
|
|
defer fnsMutex.Unlock()
|
|
|
|
delete(fns, handle)
|
2018-01-25 11:10:21 +01:00
|
|
|
}
|
|
|
|
|
2020-05-20 12:39:20 +02:00
|
|
|
// IgnoreSignals disables the signal handler and prevents Run from being executed automatically
|
2018-05-12 11:40:44 +02:00
|
|
|
func IgnoreSignals() {
|
2021-03-20 15:28:50 +01:00
|
|
|
if running() {
|
|
|
|
return
|
|
|
|
}
|
2018-05-12 11:40:44 +02:00
|
|
|
registerOnce.Do(func() {})
|
|
|
|
if exitChan != nil {
|
|
|
|
signal.Stop(exitChan)
|
|
|
|
close(exitChan)
|
|
|
|
exitChan = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 11:10:21 +01:00
|
|
|
// Run all the at exit functions if they haven't been run already
|
|
|
|
func Run() {
|
2021-03-20 15:28:50 +01:00
|
|
|
atomic.StoreInt32(&runCalled, 1)
|
|
|
|
// Take the lock here (not inside the exitOnce) so we wait
|
|
|
|
// until the exit handlers have run before any calls to Run()
|
|
|
|
// return.
|
2021-01-25 18:03:57 +01:00
|
|
|
fnsMutex.Lock()
|
|
|
|
defer fnsMutex.Unlock()
|
2018-01-25 11:10:21 +01:00
|
|
|
exitOnce.Do(func() {
|
2019-02-23 08:17:01 +01:00
|
|
|
for fnHandle := range fns {
|
|
|
|
(*fnHandle)()
|
2018-01-25 11:10:21 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-06-04 13:02:48 +02: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
|
|
|
|
//
|
2022-08-05 17:35:41 +02:00
|
|
|
// defer OnError(&err, cancelFunc)()
|
2020-06-04 13:02:48 +02:00
|
|
|
//
|
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|