2021-10-30 00:39:56 +00:00
|
|
|
// Copyright 2021 The Go Authors. All rights reserved.
|
|
|
|
// Copyright 2015 Microsoft
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2019-05-23 12:53:44 +00:00
|
|
|
|
2021-10-30 00:39:56 +00:00
|
|
|
//go:build windows
|
|
|
|
// +build windows
|
2021-03-03 11:26:59 +00:00
|
|
|
|
2021-10-30 00:39:56 +00:00
|
|
|
package namedpipe
|
2019-05-23 12:53:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2021-03-03 11:26:59 +00:00
|
|
|
"os"
|
2019-05-23 12:53:44 +00:00
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
2021-03-03 11:26:59 +00:00
|
|
|
"unsafe"
|
2019-09-17 05:39:16 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
2019-05-23 12:53:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type timeoutChan chan struct{}
|
|
|
|
|
2021-12-09 16:55:50 +00:00
|
|
|
var (
|
|
|
|
ioInitOnce sync.Once
|
|
|
|
ioCompletionPort windows.Handle
|
|
|
|
)
|
2019-05-23 12:53:44 +00:00
|
|
|
|
|
|
|
// ioResult contains the result of an asynchronous IO operation
|
|
|
|
type ioResult struct {
|
|
|
|
bytes uint32
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// ioOperation represents an outstanding asynchronous Win32 IO
|
|
|
|
type ioOperation struct {
|
2019-09-17 05:39:16 +00:00
|
|
|
o windows.Overlapped
|
2019-05-23 12:53:44 +00:00
|
|
|
ch chan ioResult
|
|
|
|
}
|
|
|
|
|
|
|
|
func initIo() {
|
2021-03-03 11:26:59 +00:00
|
|
|
h, err := windows.CreateIoCompletionPort(windows.InvalidHandle, 0, 0, 0)
|
2019-05-23 12:53:44 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
ioCompletionPort = h
|
|
|
|
go ioCompletionProcessor(h)
|
|
|
|
}
|
|
|
|
|
2021-03-03 11:26:59 +00:00
|
|
|
// file implements Reader, Writer, and Closer on a Win32 handle without blocking in a syscall.
|
2019-05-23 12:53:44 +00:00
|
|
|
// It takes ownership of this handle and will close it if it is garbage collected.
|
2021-03-03 11:26:59 +00:00
|
|
|
type file struct {
|
2019-09-17 05:39:16 +00:00
|
|
|
handle windows.Handle
|
2019-05-23 12:53:44 +00:00
|
|
|
wg sync.WaitGroup
|
|
|
|
wgLock sync.RWMutex
|
2021-03-03 11:26:59 +00:00
|
|
|
closing uint32 // used as atomic boolean
|
2019-05-23 12:53:44 +00:00
|
|
|
socket bool
|
|
|
|
readDeadline deadlineHandler
|
|
|
|
writeDeadline deadlineHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
type deadlineHandler struct {
|
|
|
|
setLock sync.Mutex
|
|
|
|
channel timeoutChan
|
|
|
|
channelLock sync.RWMutex
|
|
|
|
timer *time.Timer
|
2021-03-03 11:26:59 +00:00
|
|
|
timedout uint32 // used as atomic boolean
|
2019-05-23 12:53:44 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 11:26:59 +00:00
|
|
|
// makeFile makes a new file from an existing file handle
|
|
|
|
func makeFile(h windows.Handle) (*file, error) {
|
|
|
|
f := &file{handle: h}
|
2019-05-23 12:53:44 +00:00
|
|
|
ioInitOnce.Do(initIo)
|
2021-03-03 11:26:59 +00:00
|
|
|
_, err := windows.CreateIoCompletionPort(h, ioCompletionPort, 0, 0)
|
2019-05-23 12:53:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-03 11:26:59 +00:00
|
|
|
err = windows.SetFileCompletionNotificationModes(h, windows.FILE_SKIP_COMPLETION_PORT_ON_SUCCESS|windows.FILE_SKIP_SET_EVENT_ON_HANDLE)
|
2019-05-23 12:53:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f.readDeadline.channel = make(timeoutChan)
|
|
|
|
f.writeDeadline.channel = make(timeoutChan)
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// closeHandle closes the resources associated with a Win32 handle
|
2021-03-03 11:26:59 +00:00
|
|
|
func (f *file) closeHandle() {
|
2019-05-23 12:53:44 +00:00
|
|
|
f.wgLock.Lock()
|
|
|
|
// Atomically set that we are closing, releasing the resources only once.
|
2021-03-03 11:26:59 +00:00
|
|
|
if atomic.SwapUint32(&f.closing, 1) == 0 {
|
2019-05-23 12:53:44 +00:00
|
|
|
f.wgLock.Unlock()
|
|
|
|
// cancel all IO and wait for it to complete
|
2021-03-03 11:26:59 +00:00
|
|
|
windows.CancelIoEx(f.handle, nil)
|
2019-05-23 12:53:44 +00:00
|
|
|
f.wg.Wait()
|
|
|
|
// at this point, no new IO can start
|
2019-09-17 05:39:16 +00:00
|
|
|
windows.Close(f.handle)
|
2019-05-23 12:53:44 +00:00
|
|
|
f.handle = 0
|
|
|
|
} else {
|
|
|
|
f.wgLock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 11:26:59 +00:00
|
|
|
// Close closes a file.
|
|
|
|
func (f *file) Close() error {
|
2019-05-23 12:53:44 +00:00
|
|
|
f.closeHandle()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareIo prepares for a new IO operation.
|
|
|
|
// The caller must call f.wg.Done() when the IO is finished, prior to Close() returning.
|
2021-03-03 11:26:59 +00:00
|
|
|
func (f *file) prepareIo() (*ioOperation, error) {
|
2019-05-23 12:53:44 +00:00
|
|
|
f.wgLock.RLock()
|
2021-03-03 11:26:59 +00:00
|
|
|
if atomic.LoadUint32(&f.closing) == 1 {
|
2019-05-23 12:53:44 +00:00
|
|
|
f.wgLock.RUnlock()
|
2021-03-03 11:26:59 +00:00
|
|
|
return nil, os.ErrClosed
|
2019-05-23 12:53:44 +00:00
|
|
|
}
|
|
|
|
f.wg.Add(1)
|
|
|
|
f.wgLock.RUnlock()
|
|
|
|
c := &ioOperation{}
|
|
|
|
c.ch = make(chan ioResult)
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ioCompletionProcessor processes completed async IOs forever
|
2019-09-17 05:39:16 +00:00
|
|
|
func ioCompletionProcessor(h windows.Handle) {
|
2019-05-23 12:53:44 +00:00
|
|
|
for {
|
|
|
|
var bytes uint32
|
|
|
|
var key uintptr
|
|
|
|
var op *ioOperation
|
2021-03-03 11:26:59 +00:00
|
|
|
err := windows.GetQueuedCompletionStatus(h, &bytes, &key, (**windows.Overlapped)(unsafe.Pointer(&op)), windows.INFINITE)
|
2019-05-23 12:53:44 +00:00
|
|
|
if op == nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
op.ch <- ioResult{bytes, err}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// asyncIo processes the return value from ReadFile or WriteFile, blocking until
|
|
|
|
// the operation has actually completed.
|
2021-03-03 11:26:59 +00:00
|
|
|
func (f *file) asyncIo(c *ioOperation, d *deadlineHandler, bytes uint32, err error) (int, error) {
|
2019-09-17 05:39:16 +00:00
|
|
|
if err != windows.ERROR_IO_PENDING {
|
2019-05-23 12:53:44 +00:00
|
|
|
return int(bytes), err
|
|
|
|
}
|
|
|
|
|
2021-03-03 11:26:59 +00:00
|
|
|
if atomic.LoadUint32(&f.closing) == 1 {
|
|
|
|
windows.CancelIoEx(f.handle, &c.o)
|
2019-05-23 12:53:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var timeout timeoutChan
|
|
|
|
if d != nil {
|
|
|
|
d.channelLock.Lock()
|
|
|
|
timeout = d.channel
|
|
|
|
d.channelLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
var r ioResult
|
|
|
|
select {
|
|
|
|
case r = <-c.ch:
|
|
|
|
err = r.err
|
2019-09-17 05:39:16 +00:00
|
|
|
if err == windows.ERROR_OPERATION_ABORTED {
|
2021-03-03 11:26:59 +00:00
|
|
|
if atomic.LoadUint32(&f.closing) == 1 {
|
|
|
|
err = os.ErrClosed
|
2019-05-23 12:53:44 +00:00
|
|
|
}
|
|
|
|
} else if err != nil && f.socket {
|
|
|
|
// err is from Win32. Query the overlapped structure to get the winsock error.
|
|
|
|
var bytes, flags uint32
|
2021-03-03 11:26:59 +00:00
|
|
|
err = windows.WSAGetOverlappedResult(f.handle, &c.o, &bytes, false, &flags)
|
2019-05-23 12:53:44 +00:00
|
|
|
}
|
|
|
|
case <-timeout:
|
2021-03-03 11:26:59 +00:00
|
|
|
windows.CancelIoEx(f.handle, &c.o)
|
2019-05-23 12:53:44 +00:00
|
|
|
r = <-c.ch
|
|
|
|
err = r.err
|
2019-09-17 05:39:16 +00:00
|
|
|
if err == windows.ERROR_OPERATION_ABORTED {
|
2021-03-03 11:26:59 +00:00
|
|
|
err = os.ErrDeadlineExceeded
|
2019-05-23 12:53:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// runtime.KeepAlive is needed, as c is passed via native
|
|
|
|
// code to ioCompletionProcessor, c must remain alive
|
|
|
|
// until the channel read is complete.
|
|
|
|
runtime.KeepAlive(c)
|
|
|
|
return int(r.bytes), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read reads from a file handle.
|
2021-03-03 11:26:59 +00:00
|
|
|
func (f *file) Read(b []byte) (int, error) {
|
2019-05-23 12:53:44 +00:00
|
|
|
c, err := f.prepareIo()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer f.wg.Done()
|
|
|
|
|
2021-03-03 11:26:59 +00:00
|
|
|
if atomic.LoadUint32(&f.readDeadline.timedout) == 1 {
|
|
|
|
return 0, os.ErrDeadlineExceeded
|
2019-05-23 12:53:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var bytes uint32
|
2019-09-17 05:39:16 +00:00
|
|
|
err = windows.ReadFile(f.handle, b, &bytes, &c.o)
|
2019-05-23 12:53:44 +00:00
|
|
|
n, err := f.asyncIo(c, &f.readDeadline, bytes, err)
|
|
|
|
runtime.KeepAlive(b)
|
|
|
|
|
|
|
|
// Handle EOF conditions.
|
|
|
|
if err == nil && n == 0 && len(b) != 0 {
|
|
|
|
return 0, io.EOF
|
2019-09-17 05:39:16 +00:00
|
|
|
} else if err == windows.ERROR_BROKEN_PIPE {
|
2019-05-23 12:53:44 +00:00
|
|
|
return 0, io.EOF
|
|
|
|
} else {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes to a file handle.
|
2021-03-03 11:26:59 +00:00
|
|
|
func (f *file) Write(b []byte) (int, error) {
|
2019-05-23 12:53:44 +00:00
|
|
|
c, err := f.prepareIo()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer f.wg.Done()
|
|
|
|
|
2021-03-03 11:26:59 +00:00
|
|
|
if atomic.LoadUint32(&f.writeDeadline.timedout) == 1 {
|
|
|
|
return 0, os.ErrDeadlineExceeded
|
2019-05-23 12:53:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var bytes uint32
|
2019-09-17 05:39:16 +00:00
|
|
|
err = windows.WriteFile(f.handle, b, &bytes, &c.o)
|
2019-05-23 12:53:44 +00:00
|
|
|
n, err := f.asyncIo(c, &f.writeDeadline, bytes, err)
|
|
|
|
runtime.KeepAlive(b)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2021-03-03 11:26:59 +00:00
|
|
|
func (f *file) SetReadDeadline(deadline time.Time) error {
|
2019-05-23 12:53:44 +00:00
|
|
|
return f.readDeadline.set(deadline)
|
|
|
|
}
|
|
|
|
|
2021-03-03 11:26:59 +00:00
|
|
|
func (f *file) SetWriteDeadline(deadline time.Time) error {
|
2019-05-23 12:53:44 +00:00
|
|
|
return f.writeDeadline.set(deadline)
|
|
|
|
}
|
|
|
|
|
2021-03-03 11:26:59 +00:00
|
|
|
func (f *file) Flush() error {
|
2019-09-17 05:39:16 +00:00
|
|
|
return windows.FlushFileBuffers(f.handle)
|
2019-05-23 12:53:44 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 11:26:59 +00:00
|
|
|
func (f *file) Fd() uintptr {
|
2019-05-23 12:53:44 +00:00
|
|
|
return uintptr(f.handle)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *deadlineHandler) set(deadline time.Time) error {
|
|
|
|
d.setLock.Lock()
|
|
|
|
defer d.setLock.Unlock()
|
|
|
|
|
|
|
|
if d.timer != nil {
|
|
|
|
if !d.timer.Stop() {
|
|
|
|
<-d.channel
|
|
|
|
}
|
|
|
|
d.timer = nil
|
|
|
|
}
|
2021-03-03 11:26:59 +00:00
|
|
|
atomic.StoreUint32(&d.timedout, 0)
|
2019-05-23 12:53:44 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-d.channel:
|
|
|
|
d.channelLock.Lock()
|
|
|
|
d.channel = make(chan struct{})
|
|
|
|
d.channelLock.Unlock()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
if deadline.IsZero() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
timeoutIO := func() {
|
2021-03-03 11:26:59 +00:00
|
|
|
atomic.StoreUint32(&d.timedout, 1)
|
2019-05-23 12:53:44 +00:00
|
|
|
close(d.channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
duration := deadline.Sub(now)
|
|
|
|
if deadline.After(now) {
|
|
|
|
// Deadline is in the future, set a timer to wait
|
|
|
|
d.timer = time.AfterFunc(duration, timeoutIO)
|
|
|
|
} else {
|
|
|
|
// Deadline is in the past. Cancel all pending IO now.
|
|
|
|
timeoutIO()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|