// Copyright 2020 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build !windows

package invoke

import (
	"os/exec"
	"syscall"

	"go.chromium.org/luci/common/errors"
)

func setSysProcAttr(cmd *exec.Cmd) {
	cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
}

func (s *Subprocess) terminate() error {
	if err := syscall.Kill(-s.cmd.Process.Pid, syscall.SIGTERM); err != nil {
		return errors.Annotate(err, "send SIGTERM").Err()
	}
	return nil
}

// Copyright 2020 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build !windows

package invoke

import (
	"os/exec"
	"syscall"

	"go.chromium.org/luci/common/errors"
)

func setSysProcAttr(_ *exec.Cmd) {}

func (s *Subprocess) terminate() error {
	if err := s.cmd.Process.Signal(syscall.SIGTERM); err != nil {
		return errors.Annotate(err, "send SIGTERM").Err()
	}
	return nil
}

--- 
+++ 
@@ -23,10 +23,12 @@
 	"go.chromium.org/luci/common/errors"
 )
 
-func setSysProcAttr(_ *exec.Cmd) {}
+func setSysProcAttr(cmd *exec.Cmd) {
+	cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
+}
 
 func (s *Subprocess) terminate() error {
-	if err := s.cmd.Process.Signal(syscall.SIGTERM); err != nil {
+	if err := syscall.Kill(-s.cmd.Process.Pid, syscall.SIGTERM); err != nil {
 		return errors.Annotate(err, "send SIGTERM").Err()
 	}
 	return nil
[bbagent] Make and signal process group on *nix.

This should enable bootstrapper-based builds to cancel correctly.

R=chanli, yiwzhang

Change-Id: I786a527a1023c58d79805aaf19fac5d7cd085886
Reviewed-on: https://chromium-review.googlesource.com/c/infra/luci/luci-go/+/3340242
Auto-Submit: Robbie Iannucci <40f3d43a28ebae3cb819288542e1c84d73d962d5@chromium.org>
Reviewed-by: Yiwei Zhang <50b2a565e2e78c292794832469a30ce5abc9959c@google.com>
Commit-Queue: Yiwei Zhang <50b2a565e2e78c292794832469a30ce5abc9959c@google.com>
