Skip to content

Commit 7de268b

Browse files
committed
libct: Use RetryOnEINTR() instead of open-coding it
Let's move the existing code to the new helper. There are two places that are not moved, because they currently use an unbounded for loop for other reasons and the code seems cleaner keeping it as it is. Signed-off-by: Rodrigo Campos <rodrigoca@microsoft.com>
1 parent 471094e commit 7de268b

File tree

4 files changed

+21
-36
lines changed

4 files changed

+21
-36
lines changed

libcontainer/specconv/spec_linux.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/opencontainers/runc/libcontainer/configs"
2020
"github.com/opencontainers/runc/libcontainer/internal/userns"
2121
"github.com/opencontainers/runc/libcontainer/seccomp"
22+
"github.com/opencontainers/runc/libcontainer/utils"
2223
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
2324
"github.com/opencontainers/runtime-spec/specs-go"
2425
"github.com/sirupsen/logrus"
@@ -348,12 +349,9 @@ type CreateOpts struct {
348349
// the value from the kernel, which guarantees the returned value
349350
// to be absolute and clean.
350351
func getwd() (wd string, err error) {
351-
for {
352-
wd, err = unix.Getwd()
353-
if err != unix.EINTR {
354-
break
355-
}
356-
}
352+
wd, err = utils.RetryOnEINTR2(func() (string, error) {
353+
return unix.Getwd()
354+
})
357355
return wd, os.NewSyscallError("getwd", err)
358356
}
359357

libcontainer/sync_unix.go

Lines changed: 5 additions & 12 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/libcontainer/utils"
910
"golang.org/x/sys/unix"
1011
)
1112

@@ -42,18 +43,10 @@ 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 := utils.RetryOnEINTR2(func() (int, error) {
47+
size, _, err := unix.Recvfrom(int(s.f.Fd()), nil, unix.MSG_TRUNC|unix.MSG_PEEK)
48+
return size, err
49+
})
5750
if err != nil {
5851
return nil, fmt.Errorf("fetch packet length from socket: %w", os.NewSyscallError("recvfrom", err))
5952
}

libcontainer/system/linux.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"unsafe"
1010

11+
"github.com/opencontainers/runc/libcontainer/utils"
1112
"github.com/sirupsen/logrus"
1213
"golang.org/x/sys/unix"
1314
)
@@ -33,12 +34,10 @@ func (p ParentDeathSignal) Set() error {
3334
}
3435

3536
func Exec(cmd string, args []string, env []string) error {
36-
for {
37-
err := unix.Exec(cmd, args, env)
38-
if err != unix.EINTR {
39-
return &os.PathError{Op: "exec", Path: cmd, Err: err}
40-
}
41-
}
37+
err := utils.RetryOnEINTR(func() error {
38+
return unix.Exec(cmd, args, env)
39+
})
40+
return &os.PathError{Op: "exec", Path: cmd, Err: err}
4241
}
4342

4443
func SetParentDeathSignal(sig uintptr) error {

libcontainer/utils/cmsg.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,10 @@ func RecvFile(socket *os.File) (_ *os.File, Err error) {
4747
err error
4848
)
4949

50-
for {
50+
err = RetryOnEINTR(func() error {
5151
n, oobn, _, _, err = unix.Recvmsg(int(sockfd), name, oob, unix.MSG_CMSG_CLOEXEC)
52-
if err != unix.EINTR { //nolint:errorlint // unix errors are bare
53-
break
54-
}
55-
}
56-
52+
return err
53+
})
5754
if err != nil {
5855
return nil, os.NewSyscallError("recvmsg", err)
5956
}
@@ -126,10 +123,8 @@ func SendFile(socket *os.File, file *os.File) error {
126123
// SendRawFd sends a specific file descriptor over the given AF_UNIX socket.
127124
func SendRawFd(socket *os.File, msg string, fd uintptr) error {
128125
oob := unix.UnixRights(int(fd))
129-
for {
130-
err := unix.Sendmsg(int(socket.Fd()), []byte(msg), oob, nil, 0)
131-
if err != unix.EINTR { //nolint:errorlint // unix errors are bare
132-
return os.NewSyscallError("sendmsg", err)
133-
}
134-
}
126+
err := RetryOnEINTR(func() error {
127+
return unix.Sendmsg(int(socket.Fd()), []byte(msg), oob, nil, 0)
128+
})
129+
return os.NewSyscallError("sendmsg", err)
135130
}

0 commit comments

Comments
 (0)