github.com/google/fleetspeak@v0.1.15-0.20240426164851-4f31f62c1aea/fleetspeak/src/client/daemonservice/command/command_unix.go (about)

     1  // Copyright 2017 Google Inc.
     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  //     https://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  //go:build linux || darwin
    16  
    17  package command
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  
    23  	"golang.org/x/sys/unix"
    24  )
    25  
    26  func (cmd *Command) softKill() error {
    27  	return unix.Kill(cmd.Process.Pid, unix.SIGINT)
    28  }
    29  
    30  func (cmd *Command) kill() error {
    31  	return unix.Kill(cmd.Process.Pid, unix.SIGKILL)
    32  }
    33  
    34  func (cmd *Command) addInPipeFDImpl() (*os.File, int, error) {
    35  	pr, pw, err := os.Pipe()
    36  	if err != nil {
    37  		return nil, 0, fmt.Errorf("error in os.Pipe: %v", err)
    38  	}
    39  
    40  	cmd.ExtraFiles = append(cmd.ExtraFiles, pr)
    41  	cmd.filesToClose = append(cmd.filesToClose, pr)
    42  
    43  	// Starts with 3.
    44  	// See: https://golang.org/pkg/os/exec/#Cmd
    45  	fd := len(cmd.ExtraFiles) + 2
    46  
    47  	return pw, fd, nil
    48  }
    49  
    50  func (cmd *Command) addOutPipeFDImpl() (*os.File, int, error) {
    51  	pr, pw, err := os.Pipe()
    52  	if err != nil {
    53  		return nil, 0, fmt.Errorf("error in os.Pipe: %v", err)
    54  	}
    55  
    56  	cmd.ExtraFiles = append(cmd.ExtraFiles, pw)
    57  	cmd.filesToClose = append(cmd.filesToClose, pw)
    58  
    59  	// Starts with 3.
    60  	// See: https://golang.org/pkg/os/exec/#Cmd
    61  	fd := len(cmd.ExtraFiles) + 2
    62  
    63  	return pr, fd, nil
    64  }