summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-05-14 03:43:56 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-05-14 03:44:57 +0200
commitf738c45a68a34721af4ca2738be1f8389b372bfe (patch)
tree833e01a73a7bab86dd58b8379e551a0cf123f3b5
parentc1e097d6d0cabb81b3aa180d69107f9857606bf5 (diff)
Smoother netlink shutdown
-rw-r--r--tun_linux.go49
1 files changed, 29 insertions, 20 deletions
diff --git a/tun_linux.go b/tun_linux.go
index c642fe7..a243a03 100644
--- a/tun_linux.go
+++ b/tun_linux.go
@@ -31,15 +31,15 @@ const (
)
type NativeTun struct {
- fd *os.File
- index int32 // if index
- name string // name of interface
- errors chan error // async error handling
- events chan TUNEvent // device related events
- nopi bool // the device was pased IFF_NO_PI
- rwcancel *rwcancel.RWCancel
- netlinkSock int
- shutdownHackListener chan struct{}
+ fd *os.File
+ index int32 // if index
+ name string // name of interface
+ errors chan error // async error handling
+ events chan TUNEvent // device related events
+ nopi bool // the device was pased IFF_NO_PI
+ rwcancel *rwcancel.RWCancel
+ netlinkSock int
+ statusListenersShutdown chan struct{}
}
func (tun *NativeTun) File() *os.File {
@@ -63,7 +63,7 @@ func (tun *NativeTun) RoutineHackListener() {
}
select {
case <-time.After(time.Second / 10):
- case <-tun.shutdownHackListener:
+ case <-tun.statusListenersShutdown:
return
}
}
@@ -94,6 +94,12 @@ func (tun *NativeTun) RoutineNetlinkListener() {
return
}
+ select {
+ case <-tun.statusListenersShutdown:
+ return
+ default:
+ }
+
for remain := msg[:msgn]; len(remain) >= unix.SizeofNlMsghdr; {
hdr := *(*unix.NlMsghdr)(unsafe.Pointer(&remain[0]))
@@ -328,16 +334,19 @@ func (tun *NativeTun) Events() chan TUNEvent {
}
func (tun *NativeTun) Close() error {
- err1 := tun.fd.Close()
- err2 := closeUnblock(tun.netlinkSock)
- tun.rwcancel.Cancel()
+ close(tun.statusListenersShutdown)
+ err1 := closeUnblock(tun.netlinkSock)
+ err2 := tun.fd.Close()
+ err3 := tun.rwcancel.Cancel()
close(tun.events)
- close(tun.shutdownHackListener)
if err1 != nil {
return err1
}
- return err2
+ if err2 != nil {
+ return err2
+ }
+ return err3
}
func CreateTUN(name string) (TUNDevice, error) {
@@ -387,11 +396,11 @@ func CreateTUN(name string) (TUNDevice, error) {
func CreateTUNFromFile(fd *os.File) (TUNDevice, error) {
device := &NativeTun{
- fd: fd,
- events: make(chan TUNEvent, 5),
- errors: make(chan error, 5),
- shutdownHackListener: make(chan struct{}, 0),
- nopi: false,
+ fd: fd,
+ events: make(chan TUNEvent, 5),
+ errors: make(chan error, 5),
+ statusListenersShutdown: make(chan struct{}, 0),
+ nopi: false,
}
var err error