Skip to content

Commit

Permalink
MaybeReexecUsingUserNamespace: handle SIGHUP/SIGINT/SIGTERM
Browse files Browse the repository at this point in the history
Pass along SIGHUP, SIGINT, and SIGTERM if the parent receives them while
the child process is running.  Set SIGKILL as the child's parent-death
signal so that, if we exit for some reason before the child does, it
will be stopped.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
  • Loading branch information
nalind committed Mar 21, 2022
1 parent 8e56539 commit 178c70e
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pkg/unshare/unshare_linux.go
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"os"
"os/exec"
"os/signal"
"os/user"
"runtime"
"strconv"
Expand Down Expand Up @@ -484,6 +485,30 @@ func MaybeReexecUsingUserNamespace(evenForRoot bool) {

// Finish up.
logrus.Debugf("Running %+v with environment %+v, UID map %+v, and GID map %+v", cmd.Cmd.Args, os.Environ(), cmd.UidMappings, cmd.GidMappings)

// Forward SIGHUP, SIGINT, and SIGTERM to our child process.
interrupted := make(chan os.Signal, 100)
defer func() {
signal.Stop(interrupted)
close(interrupted)
}()
cmd.Hook = func(int) error {
go func() {
for receivedSignal := range interrupted {
cmd.Cmd.Process.Signal(receivedSignal)
}
}()
return nil
}
signal.Notify(interrupted, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)

// Make sure our child process gets SIGKILLed if we exit, for whatever
// reason, before it does.
if cmd.Cmd.SysProcAttr == nil {
cmd.Cmd.SysProcAttr = &syscall.SysProcAttr{}
}
cmd.Cmd.SysProcAttr.Pdeathsig = syscall.SIGKILL

ExecRunnable(cmd, nil)
}

Expand Down

0 comments on commit 178c70e

Please sign in to comment.