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