Skip to content

Commit cc27fa6

Browse files
committed
libct: use pidfd and epoll to wait the init process exit
Signed-off-by: lfbzhm <[email protected]>
1 parent 08525df commit cc27fa6

File tree

2 files changed

+80
-13
lines changed

2 files changed

+80
-13
lines changed

delete.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,16 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8-
"time"
98

109
"github.com/opencontainers/runc/libcontainer"
1110
"github.com/urfave/cli"
12-
13-
"golang.org/x/sys/unix"
1411
)
1512

16-
func killContainer(container *libcontainer.Container) error {
17-
_ = container.Signal(unix.SIGKILL)
18-
for i := 0; i < 100; i++ {
19-
time.Sleep(100 * time.Millisecond)
20-
if err := container.Signal(unix.Signal(0)); err != nil {
21-
return container.Destroy()
22-
}
13+
func killAndDestroy(container *libcontainer.Container) error {
14+
if err := container.Kill(); err != nil {
15+
return err
2316
}
24-
return errors.New("container init still running")
17+
return container.Destroy()
2518
}
2619

2720
var deleteCommand = cli.Command{
@@ -71,7 +64,7 @@ status of "ubuntu01" as "stopped" the following will delete resources held for
7164
// namespace) there may be some leftover processes in the
7265
// container's cgroup.
7366
if force {
74-
return killContainer(container)
67+
return killAndDestroy(container)
7568
}
7669
s, err := container.Status()
7770
if err != nil {
@@ -81,7 +74,7 @@ status of "ubuntu01" as "stopped" the following will delete resources held for
8174
case libcontainer.Stopped:
8275
return container.Destroy()
8376
case libcontainer.Created:
84-
return killContainer(container)
77+
return killAndDestroy(container)
8578
default:
8679
return fmt.Errorf("cannot delete container %s that is not stopped: %s", id, s)
8780
}

libcontainer/container_linux.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,80 @@ func (c *Container) signal(s os.Signal) error {
423423
return nil
424424
}
425425

426+
func (c *Container) killViaPidfd() error {
427+
pidfd, err := unix.PidfdOpen(c.initProcess.pid(), 0)
428+
if err != nil {
429+
return err
430+
}
431+
defer unix.Close(pidfd)
432+
433+
epollfd, err := unix.EpollCreate1(unix.EPOLL_CLOEXEC)
434+
if err != nil {
435+
return err
436+
}
437+
defer unix.Close(epollfd)
438+
439+
event := unix.EpollEvent{
440+
Events: unix.EPOLLIN,
441+
Fd: int32(pidfd),
442+
}
443+
if err := unix.EpollCtl(epollfd, unix.EPOLL_CTL_ADD, pidfd, &event); err != nil {
444+
return err
445+
}
446+
447+
// We don't need unix.PidfdSendSignal because go runtime will use it if possible.
448+
_ = c.Signal(unix.SIGKILL)
449+
450+
events := make([]unix.EpollEvent, 1)
451+
for {
452+
// Set the timeout to 10s, the same as in kill below.
453+
n, err := unix.EpollWait(epollfd, events, 10000)
454+
if err != nil {
455+
if err == unix.EINTR {
456+
continue
457+
}
458+
return err
459+
}
460+
461+
if n == 0 {
462+
return errors.New("container init still running")
463+
}
464+
465+
if n > 0 {
466+
event := events[0]
467+
if event.Fd == int32(pidfd) {
468+
return nil
469+
}
470+
}
471+
}
472+
}
473+
474+
func (c *Container) kill() error {
475+
_ = c.Signal(unix.SIGKILL)
476+
for i := 0; i < 100; i++ {
477+
time.Sleep(100 * time.Millisecond)
478+
if err := c.Signal(unix.Signal(0)); err != nil {
479+
return nil
480+
}
481+
}
482+
return errors.New("container init still running")
483+
}
484+
485+
// Kill kills the container and waits for the init process to exit.
486+
func (c *Container) Kill() error {
487+
// When a container doesn't have a private pidns, we have to kill all processes
488+
// in the cgroup, it's more simpler to use `cgroup.kill` or `unix.Kill`.
489+
if c.config.Namespaces.IsPrivate(configs.NEWPID) {
490+
err := c.killViaPidfd()
491+
if err == nil {
492+
return nil
493+
}
494+
495+
logrus.Debugf("pidfd & epoll failed, falling back to unix.Signal: %v", err)
496+
}
497+
return c.kill()
498+
}
499+
426500
func (c *Container) createExecFifo() (retErr error) {
427501
rootuid, err := c.Config().HostRootUID()
428502
if err != nil {

0 commit comments

Comments
 (0)