1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Engine: new command 'kill' sends a signal to a running container

This commit is contained in:
Solomon Hykes 2013-11-17 00:26:04 +00:00
parent 7fd64e0196
commit fdb3de7b11
2 changed files with 27 additions and 18 deletions

16
api.go
View file

@ -150,19 +150,11 @@ func postContainersKill(srv *Server, version float64, w http.ResponseWriter, r *
if err := parseForm(r); err != nil { if err := parseForm(r); err != nil {
return err return err
} }
name := vars["name"] job := srv.Eng.Job("kill", vars["name"])
if sig := r.Form.Get("signal"); sig != "" {
signal := 0 job.Args = append(job.Args, sig)
if r != nil {
if s := r.Form.Get("signal"); s != "" {
s, err := strconv.Atoi(s)
if err != nil {
return err
}
signal = s
}
} }
if err := srv.ContainerKill(name, signal); err != nil { if err := job.Run(); err != nil {
return err return err
} }
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)

View file

@ -23,6 +23,7 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"strconv"
"sync" "sync"
"syscall" "syscall"
"time" "time"
@ -78,6 +79,9 @@ func jobInitApi(job *engine.Job) engine.Status {
job.Error(err) job.Error(err)
return engine.StatusErr return engine.StatusErr
} }
if err := job.Eng.Register("kill", srv.ContainerKill); err != nil {
return err.Error()
}
if err := job.Eng.Register("serveapi", srv.ListenAndServe); err != nil { if err := job.Eng.Register("serveapi", srv.ListenAndServe); err != nil {
job.Error(err) job.Error(err)
return engine.StatusErr return engine.StatusErr
@ -169,25 +173,38 @@ func (srv *Server) versionInfos() []utils.VersionInfo {
// If no signal is given (sig 0), then Kill with SIGKILL and wait // If no signal is given (sig 0), then Kill with SIGKILL and wait
// for the container to exit. // for the container to exit.
// If a signal is given, then just send it to the container and return. // 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) string {
if n := len(job.Args); n < 1 || n > 2 {
return fmt.Sprintf("Usage: %s CONTAINER [SIGNAL]", job.Name)
}
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 {
return fmt.Sprintf("Invalid signal: %s", job.Args[1])
}
}
if container := srv.runtime.Get(name); container != nil { if container := srv.runtime.Get(name); container != nil {
// If no signal is passed, perform regular Kill (SIGKILL + wait()) // If no signal is passed, perform regular Kill (SIGKILL + wait())
if sig == 0 { if sig == 0 {
if err := container.Kill(); err != nil { if err := container.Kill(); err != nil {
return fmt.Errorf("Cannot kill container %s: %s", name, err) return fmt.Sprintf("Cannot kill container %s: %s", name, err)
} }
srv.LogEvent("kill", container.ID, srv.runtime.repositories.ImageName(container.Image)) srv.LogEvent("kill", container.ID, srv.runtime.repositories.ImageName(container.Image))
} else { } else {
// Otherwise, just send the requested signal // Otherwise, just send the requested signal
if err := container.kill(sig); err != nil { if err := container.kill(int(sig)); err != nil {
return fmt.Errorf("Cannot kill container %s: %s", name, err) return fmt.Sprintf("Cannot kill container %s: %s", name, err)
} }
// FIXME: Add event for signals // FIXME: Add event for signals
} }
} else { } else {
return fmt.Errorf("No such container: %s", name) return fmt.Sprintf("No such container: %s", name)
} }
return nil return "0"
} }
func (srv *Server) ContainerExport(name string, out io.Writer) error { func (srv *Server) ContainerExport(name string, out io.Writer) error {