Added signal handling for hkexshd to respond as well-behaved daemon

Signed-off-by: Russ Magee <rmagee@gmail.com>
This commit is contained in:
Russ Magee 2018-11-15 22:57:21 -08:00
parent ee52959fab
commit 03eff74d8b

View file

@ -20,6 +20,7 @@ import (
"log" "log"
"os" "os"
"os/exec" "os/exec"
"os/signal"
"os/user" "os/user"
"path" "path"
"sync" "sync"
@ -395,6 +396,29 @@ func main() {
log.SetOutput(ioutil.Discard) 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 // Listen on TCP port 2000 on all available unicast and
// anycast IP addresses of the local system. // anycast IP addresses of the local system.
l, err := hkexnet.Listen("tcp", laddr) l, err := hkexnet.Listen("tcp", laddr)