Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/rootlesskit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ func createChildOpt(clicontext *cli.Context, pipeFDEnvKey string, targetCmd []st
PipeFDEnvKey: pipeFDEnvKey,
TargetCmd: targetCmd,
MountProcfs: clicontext.Bool("pidns"),
Reaper: clicontext.Bool("pidns"),
}
switch s := clicontext.String("net"); s {
case "host":
Expand Down
36 changes: 34 additions & 2 deletions pkg/child/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"os/signal"
"runtime"
"strconv"
"syscall"
Expand Down Expand Up @@ -165,6 +166,7 @@ type Opt struct {
CopyUpDirs []string
PortDriver port.ChildDriver
MountProcfs bool // needs to be set if (and only if) parent.Opt.CreatePIDNS is set
Reaper bool
}

func Child(opt Opt) error {
Expand Down Expand Up @@ -236,12 +238,42 @@ func Child(opt Opt) error {
if err != nil {
return err
}
if err := cmd.Run(); err != nil {
return errors.Wrapf(err, "command %v exited", opt.TargetCmd)
if opt.Reaper {
if err := runAndReap(cmd); err != nil {
return errors.Wrapf(err, "command %v exited", opt.TargetCmd)
}
} else {
if err := cmd.Run(); err != nil {
return errors.Wrapf(err, "command %v exited", opt.TargetCmd)
}
}
if opt.PortDriver != nil {
portQuitCh <- struct{}{}
return <-portErrCh
}
return nil
}

func runAndReap(cmd *exec.Cmd) error {
c := make(chan os.Signal, 32)
signal.Notify(c, syscall.SIGCHLD)
if err := cmd.Start(); err != nil {
return err
}
result := make(chan error)
go func() {
defer close(result)
for range c {
for {
if pid, err := syscall.Wait4(-1, nil, syscall.WNOHANG, nil); err != nil || pid <= 0 {
break
} else {
if pid == cmd.Process.Pid {
result <- cmd.Wait()
}
}
}
}
}()
return <-result
}