...

Source file src/github.com/opencontainers/runc/kill.go

Documentation: github.com/opencontainers/runc

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  
     8  	"github.com/urfave/cli"
     9  	"golang.org/x/sys/unix"
    10  )
    11  
    12  var killCommand = cli.Command{
    13  	Name:  "kill",
    14  	Usage: "kill sends the specified signal (default: SIGTERM) to the container's init process",
    15  	ArgsUsage: `<container-id> [signal]
    16  
    17  Where "<container-id>" is the name for the instance of the container and
    18  "[signal]" is the signal to be sent to the init process.
    19  
    20  EXAMPLE:
    21  For example, if the container id is "ubuntu01" the following will send a "KILL"
    22  signal to the init process of the "ubuntu01" container:
    23  
    24         # runc kill ubuntu01 KILL`,
    25  	Flags: []cli.Flag{
    26  		cli.BoolFlag{
    27  			Name:  "all, a",
    28  			Usage: "send the specified signal to all processes inside the container",
    29  		},
    30  	},
    31  	Action: func(context *cli.Context) error {
    32  		if err := checkArgs(context, 1, minArgs); err != nil {
    33  			return err
    34  		}
    35  		if err := checkArgs(context, 2, maxArgs); err != nil {
    36  			return err
    37  		}
    38  		container, err := getContainer(context)
    39  		if err != nil {
    40  			return err
    41  		}
    42  
    43  		sigstr := context.Args().Get(1)
    44  		if sigstr == "" {
    45  			sigstr = "SIGTERM"
    46  		}
    47  
    48  		signal, err := parseSignal(sigstr)
    49  		if err != nil {
    50  			return err
    51  		}
    52  		return container.Signal(signal, context.Bool("all"))
    53  	},
    54  }
    55  
    56  func parseSignal(rawSignal string) (unix.Signal, error) {
    57  	s, err := strconv.Atoi(rawSignal)
    58  	if err == nil {
    59  		return unix.Signal(s), nil
    60  	}
    61  	sig := strings.ToUpper(rawSignal)
    62  	if !strings.HasPrefix(sig, "SIG") {
    63  		sig = "SIG" + sig
    64  	}
    65  	signal := unix.SignalNum(sig)
    66  	if signal == 0 {
    67  		return -1, fmt.Errorf("unknown signal %q", rawSignal)
    68  	}
    69  	return signal, nil
    70  }
    71  

View as plain text