Skip to content

Commit

Permalink
libpod: Factor out the call to PidFdOpen from (*Container).WaitForExit
Browse files Browse the repository at this point in the history
This allows us to add a simple stub for FreeBSD which returns -1,
leading WaitForExit to fall back to the sleep loop approach.

[NO NEW TESTS NEEDED]

Signed-off-by: Doug Rabson <[email protected]>
  • Loading branch information
dfr authored and mheon committed Oct 18, 2022
1 parent c9c683a commit 295d0d1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
18 changes: 4 additions & 14 deletions libpod/container_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,21 +521,11 @@ func (c *Container) WaitForExit(ctx context.Context, pollInterval time.Duration)
var conmonTimer time.Timer
conmonTimerSet := false

conmonPidFd := -1
conmonPidFdTriggered := false

if c.state.ConmonPID != 0 {
// Track lifetime of conmon precisely using pidfd_open + poll.
// There are many cases for this to fail, for instance conmon is dead
// or pidfd_open is not supported (pre linux 5.3), so fall back to the
// traditional loop with poll + sleep
if fd, err := unix.PidfdOpen(c.state.ConmonPID, 0); err == nil {
conmonPidFd = fd
defer unix.Close(conmonPidFd)
} else if err != unix.ENOSYS && err != unix.ESRCH {
logrus.Debugf("PidfdOpen(%d) failed: %v", c.state.ConmonPID, err)
}
conmonPidFd := c.getConmonPidFd()
if conmonPidFd != -1 {
defer unix.Close(conmonPidFd)
}
conmonPidFdTriggered := false

getExitCode := func() (bool, int32, error) {
containerRemoved := false
Expand Down
7 changes: 7 additions & 0 deletions libpod/container_internal_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,10 @@ func setVolumeAtime(mountPoint string, st os.FileInfo) error {
func (c *Container) makePlatformBindMounts() error {
return nil
}

func (c *Container) getConmonPidFd() int {
// Note: kqueue(2) could be used here but that would require
// factoring out the call to unix.PollFd from WaitForExit so
// keeping things simple for now.
return -1
}
15 changes: 15 additions & 0 deletions libpod/container_internal_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,18 @@ func (c *Container) makePlatformBindMounts() error {
}
return nil
}

func (c *Container) getConmonPidFd() int {
if c.state.ConmonPID != 0 {
// Track lifetime of conmon precisely using pidfd_open + poll.
// There are many cases for this to fail, for instance conmon is dead
// or pidfd_open is not supported (pre linux 5.3), so fall back to the
// traditional loop with poll + sleep
if fd, err := unix.PidfdOpen(c.state.ConmonPID, 0); err == nil {
return fd
} else if err != unix.ENOSYS && err != unix.ESRCH {
logrus.Debugf("PidfdOpen(%d) failed: %v", c.state.ConmonPID, err)
}
}
return -1
}

0 comments on commit 295d0d1

Please sign in to comment.