]> Cypherpunks repositories - gostls13.git/commitdiff
internal/poll, internal/syscall/unix, net: move and export fcntl syscall wrapper
authorTobias Klauser <tklauser@distanz.ch>
Wed, 17 May 2023 07:57:34 +0000 (09:57 +0200)
committerGopher Robot <gobot@golang.org>
Thu, 18 May 2023 09:15:25 +0000 (09:15 +0000)
This will allow to use the fcntl syscall in packages other than
internal/poll.

For #60181

Change-Id: I76703766a655f2343c61dad95faf81aad58e007f
Reviewed-on: https://go-review.googlesource.com/c/go/+/494916
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

14 files changed:
src/internal/poll/fcntl_libc.go [deleted file]
src/internal/poll/fcntl_syscall.go [deleted file]
src/internal/poll/fcntl_wasm.go [deleted file]
src/internal/poll/fd_fsync_darwin.go
src/internal/poll/fd_unix.go
src/internal/poll/splice_linux.go
src/internal/syscall/unix/fcntl_linux_32bit.go [deleted file]
src/internal/syscall/unix/fcntl_unix.go [new file with mode: 0644]
src/internal/syscall/unix/fcntl_wasm.go [new file with mode: 0644]
src/internal/syscall/unix/nonblocking.go [deleted file]
src/internal/syscall/unix/nonblocking_unix.go [moved from src/internal/syscall/unix/nonblocking_libc.go with 58% similarity]
src/net/fcntl_libc_test.go [deleted file]
src/net/fcntl_syscall_test.go [deleted file]
src/net/unixsock_readmsg_test.go

diff --git a/src/internal/poll/fcntl_libc.go b/src/internal/poll/fcntl_libc.go
deleted file mode 100644 (file)
index 529b8e1..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-// Copyright 2019 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.
-
-//go:build aix || darwin || (openbsd && !mips64) || solaris
-
-package poll
-
-import _ "unsafe" // for go:linkname
-
-// Implemented in the syscall package.
-//
-//go:linkname fcntl syscall.fcntl
-func fcntl(fd int, cmd int, arg int) (int, error)
diff --git a/src/internal/poll/fcntl_syscall.go b/src/internal/poll/fcntl_syscall.go
deleted file mode 100644 (file)
index bbfc8a8..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-// Copyright 2019 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.
-
-//go:build dragonfly || freebsd || linux || netbsd || (openbsd && mips64)
-
-package poll
-
-import (
-       "internal/syscall/unix"
-       "syscall"
-)
-
-func fcntl(fd int, cmd int, arg int) (int, error) {
-       r, _, e := syscall.Syscall(unix.FcntlSyscall, uintptr(fd), uintptr(cmd), uintptr(arg))
-       if e != 0 {
-               return int(r), syscall.Errno(e)
-       }
-       return int(r), nil
-}
diff --git a/src/internal/poll/fcntl_wasm.go b/src/internal/poll/fcntl_wasm.go
deleted file mode 100644 (file)
index ab77e4d..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-// Copyright 2019 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.
-
-//go:build (js && wasm) || wasip1
-
-package poll
-
-import "syscall"
-
-// fcntl not supported on js/wasm or wasip1/wasm.
-func fcntl(fd int, cmd int, arg int) (int, error) {
-       return 0, syscall.ENOSYS
-}
index 48e7596922ba5b54a89375b92ca914911107199a..731b7fd5bd65a76cdefb5e85b259c300020700fb 100644 (file)
@@ -4,7 +4,10 @@
 
 package poll
 
-import "syscall"
+import (
+       "internal/syscall/unix"
+       "syscall"
+)
 
 // Fsync invokes SYS_FCNTL with SYS_FULLFSYNC because
 // on OS X, SYS_FSYNC doesn't fully flush contents to disk.
@@ -15,7 +18,7 @@ func (fd *FD) Fsync() error {
        }
        defer fd.decref()
        return ignoringEINTR(func() error {
-               _, err := fcntl(fd.Sysfd, syscall.F_FULLFSYNC, 0)
+               _, err := unix.Fcntl(fd.Sysfd, syscall.F_FULLFSYNC, 0)
                return err
        })
 }
