mirror of
https://git.zx2c4.com/wireguard-go
synced 2024-11-15 01:05:15 +01:00
Close UAPI socket before exit
This commit is contained in:
parent
d7a49b8b8c
commit
b03a6ab1b1
@ -186,6 +186,6 @@ func (device *Device) Close() {
|
||||
close(device.signal.stop)
|
||||
}
|
||||
|
||||
func (device *Device) Wait() {
|
||||
<-device.signal.stop
|
||||
func (device *Device) WaitChannel() chan struct{} {
|
||||
return device.signal.stop
|
||||
}
|
||||
|
@ -40,11 +40,11 @@ func NewLogger(level int) *Logger {
|
||||
|
||||
logger.Info = log.New(logInfo,
|
||||
"INFO: ",
|
||||
log.Ldate|log.Ltime|log.Lshortfile,
|
||||
log.Ldate|log.Ltime,
|
||||
)
|
||||
logger.Error = log.New(logErr,
|
||||
"ERROR: ",
|
||||
log.Ldate|log.Ltime|log.Lshortfile,
|
||||
log.Ldate|log.Ltime,
|
||||
)
|
||||
return logger
|
||||
}
|
||||
|
28
src/main.go
28
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")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user