github.com/hernad/nomad@v1.6.112/drivers/shared/executor/executor_unix.go (about)

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