int/linux: add/use Recvfrom

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin
2025-03-25 14:55:33 -07:00
parent e655abc0da
commit 491326cdeb
2 changed files with 15 additions and 13 deletions
+12
View File
@@ -53,6 +53,18 @@ func Openat(dirfd int, path string, mode int, perm uint32) (fd int, err error) {
return fd, nil return fd, nil
} }
// Recvfrom wraps [unix.Recvfrom].
func Recvfrom(fd int, p []byte, flags int) (n int, from unix.Sockaddr, err error) {
err = retryOnEINTR(func() error {
n, from, err = unix.Recvfrom(fd, p, flags)
return err
})
if err != nil {
return 0, nil, os.NewSyscallError("recvfrom", err)
}
return n, from, err
}
// Sendmsg wraps [unix.Sendmsg]. // Sendmsg wraps [unix.Sendmsg].
func Sendmsg(fd int, p, oob []byte, to unix.Sockaddr, flags int) error { func Sendmsg(fd int, p, oob []byte, to unix.Sockaddr, flags int) error {
err := retryOnEINTR(func() error { err := retryOnEINTR(func() error {
+3 -13
View File
@@ -6,6 +6,7 @@ import (
"os" "os"
"sync/atomic" "sync/atomic"
"github.com/opencontainers/runc/internal/linux"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@@ -42,20 +43,9 @@ func (s *syncSocket) WritePacket(b []byte) (int, error) {
} }
func (s *syncSocket) ReadPacket() ([]byte, error) { func (s *syncSocket) ReadPacket() ([]byte, error) {
var ( size, _, err := linux.Recvfrom(int(s.f.Fd()), nil, unix.MSG_TRUNC|unix.MSG_PEEK)
size int
err error
)
for {
size, _, err = unix.Recvfrom(int(s.f.Fd()), nil, unix.MSG_TRUNC|unix.MSG_PEEK)
if err != unix.EINTR { //nolint:errorlint // unix errors are bare
break
}
}
if err != nil { if err != nil {
return nil, fmt.Errorf("fetch packet length from socket: %w", os.NewSyscallError("recvfrom", err)) return nil, fmt.Errorf("fetch packet length from socket: %w", err)
} }
// We will only get a zero size if the socket has been closed from the // We will only get a zero size if the socket has been closed from the
// other end (otherwise recvfrom(2) will block until a packet is ready). In // other end (otherwise recvfrom(2) will block until a packet is ready). In