]> Cypherpunks.ru repositories - gostls13.git/commitdiff
syscall: implement wasip1 SetNonblock and IsNonblock
authorChris O'Hara <cohara87@gmail.com>
Mon, 8 May 2023 07:06:08 +0000 (17:06 +1000)
committerGopher Robot <gobot@golang.org>
Thu, 11 May 2023 23:29:04 +0000 (23:29 +0000)
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 <johan.brandhorst@gmail.com>
Run-TryBot: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
Reviewed-by: Julien Fabre <ju.pryz@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Achille Roussel <achille.roussel@gmail.com>
src/internal/syscall/unix/nonblocking_wasip1.go
src/syscall/fs_wasip1.go
src/syscall/net_wasip1.go
src/syscall/syscall_wasip1.go

index 49a2a232bab50b10524b18e4df039f899e741104..208db28c3efdb119dae82774ed206878445eb817 100644 (file)
@@ -6,6 +6,22 @@
 
 package unix
 
+import (
+       "syscall"
+       _ "unsafe" // for go:linkname
+)
+
 func IsNonblock(fd int) (nonblocking bool, err error) {
-       return false, nil
+       flags, e1 := fd_fdstat_get_flags(fd)
+       if e1 != nil {
+               return false, e1
+       }
+       return flags&syscall.FDFLAG_NONBLOCK != 0, nil
 }
+
+// This helper is implemented in the syscall package. It means we don't have
+// to redefine the fd_fdstat_get host import or the fdstat struct it
+// populates.
+//
+//go:linkname fd_fdstat_get_flags syscall.fd_fdstat_get_flags
+func fd_fdstat_get_flags(fd int) (uint32, error)
index 84c65c070f6da1fc70d991f8c3632e649f5da263..25cabf82343660532cec038e15348a951ac77750 100644 (file)
@@ -255,6 +255,30 @@ func path_open(rootFD int32, dirflags lookupflags, path unsafe.Pointer, pathLen
 //go:noescape
 func random_get(buf unsafe.Pointer, bufLen size) Errno
 
+// https://github.com/WebAssembly/WASI/blob/a2b96e81c0586125cc4dc79a5be0b78d9a059925/legacy/preview1/docs.md#-fdstat-record
+// fdflags must be at offset 2, hence the uint16 type rather than the
+// fdflags (uint32) type.
+type fdstat struct {
+       filetype         filetype
+       fdflags          uint16
+       rightsBase       rights
+       rightsInheriting rights
+}
+
+//go:wasmimport wasi_snapshot_preview1 fd_fdstat_get
+//go:noescape
+func fd_fdstat_get(fd int32, buf unsafe.Pointer) Errno
+
+//go:wasmimport wasi_snapshot_preview1 fd_fdstat_set_flags
+//go:noescape
+func fd_fdstat_set_flags(fd int32, flags fdflags) Errno
+
+func fd_fdstat_get_flags(fd int) (uint32, error) {
+       var stat fdstat
+       errno := fd_fdstat_get(int32(fd), unsafe.Pointer(&stat))
+       return uint32(stat.fdflags), errnoErr(errno)
+}
+
 type preopentype = uint8
 
 const (
index d41e873bed2ec0d07adfa0096df42789b1b515f3..896dd3e7708d09675d502a6bdf49ee1343bd42b1 100644 (file)
@@ -122,7 +122,3 @@ func SetWriteDeadline(fd int, t int64) error {
 func Shutdown(fd int, how int) error {
        return ENOSYS
 }
-
-func SetNonblock(fd int, nonblocking bool) error {
-       return ENOSYS
-}
index 73a461763abe1f03ec7cc95b72328c5f95479301..5d19c000ae6176656cffca0ec8850df8e4882463 100644 (file)
@@ -464,3 +464,17 @@ const (
 //go:wasmimport wasi_snapshot_preview1 clock_time_get
 //go:noescape
 func clock_time_get(id clockid, precision timestamp, time unsafe.Pointer) Errno
+
+func SetNonblock(fd int, nonblocking bool) error {
+       flags, err := fd_fdstat_get_flags(fd)
+       if err != nil {
+               return err
+       }
+       if nonblocking {
+               flags |= FDFLAG_NONBLOCK
+       } else {
+               flags &^= FDFLAG_NONBLOCK
+       }
+       errno := fd_fdstat_set_flags(int32(fd), flags)
+       return errnoErr(errno)
+}