Add Get of task and process state

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby
2017-07-31 15:56:56 -04:00
parent 2974e485f3
commit 504033e373
13 changed files with 358 additions and 270 deletions

View File

@@ -27,6 +27,7 @@ import (
type execProcess struct {
sync.WaitGroup
mu sync.Mutex
id string
console console.Console
io runc.IO
@@ -73,6 +74,8 @@ func (e *execProcess) ID() string {
}
func (e *execProcess) Pid() int {
e.mu.Lock()
defer e.mu.Unlock()
return e.pid
}
@@ -109,8 +112,13 @@ func (e *execProcess) Resize(ws console.WinSize) error {
}
func (e *execProcess) Kill(ctx context.Context, sig uint32, _ bool) error {
if err := unix.Kill(e.pid, syscall.Signal(sig)); err != nil {
return errors.Wrapf(checkKillError(err), "exec kill error")
e.mu.Lock()
pid := e.pid
e.mu.Unlock()
if pid != 0 {
if err := unix.Kill(pid, syscall.Signal(sig)); err != nil {
return errors.Wrapf(checkKillError(err), "exec kill error")
}
}
return nil
}
@@ -179,6 +187,33 @@ func (e *execProcess) Start(ctx context.Context) (err error) {
if err != nil {
return errors.Wrap(err, "failed to retrieve OCI runtime exec pid")
}
e.mu.Lock()
e.pid = pid
e.mu.Unlock()
return nil
}
func (e *execProcess) Status(ctx context.Context) (string, error) {
s, err := e.parent.Status(ctx)
if err != nil {
return "", err
}
// if the container as a whole is in the pausing/paused state, so are all
// other processes inside the container, use container state here
switch s {
case "paused", "pausing":
return s, nil
}
e.mu.Lock()
defer e.mu.Unlock()
// if we don't have a pid then the exec process has just been created
if e.pid == 0 {
return "created", nil
}
// if we have a pid and it can be signaled, the process is running
if err := unix.Kill(e.pid, 0); err == nil {
return "running", nil
}
// else if we have a pid but it can nolonger be signaled, it has stopped
return "stopped", nil
}

View File

@@ -209,8 +209,8 @@ func (p *initProcess) ExitedAt() time.Time {
return p.exited
}
// ContainerStatus return the state of the container (created, running, paused, stopped)
func (p *initProcess) ContainerStatus(ctx context.Context) (string, error) {
// Status return the state of the container (created, running, paused, stopped)
func (p *initProcess) Status(ctx context.Context) (string, error) {
c, err := p.runtime.State(ctx, p.id)
if err != nil {
return "", p.runtimeError(err, "OCI runtime state failed")
@@ -233,7 +233,7 @@ func (p *initProcess) SetExited(status int) {
}
func (p *initProcess) Delete(context context.Context) error {
status, err := p.ContainerStatus(context)
status, err := p.Status(context)
if err != nil {
return err
}

View File

@@ -40,4 +40,6 @@ type process interface {
Stdio() stdio
// Start execution of the process
Start(context.Context) error
// Status returns the process status
Status(ctx context.Context) (string, error)
}

View File

@@ -229,14 +229,11 @@ func (s *Service) ResizePty(ctx context.Context, r *shimapi.ResizePtyRequest) (*
}
func (s *Service) State(ctx context.Context, r *shimapi.StateRequest) (*shimapi.StateResponse, error) {
if s.initProcess == nil {
return nil, errdefs.ToGRPCf(errdefs.ErrFailedPrecondition, "container must be created")
}
p, ok := s.processes[r.ID]
if !ok {
return nil, errdefs.ToGRPCf(errdefs.ErrNotFound, "process id %s not found", r.ID)
}
st, err := s.initProcess.ContainerStatus(ctx)
st, err := p.Status(ctx)
if err != nil {
return nil, err
}

View File

@@ -66,7 +66,6 @@ func (t *Task) State(ctx context.Context) (runtime.State, error) {
status = runtime.StoppedStatus
case task.StatusPaused:
status = runtime.PausedStatus
// TODO: containerd.DeletedStatus
}
return runtime.State{
Pid: response.Pid,