execution/oci: Add check when loading processes

This should ensure that we don't kill a different process after a
restore (once supported)

Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
This commit is contained in:
Kenfe-Mickael Laventure 2016-12-15 08:21:04 -08:00
parent abaa421141
commit 73cb78fae3
2 changed files with 42 additions and 23 deletions

View file

@ -2,13 +2,21 @@ package oci
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"syscall"
"github.com/crosbymichael/go-runc"
"github.com/docker/containerd/execution"
starttime "github.com/opencontainers/runc/libcontainer/system"
)
func newProcess(c *execution.Container, id string, pid int) (execution.Process, error) {
func newProcess(c *execution.Container, id, stateDir string) (execution.Process, error) {
pid, err := runc.ReadPidFile(filepath.Join(stateDir, PidFilename))
if err != nil {
return nil, err
}
status := execution.Running
if err := syscall.Kill(pid, 0); err != nil {
if err == syscall.ESRCH {
@ -17,6 +25,28 @@ func newProcess(c *execution.Container, id string, pid int) (execution.Process,
return nil, err
}
}
if status == execution.Running {
stime, err := starttime.GetProcessStartTime(pid)
switch {
case os.IsNotExist(err):
status = execution.Stopped
case err != nil:
return nil, err
default:
b, err := ioutil.ReadFile(filepath.Join(stateDir, StartTimeFilename))
switch {
case os.IsNotExist(err):
err = ioutil.WriteFile(filepath.Join(stateDir, StartTimeFilename), []byte(stime), 0600)
if err != nil {
return nil, err
}
case err != nil:
return nil, err
case string(b) != stime:
status = execution.Stopped
}
}
}
return &process{
c: c,
id: id,