2016-01-06 21:32:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-02-01 19:02:41 +00:00
|
|
|
"flag"
|
2016-01-06 21:32:46 +00:00
|
|
|
"fmt"
|
2016-06-09 20:33:26 +00:00
|
|
|
"io/ioutil"
|
2016-01-06 21:32:46 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2016-03-17 21:37:26 +00:00
|
|
|
"path/filepath"
|
2016-01-06 21:32:46 +00:00
|
|
|
"syscall"
|
|
|
|
|
2016-02-16 23:10:18 +00:00
|
|
|
"github.com/docker/containerd/osutils"
|
2016-02-02 22:21:25 +00:00
|
|
|
"github.com/docker/docker/pkg/term"
|
2016-01-06 21:32:46 +00:00
|
|
|
)
|
|
|
|
|
2016-04-04 18:23:40 +00:00
|
|
|
func writeMessage(f *os.File, level string, err error) {
|
|
|
|
fmt.Fprintf(f, `{"level": "%s","msg": "%s"}`, level, err)
|
|
|
|
}
|
|
|
|
|
2016-06-17 18:03:52 +00:00
|
|
|
type controlMessage struct {
|
|
|
|
Type int
|
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
}
|
|
|
|
|
2016-02-25 20:59:34 +00:00
|
|
|
// containerd-shim is a small shim that sits in front of a runtime implementation
|
2016-01-06 21:32:46 +00:00
|
|
|
// that allows it to be repartented to init and handle reattach from the caller.
|
|
|
|
//
|
|
|
|
// the cwd of the shim should be the bundle for the container. Arg1 should be the path
|
|
|
|
// to the state directory where the shim can locate fifos and other information.
|
|
|
|
func main() {
|
2016-02-11 19:30:25 +00:00
|
|
|
flag.Parse()
|
2016-03-17 21:37:26 +00:00
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile(filepath.Join(cwd, "shim-log.json"), os.O_CREATE|os.O_WRONLY|os.O_APPEND|os.O_SYNC, 0666)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-04-04 18:23:40 +00:00
|
|
|
if err := start(f); err != nil {
|
2016-03-17 23:32:35 +00:00
|
|
|
// this means that the runtime failed starting the container and will have the
|
|
|
|
// proper error messages in the runtime log so we should to treat this as a
|
|
|
|
// shim failure because the sim executed properly
|
|
|
|
if err == errRuntime {
|
|
|
|
f.Close()
|
|
|
|
return
|
|
|
|
}
|
2016-03-17 21:37:26 +00:00
|
|
|
// log the error instead of writing to stderr because the shim will have
|
|
|
|
// /dev/null as it's stdio because it is supposed to be reparented to system
|
|
|
|
// init and will not have anyone to read from it
|
2016-04-04 18:23:40 +00:00
|
|
|
writeMessage(f, "error", err)
|
2016-03-17 21:37:26 +00:00
|
|
|
f.Close()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 18:23:40 +00:00
|
|
|
func start(log *os.File) error {
|
2016-01-06 21:32:46 +00:00
|
|
|
// start handling signals as soon as possible so that things are properly reaped
|
2016-02-25 20:59:34 +00:00
|
|
|
// or if runtime exits before we hit the handler
|
2016-02-01 19:02:41 +00:00
|
|
|
signals := make(chan os.Signal, 2048)
|
2016-01-06 21:32:46 +00:00
|
|
|
signal.Notify(signals)
|
|
|
|
// set the shim as the subreaper for all orphaned processes created by the container
|
2016-02-16 23:10:18 +00:00
|
|
|
if err := osutils.SetSubreaper(1); err != nil {
|
2016-03-17 21:37:26 +00:00
|
|
|
return err
|
2016-01-06 21:32:46 +00:00
|
|
|
}
|
|
|
|
// open the exit pipe
|
2016-02-01 19:02:41 +00:00
|
|
|
f, err := os.OpenFile("exit", syscall.O_WRONLY, 0)
|
2016-01-06 21:32:46 +00:00
|
|
|
if err != nil {
|
2016-03-17 21:37:26 +00:00
|
|
|
return err
|
2016-01-06 21:32:46 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
2016-02-02 22:21:25 +00:00
|
|
|
control, err := os.OpenFile("control", syscall.O_RDWR, 0)
|
|
|
|
if err != nil {
|
2016-03-17 21:37:26 +00:00
|
|
|
return err
|
2016-02-02 22:21:25 +00:00
|
|
|
}
|
|
|
|
defer control.Close()
|
2016-02-25 20:59:34 +00:00
|
|
|
p, err := newProcess(flag.Arg(0), flag.Arg(1), flag.Arg(2))
|
2016-01-06 21:32:46 +00:00
|
|
|
if err != nil {
|
2016-03-17 21:37:26 +00:00
|
|
|
return err
|
2016-01-06 21:32:46 +00:00
|
|
|
}
|
2016-02-25 20:59:34 +00:00
|
|
|
defer func() {
|
|
|
|
if err := p.Close(); err != nil {
|
2016-04-04 18:23:40 +00:00
|
|
|
writeMessage(log, "warn", err)
|
2016-02-25 20:59:34 +00:00
|
|
|
}
|
|
|
|
}()
|
2016-06-09 20:33:26 +00:00
|
|
|
if err := p.create(); err != nil {
|
2016-03-14 23:31:14 +00:00
|
|
|
p.delete()
|
2016-03-17 21:37:26 +00:00
|
|
|
return err
|
2016-01-06 21:32:46 +00:00
|
|
|
}
|
2016-06-17 18:03:52 +00:00
|
|
|
msgC := make(chan controlMessage, 32)
|
2016-02-02 22:21:25 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
2016-06-17 18:03:52 +00:00
|
|
|
var m controlMessage
|
|
|
|
if _, err := fmt.Fscanf(control, "%d %d %d\n", &m.Type, &m.Width, &m.Height); err != nil {
|
2016-04-04 18:23:40 +00:00
|
|
|
continue
|
2016-02-02 22:21:25 +00:00
|
|
|
}
|
2016-06-17 18:03:52 +00:00
|
|
|
msgC <- m
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
var exitShim bool
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case s := <-signals:
|
|
|
|
switch s {
|
|
|
|
case syscall.SIGCHLD:
|
|
|
|
exits, _ := osutils.Reap()
|
|
|
|
for _, e := range exits {
|
|
|
|
// check to see if runtime is one of the processes that has exited
|
|
|
|
if e.Pid == p.pid() {
|
|
|
|
exitShim = true
|
|
|
|
writeInt("exitStatus", e.Status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// runtime has exited so the shim can also exit
|
|
|
|
if exitShim {
|
|
|
|
ioutil.WriteFile(fmt.Sprintf("/tmp/shim-delete-%d", p.pid()), []byte("deleting"), 0600)
|
|
|
|
p.delete()
|
|
|
|
p.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case msg := <-msgC:
|
|
|
|
switch msg.Type {
|
2016-02-02 22:21:25 +00:00
|
|
|
case 0:
|
|
|
|
// close stdin
|
2016-03-23 17:26:05 +00:00
|
|
|
if p.stdinCloser != nil {
|
|
|
|
p.stdinCloser.Close()
|
|
|
|
}
|
2016-02-02 22:21:25 +00:00
|
|
|
case 1:
|
|
|
|
if p.console == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ws := term.Winsize{
|
2016-06-17 18:03:52 +00:00
|
|
|
Width: uint16(msg.Width),
|
|
|
|
Height: uint16(msg.Height),
|
2016-02-02 22:21:25 +00:00
|
|
|
}
|
|
|
|
term.SetWinsize(p.console.Fd(), &ws)
|
2016-06-09 20:33:26 +00:00
|
|
|
case 2:
|
|
|
|
// tell runtime to execute the init process
|
|
|
|
if err := p.start(); err != nil {
|
2016-06-17 18:03:52 +00:00
|
|
|
p.delete()
|
|
|
|
p.Wait()
|
|
|
|
return err
|
2016-06-09 20:33:26 +00:00
|
|
|
}
|
2016-02-02 22:21:25 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-06 21:32:46 +00:00
|
|
|
}
|
2016-03-17 21:37:26 +00:00
|
|
|
return nil
|
2016-01-06 21:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeInt(path string, i int) error {
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = fmt.Fprintf(f, "%d", i)
|
|
|
|
return err
|
|
|
|
}
|