mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 06:03:57 +08:00
*: introduce pidfd-socket flag
The container manager like containerd-shim can't use cgroup.kill feature or freeze all the processes in cgroup to terminate the exec init process. It's unsafe to call kill(2) since the pid can be recycled. It's good to provide the pidfd of init process through the pidfd-socket. It's similar to the console-socket. With the pidfd, the container manager like containerd-shim can send the signal to target process safely. And for the standard init process, we can have polling support to get exit event instead of blocking on wait4. Signed-off-by: Wei Fu <fuweid89@gmail.com>
This commit is contained in:
@@ -179,6 +179,16 @@ func startInitialization() (retErr error) {
|
||||
defer consoleSocket.Close()
|
||||
}
|
||||
|
||||
var pidfdSocket *os.File
|
||||
if envSockFd := os.Getenv("_LIBCONTAINER_PIDFD_SOCK"); envSockFd != "" {
|
||||
sockFd, err := strconv.Atoi(envSockFd)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to convert _LIBCONTAINER_PIDFD_SOCK: %w", err)
|
||||
}
|
||||
pidfdSocket = os.NewFile(uintptr(sockFd), "pidfd-socket")
|
||||
defer pidfdSocket.Close()
|
||||
}
|
||||
|
||||
// Get mount files (O_PATH).
|
||||
mountSrcFds, err := parseFdsFromEnv("_LIBCONTAINER_MOUNT_FDS")
|
||||
if err != nil {
|
||||
@@ -222,10 +232,10 @@ func startInitialization() (retErr error) {
|
||||
}
|
||||
|
||||
// If init succeeds, it will not return, hence none of the defers will be called.
|
||||
return containerInit(it, &config, syncPipe, consoleSocket, fifofd, logFD, dmzExe, mountFds{sourceFds: mountSrcFds, idmapFds: idmapFds})
|
||||
return containerInit(it, &config, syncPipe, consoleSocket, pidfdSocket, fifofd, logFD, dmzExe, mountFds{sourceFds: mountSrcFds, idmapFds: idmapFds})
|
||||
}
|
||||
|
||||
func containerInit(t initType, config *initConfig, pipe *syncSocket, consoleSocket *os.File, fifoFd, logFd int, dmzExe *os.File, mountFds mountFds) error {
|
||||
func containerInit(t initType, config *initConfig, pipe *syncSocket, consoleSocket, pidfdSocket *os.File, fifoFd, logFd int, dmzExe *os.File, mountFds mountFds) error {
|
||||
if err := populateProcessEnvironment(config.Env); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -240,6 +250,7 @@ func containerInit(t initType, config *initConfig, pipe *syncSocket, consoleSock
|
||||
i := &linuxSetnsInit{
|
||||
pipe: pipe,
|
||||
consoleSocket: consoleSocket,
|
||||
pidfdSocket: pidfdSocket,
|
||||
config: config,
|
||||
logFd: logFd,
|
||||
dmzExe: dmzExe,
|
||||
@@ -249,6 +260,7 @@ func containerInit(t initType, config *initConfig, pipe *syncSocket, consoleSock
|
||||
i := &linuxStandardInit{
|
||||
pipe: pipe,
|
||||
consoleSocket: consoleSocket,
|
||||
pidfdSocket: pidfdSocket,
|
||||
parentPid: unix.Getppid(),
|
||||
config: config,
|
||||
fifoFd: fifoFd,
|
||||
@@ -690,3 +702,20 @@ func signalAllProcesses(m cgroups.Manager, s unix.Signal) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// setupPidfd opens a process file descriptor of init process, and sends the
|
||||
// file descriptor back to the socket.
|
||||
func setupPidfd(socket *os.File, initType string) error {
|
||||
defer socket.Close()
|
||||
|
||||
pidFd, err := unix.PidfdOpen(os.Getpid(), 0)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to pidfd_open: %w", err)
|
||||
}
|
||||
|
||||
if err := utils.SendRawFd(socket, initType, uintptr(pidFd)); err != nil {
|
||||
unix.Close(pidFd)
|
||||
return fmt.Errorf("failed to send pidfd on socket: %w", err)
|
||||
}
|
||||
return unix.Close(pidFd)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user