diff --git a/api.go b/api.go index 3d73dff50c..58fa310240 100644 --- a/api.go +++ b/api.go @@ -151,19 +151,11 @@ func postContainersKill(srv *Server, version float64, w http.ResponseWriter, r * if err := parseForm(r); err != nil { return err } - name := vars["name"] - - signal := 0 - if r != nil { - if s := r.Form.Get("signal"); s != "" { - s, err := strconv.Atoi(s) - if err != nil { - return err - } - signal = s - } + job := srv.Eng.Job("kill", vars["name"]) + if sig := r.Form.Get("signal"); sig != "" { + job.Args = append(job.Args, sig) } - if err := srv.ContainerKill(name, signal); err != nil { + if err := job.Run(); err != nil { return err } w.WriteHeader(http.StatusNoContent) diff --git a/integration/server_test.go b/integration/server_test.go index 2f108c2b78..c1499e26cc 100644 --- a/integration/server_test.go +++ b/integration/server_test.go @@ -199,7 +199,7 @@ func TestCreateStartRestartStopStartKillRm(t *testing.T) { t.Fatal(err) } - if err := srv.ContainerKill(id, 0); err != nil { + if err := eng.Job("kill", id).Run(); err != nil { t.Fatal(err) } diff --git a/integration/utils_test.go b/integration/utils_test.go index 62ec5a51fd..8ee1a0af08 100644 --- a/integration/utils_test.go +++ b/integration/utils_test.go @@ -105,7 +105,7 @@ func containerWaitTimeout(eng *engine.Engine, id string, t utils.Fataler) error } func containerKill(eng *engine.Engine, id string, t utils.Fataler) { - if err := getContainer(eng, id, t).Kill(); err != nil { + if err := eng.Job("kill", id).Run(); err != nil { t.Fatal(err) } } diff --git a/server.go b/server.go index 6ead9bc134..436bef28e3 100644 --- a/server.go +++ b/server.go @@ -22,6 +22,7 @@ import ( "path" "path/filepath" "runtime" + "strconv" "strings" "sync" "syscall" @@ -82,6 +83,10 @@ func jobInitApi(job *engine.Job) engine.Status { job.Error(err) return engine.StatusErr } + if err := job.Eng.Register("kill", srv.ContainerKill); err != nil { + job.Error(err) + return engine.StatusErr + } if err := job.Eng.Register("serveapi", srv.ListenAndServe); err != nil { job.Error(err) return engine.StatusErr @@ -148,25 +153,43 @@ func (v *simpleVersionInfo) Version() string { // If no signal is given (sig 0), then Kill with SIGKILL and wait // for the container to exit. // If a signal is given, then just send it to the container and return. -func (srv *Server) ContainerKill(name string, sig int) error { +func (srv *Server) ContainerKill(job *engine.Job) engine.Status { + if n := len(job.Args); n < 1 || n > 2 { + job.Errorf("Usage: %s CONTAINER [SIGNAL]", job.Name) + return engine.StatusErr + } + name := job.Args[0] + var sig uint64 + if len(job.Args) == 2 && job.Args[1] != "" { + var err error + // The largest legal signal is 31, so let's parse on 5 bits + sig, err = strconv.ParseUint(job.Args[1], 10, 5) + if err != nil { + job.Errorf("Invalid signal: %s", job.Args[1]) + return engine.StatusErr + } + } if container := srv.runtime.Get(name); container != nil { // If no signal is passed, perform regular Kill (SIGKILL + wait()) if sig == 0 { if err := container.Kill(); err != nil { - return fmt.Errorf("Cannot kill container %s: %s", name, err) + job.Errorf("Cannot kill container %s: %s", name, err) + return engine.StatusErr } srv.LogEvent("kill", container.ID, srv.runtime.repositories.ImageName(container.Image)) } else { // Otherwise, just send the requested signal - if err := container.kill(sig); err != nil { - return fmt.Errorf("Cannot kill container %s: %s", name, err) + if err := container.kill(int(sig)); err != nil { + job.Errorf("Cannot kill container %s: %s", name, err) + return engine.StatusErr } // FIXME: Add event for signals } } else { - return fmt.Errorf("No such container: %s", name) + job.Errorf("No such container: %s", name) + return engine.StatusErr } - return nil + return engine.StatusOK } func (srv *Server) ContainerExport(job *engine.Job) engine.Status {