From ecb9ecfb720bc4693f3ddaccb6cbfbe274d9f3b7 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Mon, 15 May 2023 16:34:36 -0700 Subject: [release-branch.go1.19] runtime: consistently define fcntl Clean up and consolidate on a single consistent definition of fcntl, which takes three int32 arguments and returns either a positive result or a negative errno value. Change-Id: Id9505492712db4b0aab469c6bd15e4fce3c9ff6e Reviewed-on: https://go-review.googlesource.com/c/go/+/495075 Run-TryBot: Ian Lance Taylor Reviewed-by: Ian Lance Taylor TryBot-Result: Gopher Robot Auto-Submit: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Reviewed-by: Tobias Klauser Reviewed-by: Michael Pratt Reviewed-on: https://go-review.googlesource.com/c/go/+/497135 Auto-Submit: Heschi Kreinick Run-TryBot: Roland Shoemaker --- src/runtime/export_aix_test.go | 1 - src/runtime/export_darwin_test.go | 8 ------ src/runtime/export_solaris_test.go | 9 ------- src/runtime/export_unix_test.go | 1 + src/runtime/internal/syscall/defs_linux_386.go | 7 +++++ src/runtime/internal/syscall/defs_linux_amd64.go | 7 +++++ src/runtime/internal/syscall/defs_linux_arm.go | 7 +++++ src/runtime/internal/syscall/defs_linux_arm64.go | 7 +++++ src/runtime/internal/syscall/defs_linux_loong64.go | 7 +++++ src/runtime/internal/syscall/defs_linux_mips64x.go | 9 +++++++ src/runtime/internal/syscall/defs_linux_mipsx.go | 9 +++++++ src/runtime/internal/syscall/defs_linux_ppc64x.go | 9 +++++++ src/runtime/internal/syscall/defs_linux_riscv64.go | 7 +++++ src/runtime/internal/syscall/defs_linux_s390x.go | 7 +++++ src/runtime/nbpipe_fcntl_libc_test.go | 18 ------------- src/runtime/nbpipe_fcntl_unix_test.go | 17 ------------ src/runtime/nbpipe_test.go | 30 +++++++++++++--------- src/runtime/netpoll_solaris.go | 4 --- src/runtime/os3_solaris.go | 9 +++++++ src/runtime/os_aix.go | 5 +++- src/runtime/os_dragonfly.go | 1 + src/runtime/os_freebsd.go | 1 + src/runtime/os_linux.go | 10 ++++++++ src/runtime/os_netbsd.go | 1 + src/runtime/os_openbsd_syscall2.go | 1 + src/runtime/os_solaris.go | 2 +- src/runtime/sys_darwin_amd64.s | 6 +++++ src/runtime/sys_darwin_arm64.s | 7 +++++ src/runtime/sys_dragonfly_amd64.s | 12 +++++++++ src/runtime/sys_freebsd_386.s | 9 +++++++ src/runtime/sys_freebsd_amd64.s | 12 +++++++++ src/runtime/sys_freebsd_arm.s | 11 ++++++++ src/runtime/sys_freebsd_arm64.s | 13 ++++++++++ src/runtime/sys_netbsd_386.s | 9 +++++++ src/runtime/sys_netbsd_amd64.s | 12 +++++++++ src/runtime/sys_netbsd_arm.s | 10 ++++++++ src/runtime/sys_netbsd_arm64.s | 12 +++++++++ src/runtime/sys_openbsd_386.s | 6 +++++ src/runtime/sys_openbsd_amd64.s | 6 +++++ src/runtime/sys_openbsd_arm.s | 6 +++++ src/runtime/sys_openbsd_arm64.s | 6 +++++ src/runtime/sys_openbsd_mips64.s | 12 +++++++++ 42 files changed, 272 insertions(+), 71 deletions(-) delete mode 100644 src/runtime/export_solaris_test.go create mode 100644 src/runtime/internal/syscall/defs_linux_386.go create mode 100644 src/runtime/internal/syscall/defs_linux_amd64.go create mode 100644 src/runtime/internal/syscall/defs_linux_arm.go create mode 100644 src/runtime/internal/syscall/defs_linux_arm64.go create mode 100644 src/runtime/internal/syscall/defs_linux_loong64.go create mode 100644 src/runtime/internal/syscall/defs_linux_mips64x.go create mode 100644 src/runtime/internal/syscall/defs_linux_mipsx.go create mode 100644 src/runtime/internal/syscall/defs_linux_ppc64x.go create mode 100644 src/runtime/internal/syscall/defs_linux_riscv64.go create mode 100644 src/runtime/internal/syscall/defs_linux_s390x.go delete mode 100644 src/runtime/nbpipe_fcntl_libc_test.go delete mode 100644 src/runtime/nbpipe_fcntl_unix_test.go diff --git a/src/runtime/export_aix_test.go b/src/runtime/export_aix_test.go index 51df9517384..48455333c14 100644 --- a/src/runtime/export_aix_test.go +++ b/src/runtime/export_aix_test.go @@ -4,5 +4,4 @@ package runtime -var Fcntl = syscall_fcntl1 var SetNonblock = setNonblock diff --git a/src/runtime/export_darwin_test.go b/src/runtime/export_darwin_test.go index 66e2c02c4f6..48455333c14 100644 --- a/src/runtime/export_darwin_test.go +++ b/src/runtime/export_darwin_test.go @@ -4,12 +4,4 @@ package runtime -func Fcntl(fd, cmd, arg uintptr) (uintptr, uintptr) { - r := fcntl(int32(fd), int32(cmd), int32(arg)) - if r < 0 { - return ^uintptr(0), uintptr(-r) - } - return uintptr(r), 0 -} - var SetNonblock = setNonblock diff --git a/src/runtime/export_solaris_test.go b/src/runtime/export_solaris_test.go deleted file mode 100644 index e865c77691e..00000000000 --- a/src/runtime/export_solaris_test.go +++ /dev/null @@ -1,9 +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. - -package runtime - -func Fcntl(fd, cmd, arg uintptr) (uintptr, uintptr) { - return sysvicall3Err(&libc_fcntl, fd, cmd, arg) -} diff --git a/src/runtime/export_unix_test.go b/src/runtime/export_unix_test.go index a548cf7b7a5..8fe95770cdb 100644 --- a/src/runtime/export_unix_test.go +++ b/src/runtime/export_unix_test.go @@ -10,6 +10,7 @@ import "unsafe" var NonblockingPipe = nonblockingPipe var Closeonexec = closeonexec +var Fcntl = fcntl func sigismember(mask *sigset, i int) bool { clear := *mask diff --git a/src/runtime/internal/syscall/defs_linux_386.go b/src/runtime/internal/syscall/defs_linux_386.go new file mode 100644 index 00000000000..31d704e2350 --- /dev/null +++ b/src/runtime/internal/syscall/defs_linux_386.go @@ -0,0 +1,7 @@ +// 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 syscall + +const SYS_FCNTL = 55 diff --git a/src/runtime/internal/syscall/defs_linux_amd64.go b/src/runtime/internal/syscall/defs_linux_amd64.go new file mode 100644 index 00000000000..2368eb03b43 --- /dev/null +++ b/src/runtime/internal/syscall/defs_linux_amd64.go @@ -0,0 +1,7 @@ +// 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 syscall + +const SYS_FCNTL = 72 diff --git a/src/runtime/internal/syscall/defs_linux_arm.go b/src/runtime/internal/syscall/defs_linux_arm.go new file mode 100644 index 00000000000..31d704e2350 --- /dev/null +++ b/src/runtime/internal/syscall/defs_linux_arm.go @@ -0,0 +1,7 @@ +// 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 syscall + +const SYS_FCNTL = 55 diff --git a/src/runtime/internal/syscall/defs_linux_arm64.go b/src/runtime/internal/syscall/defs_linux_arm64.go new file mode 100644 index 00000000000..6292c90af5c --- /dev/null +++ b/src/runtime/internal/syscall/defs_linux_arm64.go @@ -0,0 +1,7 @@ +// 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 syscall + +const SYS_FCNTL = 25 diff --git a/src/runtime/internal/syscall/defs_linux_loong64.go b/src/runtime/internal/syscall/defs_linux_loong64.go new file mode 100644 index 00000000000..6292c90af5c --- /dev/null +++ b/src/runtime/internal/syscall/defs_linux_loong64.go @@ -0,0 +1,7 @@ +// 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 syscall + +const SYS_FCNTL = 25 diff --git a/src/runtime/internal/syscall/defs_linux_mips64x.go b/src/runtime/internal/syscall/defs_linux_mips64x.go new file mode 100644 index 00000000000..38d2acbe95b --- /dev/null +++ b/src/runtime/internal/syscall/defs_linux_mips64x.go @@ -0,0 +1,9 @@ +// 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 linux && (mips64 || mips64le) + +package syscall + +const SYS_FCNTL = 5070 diff --git a/src/runtime/internal/syscall/defs_linux_mipsx.go b/src/runtime/internal/syscall/defs_linux_mipsx.go new file mode 100644 index 00000000000..e4eb0d7bc23 --- /dev/null +++ b/src/runtime/internal/syscall/defs_linux_mipsx.go @@ -0,0 +1,9 @@ +// 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 linux && (mips || mipsle) + +package syscall + +const SYS_FCNTL = 4055 diff --git a/src/runtime/internal/syscall/defs_linux_ppc64x.go b/src/runtime/internal/syscall/defs_linux_ppc64x.go new file mode 100644 index 00000000000..cc45a3d7c89 --- /dev/null +++ b/src/runtime/internal/syscall/defs_linux_ppc64x.go @@ -0,0 +1,9 @@ +// 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 linux && (ppc64 || ppc64le) + +package syscall + +const SYS_FCNTL = 55 diff --git a/src/runtime/internal/syscall/defs_linux_riscv64.go b/src/runtime/internal/syscall/defs_linux_riscv64.go new file mode 100644 index 00000000000..6292c90af5c --- /dev/null +++ b/src/runtime/internal/syscall/defs_linux_riscv64.go @@ -0,0 +1,7 @@ +// 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 syscall + +const SYS_FCNTL = 25 diff --git a/src/runtime/internal/syscall/defs_linux_s390x.go b/src/runtime/internal/syscall/defs_linux_s390x.go new file mode 100644 index 00000000000..31d704e2350 --- /dev/null +++ b/src/runtime/internal/syscall/defs_linux_s390x.go @@ -0,0 +1,7 @@ +// 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 syscall + +const SYS_FCNTL = 55 diff --git a/src/runtime/nbpipe_fcntl_libc_test.go b/src/runtime/nbpipe_fcntl_libc_test.go deleted file mode 100644 index a9c8987438d..00000000000 --- a/src/runtime/nbpipe_fcntl_libc_test.go +++ /dev/null @@ -1,18 +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 || solaris - -package runtime_test - -import ( - "runtime" - "syscall" -) - -// Call fcntl libc function rather than calling syscall. -func fcntl(fd uintptr, cmd int, arg uintptr) (uintptr, syscall.Errno) { - res, errno := runtime.Fcntl(fd, uintptr(cmd), arg) - return res, syscall.Errno(errno) -} diff --git a/src/runtime/nbpipe_fcntl_unix_test.go b/src/runtime/nbpipe_fcntl_unix_test.go deleted file mode 100644 index 97607fa2cf7..00000000000 --- a/src/runtime/nbpipe_fcntl_unix_test.go +++ /dev/null @@ -1,17 +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 - -package runtime_test - -import ( - "internal/syscall/unix" - "syscall" -) - -func fcntl(fd uintptr, cmd int, arg uintptr) (uintptr, syscall.Errno) { - res, _, err := syscall.Syscall(unix.FcntlSyscall, fd, uintptr(cmd), arg) - return res, err -} diff --git a/src/runtime/nbpipe_test.go b/src/runtime/nbpipe_test.go index 0b0f64d0764..bb21003c358 100644 --- a/src/runtime/nbpipe_test.go +++ b/src/runtime/nbpipe_test.go @@ -14,23 +14,29 @@ import ( ) func TestNonblockingPipe(t *testing.T) { - t.Parallel() - // NonblockingPipe is the test name for nonblockingPipe. r, w, errno := runtime.NonblockingPipe() if errno != 0 { t.Fatal(syscall.Errno(errno)) } - defer func() { - runtime.Close(r) - runtime.Close(w) - }() + defer runtime.Close(w) checkIsPipe(t, r, w) checkNonblocking(t, r, "reader") checkCloseonexec(t, r, "reader") checkNonblocking(t, w, "writer") checkCloseonexec(t, w, "writer") + + // Test that fcntl returns an error as expected. + if runtime.Close(r) != 0 { + t.Fatalf("Close(%d) failed", r) + } + val := runtime.Fcntl(r, syscall.F_GETFD, 0) + if val >= 0 { + t.Errorf("Fcntl succeeded unexpectedly") + } else if syscall.Errno(-val) != syscall.EBADF { + t.Errorf("Fcntl failed with error %v, expected %v", -val, syscall.EBADF) + } } func checkIsPipe(t *testing.T, r, w int32) { @@ -49,9 +55,9 @@ func checkIsPipe(t *testing.T, r, w int32) { func checkNonblocking(t *testing.T, fd int32, name string) { t.Helper() - flags, errno := fcntl(uintptr(fd), syscall.F_GETFL, 0) - if errno != 0 { - t.Errorf("fcntl(%s, F_GETFL) failed: %v", name, syscall.Errno(errno)) + flags := runtime.Fcntl(fd, syscall.F_GETFL, 0) + if flags < 0 { + t.Errorf("fcntl(%s, F_GETFL) failed: %v", name, syscall.Errno(-flags)) } else if flags&syscall.O_NONBLOCK == 0 { t.Errorf("O_NONBLOCK not set in %s flags %#x", name, flags) } @@ -59,9 +65,9 @@ func checkNonblocking(t *testing.T, fd int32, name string) { func checkCloseonexec(t *testing.T, fd int32, name string) { t.Helper() - flags, errno := fcntl(uintptr(fd), syscall.F_GETFD, 0) - if errno != 0 { - t.Errorf("fcntl(%s, F_GETFD) failed: %v", name, syscall.Errno(errno)) + flags := runtime.Fcntl(fd, syscall.F_GETFD, 0) + if flags < 0 { + t.Errorf("fcntl(%s, F_GETFD) failed: %v", name, syscall.Errno(flags)) } else if flags&syscall.FD_CLOEXEC == 0 { t.Errorf("FD_CLOEXEC not set in %s flags %#x", name, flags) } diff --git a/src/runtime/netpoll_solaris.go b/src/runtime/netpoll_solaris.go index 6e545b3d31b..034363ea1cd 100644 --- a/src/runtime/netpoll_solaris.go +++ b/src/runtime/netpoll_solaris.go @@ -95,10 +95,6 @@ func errno() int32 { return *getg().m.perrno } -func fcntl(fd, cmd, arg int32) int32 { - return int32(sysvicall3(&libc_fcntl, uintptr(fd), uintptr(cmd), uintptr(arg))) -} - func port_create() int32 { return int32(sysvicall0(&libc_port_create)) } diff --git a/src/runtime/os3_solaris.go b/src/runtime/os3_solaris.go index 79f6b18c9ae..783a2949760 100644 --- a/src/runtime/os3_solaris.go +++ b/src/runtime/os3_solaris.go @@ -568,6 +568,15 @@ func pipe2(flags int32) (r, w int32, errno int32) { return p[0], p[1], int32(e) } +//go:nosplit +func fcntl(fd, cmd, arg int32) int32 { + r1, err := sysvicall3Err(&libc_fcntl, uintptr(fd), uintptr(cmd), uintptr(arg)) + if r := int32(r1); r >= 0 { + return r + } + return -int32(err) +} + //go:nosplit func closeonexec(fd int32) { fcntl(fd, _F_SETFD, _FD_CLOEXEC) diff --git a/src/runtime/os_aix.go b/src/runtime/os_aix.go index 104c397e8c0..66b4d94cf20 100644 --- a/src/runtime/os_aix.go +++ b/src/runtime/os_aix.go @@ -362,7 +362,10 @@ func walltime() (sec int64, nsec int32) { //go:nosplit func fcntl(fd, cmd, arg int32) int32 { - r, _ := syscall3(&libc_fcntl, uintptr(fd), uintptr(cmd), uintptr(arg)) + r, errno := syscall3(&libc_fcntl, uintptr(fd), uintptr(cmd), uintptr(arg)) + if int32(r) < 0 { + return -int32(errno) + } return int32(r) } diff --git a/src/runtime/os_dragonfly.go b/src/runtime/os_dragonfly.go index 83478143b93..728d2bcdab1 100644 --- a/src/runtime/os_dragonfly.go +++ b/src/runtime/os_dragonfly.go @@ -63,6 +63,7 @@ func kqueue() int32 func kevent(kq int32, ch *keventt, nch int32, ev *keventt, nev int32, ts *timespec) int32 func pipe2(flags int32) (r, w int32, errno int32) +func fcntl(fd, cmd, arg int32) int32 func closeonexec(fd int32) // From DragonFly's diff --git a/src/runtime/os_freebsd.go b/src/runtime/os_freebsd.go index 23efd1a46e8..94a6de2cdfd 100644 --- a/src/runtime/os_freebsd.go +++ b/src/runtime/os_freebsd.go @@ -48,6 +48,7 @@ func kqueue() int32 func kevent(kq int32, ch *keventt, nch int32, ev *keventt, nev int32, ts *timespec) int32 func pipe2(flags int32) (r, w int32, errno int32) +func fcntl(fd, cmd, arg int32) int32 func closeonexec(fd int32) // From FreeBSD's diff --git a/src/runtime/os_linux.go b/src/runtime/os_linux.go index 25aea6522dc..af4a0922c92 100644 --- a/src/runtime/os_linux.go +++ b/src/runtime/os_linux.go @@ -456,6 +456,16 @@ func osyield_no_g() { func pipe2(flags int32) (r, w int32, errno int32) +//go:nosplit +func fcntl(fd, cmd, arg int32) int32 { + r, _, errno := syscall.Syscall6(syscall.SYS_FCNTL, uintptr(fd), uintptr(cmd), uintptr(arg), 0, 0, 0) + ri := int32(r) + if ri < 0 { + return -int32(errno) + } + return ri +} + const ( _si_max_size = 128 _sigev_max_size = 64 diff --git a/src/runtime/os_netbsd.go b/src/runtime/os_netbsd.go index 3cbace38f99..34f43ddecb9 100644 --- a/src/runtime/os_netbsd.go +++ b/src/runtime/os_netbsd.go @@ -79,6 +79,7 @@ func kqueue() int32 func kevent(kq int32, ch *keventt, nch int32, ev *keventt, nev int32, ts *timespec) int32 func pipe2(flags int32) (r, w int32, errno int32) +func fcntl(fd, cmd, arg int32) int32 func closeonexec(fd int32) const ( diff --git a/src/runtime/os_openbsd_syscall2.go b/src/runtime/os_openbsd_syscall2.go index ab6b1818285..c2a43bac8ba 100644 --- a/src/runtime/os_openbsd_syscall2.go +++ b/src/runtime/os_openbsd_syscall2.go @@ -95,6 +95,7 @@ func nanotime1() int64 //go:noescape func sigaltstack(new, old *stackt) +func fcntl(fd, cmd, arg int32) int32 func closeonexec(fd int32) func walltime() (sec int64, nsec int32) diff --git a/src/runtime/os_solaris.go b/src/runtime/os_solaris.go index 8ac1b08f690..9e13c2903af 100644 --- a/src/runtime/os_solaris.go +++ b/src/runtime/os_solaris.go @@ -149,7 +149,7 @@ func sysvicall3(fn *libcFunc, a1, a2, a3 uintptr) uintptr { //go:cgo_unsafe_args // sysvicall3Err returns both the system call result and the errno value. -// This is used by sysicall3 and write1. +// This is used by sysvicall3 and write1. func sysvicall3Err(fn *libcFunc, a1, a2, a3 uintptr) (r1, err uintptr) { // Leave caller's PC/SP around for traceback. gp := getg() diff --git a/src/runtime/sys_darwin_amd64.s b/src/runtime/sys_darwin_amd64.s index ba81fcc35c4..3ba673c5ece 100644 --- a/src/runtime/sys_darwin_amd64.s +++ b/src/runtime/sys_darwin_amd64.s @@ -437,6 +437,12 @@ TEXT runtime·fcntl_trampoline(SB),NOSPLIT,$0 MOVL 0(DI), DI // arg 1 fd XORL AX, AX // vararg: say "no float args" CALL libc_fcntl(SB) + TESTL AX, AX + JGT noerr + CALL libc_error(SB) + MOVL (AX), AX + NEGL AX // caller expects negative errno value +noerr: POPQ BP RET diff --git a/src/runtime/sys_darwin_arm64.s b/src/runtime/sys_darwin_arm64.s index bf0dc9d8ccb..5f8e8243958 100644 --- a/src/runtime/sys_darwin_arm64.s +++ b/src/runtime/sys_darwin_arm64.s @@ -314,6 +314,13 @@ TEXT runtime·fcntl_trampoline(SB),NOSPLIT,$0 MOVW R2, (RSP) // arg 3 is variadic, pass on stack MOVW 0(R0), R0 // arg 1 fd BL libc_fcntl(SB) + MOVD $-1, R1 + CMP R0, R1 + BNE noerr + BL libc_error(SB) + MOVW (R0), R0 + NEG R0, R0 // caller expects negative errno value +noerr: ADD $16, RSP RET diff --git a/src/runtime/sys_dragonfly_amd64.s b/src/runtime/sys_dragonfly_amd64.s index 0cf98219fb0..e3d85ab062a 100644 --- a/src/runtime/sys_dragonfly_amd64.s +++ b/src/runtime/sys_dragonfly_amd64.s @@ -387,6 +387,18 @@ TEXT runtime·kevent(SB),NOSPLIT,$0 MOVL AX, ret+48(FP) RET +// func fcntl(fd, cmd, arg int32) int32 +TEXT runtime·fcntl(SB),NOSPLIT,$0 + MOVL fd+0(FP), DI // fd + MOVL cmd+4(FP), SI // cmd + MOVL arg+8(FP), DX // arg + MOVL $92, AX // fcntl + SYSCALL + JCC 2(PC) + NEGL AX // caller expects negative errno + MOVL AX, ret+16(FP) + RET + // void runtime·closeonexec(int32 fd); TEXT runtime·closeonexec(SB),NOSPLIT,$0 MOVL fd+0(FP), DI // fd diff --git a/src/runtime/sys_freebsd_386.s b/src/runtime/sys_freebsd_386.s index d10eb3fa4b2..3375fc1faed 100644 --- a/src/runtime/sys_freebsd_386.s +++ b/src/runtime/sys_freebsd_386.s @@ -415,6 +415,15 @@ TEXT runtime·kevent(SB),NOSPLIT,$0 MOVL AX, ret+24(FP) RET +// func fcntl(fd, cmd, arg int32) int32 +TEXT runtime·fcntl(SB),NOSPLIT,$-4 + MOVL $92, AX + INT $0x80 + JAE 2(PC) + NEGL AX // caller expects negative errno + MOVL AX, ret+12(FP) + RET + // int32 runtime·closeonexec(int32 fd); TEXT runtime·closeonexec(SB),NOSPLIT,$32 MOVL $92, AX // fcntl diff --git a/src/runtime/sys_freebsd_amd64.s b/src/runtime/sys_freebsd_amd64.s index 9f48d585145..55e7e250ae9 100644 --- a/src/runtime/sys_freebsd_amd64.s +++ b/src/runtime/sys_freebsd_amd64.s @@ -483,6 +483,18 @@ TEXT runtime·kevent(SB),NOSPLIT,$0 MOVL AX, ret+48(FP) RET +// func fcntl(fd, cmd, arg int32) int32 +TEXT runtime·fcntl(SB),NOSPLIT,$0 + MOVL fd+0(FP), DI // fd + MOVL cmd+4(FP), SI // cmd + MOVL arg+8(FP), DX // arg + MOVL $92, AX + SYSCALL + JCC 2(PC) + NEGQ AX // caller expects negative errno + MOVL AX, ret+16(FP) + RET + // void runtime·closeonexec(int32 fd); TEXT runtime·closeonexec(SB),NOSPLIT,$0 MOVL fd+0(FP), DI // fd diff --git a/src/runtime/sys_freebsd_arm.s b/src/runtime/sys_freebsd_arm.s index a6099b465cc..725303a8acf 100644 --- a/src/runtime/sys_freebsd_arm.s +++ b/src/runtime/sys_freebsd_arm.s @@ -387,6 +387,17 @@ TEXT runtime·kevent(SB),NOSPLIT,$0 MOVW R0, ret+24(FP) RET +// func fcntl(fd, cmd, arg int32) int32 +TEXT runtime·fcntl(SB),NOSPLIT,$0 + MOVW fd+0(FP), R0 // fd + MOVW cmd+4(FP), R1 // cmd + MOVW arg+8(FP), R2 // arg + MOVW $SYS_fcntl, R7 + SWI $0 + RSB.CS $0, R0 // caller expects negative errno + MOVW R0, ret+12(FP) + RET + // void runtime·closeonexec(int32 fd) TEXT runtime·closeonexec(SB),NOSPLIT,$0 MOVW fd+0(FP), R0 // fd diff --git a/src/runtime/sys_freebsd_arm64.s b/src/runtime/sys_freebsd_arm64.s index 4143a8f1eb4..4521ac61e41 100644 --- a/src/runtime/sys_freebsd_arm64.s +++ b/src/runtime/sys_freebsd_arm64.s @@ -444,6 +444,19 @@ ok: MOVW R0, ret+48(FP) RET +// func fcntl(fd, cmd, arg int32) int32 +TEXT runtime·fcntl(SB),NOSPLIT,$0 + MOVW fd+0(FP), R0 + MOVW cmd+4(FP), R1 + MOVW arg+8(FP), R2 + MOVD $SYS_fcntl, R8 + SVC + BCC ok + NEG R0, R0 // caller expects negative errno +ok: + MOVW R0, ret+16(FP) + RET + // func closeonexec(fd int32) TEXT runtime·closeonexec(SB),NOSPLIT|NOFRAME,$0 MOVW fd+0(FP), R0 diff --git a/src/runtime/sys_netbsd_386.s b/src/runtime/sys_netbsd_386.s index 7be18c61d8b..a05e1d44787 100644 --- a/src/runtime/sys_netbsd_386.s +++ b/src/runtime/sys_netbsd_386.s @@ -457,6 +457,15 @@ TEXT runtime·kevent(SB),NOSPLIT,$0 MOVL AX, ret+24(FP) RET +// func fcntl(fd, cmd, arg int32) int32 +TEXT runtime·fcntl(SB),NOSPLIT,$-4 + MOVL $SYS_fcntl, AX + INT $0x80 + JAE 2(PC) + NEGL AX // caller expects negative errno + MOVL AX, ret+12(FP) + RET + // int32 runtime·closeonexec(int32 fd) TEXT runtime·closeonexec(SB),NOSPLIT,$32 MOVL $SYS_fcntl, AX diff --git a/src/runtime/sys_netbsd_amd64.s b/src/runtime/sys_netbsd_amd64.s index 30f3f380b6f..368eef56edf 100644 --- a/src/runtime/sys_netbsd_amd64.s +++ b/src/runtime/sys_netbsd_amd64.s @@ -434,6 +434,18 @@ TEXT runtime·kevent(SB),NOSPLIT,$0 MOVL AX, ret+48(FP) RET +// func fcntl(fd, cmd, arg int32) int2 +TEXT runtime·fcntl(SB),NOSPLIT,$0 + MOVL fd+0(FP), DI // fd + MOVL cmd+4(FP), SI // cmd + MOVL arg+8(FP), DX // arg + MOVL $SYS_fcntl, AX + SYSCALL + JCC 2(PC) + NEGQ AX // caller expects negative errno + MOVL AX, ret+16(FP) + RET + // void runtime·closeonexec(int32 fd) TEXT runtime·closeonexec(SB),NOSPLIT,$0 MOVL fd+0(FP), DI // fd diff --git a/src/runtime/sys_netbsd_arm.s b/src/runtime/sys_netbsd_arm.s index 62fa852addd..f9cbcb6df1b 100644 --- a/src/runtime/sys_netbsd_arm.s +++ b/src/runtime/sys_netbsd_arm.s @@ -398,6 +398,16 @@ TEXT runtime·kevent(SB),NOSPLIT,$8 MOVW R0, ret+24(FP) RET +// func fcntl(fd, cmd, args int32) int32 +TEXT runtime·fcntl(SB),NOSPLIT,$0 + MOVW fd+0(FP), R0 + MOVW cmd+4(FP), R1 + MOVW arg+8(FP), R2 + SWI $SYS_fcntl + RSB.CS $0, R0 // caller expects negative errno + MOVW R0, ret+12(FP) + RET + // void runtime·closeonexec(int32 fd) TEXT runtime·closeonexec(SB),NOSPLIT,$0 MOVW fd+0(FP), R0 // fd diff --git a/src/runtime/sys_netbsd_arm64.s b/src/runtime/sys_netbsd_arm64.s index d57959f8d76..d27133550a8 100644 --- a/src/runtime/sys_netbsd_arm64.s +++ b/src/runtime/sys_netbsd_arm64.s @@ -421,6 +421,18 @@ ok: MOVW R0, ret+48(FP) RET +// func fcntl(fd, cmd, arg int32) int32 +TEXT runtime·fcntl(SB),NOSPLIT,$0 + MOVW fd+0(FP), R0 // fd + MOVW cmd+4(FP), R1 // cmd + MOVW arg+8(FP), R2 // arg + SVC $SYS_fcntl + BCC ok + NEG R0, R0 // caller expects negative errno +ok: + MOVW R0, ret+16(FP) + RET + // void runtime·closeonexec(int32 fd) TEXT runtime·closeonexec(SB),NOSPLIT,$0 MOVW fd+0(FP), R0 // arg 1 - fd diff --git a/src/runtime/sys_openbsd_386.s b/src/runtime/sys_openbsd_386.s index 963678a2c3c..3e5dbc2b0a8 100644 --- a/src/runtime/sys_openbsd_386.s +++ b/src/runtime/sys_openbsd_386.s @@ -542,6 +542,12 @@ TEXT runtime·fcntl_trampoline(SB),NOSPLIT,$0 MOVL CX, 8(SP) // arg 3 - arg MOVL $0, 12(SP) // vararg CALL libc_fcntl(SB) + CMPL AX, $-1 + JNE noerr + CALL libc_errno(SB) + MOVL (AX), AX + NEGL AX // caller expects negative errno +noerr: MOVL BP, SP POPL BP RET diff --git a/src/runtime/sys_openbsd_amd64.s b/src/runtime/sys_openbsd_amd64.s index 2c026c8d0db..35a57c026b6 100644 --- a/src/runtime/sys_openbsd_amd64.s +++ b/src/runtime/sys_openbsd_amd64.s @@ -393,6 +393,12 @@ TEXT runtime·fcntl_trampoline(SB),NOSPLIT,$0 MOVL 0(DI), DI // arg 1 fd XORL AX, AX // vararg: say "no float args" CALL libc_fcntl(SB) + TESTL AX, AX + JGE noerr + CALL libc_errno(SB) + MOVL (AX), AX + NEGL AX // caller expects negative errno value +noerr: POPQ BP RET diff --git a/src/runtime/sys_openbsd_arm.s b/src/runtime/sys_openbsd_arm.s index e03cfb52f60..3568d4eb941 100644 --- a/src/runtime/sys_openbsd_arm.s +++ b/src/runtime/sys_openbsd_arm.s @@ -424,6 +424,12 @@ TEXT runtime·fcntl_trampoline(SB),NOSPLIT,$0 MOVW R2, 0(R13) MOVW 0(R0), R0 // arg 1 fd BL libc_fcntl(SB) + CMP $-1, R0 + BNE noerr + BL libc_errno(SB) + MOVW (R0), R0 + RSB.CS $0, R0 // caller expects negative errno +noerr: MOVW R9, R13 RET diff --git a/src/runtime/sys_openbsd_arm64.s b/src/runtime/sys_openbsd_arm64.s index db92326efba..0bd801ff7a9 100644 --- a/src/runtime/sys_openbsd_arm64.s +++ b/src/runtime/sys_openbsd_arm64.s @@ -311,6 +311,12 @@ TEXT runtime·fcntl_trampoline(SB),NOSPLIT,$0 MOVW 0(R0), R0 // arg 1 - fd MOVD $0, R3 // vararg CALL libc_fcntl(SB) + CMP $-1, R0 + BNE noerr + CALL libc_errno(SB) + MOVW (R0), R0 + NEG R0, R0 // caller expects negative errno value +noerr: RET TEXT runtime·sigaction_trampoline(SB),NOSPLIT,$0 diff --git a/src/runtime/sys_openbsd_mips64.s b/src/runtime/sys_openbsd_mips64.s index cc37e52e161..0ddcac55954 100644 --- a/src/runtime/sys_openbsd_mips64.s +++ b/src/runtime/sys_openbsd_mips64.s @@ -364,6 +364,18 @@ TEXT runtime·kevent(SB),NOSPLIT,$0 MOVW R2, ret+48(FP) RET +// func fcntl(fd, cmd, arg int32) int32 +TEXT runtime·fcntl(SB),NOSPLIT,$0 + MOVW fd+0(FP), R4 // fd + MOVW cmd+4(FP), R5 // cmd + MOVW arg+8(FP), R6 // arg + MOVV $92, R2 // sys_fcntl + SYSCALL + BEQ R7, 2(PC) + SUBVU R2, R0, R2 // caller expects negative errno + MOVW R2, ret+16(FP) + RET + // func closeonexec(fd int32) TEXT runtime·closeonexec(SB),NOSPLIT,$0 MOVW fd+0(FP), R4 // arg 1 - fd -- cgit v1.2.3-54-g00ecf