From 2faaa8b32bcdc84f321748603fbc6c37cc52db11 Mon Sep 17 00:00:00 2001 From: jholdstock Date: Mon, 18 Sep 2023 14:00:41 +0100 Subject: [PATCH] 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. --- cmd/vspd/signal.go | 4 ++-- cmd/vspd/signal_syscall.go | 16 ++++++++++++++++ cmd/vspd/signal_unix.go | 21 --------------------- 3 files changed, 18 insertions(+), 23 deletions(-) create mode 100644 cmd/vspd/signal_syscall.go delete mode 100644 cmd/vspd/signal_unix.go diff --git a/cmd/vspd/signal.go b/cmd/vspd/signal.go index 42ff1a2..851810a 100644 --- a/cmd/vspd/signal.go +++ b/cmd/vspd/signal.go @@ -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 diff --git a/cmd/vspd/signal_syscall.go b/cmd/vspd/signal_syscall.go new file mode 100644 index 0000000..c40d3ba --- /dev/null +++ b/cmd/vspd/signal_syscall.go @@ -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) +} diff --git a/cmd/vspd/signal_unix.go b/cmd/vspd/signal_unix.go deleted file mode 100644 index ab9ceaa..0000000 --- a/cmd/vspd/signal_unix.go +++ /dev/null @@ -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, - } -}