diff --git a/internal/sysfs/dirfs_link_unsupported.go b/internal/sysfs/dirfs_link_unsupported.go index a4fe718bd5..427f306d02 100644 --- a/internal/sysfs/dirfs_link_unsupported.go +++ b/internal/sysfs/dirfs_link_unsupported.go @@ -8,5 +8,5 @@ import ( // Link implements the same method as documented on sys.FS func (d *dirFS) Link(oldName, newName string) experimentalsys.Errno { - panic("Link is not supported by TinyGo") + return experimentalsys.ENOSYS } diff --git a/internal/sysfs/sock_big.go b/internal/sysfs/sock_big.go deleted file mode 100644 index 47c0fa6769..0000000000 --- a/internal/sysfs/sock_big.go +++ /dev/null @@ -1,56 +0,0 @@ -//go:build !tinygo - -package sysfs - -import ( - "net" - - experimentalsys "github.com/tetratelabs/wazero/experimental/sys" - "github.com/tetratelabs/wazero/internal/fsapi" - socketapi "github.com/tetratelabs/wazero/internal/sock" -) - -// Accept implements the same method as documented on socketapi.TCPSock -func (f *tcpListenerFile) Accept() (socketapi.TCPConn, experimentalsys.Errno) { - // Ensure we have an incoming connection, otherwise return immediately. - if f.nonblock { - if ready, errno := _pollSock(f.tl, fsapi.POLLIN, 0); !ready || errno != 0 { - return nil, experimentalsys.EAGAIN - } - } - - // Accept normally blocks goroutines, but we - // made sure that we have an incoming connection, - // so we should be safe. - if conn, err := f.tl.Accept(); err != nil { - return nil, experimentalsys.UnwrapOSError(err) - } else { - return newTcpConn(conn.(*net.TCPConn)), 0 - } -} - -// SetNonblock implements the same method as documented on fsapi.File -func (f *tcpListenerFile) SetNonblock(enabled bool) (errno experimentalsys.Errno) { - f.nonblock = enabled - _, errno = syscallConnControl(f.tl, func(fd uintptr) (int, experimentalsys.Errno) { - return 0, setNonblockSocket(fd, enabled) - }) - return -} - -// Shutdown implements the same method as documented on experimentalsys.Conn -func (f *tcpConnFile) Shutdown(how int) experimentalsys.Errno { - // FIXME: can userland shutdown listeners? - var err error - switch how { - case socketapi.SHUT_RD: - err = f.tc.CloseRead() - case socketapi.SHUT_WR: - err = f.tc.CloseWrite() - case socketapi.SHUT_RDWR: - return f.close() - default: - return experimentalsys.EINVAL - } - return experimentalsys.UnwrapOSError(err) -} diff --git a/internal/sysfs/sock_supported.go b/internal/sysfs/sock_supported.go index 747393b4fb..6c976fb868 100644 --- a/internal/sysfs/sock_supported.go +++ b/internal/sysfs/sock_supported.go @@ -1,13 +1,61 @@ -//go:build linux || darwin || windows +//go:build (linux || darwin || windows) && !tinygo package sysfs import ( + "net" "syscall" experimentalsys "github.com/tetratelabs/wazero/experimental/sys" + "github.com/tetratelabs/wazero/internal/fsapi" + socketapi "github.com/tetratelabs/wazero/internal/sock" ) +// Accept implements the same method as documented on socketapi.TCPSock +func (f *tcpListenerFile) Accept() (socketapi.TCPConn, experimentalsys.Errno) { + // Ensure we have an incoming connection, otherwise return immediately. + if f.nonblock { + if ready, errno := _pollSock(f.tl, fsapi.POLLIN, 0); !ready || errno != 0 { + return nil, experimentalsys.EAGAIN + } + } + + // Accept normally blocks goroutines, but we + // made sure that we have an incoming connection, + // so we should be safe. + if conn, err := f.tl.Accept(); err != nil { + return nil, experimentalsys.UnwrapOSError(err) + } else { + return newTcpConn(conn.(*net.TCPConn)), 0 + } +} + +// SetNonblock implements the same method as documented on fsapi.File +func (f *tcpListenerFile) SetNonblock(enabled bool) (errno experimentalsys.Errno) { + f.nonblock = enabled + _, errno = syscallConnControl(f.tl, func(fd uintptr) (int, experimentalsys.Errno) { + return 0, setNonblockSocket(fd, enabled) + }) + return +} + +// Shutdown implements the same method as documented on experimentalsys.Conn +func (f *tcpConnFile) Shutdown(how int) experimentalsys.Errno { + // FIXME: can userland shutdown listeners? + var err error + switch how { + case socketapi.SHUT_RD: + err = f.tc.CloseRead() + case socketapi.SHUT_WR: + err = f.tc.CloseWrite() + case socketapi.SHUT_RDWR: + return f.close() + default: + return experimentalsys.EINVAL + } + return experimentalsys.UnwrapOSError(err) +} + // syscallConnControl extracts a syscall.RawConn from the given syscall.Conn and applies // the given fn to a file descriptor, returning an integer or a nonzero syscall.Errno on failure. // diff --git a/internal/sysfs/sock_tiny.go b/internal/sysfs/sock_tiny.go deleted file mode 100644 index 60060548ab..0000000000 --- a/internal/sysfs/sock_tiny.go +++ /dev/null @@ -1,30 +0,0 @@ -//go:build tinygo - -package sysfs - -import ( - experimentalsys "github.com/tetratelabs/wazero/experimental/sys" - socketapi "github.com/tetratelabs/wazero/internal/sock" -) - -// Accept implements the same method as documented on socketapi.TCPSock -func (f *tcpListenerFile) Accept() (socketapi.TCPConn, experimentalsys.Errno) { - panic("TCPSock.Accept is not implemented for TinyGo") -} - -// Shutdown implements the same method as documented on experimentalsys.Conn -func (f *tcpConnFile) Shutdown(how int) experimentalsys.Errno { - // FIXME: can userland shutdown listeners? - var err error - switch how { - case socketapi.SHUT_RD: - err = f.tc.Close() - case socketapi.SHUT_WR: - err = f.tc.Close() - case socketapi.SHUT_RDWR: - return f.close() - default: - return experimentalsys.EINVAL - } - return experimentalsys.UnwrapOSError(err) -} diff --git a/internal/sysfs/sock_unix.go b/internal/sysfs/sock_unix.go index 0f570f37b7..99ef018a4e 100644 --- a/internal/sysfs/sock_unix.go +++ b/internal/sysfs/sock_unix.go @@ -1,4 +1,4 @@ -//go:build linux || darwin +//go:build (linux || darwin) && !tinygo package sysfs diff --git a/internal/sysfs/sock_unsupported.go b/internal/sysfs/sock_unsupported.go index cee02742b5..efb12a6ecc 100644 --- a/internal/sysfs/sock_unsupported.go +++ b/internal/sysfs/sock_unsupported.go @@ -1,4 +1,4 @@ -//go:build !linux && !darwin && !windows +//go:build (!linux && !darwin && !windows) || tinygo package sysfs @@ -57,3 +57,25 @@ func recvfrom(fd uintptr, buf []byte, flags int32) (n int, errno sys.Errno) { func syscallConnControl(conn syscall.Conn, fn func(fd uintptr) (int, experimentalsys.Errno)) (n int, errno sys.Errno) { return -1, sys.ENOTSUP } + +// Accept implements the same method as documented on socketapi.TCPSock +func (f *tcpListenerFile) Accept() (socketapi.TCPConn, experimentalsys.Errno) { + panic("TCPSock.Accept is not implemented for TinyGo") +} + +// Shutdown implements the same method as documented on experimentalsys.Conn +func (f *tcpConnFile) Shutdown(how int) experimentalsys.Errno { + // FIXME: can userland shutdown listeners? + var err error + switch how { + case socketapi.SHUT_RD: + err = f.tc.Close() + case socketapi.SHUT_WR: + err = f.tc.Close() + case socketapi.SHUT_RDWR: + return f.close() + default: + return experimentalsys.EINVAL + } + return experimentalsys.UnwrapOSError(err) +}