Skip to content

Commit 192bdb7

Browse files
committed
child: refactor command execution to use goroutines with Pdeathsig
Signed-off-by: fahed dorgaa <fahed.dorgaa@gmail.com>
1 parent e83d763 commit 192bdb7

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

pkg/child/child.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -442,12 +442,6 @@ func Child(opt Opt) error {
442442

443443
// The parent calls child with Pdeathsig, but it is cleared when newuidmap SUID binary is called
444444
// https://github.com/rootless-containers/rootlesskit/issues/65#issuecomment-492343646
445-
runtime.LockOSThread()
446-
err = unix.Prctl(unix.PR_SET_PDEATHSIG, uintptr(unix.SIGKILL), 0, 0, 0)
447-
runtime.UnlockOSThread()
448-
if err != nil {
449-
return err
450-
}
451445
os.Unsetenv(opt.PipeFDEnvKey)
452446
if err := pipeR.Close(); err != nil {
453447
return fmt.Errorf("failed to close fd %d: %w", pipeFD, err)
@@ -483,8 +477,29 @@ func Child(opt Opt) error {
483477
if err != nil {
484478
return err
485479
}
480+
481+
// Create a channel to receive errors from the goroutine
482+
cmdErrCh := make(chan error, 1)
483+
486484
if opt.Reaper {
487-
if err := runAndReap(cmd); err != nil {
485+
// Launch a goroutine to execute the command with Pdeathsig
486+
go func() {
487+
// Lock the goroutine to the OS thread
488+
runtime.LockOSThread()
489+
defer runtime.UnlockOSThread()
490+
491+
// Set the parent death signal
492+
if err := unix.Prctl(unix.PR_SET_PDEATHSIG, uintptr(unix.SIGKILL), 0, 0, 0); err != nil {
493+
cmdErrCh <- err
494+
return
495+
}
496+
497+
// Run the command
498+
cmdErrCh <- runAndReap(cmd)
499+
}()
500+
501+
// Wait for the command to complete
502+
if err := <-cmdErrCh; err != nil {
488503
return fmt.Errorf("command %v exited: %w", opt.TargetCmd, err)
489504
}
490505
} else {

0 commit comments

Comments
 (0)