build: update path helpers

This commit is contained in:
Alexandre Pujol 2024-10-05 23:03:41 +01:00
parent 4b5f7f2b52
commit 7757038a4f
Failed to generate hash of commit
2 changed files with 30 additions and 3 deletions

View file

@ -55,7 +55,8 @@ func NewProcess(extraEnv []string, args ...string) (*Process, error) {
cmd: exec.Command(args[0], args[1:]...),
}
p.cmd.Env = append(os.Environ(), extraEnv...)
p.TellCommandNotToSpawnShell()
tellCommandNotToSpawnShell(p.cmd) // windows specific
tellCommandToStartOnNewProcessGroup(p.cmd) // linux specific
// This is required because some tools detects if the program is running
// from terminal by looking at the stdin/out bindings.
@ -146,7 +147,7 @@ func (p *Process) Signal(sig os.Signal) error {
// actually exited. This only kills the Process itself, not any other processes it may
// have started.
func (p *Process) Kill() error {
return p.cmd.Process.Kill()
return kill(p.cmd)
}
// SetDir sets the working directory of the command. If Dir is the empty string, Run

View file

@ -31,8 +31,34 @@
package paths
import "os/exec"
import (
"os/exec"
"syscall"
)
func tellCommandNotToSpawnShell(_ *exec.Cmd) {
// no op
}
func tellCommandToStartOnNewProcessGroup(oscmd *exec.Cmd) {
// https://groups.google.com/g/golang-nuts/c/XoQ3RhFBJl8
// Start the process in a new process group.
// This is needed to kill the process and its children
// if we need to kill the process.
if oscmd.SysProcAttr == nil {
oscmd.SysProcAttr = &syscall.SysProcAttr{}
}
oscmd.SysProcAttr.Setpgid = true
}
func kill(oscmd *exec.Cmd) error {
// https://groups.google.com/g/golang-nuts/c/XoQ3RhFBJl8
// Kill the process group
pgid, err := syscall.Getpgid(oscmd.Process.Pid)
if err != nil {
return err
}
return syscall.Kill(-pgid, syscall.SIGKILL)
}