github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/internal/sysfs/sock_supported.go (about)

     1  //go:build linux || darwin || windows
     2  
     3  package sysfs
     4  
     5  import (
     6  	"syscall"
     7  
     8  	experimentalsys "github.com/bananabytelabs/wazero/experimental/sys"
     9  )
    10  
    11  // syscallConnControl extracts a syscall.RawConn from the given syscall.Conn and applies
    12  // the given fn to a file descriptor, returning an integer or a nonzero syscall.Errno on failure.
    13  //
    14  // syscallConnControl streamlines the pattern of extracting the syscall.Rawconn,
    15  // invoking its syscall.RawConn.Control method, then handling properly the errors that may occur
    16  // within fn or returned by syscall.RawConn.Control itself.
    17  func syscallConnControl(conn syscall.Conn, fn func(fd uintptr) (int, experimentalsys.Errno)) (n int, errno experimentalsys.Errno) {
    18  	syscallConn, err := conn.SyscallConn()
    19  	if err != nil {
    20  		return 0, experimentalsys.UnwrapOSError(err)
    21  	}
    22  	// Prioritize the inner errno over Control
    23  	if controlErr := syscallConn.Control(func(fd uintptr) {
    24  		n, errno = fn(fd)
    25  	}); errno == 0 {
    26  		errno = experimentalsys.UnwrapOSError(controlErr)
    27  	}
    28  	return
    29  }