Skip to content

Commit 4189338

Browse files
chrisogopherbot
authored andcommittedMay 11, 2023
syscall: implement wasip1 SetNonblock and IsNonblock
Allows for the NONBLOCK file descriptor flag to be set and queried on wasip1. syscall.SetNonblock uses the fd_fdstat_set_flags WASI system call and unix.IsNonblock uses the fd_fdstat_get system call. This is a prerequisite for non-blocking I/O support. Change-Id: I2bf79fd57142b2ec53eed3977d9aac8c6337eb80 Reviewed-on: https://go-review.googlesource.com/c/go/+/493356 Auto-Submit: Johan Brandhorst-Satzkorn <[email protected]> Run-TryBot: Johan Brandhorst-Satzkorn <[email protected]> Reviewed-by: Julien Fabre <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Johan Brandhorst-Satzkorn <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Achille Roussel <[email protected]>
1 parent 7024712 commit 4189338

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed
 

‎src/internal/syscall/unix/nonblocking_wasip1.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@
66

77
package unix
88

9+
import (
10+
"syscall"
11+
_ "unsafe" // for go:linkname
12+
)
13+
914
func IsNonblock(fd int) (nonblocking bool, err error) {
10-
return false, nil
15+
flags, e1 := fd_fdstat_get_flags(fd)
16+
if e1 != nil {
17+
return false, e1
18+
}
19+
return flags&syscall.FDFLAG_NONBLOCK != 0, nil
1120
}
21+
22+
// This helper is implemented in the syscall package. It means we don't have
23+
// to redefine the fd_fdstat_get host import or the fdstat struct it
24+
// populates.
25+
//
26+
//go:linkname fd_fdstat_get_flags syscall.fd_fdstat_get_flags
27+
func fd_fdstat_get_flags(fd int) (uint32, error)

‎src/syscall/fs_wasip1.go

+24
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,30 @@ func path_open(rootFD int32, dirflags lookupflags, path unsafe.Pointer, pathLen
255255
//go:noescape
256256
func random_get(buf unsafe.Pointer, bufLen size) Errno
257257

258+
// https://github.com/WebAssembly/WASI/blob/a2b96e81c0586125cc4dc79a5be0b78d9a059925/legacy/preview1/docs.md#-fdstat-record
259+
// fdflags must be at offset 2, hence the uint16 type rather than the
260+
// fdflags (uint32) type.
261+
type fdstat struct {
262+
filetype filetype
263+
fdflags uint16
264+
rightsBase rights
265+
rightsInheriting rights
266+
}
267+
268+
//go:wasmimport wasi_snapshot_preview1 fd_fdstat_get
269+
//go:noescape
270+
func fd_fdstat_get(fd int32, buf unsafe.Pointer) Errno
271+
272+
//go:wasmimport wasi_snapshot_preview1 fd_fdstat_set_flags
273+
//go:noescape
274+
func fd_fdstat_set_flags(fd int32, flags fdflags) Errno
275+
276+
func fd_fdstat_get_flags(fd int) (uint32, error) {
277+
var stat fdstat
278+
errno := fd_fdstat_get(int32(fd), unsafe.Pointer(&stat))
279+
return uint32(stat.fdflags), errnoErr(errno)
280+
}
281+
258282
type preopentype = uint8
259283

260284
const (

‎src/syscall/net_wasip1.go

-4
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,3 @@ func SetWriteDeadline(fd int, t int64) error {
122122
func Shutdown(fd int, how int) error {
123123
return ENOSYS
124124
}
125-
126-
func SetNonblock(fd int, nonblocking bool) error {
127-
return ENOSYS
128-
}

‎src/syscall/syscall_wasip1.go

+14
Original file line numberDiff line numberDiff line change
@@ -464,3 +464,17 @@ const (
464464
//go:wasmimport wasi_snapshot_preview1 clock_time_get
465465
//go:noescape
466466
func clock_time_get(id clockid, precision timestamp, time unsafe.Pointer) Errno
467+
468+
func SetNonblock(fd int, nonblocking bool) error {
469+
flags, err := fd_fdstat_get_flags(fd)
470+
if err != nil {
471+
return err
472+
}
473+
if nonblocking {
474+
flags |= FDFLAG_NONBLOCK
475+
} else {
476+
flags &^= FDFLAG_NONBLOCK
477+
}
478+
errno := fd_fdstat_set_flags(int32(fd), flags)
479+
return errnoErr(errno)
480+
}

0 commit comments

Comments
 (0)