2021-09-05 14:00:43 +00:00
|
|
|
//go:build !windows
|
2020-02-19 15:09:24 +00:00
|
|
|
// +build !windows
|
|
|
|
|
2019-01-02 00:55:51 +00:00
|
|
|
/* SPDX-License-Identifier: MIT
|
2018-05-13 22:28:30 +00:00
|
|
|
*
|
2021-01-28 16:52:15 +00:00
|
|
|
* Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
|
2018-05-13 22:28:30 +00:00
|
|
|
*/
|
|
|
|
|
2020-02-19 15:09:24 +00:00
|
|
|
// Package rwcancel implements cancelable read/write operations on
|
|
|
|
// a file descriptor.
|
2018-05-13 22:28:30 +00:00
|
|
|
package rwcancel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
2019-05-14 07:09:52 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2018-05-13 22:28:30 +00:00
|
|
|
)
|
|
|
|
|
2018-05-17 22:58:54 +00:00
|
|
|
type RWCancel struct {
|
|
|
|
fd int
|
|
|
|
closingReader *os.File
|
|
|
|
closingWriter *os.File
|
|
|
|
}
|
|
|
|
|
2018-05-13 22:28:30 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-05-24 13:29:16 +00:00
|
|
|
func RetryAfterError(err error) bool {
|
2021-02-09 18:54:00 +00:00
|
|
|
return errors.Is(err, syscall.EAGAIN) || errors.Is(err, syscall.EINTR)
|
2018-05-13 22:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *RWCancel) ReadyRead() bool {
|
2021-05-20 15:42:34 +00:00
|
|
|
closeFd := int32(rw.closingReader.Fd())
|
|
|
|
|
|
|
|
pollFds := []unix.PollFd{{Fd: int32(rw.fd), Events: unix.POLLIN}, {Fd: closeFd, Events: unix.POLLIN}}
|
2019-10-12 07:46:13 +00:00
|
|
|
var err error
|
|
|
|
for {
|
2021-09-27 20:18:38 +00:00
|
|
|
_, err = unix.Poll(pollFds, -1)
|
2019-10-12 07:46:13 +00:00
|
|
|
if err == nil || !RetryAfterError(err) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-05-13 22:28:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2021-05-20 15:42:34 +00:00
|
|
|
if pollFds[1].Revents != 0 {
|
2018-05-13 22:28:30 +00:00
|
|
|
return false
|
|
|
|
}
|
2021-05-20 15:42:34 +00:00
|
|
|
return pollFds[0].Revents != 0
|
2018-05-13 22:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *RWCancel) ReadyWrite() bool {
|
2021-05-20 15:42:34 +00:00
|
|
|
closeFd := int32(rw.closingReader.Fd())
|
|
|
|
pollFds := []unix.PollFd{{Fd: int32(rw.fd), Events: unix.POLLOUT}, {Fd: closeFd, Events: unix.POLLOUT}}
|
2019-10-12 07:46:13 +00:00
|
|
|
var err error
|
|
|
|
for {
|
2021-09-27 20:18:38 +00:00
|
|
|
_, err = unix.Poll(pollFds, -1)
|
2019-10-12 07:46:13 +00:00
|
|
|
if err == nil || !RetryAfterError(err) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-05-13 22:28:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2021-05-20 15:42:34 +00:00
|
|
|
|
|
|
|
if pollFds[1].Revents != 0 {
|
2018-05-13 22:28:30 +00:00
|
|
|
return false
|
|
|
|
}
|
2021-05-20 15:42:34 +00:00
|
|
|
return pollFds[0].Revents != 0
|
2018-05-13 22:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *RWCancel) Read(p []byte) (n int, err error) {
|
|
|
|
for {
|
|
|
|
n, err := unix.Read(rw.fd, p)
|
2018-05-24 13:29:16 +00:00
|
|
|
if err == nil || !RetryAfterError(err) {
|
2018-05-13 22:28:30 +00:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
if !rw.ReadyRead() {
|
2021-05-20 15:56:36 +00:00
|
|
|
return 0, os.ErrClosed
|
2018-05-13 22:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *RWCancel) Write(p []byte) (n int, err error) {
|
|
|
|
for {
|
|
|
|
n, err := unix.Write(rw.fd, p)
|
2018-05-24 13:29:16 +00:00
|
|
|
if err == nil || !RetryAfterError(err) {
|
2018-05-13 22:28:30 +00:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
if !rw.ReadyWrite() {
|
2021-05-20 15:56:36 +00:00
|
|
|
return 0, os.ErrClosed
|
2018-05-13 22:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *RWCancel) Cancel() (err error) {
|
|
|
|
_, err = rw.closingWriter.Write([]byte{0})
|
|
|
|
return
|
|
|
|
}
|
2021-02-09 19:18:21 +00:00
|
|
|
|
|
|
|
func (rw *RWCancel) Close() {
|
|
|
|
rw.closingReader.Close()
|
|
|
|
rw.closingWriter.Close()
|
|
|
|
}
|