2015-05-01 08:28:01 +02:00
|
|
|
// Copyright (c) 2013-2014 The btcsuite developers
|
2013-08-06 23:55:22 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
)
|
|
|
|
|
|
|
|
// interruptChannel is used to receive SIGINT (Ctrl+C) signals.
|
|
|
|
var interruptChannel chan os.Signal
|
|
|
|
|
2013-10-03 16:28:18 +02:00
|
|
|
// addHandlerChannel is used to add an interrupt handler to the list of handlers
|
|
|
|
// to be invoked on SIGINT (Ctrl+C) signals.
|
|
|
|
var addHandlerChannel = make(chan func())
|
|
|
|
|
2016-05-05 21:16:42 +02:00
|
|
|
// signals defines the default signals to catch in order to do a proper
|
|
|
|
// shutdown.
|
|
|
|
var signals = []os.Signal{os.Interrupt}
|
|
|
|
|
2013-10-03 16:28:18 +02:00
|
|
|
// mainInterruptHandler listens for SIGINT (Ctrl+C) signals on the
|
|
|
|
// interruptChannel and invokes the registered interruptCallbacks accordingly.
|
|
|
|
// It also listens for callback registration. It must be run as a goroutine.
|
|
|
|
func mainInterruptHandler() {
|
|
|
|
// interruptCallbacks is a list of callbacks to invoke when a
|
|
|
|
// SIGINT (Ctrl+C) is received.
|
|
|
|
var interruptCallbacks []func()
|
|
|
|
|
2014-07-22 15:21:58 +02:00
|
|
|
// 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
|
|
|
|
|
2013-10-03 16:28:18 +02:00
|
|
|
for {
|
|
|
|
select {
|
2016-05-05 21:16:42 +02:00
|
|
|
case sig := <-interruptChannel:
|
2014-07-22 15:21:58 +02:00
|
|
|
// Ignore more than one shutdown signal.
|
|
|
|
if isShutdown {
|
2016-05-05 21:16:42 +02:00
|
|
|
btcdLog.Infof("Received signal (%s). "+
|
|
|
|
"Already shutting down...", sig)
|
2014-07-22 15:21:58 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
isShutdown = true
|
2016-05-05 21:16:42 +02:00
|
|
|
btcdLog.Infof("Received signal (%s). Shutting down...",
|
|
|
|
sig)
|
2014-07-22 15:21:58 +02:00
|
|
|
|
|
|
|
// Run handlers in LIFO order.
|
2013-12-11 01:41:03 +01:00
|
|
|
for i := range interruptCallbacks {
|
|
|
|
idx := len(interruptCallbacks) - 1 - i
|
2013-12-10 20:46:32 +01:00
|
|
|
callback := interruptCallbacks[idx]
|
2013-10-03 16:28:18 +02:00
|
|
|
callback()
|
|
|
|
}
|
2013-10-26 23:02:23 +02:00
|
|
|
|
|
|
|
// Signal the main goroutine to shutdown.
|
2014-07-22 15:21:58 +02:00
|
|
|
go func() {
|
|
|
|
shutdownChannel <- struct{}{}
|
|
|
|
}()
|
2013-10-03 16:28:18 +02:00
|
|
|
|
|
|
|
case handler := <-addHandlerChannel:
|
2014-07-22 15:21:58 +02:00
|
|
|
// The shutdown signal has already been received, so
|
|
|
|
// just invoke and new handlers immediately.
|
|
|
|
if isShutdown {
|
|
|
|
handler()
|
|
|
|
}
|
|
|
|
|
2013-10-03 16:28:18 +02:00
|
|
|
interruptCallbacks = append(interruptCallbacks, handler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
|
|
|
|
// addInterruptHandler adds a handler to call when a SIGINT (Ctrl+C) is
|
|
|
|
// received.
|
|
|
|
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)
|
2016-05-05 21:16:42 +02:00
|
|
|
signal.Notify(interruptChannel, signals...)
|
2013-10-03 16:28:18 +02:00
|
|
|
go mainInterruptHandler()
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
2013-10-03 16:28:18 +02:00
|
|
|
|
|
|
|
addHandlerChannel <- handler
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|