From ca7c5040685a441b011bbb1d3513b241d34be012 Mon Sep 17 00:00:00 2001 From: HuKeping Date: Fri, 22 Apr 2016 17:42:08 -0400 Subject: [PATCH] Bugfix: ctr container list can not get the proper status of container Prior to this patch, when list containers by "ctr containers" or "ctr containers xxx", it will not get the proper status of conatinser(s). That was caused by the wrong implementation of State() for structure process, it only send a signal "0" to ping the "init" process and do nothing. Since the OCI/runc has implemented an interface Status(), we can use that. And I think this is more compatible with the design for containerd: - containerd -> runtime -> fun() Signed-off-by: Hu Keping --- api/grpc/server/server.go | 6 +++++- runtime/container_linux.go | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/api/grpc/server/server.go b/api/grpc/server/server.go index 1b8736ecc..bc696e7b4 100644 --- a/api/grpc/server/server.go +++ b/api/grpc/server/server.go @@ -146,7 +146,11 @@ func createAPIContainer(c runtime.Container, getPids bool) (*types.Container, er procs = append(procs, appendToProcs) } var pids []int - state := c.State() + state, err := c.Status() + if err != nil { + return nil, grpc.Errorf(codes.Internal, "get status for container: "+err.Error()) + } + if getPids && (state == runtime.Running || state == runtime.Paused) { if pids, err = c.Pids(); err != nil { return nil, grpc.Errorf(codes.Internal, "get all pids for container: "+err.Error()) diff --git a/runtime/container_linux.go b/runtime/container_linux.go index f573473bb..2c6bc7fb1 100644 --- a/runtime/container_linux.go +++ b/runtime/container_linux.go @@ -286,7 +286,22 @@ func (c *container) Stats() (*Stat, error) { // Status implements the runtime Container interface. func (c *container) Status() (State, error) { - return "running", nil + args := c.runtimeArgs + args = append(args, "state", c.id) + + out, err := exec.Command(c.runtime, args...).CombinedOutput() + if err != nil { + return "", fmt.Errorf(string(out)) + } + + // We only require the runtime json output to have a top level Status field. + var s struct { + Status State `json:"status"` + } + if err := json.Unmarshal(out, &s); err != nil { + return "", err + } + return s.Status, nil } func (c *container) OOM() (OOM, error) {