Skip to content

Commit 491326c

Browse files
committed
int/linux: add/use Recvfrom
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
1 parent e655abc commit 491326c

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

internal/linux/linux.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ func Openat(dirfd int, path string, mode int, perm uint32) (fd int, err error) {
5353
return fd, nil
5454
}
5555

56+
// Recvfrom wraps [unix.Recvfrom].
57+
func Recvfrom(fd int, p []byte, flags int) (n int, from unix.Sockaddr, err error) {
58+
err = retryOnEINTR(func() error {
59+
n, from, err = unix.Recvfrom(fd, p, flags)
60+
return err
61+
})
62+
if err != nil {
63+
return 0, nil, os.NewSyscallError("recvfrom", err)
64+
}
65+
return n, from, err
66+
}
67+
5668
// Sendmsg wraps [unix.Sendmsg].
5769
func Sendmsg(fd int, p, oob []byte, to unix.Sockaddr, flags int) error {
5870
err := retryOnEINTR(func() error {

libcontainer/sync_unix.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"sync/atomic"
88

9+
"github.com/opencontainers/runc/internal/linux"
910
"golang.org/x/sys/unix"
1011
)
1112

@@ -42,20 +43,9 @@ func (s *syncSocket) WritePacket(b []byte) (int, error) {
4243
}
4344

4445
func (s *syncSocket) ReadPacket() ([]byte, error) {
45-
var (
46-
size int
47-
err error
48-
)
49-
50-
for {
51-
size, _, err = unix.Recvfrom(int(s.f.Fd()), nil, unix.MSG_TRUNC|unix.MSG_PEEK)
52-
if err != unix.EINTR { //nolint:errorlint // unix errors are bare
53-
break
54-
}
55-
}
56-
46+
size, _, err := linux.Recvfrom(int(s.f.Fd()), nil, unix.MSG_TRUNC|unix.MSG_PEEK)
5747
if err != nil {
58-
return nil, fmt.Errorf("fetch packet length from socket: %w", os.NewSyscallError("recvfrom", err))
48+
return nil, fmt.Errorf("fetch packet length from socket: %w", err)
5949
}
6050
// We will only get a zero size if the socket has been closed from the
6151
// other end (otherwise recvfrom(2) will block until a packet is ready). In

0 commit comments

Comments
 (0)