2016-08-11 18:39:23 +00:00
|
|
|
// Copyright (c) 2013-2016 The btcsuite developers
|
2015-08-26 04:03:18 +00:00
|
|
|
// Copyright (c) 2015-2016 The Decred developers
|
2013-08-06 21:55:22 +00:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2019-03-31 02:02:52 +00:00
|
|
|
"context"
|
2013-08-06 21:55:22 +00:00
|
|
|
"os"
|
|
|
|
|
"os/signal"
|
|
|
|
|
)
|
|
|
|
|
|
2016-08-31 14:45:16 +00:00
|
|
|
// shutdownRequestChannel is used to initiate shutdown from one of the
|
|
|
|
|
// subsystems using the same code paths as when an interrupt signal is received.
|
|
|
|
|
var shutdownRequestChannel = make(chan struct{})
|
2013-08-06 21:55:22 +00:00
|
|
|
|
2016-08-31 14:45:16 +00:00
|
|
|
// interruptSignals defines the default signals to catch in order to do a proper
|
|
|
|
|
// shutdown. This may be modified during init depending on the platform.
|
|
|
|
|
var interruptSignals = []os.Signal{os.Interrupt}
|
2013-10-03 14:28:18 +00:00
|
|
|
|
2019-03-31 02:02:52 +00:00
|
|
|
// shutdownListener listens for OS Signals such as SIGINT (Ctrl+C) and shutdown
|
|
|
|
|
// requests from shutdownRequestChannel. It returns a context that is canceled
|
2016-08-11 18:39:23 +00:00
|
|
|
// when either signal is received.
|
2019-03-31 02:02:52 +00:00
|
|
|
func shutdownListener() context.Context {
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2016-08-31 14:45:16 +00:00
|
|
|
go func() {
|
|
|
|
|
interruptChannel := make(chan os.Signal, 1)
|
|
|
|
|
signal.Notify(interruptChannel, interruptSignals...)
|
2013-10-03 14:28:18 +00:00
|
|
|
|
2019-03-31 02:02:52 +00:00
|
|
|
// Listen for initial shutdown signal and cancel the returned context.
|
2013-10-03 14:28:18 +00:00
|
|
|
select {
|
2016-05-06 13:11:35 +00:00
|
|
|
case sig := <-interruptChannel:
|
2019-03-31 02:02:52 +00:00
|
|
|
dcrdLog.Infof("Received signal (%s). Shutting down...", sig)
|
2014-07-22 13:21:58 +00:00
|
|
|
|
2016-08-31 14:45:16 +00:00
|
|
|
case <-shutdownRequestChannel:
|
|
|
|
|
dcrdLog.Infof("Shutdown requested. Shutting down...")
|
|
|
|
|
}
|
2019-03-31 02:02:52 +00:00
|
|
|
cancel()
|
2013-10-26 21:02:23 +00:00
|
|
|
|
2016-08-11 18:39:23 +00:00
|
|
|
// Listen for repeated signals and display a message so the user
|
|
|
|
|
// knows the shutdown is in progress and the process is not
|
|
|
|
|
// hung.
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case sig := <-interruptChannel:
|
2016-11-18 18:06:28 +00:00
|
|
|
dcrdLog.Infof("Received signal (%s). Already "+
|
2016-08-11 18:39:23 +00:00
|
|
|
"shutting down...", sig)
|
2013-10-03 14:28:18 +00:00
|
|
|
|
2016-08-11 18:39:23 +00:00
|
|
|
case <-shutdownRequestChannel:
|
2016-11-18 18:06:28 +00:00
|
|
|
dcrdLog.Info("Shutdown requested. Already " +
|
2016-08-11 18:39:23 +00:00
|
|
|
"shutting down...")
|
2014-07-22 13:21:58 +00:00
|
|
|
}
|
2013-10-03 14:28:18 +00:00
|
|
|
}
|
2016-08-31 14:45:16 +00:00
|
|
|
}()
|
2014-07-22 13:21:58 +00:00
|
|
|
|
2019-03-31 02:02:52 +00:00
|
|
|
return ctx
|
2013-10-03 14:28:18 +00:00
|
|
|
}
|
2013-08-06 21:55:22 +00:00
|
|
|
|
2019-03-31 02:02:52 +00:00
|
|
|
// shutdownRequested returns true when the context returned by shutdownListener
|
|
|
|
|
// was canceled. This simplifies early shutdown slightly since the caller can
|
|
|
|
|
// just use an if statement instead of a select.
|
|
|
|
|
func shutdownRequested(ctx context.Context) bool {
|
2016-08-31 14:45:16 +00:00
|
|
|
select {
|
2019-03-31 02:02:52 +00:00
|
|
|
case <-ctx.Done():
|
2016-08-31 14:45:16 +00:00
|
|
|
return true
|
|
|
|
|
default:
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
2013-10-03 14:28:18 +00:00
|
|
|
|
2016-08-11 18:39:23 +00:00
|
|
|
return false
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|