Fix process wait with monitor

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2016-10-06 11:46:03 -07:00
parent 90e4f130c8
commit 4641ab4101
2 changed files with 23 additions and 3 deletions

View file

@ -200,9 +200,7 @@ func (p *process) FD() int {
} }
func (p *process) Wait() (rst uint32, rerr error) { func (p *process) Wait() (rst uint32, rerr error) {
if _, err := ioutil.ReadAll(p.exit); err != nil { <-p.done
return 255, err
}
data, err := ioutil.ReadFile(filepath.Join(p.root, "exitStatus")) data, err := ioutil.ReadFile(filepath.Join(p.root, "exitStatus"))
defer func() { defer func() {
if rerr != nil { if rerr != nil {

View file

@ -12,6 +12,8 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/Sirupsen/logrus"
"github.com/docker/containerd/monitor"
"github.com/docker/containerd/oci" "github.com/docker/containerd/oci"
"github.com/docker/containerkit" "github.com/docker/containerkit"
specs "github.com/opencontainers/runtime-spec/specs-go" specs "github.com/opencontainers/runtime-spec/specs-go"
@ -62,13 +64,19 @@ func New(opts Opts) (*Shim, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
m, err := monitor.New()
if err != nil {
return nil, err
}
s := &Shim{ s := &Shim{
root: opts.Root, root: opts.Root,
name: opts.Name, name: opts.Name,
timeout: opts.Timeout, timeout: opts.Timeout,
runtime: r, runtime: r,
processes: make(map[string]*process), processes: make(map[string]*process),
m: m,
} }
go s.startMonitor()
f, err := os.Create(filepath.Join(opts.Root, "state.json")) f, err := os.Create(filepath.Join(opts.Root, "state.json"))
if err != nil { if err != nil {
return nil, err return nil, err
@ -131,6 +139,7 @@ type Shim struct {
processes map[string]*process processes map[string]*process
bundle string bundle string
checkpoint string checkpoint string
m *monitor.Monitor
} }
type state struct { type state struct {
@ -198,6 +207,9 @@ func (s *Shim) Create(c *containerkit.Container) (containerkit.ProcessDelegate,
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := s.m.Add(p); err != nil {
return nil, err
}
s.pmu.Lock() s.pmu.Lock()
s.processes["init"] = p s.processes["init"] = p
s.pmu.Unlock() s.pmu.Unlock()
@ -302,6 +314,16 @@ func (s *Shim) command(args ...string) *exec.Cmd {
return exec.Command(s.name, args...) return exec.Command(s.name, args...)
} }
func (s *Shim) startMonitor() {
for m := range s.m.Events() {
p := m.(*process)
close(p.done)
if err := s.m.Remove(p); err != nil {
logrus.Error(err)
}
}
}
// checkShimNotFound checks the error returned from a exec call to see if the binary // checkShimNotFound checks the error returned from a exec call to see if the binary
// that was called exists on the system and returns true if the shim binary does not exist // that was called exists on the system and returns true if the shim binary does not exist
func checkShimNotFound(err error) bool { func checkShimNotFound(err error) bool {