Refactor process.go for platform specific

Signed-off-by: John Howard <jhoward@microsoft.com>

Move process sorter to new file

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>

Sort containers by id

This will not be the most accurate sorting but atleast the list will be
consistent inbetween calls.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>

Allow runtime to be configurable via daemon start

This allows people to pass an alternate name or location to the runtime
binary to start containers.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>

Fix state output for containers

Return the proper state/status for a container by checking if the pid is
still alive.  Also fix the cleanup handling in the shim to make sure
containers are not left behind.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>

Properly wait for container start

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
John Howard 2016-02-25 12:59:34 -08:00 committed by Michael Crosby
parent 0ad7654f80
commit b044ff0f29
19 changed files with 323 additions and 206 deletions

View file

@ -32,6 +32,10 @@ func getRootIDs(s *PlatformSpec) (int, int, error) {
return uid, gid, nil
}
func (c *container) Runtime() string {
return c.runtime
}
func (c *container) Pause() error {
return exec.Command("runc", "pause", c.id).Run()
}
@ -115,7 +119,7 @@ func (c *container) Start(checkpoint string, s Stdio) (Process, error) {
return nil, err
}
cmd := exec.Command("containerd-shim",
c.id, c.bundle,
c.id, c.bundle, c.runtime,
)
cmd.Dir = processRoot
cmd.SysProcAttr = &syscall.SysProcAttr{
@ -141,8 +145,8 @@ func (c *container) Start(checkpoint string, s Stdio) (Process, error) {
if err := cmd.Start(); err != nil {
return nil, err
}
if _, err := p.getPid(); err != nil {
return p, nil
if err := waitForStart(p, cmd); err != nil {
return nil, err
}
c.processes[InitProcessID] = p
return p, nil
@ -154,7 +158,7 @@ func (c *container) Exec(pid string, spec ProcessSpec, s Stdio) (Process, error)
return nil, err
}
cmd := exec.Command("containerd-shim",
c.id, c.bundle,
c.id, c.bundle, c.runtime,
)
cmd.Dir = processRoot
cmd.SysProcAttr = &syscall.SysProcAttr{
@ -175,8 +179,8 @@ func (c *container) Exec(pid string, spec ProcessSpec, s Stdio) (Process, error)
if err := cmd.Start(); err != nil {
return nil, err
}
if _, err := p.getPid(); err != nil {
return p, nil
if err := waitForStart(p, cmd); err != nil {
return nil, err
}
c.processes[pid] = p
return p, nil
@ -222,3 +226,34 @@ func (c *container) Stats() (*Stat, error) {
Data: stats,
}, nil
}
func waitForStart(p *process, cmd *exec.Cmd) error {
for i := 0; i < 50; i++ {
if _, err := p.getPidFromFile(); err != nil {
if os.IsNotExist(err) {
alive, err := isAlive(cmd)
if err != nil {
return err
}
if !alive {
return ErrContainerNotStarted
}
time.Sleep(100 * time.Millisecond)
continue
}
return err
}
return nil
}
return errNoPidFile
}
func isAlive(cmd *exec.Cmd) (bool, error) {
if err := syscall.Kill(cmd.Process.Pid, 0); err != nil {
if err == syscall.ESRCH {
return false, nil
}
return false, err
}
return true, nil
}