From 57a9fbf3932ca5bd726f7381b92c106a68b50b89 Mon Sep 17 00:00:00 2001 From: Christopher Young Date: Tue, 19 Jan 2016 10:40:40 -0500 Subject: [PATCH] Catch SIGINT and SIGTERM - shutdown SDR functions. Fixes #204. --- main/gen_gdl90.go | 17 +++++++++++++++++ main/sdr.go | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/main/gen_gdl90.go b/main/gen_gdl90.go index a9a443a3..588e1aff 100644 --- a/main/gen_gdl90.go +++ b/main/gen_gdl90.go @@ -21,9 +21,11 @@ import ( "io/ioutil" "log" "os" + "os/signal" "runtime" "strconv" "strings" + "syscall" "time" humanize "github.com/dustin/go-humanize" @@ -1200,8 +1202,23 @@ func openReplayFile(fn string) ReadCloser { } var stratuxClock *monotonic +var sigs = make(chan os.Signal, 1) // Signal catch channel (shutdown). + +// Graceful shutdown. +func signalWatcher() { + sig := <-sigs + log.Printf("signal caught: %s - shutting down.\n", sig.String()) + // Shut down SDRs. + sdrKill() + //TODO: Any other graceful shutdown functions. + os.Exit(1) +} func main() { + // Catch signals for graceful shutdown. + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + go signalWatcher() + stratuxClock = NewMonotonic() // Start our "stratux clock". // replayESFilename := flag.String("eslog", "none", "ES Log filename") diff --git a/main/sdr.go b/main/sdr.go index 79d71fa7..f41fa1a4 100644 --- a/main/sdr.go +++ b/main/sdr.go @@ -284,6 +284,17 @@ func (e *ES) shutdown() { var devMap = map[int]string{0: "", 1: ""} +var sdrShutdown bool + +func sdrKill() { + // Send signal to shutdown to sdrWatcher(). + sdrShutdown = true + // Spin until all devices have been de-initialized. + for UATDev != nil || ESDev != nil { + time.Sleep(1 * time.Second) + } +} + // Watch for config/device changes. func sdrWatcher() { stopCheckingUATUntil := time.Time{} @@ -292,6 +303,17 @@ func sdrWatcher() { for { time.Sleep(1 * time.Second) + if sdrShutdown { + if UATDev != nil { + UATDev.shutdown() + UATDev = nil + } + if ESDev != nil { + ESDev.shutdown() + ESDev = nil + } + return + } count := rtl.GetDeviceCount() atomic.StoreUint32(&globalStatus.Devices, uint32(count)) // log.Println("DeviceCount...", count)