Close UAPI socket before exit

This commit is contained in:
Mathias Hall-Andersen 2017-08-01 12:14:38 +02:00
parent d7a49b8b8c
commit b03a6ab1b1
3 changed files with 29 additions and 7 deletions

View file

@ -186,6 +186,6 @@ func (device *Device) Close() {
close(device.signal.stop) close(device.signal.stop)
} }
func (device *Device) Wait() { func (device *Device) WaitChannel() chan struct{} {
<-device.signal.stop return device.signal.stop
} }

View file

@ -40,11 +40,11 @@ func NewLogger(level int) *Logger {
logger.Info = log.New(logInfo, logger.Info = log.New(logInfo,
"INFO: ", "INFO: ",
log.Ldate|log.Ltime|log.Lshortfile, log.Ldate|log.Ltime,
) )
logger.Error = log.New(logErr, logger.Error = log.New(logErr,
"ERROR: ", "ERROR: ",
log.Ldate|log.Ltime|log.Lshortfile, log.Ldate|log.Ltime,
) )
return logger return logger
} }

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"os/signal"
"runtime" "runtime"
) )
@ -78,17 +79,38 @@ func main() {
if err != nil { if err != nil {
logError.Fatal("UAPI listen error:", err) logError.Fatal("UAPI listen error:", err)
} }
defer uapi.Close()
errs := make(chan error)
term := make(chan os.Signal)
wait := device.WaitChannel()
go func() { go func() {
for { for {
conn, err := uapi.Accept() conn, err := uapi.Accept()
if err != nil { if err != nil {
logError.Fatal("UAPI accept error:", err) errs <- err
return
} }
go ipcHandle(device, conn) 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")
} }