From 2f2eca894744baef365aaa07554f56979159d988 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Thu, 24 May 2018 15:29:16 +0200 Subject: [PATCH] Catch EINTR --- conn_linux.go | 4 ++-- cookie.go | 2 +- keypair.go | 2 +- main.go | 2 +- noise-protocol.go | 2 +- rwcancel/rwcancel.go | 17 +++++++++-------- tun/tun_darwin.go | 9 +++++++-- tun/tun_freebsd.go | 9 +++++++-- tun/tun_linux.go | 6 +++--- tun/tun_openbsd.go | 12 +++++++----- uapi_linux.go | 2 +- 11 files changed, 40 insertions(+), 27 deletions(-) diff --git a/conn_linux.go b/conn_linux.go index 3447f3a..0227f04 100644 --- a/conn_linux.go +++ b/conn_linux.go @@ -18,8 +18,8 @@ package main import ( - "git.zx2c4.com/wireguard-go/rwcancel" "errors" + "git.zx2c4.com/wireguard-go/rwcancel" "golang.org/x/sys/unix" "net" "strconv" @@ -563,7 +563,7 @@ func (bind *NativeBind) routineRouteListener(device *Device) { var msgn int for { msgn, _, _, _, err = unix.Recvmsg(bind.netlinkSock, msg[:], nil, 0) - if err == nil || !rwcancel.ErrorIsEAGAIN(err) { + if err == nil || !rwcancel.RetryAfterError(err) { break } if !bind.netlinkCancel.ReadyRead() { diff --git a/cookie.go b/cookie.go index 8211323..1915272 100644 --- a/cookie.go +++ b/cookie.go @@ -7,9 +7,9 @@ package main import ( - "git.zx2c4.com/wireguard-go/xchacha20poly1305" "crypto/hmac" "crypto/rand" + "git.zx2c4.com/wireguard-go/xchacha20poly1305" "golang.org/x/crypto/blake2s" "golang.org/x/crypto/chacha20poly1305" "sync" diff --git a/keypair.go b/keypair.go index b125189..c43a3d2 100644 --- a/keypair.go +++ b/keypair.go @@ -7,8 +7,8 @@ package main import ( - "git.zx2c4.com/wireguard-go/replay" "crypto/cipher" + "git.zx2c4.com/wireguard-go/replay" "sync" "time" ) diff --git a/main.go b/main.go index f21de41..bb2786b 100644 --- a/main.go +++ b/main.go @@ -7,8 +7,8 @@ package main import ( - "git.zx2c4.com/wireguard-go/tun" "fmt" + "git.zx2c4.com/wireguard-go/tun" "os" "os/signal" "runtime" diff --git a/noise-protocol.go b/noise-protocol.go index a527be9..45dca72 100644 --- a/noise-protocol.go +++ b/noise-protocol.go @@ -7,8 +7,8 @@ package main import ( - "git.zx2c4.com/wireguard-go/tai64n" "errors" + "git.zx2c4.com/wireguard-go/tai64n" "golang.org/x/crypto/blake2s" "golang.org/x/crypto/chacha20poly1305" "golang.org/x/crypto/poly1305" diff --git a/rwcancel/rwcancel.go b/rwcancel/rwcancel.go index aac743a..e73c58a 100644 --- a/rwcancel/rwcancel.go +++ b/rwcancel/rwcancel.go @@ -40,15 +40,16 @@ func NewRWCancel(fd int) (*RWCancel, error) { return &rwcancel, nil } -/* https://golang.org/src/crypto/rand/eagain.go */ -func ErrorIsEAGAIN(err error) bool { +func RetryAfterError(err error) bool { if pe, ok := err.(*os.PathError); ok { - if errno, ok := pe.Err.(syscall.Errno); ok && errno == syscall.EAGAIN { + err = pe.Err + } + if errno, ok := err.(syscall.Errno); ok { + switch errno { + case syscall.EAGAIN, syscall.EINTR: return true } - } - if errno, ok := err.(syscall.Errno); ok && errno == syscall.EAGAIN { - return true + } return false } @@ -86,7 +87,7 @@ func (rw *RWCancel) ReadyWrite() bool { func (rw *RWCancel) Read(p []byte) (n int, err error) { for { n, err := unix.Read(rw.fd, p) - if err == nil || !ErrorIsEAGAIN(err) { + if err == nil || !RetryAfterError(err) { return n, err } if !rw.ReadyRead() { @@ -98,7 +99,7 @@ func (rw *RWCancel) Read(p []byte) (n int, err error) { func (rw *RWCancel) Write(p []byte) (n int, err error) { for { n, err := unix.Write(rw.fd, p) - if err == nil || !ErrorIsEAGAIN(err) { + if err == nil || !RetryAfterError(err) { return n, err } if !rw.ReadyWrite() { diff --git a/tun/tun_darwin.go b/tun/tun_darwin.go index 04020cb..f692bbe 100644 --- a/tun/tun_darwin.go +++ b/tun/tun_darwin.go @@ -7,14 +7,15 @@ package tun import ( - "git.zx2c4.com/wireguard-go/rwcancel" "errors" "fmt" + "git.zx2c4.com/wireguard-go/rwcancel" "golang.org/x/net/ipv6" "golang.org/x/sys/unix" "io/ioutil" "net" "os" + "syscall" "unsafe" ) @@ -54,8 +55,12 @@ func (tun *nativeTun) routineRouteListener(tunIfindex int) { data := make([]byte, os.Getpagesize()) for { + retry: n, err := unix.Read(tun.routeSocket, data) if err != nil { + if errno, ok := err.(syscall.Errno); ok && errno == syscall.EINTR { + goto retry + } tun.errors <- err return } @@ -259,7 +264,7 @@ func (tun *nativeTun) doRead(buff []byte, offset int) (int, error) { func (tun *nativeTun) Read(buff []byte, offset int) (int, error) { for { n, err := tun.doRead(buff, offset) - if err == nil || !rwcancel.ErrorIsEAGAIN(err) { + if err == nil || !rwcancel.RetryAfterError(err) { return n, err } if !tun.rwcancel.ReadyRead() { diff --git a/tun/tun_freebsd.go b/tun/tun_freebsd.go index bd70104..435ff91 100644 --- a/tun/tun_freebsd.go +++ b/tun/tun_freebsd.go @@ -6,14 +6,15 @@ package tun import ( - "git.zx2c4.com/wireguard-go/rwcancel" "bytes" "errors" "fmt" + "git.zx2c4.com/wireguard-go/rwcancel" "golang.org/x/net/ipv6" "golang.org/x/sys/unix" "net" "os" + "syscall" "unsafe" ) @@ -67,8 +68,12 @@ func (tun *nativeTun) routineRouteListener(tunIfindex int) { data := make([]byte, os.Getpagesize()) for { + retry: n, err := unix.Read(tun.routeSocket, data) if err != nil { + if errno, ok := err.(syscall.Errno); ok && errno == syscall.EINTR { + goto retry + } tun.errors <- err return } @@ -392,7 +397,7 @@ func (tun *nativeTun) doRead(buff []byte, offset int) (int, error) { func (tun *nativeTun) Read(buff []byte, offset int) (int, error) { for { n, err := tun.doRead(buff, offset) - if err == nil || !rwcancel.ErrorIsEAGAIN(err) { + if err == nil || !rwcancel.RetryAfterError(err) { return n, err } if !tun.rwcancel.ReadyRead() { diff --git a/tun/tun_linux.go b/tun/tun_linux.go index d048c21..2119696 100644 --- a/tun/tun_linux.go +++ b/tun/tun_linux.go @@ -12,10 +12,10 @@ package tun */ import ( - "git.zx2c4.com/wireguard-go/rwcancel" "bytes" "errors" "fmt" + "git.zx2c4.com/wireguard-go/rwcancel" "golang.org/x/net/ipv6" "golang.org/x/sys/unix" "net" @@ -102,7 +102,7 @@ func (tun *nativeTun) routineNetlinkListener() { var msgn int for { msgn, _, _, _, err = unix.Recvmsg(tun.netlinkSock, msg[:], nil, 0) - if err == nil || !rwcancel.ErrorIsEAGAIN(err) { + if err == nil || !rwcancel.RetryAfterError(err) { break } if !tun.netlinkCancel.ReadyRead() { @@ -334,7 +334,7 @@ func (tun *nativeTun) doRead(buff []byte, offset int) (int, error) { func (tun *nativeTun) Read(buff []byte, offset int) (int, error) { for { n, err := tun.doRead(buff, offset) - if err == nil || !rwcancel.ErrorIsEAGAIN(err) { + if err == nil || !rwcancel.RetryAfterError(err) { return n, err } if !tun.fdCancel.ReadyRead() { diff --git a/tun/tun_openbsd.go b/tun/tun_openbsd.go index 709b5cd..3c1878b 100644 --- a/tun/tun_openbsd.go +++ b/tun/tun_openbsd.go @@ -6,9 +6,9 @@ package tun import ( - "git.zx2c4.com/wireguard-go/rwcancel" "errors" "fmt" + "git.zx2c4.com/wireguard-go/rwcancel" "golang.org/x/net/ipv6" "golang.org/x/sys/unix" "io/ioutil" @@ -46,8 +46,12 @@ func (tun *nativeTun) routineRouteListener(tunIfindex int) { data := make([]byte, os.Getpagesize()) for { + retry: n, err := unix.Read(tun.routeSocket, data) if err != nil { + if errno, ok := err.(syscall.Errno); ok && errno == syscall.EINTR { + goto retry + } tun.errors <- err return } @@ -90,9 +94,7 @@ func (tun *nativeTun) routineRouteListener(tunIfindex int) { func errorIsEBUSY(err error) bool { if pe, ok := err.(*os.PathError); ok { - if errno, ok := pe.Err.(syscall.Errno); ok && errno == syscall.EBUSY { - return true - } + err = pe.Err } if errno, ok := err.(syscall.Errno); ok && errno == syscall.EBUSY { return true @@ -237,7 +239,7 @@ func (tun *nativeTun) doRead(buff []byte, offset int) (int, error) { func (tun *nativeTun) Read(buff []byte, offset int) (int, error) { for { n, err := tun.doRead(buff, offset) - if err == nil || !rwcancel.ErrorIsEAGAIN(err) { + if err == nil || !rwcancel.RetryAfterError(err) { return n, err } if !tun.rwcancel.ReadyRead() { diff --git a/uapi_linux.go b/uapi_linux.go index 4b74587..0cb2924 100644 --- a/uapi_linux.go +++ b/uapi_linux.go @@ -7,9 +7,9 @@ package main import ( - "git.zx2c4.com/wireguard-go/rwcancel" "errors" "fmt" + "git.zx2c4.com/wireguard-go/rwcancel" "golang.org/x/sys/unix" "net" "os"