Skip to content

Commit 2145381

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

File tree

3 files changed

+89
-15
lines changed

3 files changed

+89
-15
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.KillAndWaitExit(); 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/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ container.Resume()
230230
// send signal to container's init process.
231231
container.Signal(signal)
232232

233+
// send signal to container's init process and wait it to exit.
234+
container.KillAndWaitExit()
235+
233236
// update container resource constraints.
234237
container.Set(config)
235238

libcontainer/container_linux.go

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,13 @@ func (c *Container) start(process *Process) (retErr error) {
377377

378378
// Signal sends a specified signal to container's init.
379379
//
380-
// When s is SIGKILL and the container does not have its own PID namespace, all
381-
// the container's processes are killed. In this scenario, the libcontainer
380+
// When s is SIGKILL:
381+
// 1. If the container does not have its own PID namespace, all the
382+
// container's processes are killed. In this scenario, the libcontainer
382383
// user may be required to implement a proper child reaper.
384+
// 2. Otherwise, we just send the SIGKILL signal to the init process,
385+
// but we don't wait the init process exit. If you want to wait it,
386+
// please use c.KillAndWaitExit instead.
383387
func (c *Container) Signal(s os.Signal) error {
384388
c.m.Lock()
385389
defer c.m.Unlock()
@@ -431,6 +435,80 @@ func (c *Container) signal(s os.Signal) error {
431435
return nil
432436
}
433437

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

0 commit comments

Comments
 (0)