Skip to content

Commit

Permalink
Merge pull request #1170 from nalind/forward-signals
Browse files Browse the repository at this point in the history
MaybeReexecUsingUserNamespace: handle SIGHUP/SIGINT/SIGTERM
  • Loading branch information
giuseppe committed Mar 22, 2022
2 parents 8e56539 + 178c70e commit 03b1581
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 03b1581

Please sign in to comment.