//go:build unix
package zitcli
import (
"os/exec"
"syscall"
)
// OwnGroup runs cmd in a process group of its own and makes its
// cancellation kill the whole group: what zit starts (git, and the ssh
// git starts) as well as zit. Killing zit alone leaves them holding the
// output pipe and the connection.
func OwnGroup(cmd *exec.Cmd) {
// Kept, not replaced: the child may already be bound to a memory
// group (children.ConfineMemory).
if cmd.SysProcAttr == nil {
cmd.SysProcAttr = &syscall.SysProcAttr{}
}
cmd.SysProcAttr.Setpgid = true
prev := cmd.Cancel
cmd.Cancel = func() error {
if prev != nil {
_ = prev()
}
return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
}
}