2017-11-28 03:12:09 +03:00
|
|
|
// Copyright (c) 2013-2017 The btcsuite developers
|
|
|
|
// Copyright (c) 2015-2016 The Decred developers
|
2016-03-23 04:42:01 +03:00
|
|
|
// Heavily inspired by https://github.com/btcsuite/btcd/blob/master/signal.go
|
2017-11-28 03:12:09 +03:00
|
|
|
// Copyright (C) 2015-2017 The Lightning Network Developers
|
|
|
|
|
|
|
|
package main
|
2016-03-23 04:42:01 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
)
|
|
|
|
|
|
|
|
// interruptChannel is used to receive SIGINT (Ctrl+C) signals.
|
|
|
|
var interruptChannel chan os.Signal
|
|
|
|
|
2017-05-12 00:55:56 +03:00
|
|
|
// shutdownRequestChannel is used to request the daemon to shutdown gracefully,
|
2018-02-07 06:11:11 +03:00
|
|
|
// similar to when receiving SIGINT.
|
2017-05-12 00:55:56 +03:00
|
|
|
var shutdownRequestChannel = make(chan struct{})
|
|
|
|
|
2016-03-23 04:42:01 +03:00
|
|
|
// addHandlerChannel is used to add an interrupt handler to the list of handlers
|
2017-05-12 00:55:56 +03:00
|
|
|
// to be invoked on SIGINT (Ctrl+C) signals and shutdown.
|
2016-03-23 04:42:01 +03:00
|
|
|
var addHandlerChannel = make(chan func())
|
|
|
|
|
|
|
|
// mainInterruptHandler listens for SIGINT (Ctrl+C) signals on the
|
2017-05-12 00:55:56 +03:00
|
|
|
// interruptChannel and shutdown requests on the shutdownRequestChannel, and
|
|
|
|
// invokes the registered interruptCallbacks accordingly. It also listens for
|
|
|
|
// callback registration.
|
|
|
|
// It must be run as a goroutine.
|
2016-03-23 04:42:01 +03:00
|
|
|
func mainInterruptHandler() {
|
|
|
|
// interruptCallbacks is a list of callbacks to invoke when a
|
2017-05-12 00:55:56 +03:00
|
|
|
// SIGINT (Ctrl+C) or a shutdown request is received.
|
2016-03-23 04:42:01 +03:00
|
|
|
var interruptCallbacks []func()
|
|
|
|
|
|
|
|
// isShutdown is a flag which is used to indicate whether or not
|
|
|
|
// the shutdown signal has already been received and hence any future
|
|
|
|
// attempts to add a new interrupt handler should invoke them
|
|
|
|
// immediately.
|
|
|
|
var isShutdown bool
|
|
|
|
|
2017-05-12 00:55:56 +03:00
|
|
|
// shutdown invokes the registered interrupt handlers, then signals the
|
|
|
|
// shutdownChannel.
|
|
|
|
shutdown := func() {
|
|
|
|
// Ignore more than one shutdown signal.
|
|
|
|
if isShutdown {
|
|
|
|
ltndLog.Infof("Already shutting down...")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
isShutdown = true
|
|
|
|
ltndLog.Infof("Shutting down...")
|
|
|
|
|
2018-05-23 09:55:30 +03:00
|
|
|
// Execute the interrupt callbacks in FIFO order.
|
|
|
|
for _, callback := range interruptCallbacks {
|
2017-05-12 00:55:56 +03:00
|
|
|
callback()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signal the main goroutine to shutdown.
|
|
|
|
go func() {
|
|
|
|
shutdownChannel <- struct{}{}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:42:01 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-interruptChannel:
|
2017-05-12 00:55:56 +03:00
|
|
|
ltndLog.Infof("Received SIGINT (Ctrl+C).")
|
|
|
|
shutdown()
|
2016-03-23 04:42:01 +03:00
|
|
|
|
2017-05-12 00:55:56 +03:00
|
|
|
case <-shutdownRequestChannel:
|
|
|
|
ltndLog.Infof("Received shutdown request.")
|
|
|
|
shutdown()
|
2016-03-23 04:42:01 +03:00
|
|
|
|
|
|
|
case handler := <-addHandlerChannel:
|
|
|
|
// The shutdown signal has already been received, so
|
2017-05-12 00:55:56 +03:00
|
|
|
// just invoke any new handlers immediately.
|
2016-03-23 04:42:01 +03:00
|
|
|
if isShutdown {
|
|
|
|
handler()
|
|
|
|
}
|
|
|
|
|
|
|
|
interruptCallbacks = append(interruptCallbacks, handler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-12 00:55:56 +03:00
|
|
|
// addInterruptHandler adds a handler to call when a SIGINT (Ctrl+C) or a
|
|
|
|
// shutdown request is received.
|
2016-03-23 04:42:01 +03:00
|
|
|
func addInterruptHandler(handler func()) {
|
|
|
|
// Create the channel and start the main interrupt handler which invokes
|
|
|
|
// all other callbacks and exits if not already done.
|
|
|
|
if interruptChannel == nil {
|
|
|
|
interruptChannel = make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interruptChannel, os.Interrupt)
|
|
|
|
go mainInterruptHandler()
|
|
|
|
}
|
|
|
|
|
|
|
|
addHandlerChannel <- handler
|
|
|
|
}
|