| runCmdContext = runCmdContextLinux |
| // runCommandContext runs cmd controlled by ctx, killing it and all its |
| // children if necessary. cmd.SysProcAttr must be unset. |
| func runCmdContextLinux(ctx context.Context, cmd *exec.Cmd) error { |
| if cmd.SysProcAttr != nil { |
| return fmt.Errorf("cmd.SysProcAttr must be nil") |
| cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} |
| if err := cmd.Start(); err != nil { |
| resChan := make(chan error, 1) |
| // Canceled. Interrupt and see if it ends voluntarily. |
| cmd.Process.Signal(os.Interrupt) |
| case <-time.After(time.Second): |
| // Didn't shut down in response to interrupt. It may have child processes |
| // holding stdout/sterr open. Kill its process group hard. |
| syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) |