This is part of an effort to break apart the deprecated server/ package
Docker-DCO-1.1-Signed-off-by: Solomon Hykes <solomon@docker.com> (github: shykes)
| ... | ... |
@@ -117,6 +117,9 @@ func (daemon *Daemon) Install(eng *engine.Engine) error {
|
| 117 | 117 |
if err := eng.Register("unpause", daemon.ContainerUnpause); err != nil {
|
| 118 | 118 |
return err |
| 119 | 119 |
} |
| 120 |
+ if err := eng.Register("kill", daemon.ContainerKill); err != nil {
|
|
| 121 |
+ return err |
|
| 122 |
+ } |
|
| 120 | 123 |
return nil |
| 121 | 124 |
} |
| 122 | 125 |
|
| 123 | 126 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,59 @@ |
| 0 |
+package daemon |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "strconv" |
|
| 4 |
+ "strings" |
|
| 5 |
+ "syscall" |
|
| 6 |
+ |
|
| 7 |
+ "github.com/docker/docker/engine" |
|
| 8 |
+ "github.com/docker/docker/pkg/signal" |
|
| 9 |
+) |
|
| 10 |
+ |
|
| 11 |
+// ContainerKill send signal to the container |
|
| 12 |
+// If no signal is given (sig 0), then Kill with SIGKILL and wait |
|
| 13 |
+// for the container to exit. |
|
| 14 |
+// If a signal is given, then just send it to the container and return. |
|
| 15 |
+func (daemon *Daemon) ContainerKill(job *engine.Job) engine.Status {
|
|
| 16 |
+ if n := len(job.Args); n < 1 || n > 2 {
|
|
| 17 |
+ return job.Errorf("Usage: %s CONTAINER [SIGNAL]", job.Name)
|
|
| 18 |
+ } |
|
| 19 |
+ var ( |
|
| 20 |
+ name = job.Args[0] |
|
| 21 |
+ sig uint64 |
|
| 22 |
+ err error |
|
| 23 |
+ ) |
|
| 24 |
+ |
|
| 25 |
+ // If we have a signal, look at it. Otherwise, do nothing |
|
| 26 |
+ if len(job.Args) == 2 && job.Args[1] != "" {
|
|
| 27 |
+ // Check if we passed the signal as a number: |
|
| 28 |
+ // The largest legal signal is 31, so let's parse on 5 bits |
|
| 29 |
+ sig, err = strconv.ParseUint(job.Args[1], 10, 5) |
|
| 30 |
+ if err != nil {
|
|
| 31 |
+ // The signal is not a number, treat it as a string (either like "KILL" or like "SIGKILL") |
|
| 32 |
+ sig = uint64(signal.SignalMap[strings.TrimPrefix(job.Args[1], "SIG")]) |
|
| 33 |
+ } |
|
| 34 |
+ |
|
| 35 |
+ if sig == 0 {
|
|
| 36 |
+ return job.Errorf("Invalid signal: %s", job.Args[1])
|
|
| 37 |
+ } |
|
| 38 |
+ } |
|
| 39 |
+ |
|
| 40 |
+ if container := daemon.Get(name); container != nil {
|
|
| 41 |
+ // If no signal is passed, or SIGKILL, perform regular Kill (SIGKILL + wait()) |
|
| 42 |
+ if sig == 0 || syscall.Signal(sig) == syscall.SIGKILL {
|
|
| 43 |
+ if err := container.Kill(); err != nil {
|
|
| 44 |
+ return job.Errorf("Cannot kill container %s: %s", name, err)
|
|
| 45 |
+ } |
|
| 46 |
+ job.Eng.Job("log", "kill", container.ID, daemon.Repositories().ImageName(container.Image)).Run()
|
|
| 47 |
+ } else {
|
|
| 48 |
+ // Otherwise, just send the requested signal |
|
| 49 |
+ if err := container.KillSig(int(sig)); err != nil {
|
|
| 50 |
+ return job.Errorf("Cannot kill container %s: %s", name, err)
|
|
| 51 |
+ } |
|
| 52 |
+ // FIXME: Add event for signals |
|
| 53 |
+ } |
|
| 54 |
+ } else {
|
|
| 55 |
+ return job.Errorf("No such container: %s", name)
|
|
| 56 |
+ } |
|
| 57 |
+ return engine.StatusOK |
|
| 58 |
+} |
| ... | ... |
@@ -17,7 +17,6 @@ import ( |
| 17 | 17 |
"path/filepath" |
| 18 | 18 |
"strconv" |
| 19 | 19 |
"strings" |
| 20 |
- "syscall" |
|
| 21 | 20 |
"time" |
| 22 | 21 |
|
| 23 | 22 |
"github.com/docker/docker/daemon" |
| ... | ... |
@@ -25,61 +24,11 @@ import ( |
| 25 | 25 |
"github.com/docker/docker/graph" |
| 26 | 26 |
"github.com/docker/docker/pkg/graphdb" |
| 27 | 27 |
"github.com/docker/docker/pkg/parsers" |
| 28 |
- "github.com/docker/docker/pkg/signal" |
|
| 29 | 28 |
"github.com/docker/docker/pkg/tailfile" |
| 30 | 29 |
"github.com/docker/docker/runconfig" |
| 31 | 30 |
"github.com/docker/docker/utils" |
| 32 | 31 |
) |
| 33 | 32 |
|
| 34 |
-// ContainerKill send signal to the container |
|
| 35 |
-// If no signal is given (sig 0), then Kill with SIGKILL and wait |
|
| 36 |
-// for the container to exit. |
|
| 37 |
-// If a signal is given, then just send it to the container and return. |
|
| 38 |
-func (srv *Server) ContainerKill(job *engine.Job) engine.Status {
|
|
| 39 |
- if n := len(job.Args); n < 1 || n > 2 {
|
|
| 40 |
- return job.Errorf("Usage: %s CONTAINER [SIGNAL]", job.Name)
|
|
| 41 |
- } |
|
| 42 |
- var ( |
|
| 43 |
- name = job.Args[0] |
|
| 44 |
- sig uint64 |
|
| 45 |
- err error |
|
| 46 |
- ) |
|
| 47 |
- |
|
| 48 |
- // If we have a signal, look at it. Otherwise, do nothing |
|
| 49 |
- if len(job.Args) == 2 && job.Args[1] != "" {
|
|
| 50 |
- // Check if we passed the signal as a number: |
|
| 51 |
- // The largest legal signal is 31, so let's parse on 5 bits |
|
| 52 |
- sig, err = strconv.ParseUint(job.Args[1], 10, 5) |
|
| 53 |
- if err != nil {
|
|
| 54 |
- // The signal is not a number, treat it as a string (either like "KILL" or like "SIGKILL") |
|
| 55 |
- sig = uint64(signal.SignalMap[strings.TrimPrefix(job.Args[1], "SIG")]) |
|
| 56 |
- } |
|
| 57 |
- |
|
| 58 |
- if sig == 0 {
|
|
| 59 |
- return job.Errorf("Invalid signal: %s", job.Args[1])
|
|
| 60 |
- } |
|
| 61 |
- } |
|
| 62 |
- |
|
| 63 |
- if container := srv.daemon.Get(name); container != nil {
|
|
| 64 |
- // If no signal is passed, or SIGKILL, perform regular Kill (SIGKILL + wait()) |
|
| 65 |
- if sig == 0 || syscall.Signal(sig) == syscall.SIGKILL {
|
|
| 66 |
- if err := container.Kill(); err != nil {
|
|
| 67 |
- return job.Errorf("Cannot kill container %s: %s", name, err)
|
|
| 68 |
- } |
|
| 69 |
- srv.LogEvent("kill", container.ID, srv.daemon.Repositories().ImageName(container.Image))
|
|
| 70 |
- } else {
|
|
| 71 |
- // Otherwise, just send the requested signal |
|
| 72 |
- if err := container.KillSig(int(sig)); err != nil {
|
|
| 73 |
- return job.Errorf("Cannot kill container %s: %s", name, err)
|
|
| 74 |
- } |
|
| 75 |
- // FIXME: Add event for signals |
|
| 76 |
- } |
|
| 77 |
- } else {
|
|
| 78 |
- return job.Errorf("No such container: %s", name)
|
|
| 79 |
- } |
|
| 80 |
- return engine.StatusOK |
|
| 81 |
-} |
|
| 82 |
- |
|
| 83 | 33 |
func (srv *Server) ContainerExport(job *engine.Job) engine.Status {
|
| 84 | 34 |
if len(job.Args) != 1 {
|
| 85 | 35 |
return job.Errorf("Usage: %s container_id", job.Name)
|
| ... | ... |
@@ -91,7 +91,6 @@ func InitServer(job *engine.Job) engine.Status {
|
| 91 | 91 |
"stop": srv.ContainerStop, |
| 92 | 92 |
"restart": srv.ContainerRestart, |
| 93 | 93 |
"start": srv.ContainerStart, |
| 94 |
- "kill": srv.ContainerKill, |
|
| 95 | 94 |
"wait": srv.ContainerWait, |
| 96 | 95 |
"tag": srv.ImageTag, // FIXME merge with "image_tag" |
| 97 | 96 |
"resize": srv.ContainerResize, |