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)
//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 (
func Shutdown(fd int, how int) error {
return ENOSYS
}
-
-func SetNonblock(fd int, nonblocking bool) error {
- return ENOSYS
-}
//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)
+}