From d8e381e70cccc31aa6f173108d5129990f1c6534 Mon Sep 17 00:00:00 2001 From: leongross Date: Fri, 4 Oct 2024 09:17:03 +0200 Subject: [PATCH] fcntl: musl wrapper, move to syscall_unix.go, os_linux Signed-off-by: leongross --- src/runtime/os_linux.go | 5 ++--- src/syscall/syscall_libc.go | 16 ++++++++++++++++ src/syscall/syscall_unix.go | 9 +++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/runtime/os_linux.go b/src/runtime/os_linux.go index 53c114bd7c..70f93b1c70 100644 --- a/src/runtime/os_linux.go +++ b/src/runtime/os_linux.go @@ -142,6 +142,5 @@ func hardwareRand() (n uint64, ok bool) { //export getrandom func libc_getrandom(buf unsafe.Pointer, buflen uintptr, flags uint32) uint32 -func fcntl(fd, cmd, arg int32) (ret int32, errno int32){ - return 0, 0 -} +//go:linkname fcntl syscall.Fcntl +func fcntl(fd, cmd, arg int32) (ret int32, errno int32) diff --git a/src/syscall/syscall_libc.go b/src/syscall/syscall_libc.go index 3d13f51504..61686a4c47 100644 --- a/src/syscall/syscall_libc.go +++ b/src/syscall/syscall_libc.go @@ -212,12 +212,23 @@ func Truncate(path string, length int64) (err error) { return } +/* //go:linkname syscall_fcntl runtime/runtime.fcntl func syscall_fcntl(fd, cmd, arg int32) (ret int32, errno int32) { // https://cs.opensource.google/go/go/+/master:src/runtime/os_linux.go;l=452?q=runtime.fcntl&ss=go%2Fgo r, _, err := Syscall6(SYS_FCNTL, uintptr(fd), uintptr(cmd), uintptr(arg), 0, 0, 0) return int32(r), int32(err) } +*/ + +//go:linkname Fcntl runtime/runtime.fcntl +func Fcntl(fd int, cmd int, args ...int) (int, error) { + ret := libc_fcntl(fd, cmd, args...) + if ret < 0 { + return 0, getErrno() + } + return ret, nil +} func Faccessat(dirfd int, path string, mode uint32, flags int) (err error) @@ -472,3 +483,8 @@ func libc_execve(filename *byte, argv **byte, envp **byte) int // //export truncate func libc_truncate(path *byte, length int64) int32 + +// int fcntl(int, int, ...); +// +// export fcntl +func libc_fcntl(fd int, cmd int, args ...int) int diff --git a/src/syscall/syscall_unix.go b/src/syscall/syscall_unix.go index 23d81fb891..629ba78da1 100644 --- a/src/syscall/syscall_unix.go +++ b/src/syscall/syscall_unix.go @@ -1,3 +1,5 @@ +//go:build tinygo + package syscall func Exec(argv0 string, argv []string, envv []string) (err error) @@ -16,3 +18,10 @@ type SockaddrInet6 struct { Addr [16]byte raw RawSockaddrInet6 } + +//go:linkname syscall_fcntl runtime/runtime.fcntl +func syscall_fcntl(fd, cmd, arg int32) (ret int32, errno int32) { + // https://cs.opensource.google/go/go/+/master:src/runtime/os_linux.go;l=452?q=runtime.fcntl&ss=go%2Fgo + r, _, err := Syscall6(SYS_FCNTL, uintptr(fd), uintptr(cmd), uintptr(arg), 0, 0, 0) + return int32(r), int32(err) +}