2018-06-15 05:15:19 +02:00
|
|
|
// Copyright (c) 2013-2017 The btcsuite developers
|
|
|
|
// Copyright (c) 2015-2016 The Decred developers
|
|
|
|
// Heavily inspired by https://github.com/btcsuite/btcd/blob/master/signal.go
|
|
|
|
// Copyright (C) 2015-2017 The Lightning Network Developers
|
|
|
|
|
|
|
|
package signal
|
|
|
|
|
|
|
|
import (
|
2020-08-24 08:54:34 +02:00
|
|
|
"errors"
|
2021-07-17 13:54:41 +02:00
|
|
|
"fmt"
|
2018-06-15 05:15:19 +02:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2020-08-24 08:54:34 +02:00
|
|
|
"sync/atomic"
|
2019-03-27 02:15:07 +01:00
|
|
|
"syscall"
|
2021-07-17 13:54:41 +02:00
|
|
|
|
|
|
|
"github.com/coreos/go-systemd/daemon"
|
2018-06-15 05:15:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-08-24 08:54:34 +02:00
|
|
|
// started indicates whether we have started our main interrupt handler.
|
|
|
|
// This field should be used atomically.
|
|
|
|
started int32
|
2020-08-28 11:18:15 +02:00
|
|
|
)
|
2020-08-24 08:54:34 +02:00
|
|
|
|
2021-07-17 13:54:41 +02:00
|
|
|
// systemdNotifyReady notifies systemd about LND being ready, logs the result of
|
|
|
|
// the operation or possible error. Besides logging, systemd being unavailable
|
|
|
|
// is ignored.
|
|
|
|
func systemdNotifyReady() error {
|
|
|
|
notified, err := daemon.SdNotify(false, daemon.SdNotifyReady)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("failed to notify systemd %v (if you aren't "+
|
|
|
|
"running systemd clear the environment variable "+
|
|
|
|
"NOTIFY_SOCKET)", err)
|
|
|
|
log.Error(err)
|
|
|
|
|
|
|
|
// The SdNotify doc says it's common to ignore the
|
|
|
|
// error. We don't want to ignore it because if someone
|
|
|
|
// set up systemd to wait for initialization other
|
|
|
|
// processes would get stuck.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if notified {
|
|
|
|
log.Info("Systemd was notified about our readiness")
|
|
|
|
} else {
|
|
|
|
log.Info("We're not running within systemd")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// systemdNotifyStop notifies systemd that LND is stopping and logs error if
|
|
|
|
// the notification failed. It also logs if the notification was actually sent.
|
|
|
|
// Systemd being unavailable is intentionally ignored.
|
|
|
|
func systemdNotifyStop() {
|
|
|
|
notified, err := daemon.SdNotify(false, daemon.SdNotifyStopping)
|
|
|
|
|
|
|
|
// Just log - we're stopping anyway.
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to notify systemd: %v", err)
|
|
|
|
}
|
|
|
|
if notified {
|
|
|
|
log.Infof("Systemd was notified about stopping")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notifier handles notifications about status of LND.
|
|
|
|
type Notifier struct {
|
|
|
|
// notifiedReady remembers whether Ready was sent to avoid sending it
|
|
|
|
// multiple times.
|
|
|
|
notifiedReady bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyReady notifies other applications that RPC is ready.
|
|
|
|
func (notifier *Notifier) NotifyReady(walletUnlocked bool) error {
|
|
|
|
if !notifier.notifiedReady {
|
|
|
|
err := systemdNotifyReady()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
notifier.notifiedReady = true
|
|
|
|
}
|
|
|
|
if walletUnlocked {
|
|
|
|
_, _ = daemon.SdNotify(false, "STATUS=Wallet unlocked")
|
|
|
|
} else {
|
|
|
|
_, _ = daemon.SdNotify(false, "STATUS=Wallet locked")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// notifyStop notifies other applications that LND is stopping.
|
|
|
|
func (notifier *Notifier) notifyStop() {
|
|
|
|
systemdNotifyStop()
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:18:15 +02:00
|
|
|
// Interceptor contains channels and methods regarding application shutdown
|
2021-07-17 13:54:41 +02:00
|
|
|
// and interrupt signals.
|
2020-08-28 11:18:15 +02:00
|
|
|
type Interceptor struct {
|
|
|
|
// interruptChannel is used to receive SIGINT (Ctrl+C) signals.
|
|
|
|
interruptChannel chan os.Signal
|
2018-06-15 05:15:19 +02:00
|
|
|
|
|
|
|
// shutdownChannel is closed once the main interrupt handler exits.
|
2020-08-28 11:18:15 +02:00
|
|
|
shutdownChannel chan struct{}
|
2018-06-15 05:15:19 +02:00
|
|
|
|
2020-08-28 11:18:15 +02:00
|
|
|
// shutdownRequestChannel is used to request the daemon to shutdown
|
|
|
|
// gracefully, similar to when receiving SIGINT.
|
|
|
|
shutdownRequestChannel chan struct{}
|
|
|
|
|
|
|
|
// quit is closed when instructing the main interrupt handler to exit.
|
2021-07-17 13:54:41 +02:00
|
|
|
// Note that to avoid losing notifications, only shutdown func may
|
|
|
|
// close this channel.
|
2020-08-28 11:18:15 +02:00
|
|
|
quit chan struct{}
|
2021-07-17 13:54:41 +02:00
|
|
|
|
|
|
|
// Notifier handles sending shutdown notifications.
|
|
|
|
Notifier Notifier
|
2020-08-28 11:18:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Intercept starts the interception of interrupt signals and returns an `Interceptor` instance.
|
2021-07-17 13:54:41 +02:00
|
|
|
// Note that any previous active interceptor must be stopped before a new one can be created.
|
2020-08-28 11:18:15 +02:00
|
|
|
func Intercept() (Interceptor, error) {
|
2020-08-24 08:54:34 +02:00
|
|
|
if !atomic.CompareAndSwapInt32(&started, 0, 1) {
|
2020-08-28 11:18:15 +02:00
|
|
|
return Interceptor{}, errors.New("intercept already started")
|
|
|
|
}
|
|
|
|
|
|
|
|
channels := Interceptor{
|
|
|
|
interruptChannel: make(chan os.Signal, 1),
|
|
|
|
shutdownChannel: make(chan struct{}),
|
|
|
|
shutdownRequestChannel: make(chan struct{}),
|
|
|
|
quit: make(chan struct{}),
|
2020-08-24 08:54:34 +02:00
|
|
|
}
|
|
|
|
|
2019-03-27 02:15:07 +01:00
|
|
|
signalsToCatch := []os.Signal{
|
|
|
|
os.Interrupt,
|
|
|
|
os.Kill,
|
|
|
|
syscall.SIGTERM,
|
|
|
|
syscall.SIGQUIT,
|
|
|
|
}
|
2020-08-28 11:18:15 +02:00
|
|
|
signal.Notify(channels.interruptChannel, signalsToCatch...)
|
|
|
|
go channels.mainInterruptHandler()
|
2020-08-24 08:54:34 +02:00
|
|
|
|
2020-08-28 11:18:15 +02:00
|
|
|
return channels, nil
|
2018-06-15 05:15:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// mainInterruptHandler listens for SIGINT (Ctrl+C) signals on the
|
|
|
|
// 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.
|
2020-08-28 11:18:15 +02:00
|
|
|
func (c *Interceptor) mainInterruptHandler() {
|
|
|
|
defer atomic.StoreInt32(&started, 0)
|
2018-06-15 05:15:19 +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
|
|
|
|
|
|
|
|
// shutdown invokes the registered interrupt handlers, then signals the
|
|
|
|
// shutdownChannel.
|
|
|
|
shutdown := func() {
|
|
|
|
// Ignore more than one shutdown signal.
|
|
|
|
if isShutdown {
|
|
|
|
log.Infof("Already shutting down...")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
isShutdown = true
|
|
|
|
log.Infof("Shutting down...")
|
2021-07-17 13:54:41 +02:00
|
|
|
c.Notifier.notifyStop()
|
2018-06-15 05:15:19 +02:00
|
|
|
|
|
|
|
// Signal the main interrupt handler to exit, and stop accept
|
|
|
|
// post-facto requests.
|
2020-08-28 11:18:15 +02:00
|
|
|
close(c.quit)
|
2018-06-15 05:15:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2020-08-28 11:18:15 +02:00
|
|
|
case signal := <-c.interruptChannel:
|
2019-03-27 02:15:07 +01:00
|
|
|
log.Infof("Received %v", signal)
|
2018-06-15 05:15:19 +02:00
|
|
|
shutdown()
|
|
|
|
|
2020-08-28 11:18:15 +02:00
|
|
|
case <-c.shutdownRequestChannel:
|
2018-06-15 05:15:19 +02:00
|
|
|
log.Infof("Received shutdown request.")
|
|
|
|
shutdown()
|
|
|
|
|
2020-08-28 11:18:15 +02:00
|
|
|
case <-c.quit:
|
2018-06-15 05:15:19 +02:00
|
|
|
log.Infof("Gracefully shutting down.")
|
2020-08-28 11:18:15 +02:00
|
|
|
close(c.shutdownChannel)
|
|
|
|
signal.Stop(c.interruptChannel)
|
2018-06-15 05:15:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 08:54:34 +02:00
|
|
|
// Listening returns true if the main interrupt handler has been started, and
|
|
|
|
// has not been killed.
|
2020-08-28 11:18:15 +02:00
|
|
|
func (c *Interceptor) Listening() bool {
|
2020-08-24 08:54:34 +02:00
|
|
|
// If our started field is not set, we are not yet listening for
|
|
|
|
// interrupts.
|
|
|
|
if atomic.LoadInt32(&started) != 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have started our main goroutine, we check whether we have
|
|
|
|
// stopped it yet.
|
2020-08-28 11:18:15 +02:00
|
|
|
return c.Alive()
|
2020-08-24 08:54:34 +02:00
|
|
|
}
|
|
|
|
|
2018-06-15 05:15:19 +02:00
|
|
|
// Alive returns true if the main interrupt handler has not been killed.
|
2020-08-28 11:18:15 +02:00
|
|
|
func (c *Interceptor) Alive() bool {
|
2018-06-15 05:15:19 +02:00
|
|
|
select {
|
2020-08-28 11:18:15 +02:00
|
|
|
case <-c.quit:
|
2018-06-15 05:15:19 +02:00
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequestShutdown initiates a graceful shutdown from the application.
|
2020-08-28 11:18:15 +02:00
|
|
|
func (c *Interceptor) RequestShutdown() {
|
2018-06-15 05:15:19 +02:00
|
|
|
select {
|
2020-08-28 11:18:15 +02:00
|
|
|
case c.shutdownRequestChannel <- struct{}{}:
|
|
|
|
case <-c.quit:
|
2018-06-15 05:15:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShutdownChannel returns the channel that will be closed once the main
|
|
|
|
// interrupt handler has exited.
|
2020-08-28 11:18:15 +02:00
|
|
|
func (c *Interceptor) ShutdownChannel() <-chan struct{} {
|
|
|
|
return c.shutdownChannel
|
2018-06-15 05:15:19 +02:00
|
|
|
}
|