Return the start error and reap

Make sure that if we get an error from start it is returned and that we
reap the container's process from create before existing if we were to
send a kill signal.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2016-06-17 11:03:52 -07:00
parent 0227e9fb94
commit f44458d6b5

View file

@ -17,6 +17,12 @@ func writeMessage(f *os.File, level string, err error) {
fmt.Fprintf(f, `{"level": "%s","msg": "%s"}`, level, err) fmt.Fprintf(f, `{"level": "%s","msg": "%s"}`, level, err)
} }
type controlMessage struct {
Type int
Width int
Height int
}
// containerd-shim is a small shim that sits in front of a runtime implementation // containerd-shim is a small shim that sits in front of a runtime implementation
// that allows it to be repartented to init and handle reattach from the caller. // that allows it to be repartented to init and handle reattach from the caller.
// //
@ -82,37 +88,20 @@ func start(log *os.File) error {
p.delete() p.delete()
return err return err
} }
msgC := make(chan controlMessage, 32)
go func() { go func() {
for { for {
var msg, w, h int var m controlMessage
if _, err := fmt.Fscanf(control, "%d %d %d\n", &msg, &w, &h); err != nil { if _, err := fmt.Fscanf(control, "%d %d %d\n", &m.Type, &m.Width, &m.Height); err != nil {
continue continue
} }
switch msg { msgC <- m
case 0:
// close stdin
if p.stdinCloser != nil {
p.stdinCloser.Close()
}
case 1:
if p.console == nil {
continue
}
ws := term.Winsize{
Width: uint16(w),
Height: uint16(h),
}
term.SetWinsize(p.console.Fd(), &ws)
case 2:
// tell runtime to execute the init process
if err := p.start(); err != nil {
syscall.Kill(p.pid(), syscall.SIGKILL)
}
}
} }
}() }()
var exitShim bool var exitShim bool
for s := range signals { for {
select {
case s := <-signals:
switch s { switch s {
case syscall.SIGCHLD: case syscall.SIGCHLD:
exits, _ := osutils.Reap() exits, _ := osutils.Reap()
@ -131,6 +120,31 @@ func start(log *os.File) error {
p.Wait() p.Wait()
return nil return nil
} }
case msg := <-msgC:
switch msg.Type {
case 0:
// close stdin
if p.stdinCloser != nil {
p.stdinCloser.Close()
}
case 1:
if p.console == nil {
continue
}
ws := term.Winsize{
Width: uint16(msg.Width),
Height: uint16(msg.Height),
}
term.SetWinsize(p.console.Fd(), &ws)
case 2:
// tell runtime to execute the init process
if err := p.start(); err != nil {
p.delete()
p.Wait()
return err
}
}
}
} }
return nil return nil
} }