2018-04-14 17:55:19 +00:00
|
|
|
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
|
2016-03-17 09:53:31 +00:00
|
|
|
|
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-04-14 17:55:19 +00:00
|
|
|
"os"
|
2017-11-21 23:51:39 +00:00
|
|
|
"syscall"
|
2016-03-17 09:53:31 +00:00
|
|
|
)
|
|
|
|
|
2017-11-21 23:51:39 +00:00
|
|
|
// configure new process group for child process
|
|
|
|
func (e *UniversalExecutor) setNewProcessGroup() error {
|
2018-09-24 18:37:45 +00:00
|
|
|
if e.childCmd.SysProcAttr == nil {
|
|
|
|
e.childCmd.SysProcAttr = &syscall.SysProcAttr{}
|
2017-11-21 23:51:39 +00:00
|
|
|
}
|
2018-09-24 18:37:45 +00:00
|
|
|
e.childCmd.SysProcAttr.Setpgid = true
|
2017-11-21 23:51:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-04-14 17:55:19 +00:00
|
|
|
|
|
|
|
// Cleanup any still hanging user processes
|
|
|
|
func (e *UniversalExecutor) cleanupChildProcesses(proc *os.Process) error {
|
|
|
|
// If new process group was created upon command execution
|
|
|
|
// we can kill the whole process group now to cleanup any leftovers.
|
2018-09-24 18:37:45 +00:00
|
|
|
if e.childCmd.SysProcAttr != nil && e.childCmd.SysProcAttr.Setpgid {
|
2018-04-14 17:55:19 +00:00
|
|
|
if err := syscall.Kill(-proc.Pid, syscall.SIGKILL); err != nil && err.Error() != noSuchProcessErr {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-04-17 17:43:04 +00:00
|
|
|
return proc.Kill()
|
2018-04-14 17:55:19 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 17:43:04 +00:00
|
|
|
// Only send the process a shutdown signal (default INT), doesn't
|
|
|
|
// necessarily kill it.
|
2018-09-24 18:37:45 +00:00
|
|
|
func (e *UniversalExecutor) shutdownProcess(sig os.Signal, proc *os.Process) error {
|
|
|
|
if sig == nil {
|
|
|
|
sig = os.Interrupt
|
2018-04-14 17:55:19 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
if err := proc.Signal(sig); err != nil && err.Error() != finishedErr {
|
|
|
|
return fmt.Errorf("executor shutdown error: %v", err)
|
2018-04-14 17:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|