Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / opencontainers / runc / kill.go
1 // +build linux
2
3 package runc
4
5 import (
6         "fmt"
7         "strconv"
8         "strings"
9         "syscall"
10
11         "github.com/urfave/cli"
12 )
13
14 var signalMap = map[string]syscall.Signal{
15         "ABRT":   syscall.SIGABRT,
16         "ALRM":   syscall.SIGALRM,
17         "BUS":    syscall.SIGBUS,
18         "CHLD":   syscall.SIGCHLD,
19         "CLD":    syscall.SIGCLD,
20         "CONT":   syscall.SIGCONT,
21         "FPE":    syscall.SIGFPE,
22         "HUP":    syscall.SIGHUP,
23         "ILL":    syscall.SIGILL,
24         "INT":    syscall.SIGINT,
25         "IO":     syscall.SIGIO,
26         "IOT":    syscall.SIGIOT,
27         "KILL":   syscall.SIGKILL,
28         "PIPE":   syscall.SIGPIPE,
29         "POLL":   syscall.SIGPOLL,
30         "PROF":   syscall.SIGPROF,
31         "PWR":    syscall.SIGPWR,
32         "QUIT":   syscall.SIGQUIT,
33         "SEGV":   syscall.SIGSEGV,
34         "STKFLT": syscall.SIGSTKFLT,
35         "STOP":   syscall.SIGSTOP,
36         "SYS":    syscall.SIGSYS,
37         "TERM":   syscall.SIGTERM,
38         "TRAP":   syscall.SIGTRAP,
39         "TSTP":   syscall.SIGTSTP,
40         "TTIN":   syscall.SIGTTIN,
41         "TTOU":   syscall.SIGTTOU,
42         "UNUSED": syscall.SIGUNUSED,
43         "URG":    syscall.SIGURG,
44         "USR1":   syscall.SIGUSR1,
45         "USR2":   syscall.SIGUSR2,
46         "VTALRM": syscall.SIGVTALRM,
47         "WINCH":  syscall.SIGWINCH,
48         "XCPU":   syscall.SIGXCPU,
49         "XFSZ":   syscall.SIGXFSZ,
50 }
51
52 var killCommand = cli.Command{
53         Name:  "kill",
54         Usage: "kill sends the specified signal (default: SIGTERM) to the container's init process",
55         ArgsUsage: `<container-id> [signal]
56
57 Where "<container-id>" is the name for the instance of the container and
58 "[signal]" is the signal to be sent to the init process.
59
60 EXAMPLE:
61 For example, if the container id is "ubuntu01" the following will send a "KILL"
62 signal to the init process of the "ubuntu01" container:
63          
64        # runc kill ubuntu01 KILL`,
65         Flags: []cli.Flag{
66                 cli.BoolFlag{
67                         Name:  "all, a",
68                         Usage: "send the specified signal to all processes inside the container",
69                 },
70         },
71         Action: func(context *cli.Context) error {
72                 if err := checkArgs(context, 1, minArgs); err != nil {
73                         return err
74                 }
75                 if err := checkArgs(context, 2, maxArgs); err != nil {
76                         return err
77                 }
78                 container, err := getContainer(context)
79                 if err != nil {
80                         return err
81                 }
82
83                 sigstr := context.Args().Get(1)
84                 if sigstr == "" {
85                         sigstr = "SIGTERM"
86                 }
87
88                 signal, err := parseSignal(sigstr)
89                 if err != nil {
90                         return err
91                 }
92                 if err := container.Signal(signal, context.Bool("all")); err != nil {
93                         return err
94                 }
95                 return nil
96         },
97 }
98
99 func parseSignal(rawSignal string) (syscall.Signal, error) {
100         s, err := strconv.Atoi(rawSignal)
101         if err == nil {
102                 sig := syscall.Signal(s)
103                 for _, msig := range signalMap {
104                         if sig == msig {
105                                 return sig, nil
106                         }
107                 }
108                 return -1, fmt.Errorf("unknown signal %q", rawSignal)
109         }
110         signal, ok := signalMap[strings.TrimPrefix(strings.ToUpper(rawSignal), "SIG")]
111         if !ok {
112                 return -1, fmt.Errorf("unknown signal %q", rawSignal)
113         }
114         return signal, nil
115 }