Skip to content

Commit 7a2e938

Browse files
committed
fcntl: musl wrapper, move to syscall_unix.go, os_linux
Signed-off-by: leongross <[email protected]>
1 parent 6b15f6b commit 7a2e938

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

builder/musl.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ var libMusl = Library{
135135
"thread/*.c",
136136
"time/*.c",
137137
"unistd/*.c",
138+
"fcntl/*.c",
138139
}
139140
if arch == "arm" {
140141
// These files need to be added to the start for some reason.

src/runtime/os_linux.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,5 @@ func hardwareRand() (n uint64, ok bool) {
142142
//export getrandom
143143
func libc_getrandom(buf unsafe.Pointer, buflen uintptr, flags uint32) uint32
144144

145-
func fcntl(fd, cmd, arg int32) (ret int32, errno int32){
146-
return 0, 0
147-
}
145+
//go:linkname fcntl syscall.Fcntl
146+
func fcntl(fd, cmd, arg int32) (ret int32, errno int32)

src/syscall/syscall_libc.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,23 @@ func Truncate(path string, length int64) (err error) {
212212
return
213213
}
214214

215+
/*
215216
//go:linkname syscall_fcntl runtime/runtime.fcntl
216217
func syscall_fcntl(fd, cmd, arg int32) (ret int32, errno int32) {
217218
// https://cs.opensource.google/go/go/+/master:src/runtime/os_linux.go;l=452?q=runtime.fcntl&ss=go%2Fgo
218219
r, _, err := Syscall6(SYS_FCNTL, uintptr(fd), uintptr(cmd), uintptr(arg), 0, 0, 0)
219220
return int32(r), int32(err)
220221
}
222+
*/
223+
224+
//go:linkname Fcntl runtime/runtime.fcntl
225+
func Fcntl(fd int, cmd int, args ...int) (int, error) {
226+
ret := libc_fcntl(fd, cmd, args...)
227+
if ret < 0 {
228+
return 0, getErrno()
229+
}
230+
return ret, nil
231+
}
221232

222233
func Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
223234

@@ -472,3 +483,8 @@ func libc_execve(filename *byte, argv **byte, envp **byte) int
472483
//
473484
//export truncate
474485
func libc_truncate(path *byte, length int64) int32
486+
487+
// int fcntl(int, int, ...);
488+
//
489+
// export fcntl
490+
func libc_fcntl(fd int, cmd int, args ...int) int

src/syscall/syscall_unix.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build tinygo && unix && !baremetal && !tinygo.wasm
2+
13
package syscall
24

35
func Exec(argv0 string, argv []string, envv []string) (err error)
@@ -16,3 +18,10 @@ type SockaddrInet6 struct {
1618
Addr [16]byte
1719
raw RawSockaddrInet6
1820
}
21+
22+
//go:linkname syscall_fcntl runtime/runtime.fcntl
23+
func syscall_fcntl(fd, cmd, arg int32) (ret int32, errno int32) {
24+
// https://cs.opensource.google/go/go/+/master:src/runtime/os_linux.go;l=452?q=runtime.fcntl&ss=go%2Fgo
25+
r, _, err := Syscall6(SYS_FCNTL, uintptr(fd), uintptr(cmd), uintptr(arg), 0, 0, 0)
26+
return int32(r), int32(err)
27+
}

0 commit comments

Comments
 (0)