Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions libcontainer/notify_v2_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package libcontainer

import (
"fmt"
"os"
"path/filepath"
"unsafe"

Expand Down Expand Up @@ -40,7 +41,11 @@ func registerMemoryEventV2(cgDir, evName, cgEvName string) (<-chan struct{}, err

for {
n, err := unix.Read(fd, buffer[:])
Copy link
Member

@lifubang lifubang Feb 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM, a small suggestion:
Maybe we can use os.File to read data from this fd.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, nice idea, but fd comes from:

        fd, err := unix.InotifyInit()
...
        evFd, err := unix.InotifyAddWatch(fd, filepath.Join(cgDir, evName), unix.IN_MODIFY)
....

And os.File closes the underlying fd when go detects is not used anymore. It might cause issues here, in the inotify stuff. So I'd not do it as part of this PR, we can explore in another one if this works or not IMHO.

if err == unix.EINTR { //nolint:errorlint // unix errors are bare
continue
}
if err != nil {
err = os.NewSyscallError("read", err)
logrus.Warnf("unable to read event data from inotify, got error: %v", err)
return
}
Expand Down
15 changes: 13 additions & 2 deletions libcontainer/sync_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,20 @@ func (s *syncSocket) WritePacket(b []byte) (int, error) {
}

func (s *syncSocket) ReadPacket() ([]byte, error) {
size, _, err := unix.Recvfrom(int(s.f.Fd()), nil, unix.MSG_TRUNC|unix.MSG_PEEK)
var (
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 {
return nil, fmt.Errorf("fetch packet length from socket: %w", err)
return nil, fmt.Errorf("fetch packet length from socket: %w", os.NewSyscallError("recvfrom", err))
}
// 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
Expand Down
22 changes: 19 additions & 3 deletions libcontainer/utils/cmsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,20 @@ func RecvFile(socket *os.File) (_ *os.File, Err error) {
oob := make([]byte, oobSpace)

sockfd := socket.Fd()
n, oobn, _, _, err := unix.Recvmsg(int(sockfd), name, oob, unix.MSG_CMSG_CLOEXEC)
var (
n, oobn int
err error
)

for {
n, oobn, _, _, err = unix.Recvmsg(int(sockfd), name, oob, unix.MSG_CMSG_CLOEXEC)
if err != unix.EINTR { //nolint:errorlint // unix errors are bare
break
}
}

if err != nil {
return nil, err
return nil, os.NewSyscallError("recvmsg", err)
}
if n >= MaxNameLen || oobn != oobSpace {
return nil, fmt.Errorf("recvfile: incorrect number of bytes read (n=%d oobn=%d)", n, oobn)
Expand Down Expand Up @@ -115,5 +126,10 @@ func SendFile(socket *os.File, file *os.File) error {
// SendRawFd sends a specific file descriptor over the given AF_UNIX socket.
func SendRawFd(socket *os.File, msg string, fd uintptr) error {
oob := unix.UnixRights(int(fd))
return unix.Sendmsg(int(socket.Fd()), []byte(msg), oob, nil, 0)
for {
err := unix.Sendmsg(int(socket.Fd()), []byte(msg), oob, nil, 0)
if err != unix.EINTR { //nolint:errorlint // unix errors are bare
return os.NewSyscallError("sendmsg", err)
}
}
}