github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/internal/sysfs/sock_unix.go (about)

     1  //go:build (linux || darwin) && !tinygo
     2  
     3  package sysfs
     4  
     5  import (
     6  	"net"
     7  	"syscall"
     8  
     9  	"github.com/tetratelabs/wazero/experimental/sys"
    10  	"github.com/tetratelabs/wazero/internal/fsapi"
    11  	socketapi "github.com/tetratelabs/wazero/internal/sock"
    12  )
    13  
    14  // MSG_PEEK is the constant syscall.MSG_PEEK
    15  const MSG_PEEK = syscall.MSG_PEEK
    16  
    17  func newTCPListenerFile(tl *net.TCPListener) socketapi.TCPSock {
    18  	return newDefaultTCPListenerFile(tl)
    19  }
    20  
    21  func _pollSock(conn syscall.Conn, flag fsapi.Pflag, timeoutMillis int32) (bool, sys.Errno) {
    22  	n, errno := syscallConnControl(conn, func(fd uintptr) (int, sys.Errno) {
    23  		if ready, errno := poll(fd, fsapi.POLLIN, 0); !ready || errno != 0 {
    24  			return -1, errno
    25  		} else {
    26  			return 0, errno
    27  		}
    28  	})
    29  	return n >= 0, errno
    30  }
    31  
    32  func setNonblockSocket(fd uintptr, enabled bool) sys.Errno {
    33  	return sys.UnwrapOSError(setNonblock(fd, enabled))
    34  }
    35  
    36  func readSocket(fd uintptr, buf []byte) (int, sys.Errno) {
    37  	n, err := syscall.Read(int(fd), buf)
    38  	return n, sys.UnwrapOSError(err)
    39  }
    40  
    41  func writeSocket(fd uintptr, buf []byte) (int, sys.Errno) {
    42  	n, err := syscall.Write(int(fd), buf)
    43  	return n, sys.UnwrapOSError(err)
    44  }
    45  
    46  func recvfrom(fd uintptr, buf []byte, flags int32) (n int, errno sys.Errno) {
    47  	n, _, err := syscall.Recvfrom(int(fd), buf, int(flags))
    48  	return n, sys.UnwrapOSError(err)
    49  }