vspd: Support SIGTERM on Win and all unix variants

Go 1.14 added runtime support for handling the windows CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT, and CTRL_SHUTDOWN_EVENT events by adding a definition
for syscall.SIGTERM to windows and converting the events to that signal.
It also added definitions for other common signals, including SIGHUP,
and treats them as NOOPs on windows.

Consequently, this modifies the logic that deals with conditionally
handling SIGTERM and SIGHUP to handle them for windows as well as all
unix-like operating systems, including newer ones that are supported by
Go since the code was originally written.

Although the additional signals could probably just be added
unconditionally without too much issue now due to the runtime adding
stubs to all operating systems the project officially supports, it is
safer to be explicit when dealing with syscall definitions since there
is no guarantee that they will exist for newly-added operating systems.
This commit is contained in:
jholdstock 2023-09-18 14:00:41 +01:00 committed by Jamie Holdstock
parent 546a87fd5b
commit 2faaa8b32b
3 changed files with 18 additions and 23 deletions

View File

@ -13,8 +13,8 @@ import (
"github.com/decred/slog"
)
// interruptSignals defines the signals that are handled to do a clean shutdown.
// Conditional compilation is used to also include SIGTERM and SIGHUP on Unix.
// 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}
// shutdownListener listens for OS Signals such as SIGINT (Ctrl+C) and shutdown

View File

@ -0,0 +1,16 @@
// Copyright (c) 2016 The btcsuite developers
// Copyright (c) 2021-2023 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
//go:build windows || aix || android || darwin || dragonfly || freebsd || hurd || illumos || ios || linux || netbsd || openbsd || solaris
package main
import (
"syscall"
)
func init() {
interruptSignals = append(interruptSignals, syscall.SIGTERM, syscall.SIGHUP)
}

View File

@ -1,21 +0,0 @@
// Copyright (c) 2016 The btcsuite developers
// Copyright (c) 2021-2022 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
package main
import (
"os"
"syscall"
)
func init() {
interruptSignals = []os.Signal{
os.Interrupt,
syscall.SIGTERM,
syscall.SIGHUP,
}
}