index efc25f6a51a96c69f85f3a065f910f14e5ea511d..0175b91ecfcbc39c8277d77f969be8b3fca098cb 100644 (file)
@@ -653,18 +653,18 @@ var dupCloexecUnsupported atomic.Bool
 // DupCloseOnExec dups fd and marks it close-on-exec.
 func DupCloseOnExec(fd int) (int, string, error) {
        if syscall.F_DUPFD_CLOEXEC != 0 && !dupCloexecUnsupported.Load() {
-               r0, e1 := fcntl(fd, syscall.F_DUPFD_CLOEXEC, 0)
-               if e1 == nil {
+               r0, err := unix.Fcntl(fd, syscall.F_DUPFD_CLOEXEC, 0)
+               if err == nil {
                        return r0, "", nil
                }
-               switch e1.(syscall.Errno) {
+               switch err {
                case syscall.EINVAL, syscall.ENOSYS:
                        // Old kernel, or js/wasm (which returns
                        // ENOSYS). Fall back to the portable way from
                        // now on.
                        dupCloexecUnsupported.Store(true)
                default:
-                       return -1, "fcntl", e1
+                       return -1, "fcntl", err
                }
        }
        return dupCloseOnExecOld(fd)
index ae7e42d0e6542adf17b13f29445b394c57d19690..9505c5dcfc1eb10d06a59484eabc87621f51097b 100644 (file)
@@ -5,6 +5,7 @@
 package poll
 
 import (
+       "internal/syscall/unix"
        "runtime"
        "sync"
        "syscall"
@@ -220,7 +221,7 @@ func newPipe() *splicePipe {
        // Set the pipe buffer size to maxSpliceSize to optimize that.
        // Ignore errors here, as a smaller buffer size will work,
        // although it will require more system calls.
-       fcntl(fds[0], syscall.F_SETPIPE_SZ, maxSpliceSize)
+       unix.Fcntl(fds[0], syscall.F_SETPIPE_SZ, maxSpliceSize)
 
        return &splicePipe{splicePipeFields: splicePipeFields{rfd: fds[0], wfd: fds[1]}}
 }
diff --git a/src/internal/syscall/unix/fcntl_linux_32bit.go b/src/internal/syscall/unix/fcntl_linux_32bit.go
deleted file mode 100644 (file)
index 7b39ee7..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-// Copyright 2019 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.
-
-// On 32-bit Linux systems, use SYS_FCNTL64.
-// If you change the build tags here, see syscall/flock_linux_32bit.go.
-
-//go:build (linux && 386) || (linux && arm) || (linux && mips) || (linux && mipsle)
-
-package unix
-
-import "syscall"
-
-func init() {
-       FcntlSyscall = syscall.SYS_FCNTL64
-}
diff --git a/src/internal/syscall/unix/fcntl_unix.go b/src/internal/syscall/unix/fcntl_unix.go
new file mode 100644 (file)
index 0000000..104c5a3
--- /dev/null
@@ -0,0 +1,25 @@
+// Copyright 2023 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.
+
+//go:build unix
+
+package unix
+
+import (
+       "syscall"
+       _ "unsafe" // for go:linkname
+)
+
+// Implemented in the runtime package.
+//
+//go:linkname fcntl runtime.fcntl
+func fcntl(fd int32, cmd int32, arg int32) int32
+
+func Fcntl(fd int, cmd int, arg int) (int, error) {
+       val := fcntl(int32(fd), int32(cmd), int32(arg))
+       if val < 0 {
+               return 0, syscall.Errno(-val)
+       }
+       return int(val), nil
+}
diff --git a/src/internal/syscall/unix/fcntl_wasm.go b/src/internal/syscall/unix/fcntl_wasm.go
new file mode 100644 (file)
index 0000000..c630273
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright 2023 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
+
+import "syscall"
+
+func Fcntl(fd int, cmd int, arg int) (int, error) {
+       return 0, syscall.ENOSYS
+}
diff --git a/src/internal/syscall/unix/nonblocking.go b/src/internal/syscall/unix/nonblocking.go
deleted file mode 100644 (file)
index 6c6f067..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-// 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.
-
-//go:build dragonfly || freebsd || linux || netbsd || (openbsd && mips64)
-
-package unix
-
-import "syscall"
-
-// FcntlSyscall is the number for the fcntl system call. This is
-// usually SYS_FCNTL, but can be overridden to SYS_FCNTL64.
-var FcntlSyscall uintptr = syscall.SYS_FCNTL
-
-func IsNonblock(fd int) (nonblocking bool, err error) {
-       flag, _, e1 := syscall.Syscall(FcntlSyscall, uintptr(fd), uintptr(syscall.F_GETFL), 0)
-       if e1 != 0 {
-               return false, e1
-       }
-       return flag&syscall.O_NONBLOCK != 0, nil
-}
-
-func HasNonblockFlag(flag int) bool {
-       return flag&syscall.O_NONBLOCK != 0
-}
similarity index 58%
rename from src/internal/syscall/unix/nonblocking_libc.go
rename to src/internal/syscall/unix/nonblocking_unix.go
index 1310dbf8ce5478f82ace66958a541d50249d555d..fc0bc27916f4e99721578a4d2bfbbeddc1ef1bbe 100644 (file)
@@ -2,17 +2,14 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-//go:build aix || darwin || (openbsd && !mips64) || solaris
+//go:build unix
 
 package unix
 
-import (
-       "syscall"
-       _ "unsafe" // for go:linkname
-)
+import "syscall"
 
 func IsNonblock(fd int) (nonblocking bool, err error) {
-       flag, e1 := fcntl(fd, syscall.F_GETFL, 0)
+       flag, e1 := Fcntl(fd, syscall.F_GETFL, 0)
        if e1 != nil {
                return false, e1
        }
@@ -22,8 +19,3 @@ func IsNonblock(fd int) (nonblocking bool, err error) {
 func HasNonblockFlag(flag int) bool {
        return flag&syscall.O_NONBLOCK != 0
 }
-
-// Implemented in the syscall package.
-//
-//go:linkname fcntl syscall.fcntl
-func fcntl(fd int, cmd int, arg int) (int, error)
diff --git a/src/net/fcntl_libc_test.go b/src/net/fcntl_libc_test.go
deleted file mode 100644 (file)
index 5858865..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-// Copyright 2021 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.
-
-//go:build aix || darwin || (openbsd && !mips64) || solaris
-
-package net
-
-import _ "unsafe" // for go:linkname
-
-// Implemented in the syscall package.
-//
-//go:linkname fcntl syscall.fcntl
-func fcntl(fd int, cmd int, arg int) (int, error)
diff --git a/src/net/fcntl_syscall_test.go b/src/net/fcntl_syscall_test.go
deleted file mode 100644 (file)
index b9ac1d3..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-// Copyright 2021 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.
-
-//go:build dragonfly || freebsd || linux || netbsd || (openbsd && mips64)
-
-package net
-
-import (
-       "internal/syscall/unix"
-       "syscall"
-)
-
-func fcntl(fd int, cmd int, arg int) (int, error) {
-       r, _, e := syscall.Syscall(unix.FcntlSyscall, uintptr(fd), uintptr(cmd), uintptr(arg))
-       if e != 0 {
-               return int(r), syscall.Errno(e)
-       }
-       return int(r), nil
-}
index 414f626644b6b38c1988409b0c9b4a7f4e226d62..2d89dc4936a12904183ac85dbaa0ff560bfb9975 100644 (file)
@@ -7,6 +7,7 @@
 package net
 
 import (
+       "internal/syscall/unix"
        "os"
        "syscall"
        "testing"
@@ -94,7 +95,7 @@ func TestUnixConnReadMsgUnixSCMRightsCloseOnExec(t *testing.T) {
                }
        }()
 
-       flags, err := fcntl(gotFDs[0], syscall.F_GETFD, 0)
+       flags, err := unix.Fcntl(gotFDs[0], syscall.F_GETFD, 0)
        if err != nil {
                t.Fatalf("Can't get flags of fd:%#v, with err:%v", gotFDs[0], err)
        }