mirror of
https://git.zx2c4.com/wireguard-go
synced 2024-11-15 01:05:15 +01:00
0abb6b668c
On my Chromebook (Linux 4.19.44 in a VM) and on an AWS EC2 machine, select() was sometimes returning EINTR. This is harmless and just means you should try again. So let's try again. This eliminates a problem where the tunnel fails to come up correctly and the program needs to be restarted. Signed-off-by: Avery Pennarun <apenwarr@gmail.com>
128 lines
2.2 KiB
Go
128 lines
2.2 KiB
Go
/* SPDX-License-Identifier: MIT
|
|
*
|
|
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
|
|
*/
|
|
|
|
package rwcancel
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"syscall"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
func max(a, b int) int {
|
|
if a > b {
|
|
return a
|
|
}
|
|
return b
|
|
}
|
|
|
|
type RWCancel struct {
|
|
fd int
|
|
closingReader *os.File
|
|
closingWriter *os.File
|
|
}
|
|
|
|
func NewRWCancel(fd int) (*RWCancel, error) {
|
|
err := unix.SetNonblock(fd, true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rwcancel := RWCancel{fd: fd}
|
|
|
|
rwcancel.closingReader, rwcancel.closingWriter, err = os.Pipe()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &rwcancel, nil
|
|
}
|
|
|
|
func RetryAfterError(err error) bool {
|
|
if pe, ok := err.(*os.PathError); ok {
|
|
err = pe.Err
|
|
}
|
|
if errno, ok := err.(syscall.Errno); ok {
|
|
switch errno {
|
|
case syscall.EAGAIN, syscall.EINTR:
|
|
return true
|
|
}
|
|
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (rw *RWCancel) ReadyRead() bool {
|
|
closeFd := int(rw.closingReader.Fd())
|
|
fdset := fdSet{}
|
|
fdset.set(rw.fd)
|
|
fdset.set(closeFd)
|
|
var err error
|
|
for {
|
|
err = unixSelect(max(rw.fd, closeFd)+1, &fdset.FdSet, nil, nil, nil)
|
|
if err == nil || !RetryAfterError(err) {
|
|
break
|
|
}
|
|
}
|
|
if err != nil {
|
|
return false
|
|
}
|
|
if fdset.check(closeFd) {
|
|
return false
|
|
}
|
|
return fdset.check(rw.fd)
|
|
}
|
|
|
|
func (rw *RWCancel) ReadyWrite() bool {
|
|
closeFd := int(rw.closingReader.Fd())
|
|
fdset := fdSet{}
|
|
fdset.set(rw.fd)
|
|
fdset.set(closeFd)
|
|
var err error
|
|
for {
|
|
err = unixSelect(max(rw.fd, closeFd)+1, nil, &fdset.FdSet, nil, nil)
|
|
if err == nil || !RetryAfterError(err) {
|
|
break
|
|
}
|
|
}
|
|
if err != nil {
|
|
return false
|
|
}
|
|
if fdset.check(closeFd) {
|
|
return false
|
|
}
|
|
return fdset.check(rw.fd)
|
|
}
|
|
|
|
func (rw *RWCancel) Read(p []byte) (n int, err error) {
|
|
for {
|
|
n, err := unix.Read(rw.fd, p)
|
|
if err == nil || !RetryAfterError(err) {
|
|
return n, err
|
|
}
|
|
if !rw.ReadyRead() {
|
|
return 0, errors.New("fd closed")
|
|
}
|
|
}
|
|
}
|
|
|
|
func (rw *RWCancel) Write(p []byte) (n int, err error) {
|
|
for {
|
|
n, err := unix.Write(rw.fd, p)
|
|
if err == nil || !RetryAfterError(err) {
|
|
return n, err
|
|
}
|
|
if !rw.ReadyWrite() {
|
|
return 0, errors.New("fd closed")
|
|
}
|
|
}
|
|
}
|
|
|
|
func (rw *RWCancel) Cancel() (err error) {
|
|
_, err = rw.closingWriter.Write([]byte{0})
|
|
return
|
|
}
|