github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/kubectl/exec_windows.go (about)

     1  /*
     2  Copyright 2020 The Skaffold Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package kubectl
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"os/exec"
    23  	"unsafe"
    24  
    25  	"golang.org/x/sys/windows"
    26  )
    27  
    28  // Cmd represents an external command being prepared to run within a job object
    29  type Cmd struct {
    30  	*exec.Cmd
    31  	handle windows.Handle
    32  	ctx    context.Context
    33  }
    34  
    35  // CommandContext creates a new Cmd
    36  func CommandContext(ctx context.Context, name string, arg ...string) *Cmd {
    37  	return &Cmd{Cmd: exec.CommandContext(ctx, name, arg...), ctx: ctx}
    38  }
    39  
    40  // Start starts the specified command in a job object but does not wait for it to complete
    41  func (c *Cmd) Start() error {
    42  	handle, err := windows.CreateJobObject(nil, nil)
    43  	if err != nil {
    44  		return fmt.Errorf("could not create job object: %w", err)
    45  	}
    46  
    47  	// https://gist.github.com/hallazzang/76f3970bfc949831808bbebc8ca15209
    48  	info := windows.JOBOBJECT_EXTENDED_LIMIT_INFORMATION{
    49  		BasicLimitInformation: windows.JOBOBJECT_BASIC_LIMIT_INFORMATION{
    50  			LimitFlags: windows.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
    51  		},
    52  	}
    53  	if _, err := windows.SetInformationJobObject(
    54  		handle,
    55  		windows.JobObjectExtendedLimitInformation,
    56  		uintptr(unsafe.Pointer(&info)),
    57  		uint32(unsafe.Sizeof(info))); err != nil {
    58  		return fmt.Errorf("could not set information job object: %w", err)
    59  	}
    60  
    61  	if err := c.Cmd.Start(); err != nil {
    62  		return fmt.Errorf("could not start the command: %w", err)
    63  	}
    64  
    65  	// Use `unsafe` to extract the process handle.
    66  	processHandle := (*struct {
    67  		Pid    int
    68  		handle windows.Handle
    69  	})(unsafe.Pointer(c.Process)).handle
    70  
    71  	if err := windows.AssignProcessToJobObject(handle, processHandle); err != nil {
    72  		return fmt.Errorf("could not assign job object: %w", err)
    73  	}
    74  
    75  	c.handle = handle
    76  	go func() {
    77  		<-c.ctx.Done()
    78  		c.Terminate()
    79  	}()
    80  
    81  	return nil
    82  }
    83  
    84  // Run starts the specified command in a job object and waits for it to complete
    85  func (c *Cmd) Run() error {
    86  	if err := c.Start(); err != nil {
    87  		return err
    88  	}
    89  	return c.Wait()
    90  }
    91  
    92  // Terminate closes the job object handle which kills all connected processes
    93  func (c *Cmd) Terminate() error {
    94  	return windows.CloseHandle(c.handle)
    95  }