github.com/hernad/nomad@v1.6.112/drivers/nix/_executor/executor_unix.go (about)

     1  //go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
     2  
     3  package executor
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  	"syscall"
     9  )
    10  
    11  // configure new process group for child process
    12  func (e *UniversalExecutor) setNewProcessGroup() error {
    13  	if e.childCmd.SysProcAttr == nil {
    14  		e.childCmd.SysProcAttr = &syscall.SysProcAttr{}
    15  	}
    16  	e.childCmd.SysProcAttr.Setpgid = true
    17  	return nil
    18  }
    19  
    20  // SIGKILL the process group starting at process.Pid
    21  func (e *UniversalExecutor) killProcessTree(process *os.Process) error {
    22  	pid := process.Pid
    23  	negative := -pid // tells unix to kill entire process group
    24  	signal := syscall.SIGKILL
    25  
    26  	// If new process group was created upon command execution
    27  	// we can kill the whole process group now to cleanup any leftovers.
    28  	if e.childCmd.SysProcAttr != nil && e.childCmd.SysProcAttr.Setpgid {
    29  		e.logger.Trace("sending sigkill to process group", "pid", pid, "negative", negative, "signal", signal)
    30  		if err := syscall.Kill(negative, signal); err != nil && err.Error() != noSuchProcessErr {
    31  			return err
    32  		}
    33  		return nil
    34  	}
    35  	return process.Kill()
    36  }
    37  
    38  // Only send the process a shutdown signal (default INT), doesn't
    39  // necessarily kill it.
    40  func (e *UniversalExecutor) shutdownProcess(sig os.Signal, proc *os.Process) error {
    41  	if sig == nil {
    42  		sig = os.Interrupt
    43  	}
    44  
    45  	if err := proc.Signal(sig); err != nil && err.Error() != finishedErr {
    46  		return fmt.Errorf("executor shutdown error: %v", err)
    47  	}
    48  
    49  	return nil
    50  }