if runtime.Close(r) != 0 {
t.Fatalf("Close(%d) failed", r)
}
- val := runtime.Fcntl(r, syscall.F_GETFD, 0)
- if val >= 0 {
+ val, errno := runtime.Fcntl(r, syscall.F_GETFD, 0)
+ if val != -1 {
t.Errorf("Fcntl succeeded unexpectedly")
- } else if syscall.Errno(-val) != syscall.EBADF {
- t.Errorf("Fcntl failed with error %v, expected %v", -val, syscall.EBADF)
+ } else if syscall.Errno(errno) != syscall.EBADF {
+ t.Errorf("Fcntl failed with error %v, expected %v", syscall.Errno(errno), syscall.EBADF)
}
}
func checkNonblocking(t *testing.T, fd int32, name string) {
t.Helper()
- flags := runtime.Fcntl(fd, syscall.F_GETFL, 0)
- if flags < 0 {
- t.Errorf("fcntl(%s, F_GETFL) failed: %v", name, syscall.Errno(-flags))
+ flags, errno := runtime.Fcntl(fd, syscall.F_GETFL, 0)
+ if flags == -1 {
+ t.Errorf("fcntl(%s, F_GETFL) failed: %v", name, syscall.Errno(errno))
} else if flags&syscall.O_NONBLOCK == 0 {
t.Errorf("O_NONBLOCK not set in %s flags %#x", name, flags)
}
func checkCloseonexec(t *testing.T, fd int32, name string) {
t.Helper()
- flags := runtime.Fcntl(fd, syscall.F_GETFD, 0)
- if flags < 0 {
- t.Errorf("fcntl(%s, F_GETFD) failed: %v", name, syscall.Errno(flags))
+ flags, errno := runtime.Fcntl(fd, syscall.F_GETFD, 0)
+ if flags == -1 {
+ t.Errorf("fcntl(%s, F_GETFD) failed: %v", name, syscall.Errno(errno))
} else if flags&syscall.FD_CLOEXEC == 0 {
t.Errorf("FD_CLOEXEC not set in %s flags %#x", name, flags)
}
}
//go:nosplit
-func fcntl(fd, cmd, arg int32) int32 {
+func fcntl(fd, cmd, arg int32) (ret int32, errno int32) {
r1, err := sysvicall3Err(&libc_fcntl, uintptr(fd), uintptr(cmd), uintptr(arg))
- if r := int32(r1); r >= 0 {
- return r
- }
- return -int32(err)
+ return int32(r1), int32(err)
}
//go:nosplit
}
//go:nosplit
-func fcntl(fd, cmd, arg int32) int32 {
+func fcntl(fd, cmd, arg int32) (int32, int32) {
r, errno := syscall3(&libc_fcntl, uintptr(fd), uintptr(cmd), uintptr(arg))
- if int32(r) < 0 {
- return -int32(errno)
- }
- return int32(r)
+ return int32(r), int32(errno)
}
//go:nosplit
//go:nosplit
func setNonblock(fd int32) {
- flags := fcntl(fd, _F_GETFL, 0)
- fcntl(fd, _F_SETFL, flags|_O_NONBLOCK)
+ flags, _ := fcntl(fd, _F_GETFL, 0)
+ if flags != -1 {
+ fcntl(fd, _F_SETFL, flags|_O_NONBLOCK)
+ }
}
// sigPerThreadSyscall is only used on linux, so we assign a bogus signal
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 fcntl(fd, cmd, arg int32) (ret int32, errno int32)
func closeonexec(fd int32)
// From DragonFly's <sys/sysctl.h>
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 fcntl(fd, cmd, arg int32) (ret int32, errno int32)
func closeonexec(fd int32)
// From FreeBSD's <sys/sysctl.h>
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
+func fcntl(fd, cmd, arg int32) (ret int32, errno int32) {
+ r, _, err := syscall.Syscall6(syscall.SYS_FCNTL, uintptr(fd), uintptr(cmd), uintptr(arg), 0, 0, 0)
+ return int32(r), int32(err)
}
const (
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 fcntl(fd, cmd, arg int32) (ret int32, errno int32)
func closeonexec(fd int32)
const (
//go:noescape
func sigaltstack(new, old *stackt)
-func fcntl(fd, cmd, arg int32) int32
+func fcntl(fd, cmd, arg int32) (ret int32, errno int32)
func closeonexec(fd int32)
func walltime() (sec int64, nsec int32)
//go:nosplit
//go:cgo_unsafe_args
-func fcntl(fd, cmd, arg int32) int32 {
- return libcCall(unsafe.Pointer(abi.FuncPCABI0(fcntl_trampoline)), unsafe.Pointer(&fd))
+func fcntl(fd, cmd, arg int32) (ret int32, errno int32) {
+ args := struct {
+ fd, cmd, arg int32
+ ret, errno int32
+ }{fd, cmd, arg, 0, 0}
+ libcCall(unsafe.Pointer(abi.FuncPCABI0(fcntl_trampoline)), unsafe.Pointer(&args))
+ return args.ret, args.errno
}
func fcntl_trampoline()
//go:nosplit
func setNonblock(fd int32) {
- flags := fcntl(fd, _F_GETFL, 0)
- fcntl(fd, _F_SETFL, flags|_O_NONBLOCK)
+ flags, _ := fcntl(fd, _F_GETFL, 0)
+ if flags != -1 {
+ fcntl(fd, _F_SETFL, flags|_O_NONBLOCK)
+ }
}
// Tell the linker that the libc_* functions are to be found
TEXT runtime·fcntl_trampoline(SB),NOSPLIT,$0
PUSHQ BP
MOVQ SP, BP
- MOVL 4(DI), SI // arg 2 cmd
- MOVL 8(DI), DX // arg 3 arg
- MOVL 0(DI), DI // arg 1 fd
+ MOVQ DI, BX
+ MOVL 0(BX), DI // arg 1 fd
+ MOVL 4(BX), SI // arg 2 cmd
+ MOVL 8(BX), DX // arg 3 arg
XORL AX, AX // vararg: say "no float args"
CALL libc_fcntl(SB)
- TESTL AX, AX
- JGT noerr
+ XORL DX, DX
+ CMPQ AX, $-1
+ JNE noerr
CALL libc_error(SB)
- MOVL (AX), AX
- NEGL AX // caller expects negative errno value
+ MOVL (AX), DX
+ MOVL $-1, AX
noerr:
+ MOVL AX, 12(BX)
+ MOVL DX, 16(BX)
POPQ BP
RET
TEXT runtime·fcntl_trampoline(SB),NOSPLIT,$0
SUB $16, RSP
- MOVW 4(R0), R1 // arg 2 cmd
- MOVW 8(R0), R2 // arg 3 arg
+ MOVD R0, R19
+ MOVW 0(R19), R0 // arg 1 fd
+ MOVW 4(R19), R1 // arg 2 cmd
+ MOVW 8(R19), R2 // arg 3 arg
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
+ MOVD $0, R1
+ MOVD $-1, R2
+ CMP R0, R2
BNE noerr
BL libc_error(SB)
- MOVW (R0), R0
- NEG R0, R0 // caller expects negative errno value
+ MOVW (R0), R1
+ MOVW $-1, R0
noerr:
+ MOVW R0, 12(R19)
+ MOVW R1, 16(R19)
ADD $16, RSP
RET
MOVL AX, ret+48(FP)
RET
-// func fcntl(fd, cmd, arg int32) int32
+// func fcntl(fd, cmd, arg int32) (ret int32, errno 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
+ JCC noerr
+ MOVL $-1, ret+16(FP)
+ MOVL AX, errno+20(FP)
+ RET
+noerr:
MOVL AX, ret+16(FP)
+ MOVL $0, errno+20(FP)
RET
// void runtime·closeonexec(int32 fd);
MOVL AX, ret+24(FP)
RET
-// func fcntl(fd, cmd, arg int32) int32
+// func fcntl(fd, cmd, arg int32) (int32, int32)
TEXT runtime·fcntl(SB),NOSPLIT,$-4
MOVL $SYS_fcntl, AX
INT $0x80
- JAE 2(PC)
- NEGL AX // caller expects negative errno
+ JAE noerr
+ MOVL $-1, ret+12(FP)
+ MOVL AX, errno+16(FP)
+ RET
+noerr:
MOVL AX, ret+12(FP)
+ MOVL $0, errno+16(FP)
RET
// int32 runtime·closeonexec(int32 fd);
MOVL AX, ret+48(FP)
RET
-// func fcntl(fd, cmd, arg int32) int32
+// func fcntl(fd, cmd, arg int32) (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 $SYS_fcntl, AX
SYSCALL
- JCC 2(PC)
- NEGQ AX // caller expects negative errno
+ JCC noerr
+ MOVL $-1, ret+16(FP)
+ MOVL AX, errno+20(FP)
+ RET
+noerr:
MOVL AX, ret+16(FP)
+ MOVL $0, errno+20(FP)
RET
// void runtime·closeonexec(int32 fd);
MOVW R0, ret+24(FP)
RET
-// func fcntl(fd, cmd, arg int32) int32
+// func fcntl(fd, cmd, arg int32) (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 $0, R1
+ MOVW.CS R0, R1
+ MOVW.CS $-1, R0
MOVW R0, ret+12(FP)
+ MOVW R1, errno+16(FP)
RET
// void runtime·closeonexec(int32 fd)
MOVW R0, ret+48(FP)
RET
-// func fcntl(fd, cmd, arg int32) int32
+// func fcntl(fd, cmd, arg int32) (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:
+ BCC noerr
+ MOVW $-1, R1
+ MOVW R1, ret+16(FP)
+ MOVW R0, errno+20(FP)
+ RET
+noerr:
MOVW R0, ret+16(FP)
+ MOVW $0, errno+20(FP)
RET
// func closeonexec(fd int32)
MOVW A0, ret+48(FP)
RET
-// func fcntl(fd, cmd, arg int32) int32
+// func fcntl(fd, cmd, arg int32) (int32, int32)
TEXT runtime·fcntl(SB),NOSPLIT,$0
MOVW fd+0(FP), A0
MOVW cmd+4(FP), A1
MOVW arg+8(FP), A2
MOV $SYS_fcntl, T0
ECALL
- BEQ T0, ZERO, ok
- NEG A0, A0 // caller expects negative errno
-ok:
+ BEQ T0, ZERO, noerr
+ MOV $-1, A1
+ MOVW A1, ret+16(FP)
+ MOVW A0, errno+20(FP)
+ RET
+noerr:
MOVW A0, ret+16(FP)
+ MOVW ZERO, errno+20(FP)
RET
// func closeonexec(fd int32)
MOVL AX, ret+24(FP)
RET
-// func fcntl(fd, cmd, arg int32) int32
+// func fcntl(fd, cmd, arg int32) (int32, int32)
TEXT runtime·fcntl(SB),NOSPLIT,$-4
MOVL $SYS_fcntl, AX
INT $0x80
- JAE 2(PC)
- NEGL AX // caller expects negative errno
+ JAE noerr
+ MOVL $-1, ret+12(FP)
+ MOVL AX, errno+16(FP)
+ RET
+noerr:
MOVL AX, ret+12(FP)
+ MOVL $0, errno+16(FP)
RET
// int32 runtime·closeonexec(int32 fd)
MOVL AX, ret+48(FP)
RET
-// func fcntl(fd, cmd, arg int32) int2
+// func fcntl(fd, cmd, arg int32) (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 $SYS_fcntl, AX
SYSCALL
- JCC 2(PC)
- NEGQ AX // caller expects negative errno
+ JCC noerr
+ MOVL $-1, ret+16(FP)
+ MOVL AX, errno+20(FP)
+ RET
+noerr:
MOVL AX, ret+16(FP)
+ MOVL $0, errno+20(FP)
RET
// void runtime·closeonexec(int32 fd)
MOVW cmd+4(FP), R1
MOVW arg+8(FP), R2
SWI $SYS_fcntl
- RSB.CS $0, R0 // caller expects negative errno
+ MOVW $0, R1
+ MOVW.CS R0, R1
+ MOVW.CS $-1, R0
MOVW R0, ret+12(FP)
+ MOVW R1, errno+16(FP)
RET
// void runtime·closeonexec(int32 fd)
MOVW R0, ret+48(FP)
RET
-// func fcntl(fd, cmd, arg int32) int32
+// func fcntl(fd, cmd, arg int32) (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:
+ BCC noerr
+ MOVW $-1, R1
+ MOVW R1, ret+16(FP)
+ MOVW R0, errno+20(FP)
+ RET
+noerr:
MOVW R0, ret+16(FP)
+ MOVW $0, errno+20(FP)
RET
// void runtime·closeonexec(int32 fd)
//go:nosplit
//go:cgo_unsafe_args
-func fcntl(fd, cmd, arg int32) int32 {
- return libcCall(unsafe.Pointer(abi.FuncPCABI0(fcntl_trampoline)), unsafe.Pointer(&fd))
+func fcntl(fd, cmd, arg int32) (ret int32, errno int32) {
+ args := struct {
+ fd, cmd, arg int32
+ ret, errno int32
+ }{fd, cmd, arg, 0, 0}
+ libcCall(unsafe.Pointer(abi.FuncPCABI0(fcntl_trampoline)), unsafe.Pointer(&args))
+ return args.ret, args.errno
}
func fcntl_trampoline()
MOVL CX, 8(SP) // arg 3 - arg
MOVL $0, 12(SP) // vararg
CALL libc_fcntl(SB)
+ MOVL $0, BX
CMPL AX, $-1
JNE noerr
CALL libc_errno(SB)
- MOVL (AX), AX
- NEGL AX // caller expects negative errno
+ MOVL (AX), BX
+ MOVL $-1, AX
noerr:
+ MOVL 24(SP), DX // pointer to args
+ MOVL AX, 12(DX)
+ MOVL BX, 16(DX)
MOVL BP, SP
POPL BP
RET
TEXT runtime·fcntl_trampoline(SB),NOSPLIT,$0
PUSHQ BP
MOVQ SP, BP
- MOVL 4(DI), SI // arg 2 cmd
- MOVL 8(DI), DX // arg 3 arg
- MOVL 0(DI), DI // arg 1 fd
+ MOVQ DI, BX
+ MOVL 0(BX), DI // arg 1 fd
+ MOVL 4(BX), SI // arg 2 cmd
+ MOVL 8(BX), DX // arg 3 arg
XORL AX, AX // vararg: say "no float args"
CALL libc_fcntl(SB)
- TESTL AX, AX
- JGE noerr
+ XORL DX, DX
+ CMPL AX, $-1
+ JNE noerr
CALL libc_errno(SB)
- MOVL (AX), AX
- NEGL AX // caller expects negative errno value
+ MOVL (AX), DX
+ MOVL $-1, AX
noerr:
+ MOVL AX, 12(BX)
+ MOVL DX, 16(BX)
POPQ BP
RET
MOVW R13, R9
SUB $8, R13
BIC $0x7, R13 // align for ELF ABI
- MOVW 4(R0), R1 // arg 2 cmd
- MOVW 8(R0), R2 // arg 3 arg (vararg, on stack)
+ MOVW R0, R8
+ MOVW 0(R8), R0 // arg 1 fd
+ MOVW 4(R8), R1 // arg 2 cmd
+ MOVW 8(R8), R2 // arg 3 arg (vararg, on stack)
MOVW R2, 0(R13)
- MOVW 0(R0), R0 // arg 1 fd
BL libc_fcntl(SB)
+ MOVW $0, R1
CMP $-1, R0
BNE noerr
BL libc_errno(SB)
- MOVW (R0), R0
- RSB.CS $0, R0 // caller expects negative errno
+ MOVW (R0), R1
+ MOVW $-1, R0
noerr:
+ MOVW R0, 12(R8)
+ MOVW R1, 16(R8)
MOVW R9, R13
RET
RET
TEXT runtime·fcntl_trampoline(SB),NOSPLIT,$0
- MOVW 4(R0), R1 // arg 2 - cmd
- MOVW 8(R0), R2 // arg 3 - arg
- MOVW 0(R0), R0 // arg 1 - fd
+ MOVD R0, R19
+ MOVW 0(R19), R0 // arg 1 - fd
+ MOVW 4(R19), R1 // arg 2 - cmd
+ MOVW 8(R19), R2 // arg 3 - arg
MOVD $0, R3 // vararg
CALL libc_fcntl(SB)
+ MOVD $0, R1
CMP $-1, R0
BNE noerr
CALL libc_errno(SB)
- MOVW (R0), R0
- NEG R0, R0 // caller expects negative errno value
+ MOVW (R0), R1
+ MOVW $-1, R0
noerr:
+ MOVW R0, 12(R19)
+ MOVW R1, 16(R19)
RET
TEXT runtime·sigaction_trampoline(SB),NOSPLIT,$0
MOVW R2, ret+48(FP)
RET
-// func fcntl(fd, cmd, arg int32) int32
+// func fcntl(fd, cmd, arg int32) (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
+ MOVV $0, R4
+ BEQ R7, noerr
+ MOVV R2, R4
+ MOVW $-1, R2
+noerr:
MOVW R2, ret+16(FP)
+ MOVW R4, errno+20(FP)
RET
// func closeonexec(fd int32)