From 83406bc4737b82cba933a398fe6416dde045f985 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 27 Nov 2020 10:49:24 +0000 Subject: [PATCH] atexit: add Signalled() function - set if called from a signal #4804 --- lib/atexit/atexit.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/atexit/atexit.go b/lib/atexit/atexit.go index d52d50371..d0c3b08ad 100644 --- a/lib/atexit/atexit.go +++ b/lib/atexit/atexit.go @@ -8,6 +8,7 @@ import ( "os" "os/signal" "sync" + "sync/atomic" "github.com/rclone/rclone/fs" ) @@ -18,6 +19,7 @@ var ( exitChan chan os.Signal exitOnce sync.Once registerOnce sync.Once + signalled int32 ) // FnHandle is the type of the handle returned by function `Register` @@ -40,6 +42,7 @@ func Register(fn func()) FnHandle { if sig == nil { return } + atomic.StoreInt32(&signalled, 1) fs.Infof(nil, "Signal received: %s", sig) Run() fs.Infof(nil, "Exiting...") @@ -50,6 +53,11 @@ func Register(fn func()) FnHandle { return &fn } +// Signalled returns true if an exit signal has been received +func Signalled() bool { + return atomic.LoadInt32(&signalled) != 0 +} + // Unregister a function using the handle returned by `Register` func Unregister(handle FnHandle) { fnsMutex.Lock()