Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: add runtime.fnctl link directives #4481

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/runtime/os_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package runtime
// This file is for systems that are _actually_ Linux (not systems that pretend
// to be Linux, like baremetal systems).

import "unsafe"
import (
"unsafe"
)

const GOOS = "linux"

Expand Down Expand Up @@ -139,3 +141,6 @@ func hardwareRand() (n uint64, ok bool) {
//
//export getrandom
func libc_getrandom(buf unsafe.Pointer, buflen uintptr, flags uint32) uint32

//go:linkname fcntl syscall.Fcntl
func fcntl(fd, cmd, arg int32) (ret int32, errno int32)
23 changes: 23 additions & 0 deletions src/syscall/syscall_libc.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,24 @@ func Truncate(path string, length int64) (err error) {
return
}

/*
//go:linkname syscall_fcntl runtime/runtime.fcntl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you meant runtime.fcntl?

Suggested change
//go:linkname syscall_fcntl runtime/runtime.fcntl
//go:linkname syscall_fcntl 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you trying to do a direct system call in syscall_libc.go? Syscall6 and the like are only supported on Windows and Linux, not on many other systems.

What about calling the libc function fcntl instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a wrapper to the libc sounds like a good idea.

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)

func Kill(pid int, sig Signal) (err error) {
Expand Down Expand Up @@ -465,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
9 changes: 9 additions & 0 deletions src/syscall/syscall_unix.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build tinygo

package syscall

func Exec(argv0 string, argv []string, envv []string) (err error)
Expand All @@ -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)
}
Loading