github.com/bazelbuild/bazel-watcher@v0.25.2/internal/ibazel/process_group/process_group_unix.go (about)

     1  // Copyright 2018 The Bazel Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build !windows
    16  
    17  package process_group
    18  
    19  import (
    20  	"os/exec"
    21  	"syscall"
    22  )
    23  
    24  type unixProcessGroup struct {
    25  	root *exec.Cmd
    26  }
    27  
    28  // Command creates a new ProcessGroup with a root command specified by the
    29  // arguments.
    30  func Command(name string, arg ...string) ProcessGroup {
    31  	root := exec.Command(name, arg...)
    32  	root.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
    33  	return &unixProcessGroup{root}
    34  }
    35  
    36  func (pg *unixProcessGroup) RootProcess() *exec.Cmd {
    37  	return pg.root
    38  }
    39  
    40  func (pg *unixProcessGroup) Start() error {
    41  	return pg.root.Start()
    42  }
    43  
    44  func (pg *unixProcessGroup) Signal(signum syscall.Signal) error {
    45  	// Send the signal to the process PID which should propagate down to any
    46  	// subprocesses in the PGID (Process Group ID). To send to the PGID, send the
    47  	// signal to the negative of the process PID.
    48  	return syscall.Kill(-pg.root.Process.Pid, signum)
    49  }
    50  
    51  func (pg *unixProcessGroup) Wait() error {
    52  	return pg.root.Wait()
    53  }
    54  
    55  func (pg *unixProcessGroup) Close() error {
    56  	return nil
    57  }
    58  
    59  func (pg *unixProcessGroup) CombinedOutput() ([]byte, error) {
    60  	return pg.root.CombinedOutput()
    61  }