...

Source file src/github.com/Microsoft/hcsshim/cmd/runhcs/kill.go

Documentation: github.com/Microsoft/hcsshim/cmd/runhcs

     1  //go:build windows
     2  
     3  package main
     4  
     5  import (
     6  	gcontext "context"
     7  
     8  	"github.com/Microsoft/hcsshim/internal/appargs"
     9  	"github.com/Microsoft/hcsshim/internal/hcs"
    10  	"github.com/Microsoft/hcsshim/internal/hcs/schema1"
    11  	"github.com/Microsoft/hcsshim/internal/signals"
    12  	"github.com/Microsoft/hcsshim/osversion"
    13  	"github.com/urfave/cli"
    14  )
    15  
    16  var killCommand = cli.Command{
    17  	Name:  "kill",
    18  	Usage: "kill sends the specified signal (default: SIGTERM) to the container's init process",
    19  	ArgsUsage: `<container-id> [signal]
    20  
    21  Where "<container-id>" is the name for the instance of the container and
    22  "[signal]" is the signal to be sent to the init process.
    23  
    24  EXAMPLE:
    25  For example, if the container id is "ubuntu01" the following will send a "KILL"
    26  signal to the init process of the "ubuntu01" container:
    27  
    28         # runhcs kill ubuntu01 KILL`,
    29  	Flags:  []cli.Flag{},
    30  	Before: appargs.Validate(argID, appargs.Optional(appargs.String)),
    31  	Action: func(context *cli.Context) error {
    32  		id := context.Args().First()
    33  		c, err := getContainer(id, true)
    34  		if err != nil {
    35  			return err
    36  		}
    37  		defer c.Close()
    38  		status, err := c.Status()
    39  		if err != nil {
    40  			return err
    41  		}
    42  		if status != containerRunning {
    43  			return errContainerStopped
    44  		}
    45  
    46  		signalsSupported := false
    47  
    48  		// The Signal feature was added in RS5
    49  		if osversion.Build() >= osversion.RS5 {
    50  			if c.IsHost || c.HostID != "" {
    51  				var hostID string
    52  				if c.IsHost {
    53  					// This is the LCOW, Pod Sandbox, or Windows Xenon V2 for RS5+
    54  					hostID = vmID(c.ID)
    55  				} else {
    56  					// This is the Nth container in a Pod
    57  					hostID = c.HostID
    58  				}
    59  				uvm, err := hcs.OpenComputeSystem(gcontext.Background(), hostID)
    60  				if err != nil {
    61  					return err
    62  				}
    63  				defer uvm.Close()
    64  				if props, err := uvm.Properties(gcontext.Background(), schema1.PropertyTypeGuestConnection); err == nil &&
    65  					props.GuestConnectionInfo.GuestDefinedCapabilities.SignalProcessSupported {
    66  					signalsSupported = true
    67  				}
    68  			} else if c.Spec.Linux == nil && c.Spec.Windows.HyperV == nil {
    69  				// RS5+ Windows Argon
    70  				signalsSupported = true
    71  			}
    72  		}
    73  
    74  		var sigOptions interface{}
    75  		if signalsSupported {
    76  			sigStr := context.Args().Get(1)
    77  			if c.Spec.Linux == nil {
    78  				opts, err := signals.ValidateSigstrWCOW(sigStr, signalsSupported)
    79  				if err != nil {
    80  					return err
    81  				}
    82  				sigOptions = opts
    83  			} else {
    84  				opts, err := signals.ValidateSigstrLCOW(sigStr, signalsSupported)
    85  				if err != nil {
    86  					return err
    87  				}
    88  				sigOptions = opts
    89  			}
    90  		}
    91  
    92  		var pid int
    93  		if err := stateKey.Get(id, keyInitPid, &pid); err != nil {
    94  			return err
    95  		}
    96  
    97  		p, err := c.hc.OpenProcess(gcontext.Background(), pid)
    98  		if err != nil {
    99  			return err
   100  		}
   101  		defer p.Close()
   102  
   103  		if signalsSupported && sigOptions != nil && (c.Spec.Linux != nil || !c.Spec.Process.Terminal) {
   104  			_, err = p.Signal(gcontext.Background(), sigOptions)
   105  		} else {
   106  			// Legacy signal issue a kill
   107  			_, err = p.Kill(gcontext.Background())
   108  		}
   109  
   110  		return err
   111  	},
   112  }
   113  

View as plain text