2018-04-11 00:22:27 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os/exec"
|
|
|
|
"syscall"
|
2019-01-22 22:04:54 +00:00
|
|
|
"time"
|
2018-04-11 00:22:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func runProcessGroup(ctx context.Context, cmd *exec.Cmd) error {
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
waitDone := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2019-01-22 22:04:54 +00:00
|
|
|
syscall.Kill(-cmd.Process.Pid, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-waitDone:
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
|
|
|
}
|
|
|
|
}()
|
2018-04-11 00:22:27 +00:00
|
|
|
case <-waitDone:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
err := cmd.Wait()
|
|
|
|
close(waitDone)
|
|
|
|
return err
|
|
|
|
}
|