From 03eff74d8b6c069a307c40de8581b6dd79cdbaf8 Mon Sep 17 00:00:00 2001 From: Russ Magee Date: Thu, 15 Nov 2018 22:57:21 -0800 Subject: [PATCH] Added signal handling for hkexshd to respond as well-behaved daemon Signed-off-by: Russ Magee --- hkexshd/hkexshd.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/hkexshd/hkexshd.go b/hkexshd/hkexshd.go index 8cb10f2..817e3b0 100755 --- a/hkexshd/hkexshd.go +++ b/hkexshd/hkexshd.go @@ -20,6 +20,7 @@ import ( "log" "os" "os/exec" + "os/signal" "os/user" "path" "sync" @@ -395,6 +396,29 @@ func main() { log.SetOutput(ioutil.Discard) } + // Set up handler for daemon signalling + exitCh := make(chan os.Signal, 1) + signal.Notify(exitCh, os.Signal(syscall.SIGTERM), os.Signal(syscall.SIGINT), os.Signal(syscall.SIGHUP), os.Signal(syscall.SIGUSR1), os.Signal(syscall.SIGUSR2)) + go func() { + for { + sig := <-exitCh + switch sig.String() { + case "terminated": + logger.LogNotice(fmt.Sprintf("[Got signal: %s]", sig)) + signal.Reset() + syscall.Kill(0, syscall.SIGTERM) + case "interrupt": + logger.LogNotice(fmt.Sprintf("[Got signal: %s]", sig)) + signal.Reset() + syscall.Kill(0, syscall.SIGINT) + case "hangup": + logger.LogNotice(fmt.Sprintf("[Got signal: %s - nop]", sig)) + default: + logger.LogNotice(fmt.Sprintf("[Got signal: %s - ignored]", sig)) + } + } + }() + // Listen on TCP port 2000 on all available unicast and // anycast IP addresses of the local system. l, err := hkexnet.Listen("tcp", laddr)