github.com/google/fleetspeak@v0.1.15-0.20240426164851-4f31f62c1aea/fleetspeak/src/client/daemonservice/command/command_windows.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 windows
    16  
    17  package command
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"syscall"
    23  )
    24  
    25  func (cmd *Command) softKill() error {
    26  	return cmd.Kill()
    27  }
    28  
    29  func (cmd *Command) kill() error {
    30  	return cmd.Cmd.Process.Kill()
    31  }
    32  
    33  func (cmd *Command) addInPipeFDImpl() (*os.File, int, error) {
    34  	pr, pw, err := os.Pipe()
    35  	if err != nil {
    36  		return nil, 0, fmt.Errorf("error in os.Pipe: %v", err)
    37  	}
    38  
    39  	fd := pr.Fd()
    40  	syscall.SetHandleInformation(syscall.Handle(fd), syscall.HANDLE_FLAG_INHERIT, 1)
    41  	cmd.filesToClose = append(cmd.filesToClose, pr)
    42  
    43  	if cmd.SysProcAttr == nil {
    44  		cmd.SysProcAttr = &syscall.SysProcAttr{}
    45  	}
    46  	cmd.SysProcAttr.AdditionalInheritedHandles = append(cmd.SysProcAttr.AdditionalInheritedHandles, syscall.Handle(fd))
    47  
    48  	return pw, int(fd), nil
    49  }
    50  
    51  func (cmd *Command) addOutPipeFDImpl() (*os.File, int, error) {
    52  	pr, pw, err := os.Pipe()
    53  	if err != nil {
    54  		return nil, 0, fmt.Errorf("error in os.Pipe: %v", err)
    55  	}
    56  
    57  	fd := pw.Fd()
    58  	syscall.SetHandleInformation(syscall.Handle(fd), syscall.HANDLE_FLAG_INHERIT, 1)
    59  	cmd.filesToClose = append(cmd.filesToClose, pw)
    60  
    61  	if cmd.SysProcAttr == nil {
    62  		cmd.SysProcAttr = &syscall.SysProcAttr{}
    63  	}
    64  	cmd.SysProcAttr.AdditionalInheritedHandles = append(cmd.SysProcAttr.AdditionalInheritedHandles, syscall.Handle(fd))
    65  
    66  	return pr, int(fd), nil
    67  }