kill.go 2.2 KB

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