"internal/poll": {"L0", "internal/race", "syscall", "time", "unicode/utf16", "unicode/utf8", "internal/syscall/windows"},
"internal/testlog": {"L0"},
- "os": {"L1", "os", "syscall", "time", "internal/poll", "internal/syscall/windows", "internal/testlog"},
+ "os": {"L1", "os", "syscall", "time", "internal/poll", "internal/syscall/windows", "internal/syscall/unix", "internal/testlog"},
"path/filepath": {"L2", "os", "syscall", "internal/syscall/windows"},
"io/ioutil": {"L2", "os", "path/filepath", "time"},
"os/exec": {"L2", "os", "context", "path/filepath", "syscall"},
--- /dev/null
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// This file is here just to make the go tool happy. It allows
+// empty function declarations (no function body).
+// It is used with "go:linkname".
--- /dev/null
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build darwin dragonfly freebsd linux netbsd openbsd solaris
+
+package unix
+
+import (
+ "syscall"
+ _ "unsafe" // for go:linkname
+)
+
+//go:linkname syscall_fcntl syscall.fcntl
+func syscall_fcntl(fd int, cmd int, arg int) (val int, err error)
+
+func IsNonblock(fd int) (nonblocking bool, err error) {
+ flag, err := syscall_fcntl(fd, syscall.F_GETFL, 0)
+ if err != nil {
+ return false, err
+ }
+ return flag&syscall.O_NONBLOCK != 0, nil
+}
--- /dev/null
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package unix
+
+func IsNonblock(fd int) (nonblocking bool, err error) {
+ return false, nil
+}
// stdin, stdout, stderr, epoll/kqueue, maybe testlog
func basefds() uintptr {
n := os.Stderr.Fd() + 1
+ // The poll (epoll/kqueue) descriptor can be numerically
+ // either between stderr and the testlog-fd, or after
+ // testlog-fd.
+ if poll.PollDescriptor() == n {
+ n++
+ }
for _, arg := range os.Args {
if strings.HasPrefix(arg, "-test.testlogfile=") {
n++
import (
"internal/poll"
+ "internal/syscall/unix"
"runtime"
"syscall"
)
// NewFile returns a new File with the given file descriptor and
// name. The returned value will be nil if fd is not a valid file
-// descriptor.
+// descriptor. On Unix systems, if the file descriptor is in
+// non-blocking mode, NewFile will attempt to return a pollable File
+// (one for which the SetDeadline methods work).
func NewFile(fd uintptr, name string) *File {
- return newFile(fd, name, kindNewFile)
+ kind := kindNewFile
+ if nb, err := unix.IsNonblock(int(fd)); err == nil && nb {
+ kind = kindNonBlock
+ }
+ return newFile(fd, name, kind)
}
// newFileKind describes the kind of file to newFile.
kindNewFile newFileKind = iota
kindOpenFile
kindPipe
+ kindNonBlock
)
// newFile is like NewFile, but if called from OpenFile or Pipe
// Don't try to use kqueue with regular files on FreeBSD.
// It crashes the system unpredictably while running all.bash.
// Issue 19093.
+ // If the caller passed a non-blocking filedes (kindNonBlock),
+ // we assume they know what they are doing so we allow it to be
+ // used with kqueue.
if runtime.GOOS == "freebsd" && kind == kindOpenFile {
kind = kindNewFile
}
- pollable := kind == kindOpenFile || kind == kindPipe
+ pollable := kind == kindOpenFile || kind == kindPipe || kind == kindNonBlock
if err := f.pfd.Init("file", pollable); err != nil {
// An error here indicates a failure to register
// with the netpoll system. That can happen for
"strings"
"syscall"
"testing"
+ "time"
)
func init() {
t.Errorf("unexpected mode %s", mode)
}
}
+
+// See also issues: 22939, 24331
+func newFileTest(t *testing.T, blocking bool) {
+ p := make([]int, 2)
+ if err := syscall.Pipe(p); err != nil {
+ t.Fatalf("pipe: %v", err)
+ }
+ defer syscall.Close(p[1])
+
+ // Set the the read-side to non-blocking.
+ if !blocking {
+ if err := syscall.SetNonblock(p[0], true); err != nil {
+ syscall.Close(p[0])
+ t.Fatalf("SetNonblock: %v", err)
+ }
+ }
+ // Convert it to a file.
+ file := NewFile(uintptr(p[0]), "notapipe")
+ if file == nil {
+ syscall.Close(p[0])
+ t.Fatalf("failed to convert fd to file!")
+ }
+ defer file.Close()
+
+ // Try to read with deadline (but don't block forever).
+ b := make([]byte, 1)
+ // Send something after 100ms.
+ timer := time.AfterFunc(100*time.Millisecond, func() { syscall.Write(p[1], []byte("a")) })
+ defer timer.Stop()
+ file.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
+ _, err := file.Read(b)
+ if !blocking {
+ // We want it to fail with a timeout.
+ if !IsTimeout(err) {
+ t.Fatalf("No timeout reading from file: %v", err)
+ }
+ } else {
+ // We want it to succeed after 100ms
+ if err != nil {
+ t.Fatalf("Error reading from file: %v", err)
+ }
+ }
+}
+
+func TestNewFileBlock(t *testing.T) {
+ t.Parallel()
+ newFileTest(t, true)
+}
+
+func TestNewFileNonBlock(t *testing.T) {
+ t.Parallel()
+ newFileTest(t, false)
+}