summaryrefslogtreecommitdiff
path: root/vendor/github.com/tetratelabs/wazero/internal/sysfs/sock_unix.go
blob: 99ef018a4e292408cf5906a26124018dac0949fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//go:build (linux || darwin) && !tinygo

package sysfs

import (
	"net"
	"syscall"

	"github.com/tetratelabs/wazero/experimental/sys"
	"github.com/tetratelabs/wazero/internal/fsapi"
	socketapi "github.com/tetratelabs/wazero/internal/sock"
)

// MSG_PEEK is the constant syscall.MSG_PEEK
const MSG_PEEK = syscall.MSG_PEEK

func newTCPListenerFile(tl *net.TCPListener) socketapi.TCPSock {
	return newDefaultTCPListenerFile(tl)
}

func _pollSock(conn syscall.Conn, flag fsapi.Pflag, timeoutMillis int32) (bool, sys.Errno) {
	n, errno := syscallConnControl(conn, func(fd uintptr) (int, sys.Errno) {
		if ready, errno := poll(fd, fsapi.POLLIN, 0); !ready || errno != 0 {
			return -1, errno
		} else {
			return 0, errno
		}
	})
	return n >= 0, errno
}

func setNonblockSocket(fd uintptr, enabled bool) sys.Errno {
	return sys.UnwrapOSError(setNonblock(fd, enabled))
}

func readSocket(fd uintptr, buf []byte) (int, sys.Errno) {
	n, err := syscall.Read(int(fd), buf)
	return n, sys.UnwrapOSError(err)
}

func writeSocket(fd uintptr, buf []byte) (int, sys.Errno) {
	n, err := syscall.Write(int(fd), buf)
	return n, sys.UnwrapOSError(err)
}

func recvfrom(fd uintptr, buf []byte, flags int32) (n int, errno sys.Errno) {
	n, _, err := syscall.Recvfrom(int(fd), buf, int(flags))
	return n, sys.UnwrapOSError(err)
}