mirror of
https://github.com/aljazceru/kata-containers.git
synced 2025-12-19 23:34:23 +01:00
runtime: use pidfd to wait for processes on Linux
Use pidfd_open and poll on newer versions of Linux to wait for the process to exit. For older versions use existing wait logic Fixes: #5617 Signed-off-by: Alexandru Matei <alexandru.matei@uipath.com>
This commit is contained in:
@@ -14,8 +14,10 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
@@ -151,3 +153,60 @@ func IsAPVFIOMediatedDevice(sysfsdev string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func waitProcessUsingPidfd(pid int, timeoutSecs uint, logger *logrus.Entry) (bool, error) {
|
||||
pidfd, err := unix.PidfdOpen(pid, 0)
|
||||
|
||||
if err != nil {
|
||||
if err == unix.ESRCH {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return true, err
|
||||
}
|
||||
|
||||
defer unix.Close(pidfd)
|
||||
var n int
|
||||
|
||||
maxDelay := time.Duration(timeoutSecs) * time.Second
|
||||
end := time.Now().Add(maxDelay)
|
||||
|
||||
for {
|
||||
remaining := time.Until(end).Milliseconds()
|
||||
if remaining < 0 {
|
||||
remaining = 0
|
||||
}
|
||||
|
||||
n, err = unix.Poll([]unix.PollFd{{Fd: int32(pidfd), Events: unix.POLLIN}}, int(remaining))
|
||||
if err != unix.EINTR {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil || n != 1 {
|
||||
logger.Warnf("process %v still running after waiting %ds", pid, timeoutSecs)
|
||||
return true, err
|
||||
}
|
||||
|
||||
for {
|
||||
err := unix.Waitid(unix.P_PIDFD, pidfd, nil, unix.WEXITED, nil)
|
||||
if err == unix.EINVAL {
|
||||
err = unix.Waitid(unix.P_PID, pid, nil, unix.WEXITED, nil)
|
||||
}
|
||||
|
||||
if err != unix.EINTR {
|
||||
break
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func waitForProcessCompletion(pid int, timeoutSecs uint, logger *logrus.Entry) bool {
|
||||
pidRunning, err := waitProcessUsingPidfd(pid, timeoutSecs, logger)
|
||||
|
||||
if err == unix.ENOSYS {
|
||||
pidRunning = waitProcessUsingWaitLoop(pid, timeoutSecs, logger)
|
||||
}
|
||||
|
||||
return pidRunning
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user