Skip to content
quarry Artifact [808a9f58fb]
Login

Artifact [808a9f58fb]

Artifact 808a9f58fba21cc82ffc4974fce3757d141c6f14741111fd71537a01d6b70fd8:


//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)
	}
}