2016-02-01 19:02:41 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"syscall"
|
|
|
|
|
2016-02-01 23:07:02 +00:00
|
|
|
"github.com/docker/containerd/runtime"
|
2016-02-01 19:02:41 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type process struct {
|
|
|
|
id string
|
|
|
|
bundle string
|
|
|
|
stdio *stdio
|
|
|
|
exec bool
|
|
|
|
containerPid int
|
2016-02-01 23:07:02 +00:00
|
|
|
checkpoint *runtime.Checkpoint
|
2016-02-02 22:21:25 +00:00
|
|
|
shimIO *IO
|
|
|
|
console libcontainer.Console
|
|
|
|
consolePath string
|
2016-02-03 21:56:15 +00:00
|
|
|
state *runtime.ProcessState
|
2016-02-01 19:02:41 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 23:07:02 +00:00
|
|
|
func newProcess(id, bundle string, exec bool, checkpoint string) (*process, error) {
|
2016-02-01 19:02:41 +00:00
|
|
|
p := &process{
|
|
|
|
id: id,
|
|
|
|
bundle: bundle,
|
|
|
|
exec: exec,
|
|
|
|
}
|
2016-02-01 23:07:02 +00:00
|
|
|
s, err := loadProcess()
|
|
|
|
if err != nil {
|
2016-02-01 19:02:41 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-03 21:56:15 +00:00
|
|
|
p.state = s
|
2016-02-01 23:07:02 +00:00
|
|
|
if checkpoint != "" {
|
|
|
|
cpt, err := loadCheckpoint(bundle, checkpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.checkpoint = cpt
|
|
|
|
}
|
2016-02-01 19:02:41 +00:00
|
|
|
if err := p.openIO(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2016-02-03 21:56:15 +00:00
|
|
|
func loadProcess() (*runtime.ProcessState, error) {
|
2016-02-01 23:07:02 +00:00
|
|
|
f, err := os.Open("process.json")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2016-02-03 21:56:15 +00:00
|
|
|
var s runtime.ProcessState
|
2016-02-01 23:07:02 +00:00
|
|
|
if err := json.NewDecoder(f).Decode(&s); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadCheckpoint(bundle, name string) (*runtime.Checkpoint, error) {
|
|
|
|
f, err := os.Open(filepath.Join(bundle, "checkpoints", name, "config.json"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
var cpt runtime.Checkpoint
|
|
|
|
if err := json.NewDecoder(f).Decode(&cpt); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &cpt, nil
|
|
|
|
}
|
|
|
|
|
2016-02-01 19:02:41 +00:00
|
|
|
func (p *process) start() error {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-11 00:48:04 +00:00
|
|
|
args := []string{}
|
2016-02-01 19:02:41 +00:00
|
|
|
if p.exec {
|
|
|
|
args = append(args, "exec",
|
2016-02-01 23:07:02 +00:00
|
|
|
"--process", filepath.Join(cwd, "process.json"),
|
2016-02-02 22:21:25 +00:00
|
|
|
"--console", p.consolePath,
|
2016-02-01 23:07:02 +00:00
|
|
|
)
|
|
|
|
} else if p.checkpoint != nil {
|
|
|
|
args = append(args, "restore",
|
|
|
|
"--image-path", filepath.Join(p.bundle, "checkpoints", p.checkpoint.Name),
|
|
|
|
)
|
|
|
|
add := func(flags ...string) {
|
|
|
|
args = append(args, flags...)
|
|
|
|
}
|
|
|
|
if p.checkpoint.Shell {
|
|
|
|
add("--shell-job")
|
|
|
|
}
|
|
|
|
if p.checkpoint.Tcp {
|
|
|
|
add("--tcp-established")
|
|
|
|
}
|
|
|
|
if p.checkpoint.UnixSockets {
|
|
|
|
add("--ext-unix-sk")
|
|
|
|
}
|
2016-02-01 19:02:41 +00:00
|
|
|
} else {
|
|
|
|
args = append(args, "start",
|
2016-02-01 23:07:02 +00:00
|
|
|
"--bundle", p.bundle,
|
2016-02-02 22:21:25 +00:00
|
|
|
"--console", p.consolePath,
|
2016-02-01 23:07:02 +00:00
|
|
|
)
|
2016-02-01 19:02:41 +00:00
|
|
|
}
|
|
|
|
args = append(args,
|
|
|
|
"-d",
|
|
|
|
"--pid-file", filepath.Join(cwd, "pid"),
|
2016-02-11 00:48:04 +00:00
|
|
|
p.id,
|
2016-02-01 19:02:41 +00:00
|
|
|
)
|
|
|
|
cmd := exec.Command("runc", args...)
|
2016-02-01 23:07:02 +00:00
|
|
|
cmd.Dir = p.bundle
|
2016-02-01 19:02:41 +00:00
|
|
|
cmd.Stdin = p.stdio.stdin
|
|
|
|
cmd.Stdout = p.stdio.stdout
|
|
|
|
cmd.Stderr = p.stdio.stderr
|
|
|
|
// set the parent death signal to SIGKILL so that if the shim dies the container
|
|
|
|
// process also dies
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
|
|
Pdeathsig: syscall.SIGKILL,
|
|
|
|
}
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
data, err := ioutil.ReadFile("pid")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pid, err := strconv.Atoi(string(data))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.containerPid = pid
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *process) pid() int {
|
|
|
|
return p.containerPid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *process) delete() error {
|
|
|
|
if !p.exec {
|
2016-02-11 00:48:04 +00:00
|
|
|
return exec.Command("runc", "delete", p.id).Run()
|
2016-02-01 19:02:41 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// openIO opens the pre-created fifo's for use with the container
|
|
|
|
// in RDWR so that they remain open if the other side stops listening
|
|
|
|
func (p *process) openIO() error {
|
|
|
|
p.stdio = &stdio{}
|
2016-02-03 21:56:15 +00:00
|
|
|
var (
|
|
|
|
uid = int(p.state.User.UID)
|
|
|
|
gid = int(p.state.User.GID)
|
|
|
|
)
|
|
|
|
if p.state.Terminal {
|
2016-02-01 19:02:41 +00:00
|
|
|
// FIXME: this is wrong for user namespaces and will need to be translated
|
2016-02-03 21:56:15 +00:00
|
|
|
console, err := libcontainer.NewConsole(uid, gid)
|
2016-02-01 19:02:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-02 22:21:25 +00:00
|
|
|
p.console = console
|
|
|
|
p.consolePath = console.Path()
|
2016-02-03 21:56:15 +00:00
|
|
|
stdin, err := os.OpenFile(p.state.Stdin, syscall.O_RDWR, 0)
|
2016-02-01 19:02:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-01 23:07:02 +00:00
|
|
|
go io.Copy(console, stdin)
|
2016-02-03 21:56:15 +00:00
|
|
|
stdout, err := os.OpenFile(p.state.Stdout, syscall.O_RDWR, 0)
|
2016-02-01 19:02:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
io.Copy(stdout, console)
|
|
|
|
console.Close()
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-03 21:56:15 +00:00
|
|
|
i, err := p.initializeIO(uid)
|
2016-02-01 23:07:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-02 22:21:25 +00:00
|
|
|
p.shimIO = i
|
2016-02-01 19:02:41 +00:00
|
|
|
// non-tty
|
2016-02-01 23:07:02 +00:00
|
|
|
for name, dest := range map[string]func(f *os.File){
|
2016-02-03 21:56:15 +00:00
|
|
|
p.state.Stdin: func(f *os.File) {
|
2016-02-01 23:07:02 +00:00
|
|
|
go io.Copy(i.Stdin, f)
|
|
|
|
},
|
2016-02-03 21:56:15 +00:00
|
|
|
p.state.Stdout: func(f *os.File) {
|
2016-02-01 23:07:02 +00:00
|
|
|
go io.Copy(f, i.Stdout)
|
|
|
|
},
|
2016-02-03 21:56:15 +00:00
|
|
|
p.state.Stderr: func(f *os.File) {
|
2016-02-01 23:07:02 +00:00
|
|
|
go io.Copy(f, i.Stderr)
|
|
|
|
},
|
2016-02-01 19:02:41 +00:00
|
|
|
} {
|
|
|
|
f, err := os.OpenFile(name, syscall.O_RDWR, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-01 23:07:02 +00:00
|
|
|
dest(f)
|
2016-02-01 19:02:41 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-01 23:07:02 +00:00
|
|
|
type IO struct {
|
|
|
|
Stdin io.WriteCloser
|
|
|
|
Stdout io.ReadCloser
|
|
|
|
Stderr io.ReadCloser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *process) initializeIO(rootuid int) (i *IO, err error) {
|
|
|
|
var fds []uintptr
|
|
|
|
i = &IO{}
|
|
|
|
// cleanup in case of an error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
for _, fd := range fds {
|
|
|
|
syscall.Close(int(fd))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
// STDIN
|
|
|
|
r, w, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fds = append(fds, r.Fd(), w.Fd())
|
|
|
|
p.stdio.stdin, i.Stdin = r, w
|
|
|
|
// STDOUT
|
|
|
|
if r, w, err = os.Pipe(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fds = append(fds, r.Fd(), w.Fd())
|
|
|
|
p.stdio.stdout, i.Stdout = w, r
|
|
|
|
// STDERR
|
|
|
|
if r, w, err = os.Pipe(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fds = append(fds, r.Fd(), w.Fd())
|
|
|
|
p.stdio.stderr, i.Stderr = w, r
|
|
|
|
// change ownership of the pipes incase we are in a user namespace
|
|
|
|
for _, fd := range fds {
|
|
|
|
if err := syscall.Fchown(int(fd), rootuid, rootuid); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i, nil
|
|
|
|
}
|
2016-02-01 19:02:41 +00:00
|
|
|
func (p *process) Close() error {
|
|
|
|
return p.stdio.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
type stdio struct {
|
2016-02-02 22:21:25 +00:00
|
|
|
stdin *os.File
|
|
|
|
stdout *os.File
|
|
|
|
stderr *os.File
|
2016-02-01 19:02:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stdio) Close() error {
|
|
|
|
err := s.stdin.Close()
|
|
|
|
if oerr := s.stdout.Close(); err == nil {
|
|
|
|
err = oerr
|
|
|
|
}
|
|
|
|
if oerr := s.stderr.Close(); err == nil {
|
|
|
|
err = oerr
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|