diff options
author | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2017-08-01 12:14:38 +0200 |
---|---|---|
committer | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2017-08-01 12:15:20 +0200 |
commit | b03a6ab1b1ef422d832a5451312ecae1363fa171 (patch) | |
tree | f40c86a1d23a8efc823df28035445888b0e05a49 /src/main.go | |
parent | d7a49b8b8c43d92fd601d32b2f5130d2dabbc748 (diff) |
Close UAPI socket before exit
Diffstat (limited to 'src/main.go')
-rw-r--r-- | src/main.go | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/src/main.go b/src/main.go index 0857999..dde21fb 100644 --- a/src/main.go +++ b/src/main.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "os/signal" "runtime" ) @@ -78,17 +79,38 @@ func main() { if err != nil { logError.Fatal("UAPI listen error:", err) } - defer uapi.Close() + + errs := make(chan error) + term := make(chan os.Signal) + wait := device.WaitChannel() go func() { for { conn, err := uapi.Accept() if err != nil { - logError.Fatal("UAPI accept error:", err) + errs <- err + return } go ipcHandle(device, conn) } }() - device.Wait() + logInfo.Println("UAPI listener started") + + // wait for program to terminate + + signal.Notify(term, os.Kill) + signal.Notify(term, os.Interrupt) + + select { + case <-wait: + case <-term: + case <-errs: + } + + // clean up UAPI bind + + uapi.Close() + + logInfo.Println("Closing") } |