fix(agent): Allow signal propagation when running as PID 1 (#6141)

This commit is contained in:
Mathias Fredriksson
2023-02-09 23:07:21 +02:00
committed by GitHub
parent af59e2bcfa
commit 6f3f7f2937
4 changed files with 89 additions and 11 deletions

View File

@ -4,6 +4,7 @@ package reaper
import (
"os"
"os/signal"
"syscall"
"github.com/hashicorp/go-reap"
@ -15,6 +16,24 @@ func IsInitProcess() bool {
return os.Getpid() == 1
}
func catchSignals(pid int, sigs []os.Signal) {
if len(sigs) == 0 {
return
}
sc := make(chan os.Signal, 1)
signal.Notify(sc, sigs...)
defer signal.Stop(sc)
for {
s := <-sc
sig, ok := s.(syscall.Signal)
if ok {
_ = syscall.Kill(pid, sig)
}
}
}
// ForkReap spawns a goroutine that reaps children. In order to avoid
// complications with spawning `exec.Commands` in the same process that
// is reaping, we forkexec a child process. This prevents a race between
@ -51,13 +70,17 @@ func ForkReap(opt ...Option) error {
}
//#nosec G204
pid, _ := syscall.ForkExec(opts.ExecArgs[0], opts.ExecArgs, pattrs)
pid, err := syscall.ForkExec(opts.ExecArgs[0], opts.ExecArgs, pattrs)
if err != nil {
return xerrors.Errorf("fork exec: %w", err)
}
go catchSignals(pid, opts.CatchSignals)
var wstatus syscall.WaitStatus
_, err = syscall.Wait4(pid, &wstatus, 0, nil)
for xerrors.Is(err, syscall.EINTR) {
_, err = syscall.Wait4(pid, &wstatus, 0, nil)
}
return nil
return err
}