]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
Catch EINTR
authorJason A. Donenfeld <Jason@zx2c4.com>
Thu, 24 May 2018 13:29:16 +0000 (15:29 +0200)
committerJason A. Donenfeld <Jason@zx2c4.com>
Thu, 24 May 2018 13:36:29 +0000 (15:36 +0200)
conn_linux.go
cookie.go
keypair.go
main.go
noise-protocol.go
rwcancel/rwcancel.go
tun/tun_darwin.go
tun/tun_freebsd.go
tun/tun_linux.go
tun/tun_openbsd.go
uapi_linux.go

index 3447f3a0af7bcffabff0a764b3ccc1f04fb1c156..0227f044b3395a50ab77ee4a7a82029a9499c410 100644 (file)
@@ -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() {
index 8211323511a14cb5f8fcbff2064829bc9892f226..1915272736b841b66fda74becf3002bd7981f607 100644 (file)
--- 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"
index b125189d30fe8cf311095177ee7477d47eb8ee3c..c43a3d2defb62ccfbed218ebbc1c391881fb15f9 100644 (file)
@@ -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 f21de41a58d0ec3ccffbe2479392deeda0d66b7c..bb2786b425aca485ec2d1a0137f5d91c91fb009a 100644 (file)
--- 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"
index a527be946a443ef2a9af7b0e0255b64efc8f4e9f..45dca72a81b3893f5a4cc80033ea8ea7801a9277 100644 (file)
@@ -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"
index aac743a35cc504e4f91aef6293ead09995e469c2..e73c58a635916042cc4c261742b1b1ec908d978f 100644 (file)
@@ -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() {
index 04020cbd1e625189cd2c3cf822d7f94470ee638f..f692bbe8f0654e1f7661c659c3e399687ade5b6a 100644 (file)
@@ -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() {
index bd70104760d8d2b9ff22c8a6bdd5df832fd15938..435ff91be074c39debb70419f00ac9940d94642c 100644 (file)
@@ -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() {
index d048c21afda7e4315f8efdd82ebbac5d3fac933e..2119696033b6cdcaf9a0dc96319f92bae44c6ea6 100644 (file)
@@ -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() {
index 709b5cd5ffa503e9d1a3fdbbc585c214e90b0d97..3c1878b26ed2eca05e7f48885e738dfdb9ab18c9 100644 (file)
@@ -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() {
index 4b745871e0d54ddef6842fa7073443dd7904e509..0cb2924e49274543c1a7fbf83db940bbb83f3c6c 100644 (file)
@@ -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"