2016-02-01 19:02:41 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-03-17 23:32:35 +00:00
|
|
|
"errors"
|
2016-02-25 20:59:34 +00:00
|
|
|
"fmt"
|
2016-02-01 19:02:41 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2016-03-11 22:26:07 +00:00
|
|
|
"sync"
|
2016-02-01 19:02:41 +00:00
|
|
|
"syscall"
|
2016-04-04 18:23:40 +00:00
|
|
|
"time"
|
2016-02-01 19:02:41 +00:00
|
|
|
|
2016-11-07 21:15:32 +00:00
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
2016-02-01 19:02:41 +00:00
|
|
|
)
|
|
|
|
|
2016-03-17 23:32:35 +00:00
|
|
|
var errRuntime = errors.New("shim: runtime execution error")
|
|
|
|
|
2016-04-04 18:23:40 +00:00
|
|
|
type checkpoint struct {
|
|
|
|
// Timestamp is the time that checkpoint happened
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
// Name is the name of the checkpoint
|
|
|
|
Name string `json:"name"`
|
2016-06-03 22:00:49 +00:00
|
|
|
// TCP checkpoints open tcp connections
|
|
|
|
TCP bool `json:"tcp"`
|
2016-04-04 18:23:40 +00:00
|
|
|
// UnixSockets persists unix sockets in the checkpoint
|
|
|
|
UnixSockets bool `json:"unixSockets"`
|
|
|
|
// Shell persists tty sessions in the checkpoint
|
|
|
|
Shell bool `json:"shell"`
|
|
|
|
// Exit exits the container after the checkpoint is finished
|
|
|
|
Exit bool `json:"exit"`
|
2016-06-07 20:25:54 +00:00
|
|
|
// EmptyNS tells CRIU not to restore a particular namespace
|
|
|
|
EmptyNS []string `json:"emptyNS,omitempty"`
|
2016-04-04 18:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type processState struct {
|
2016-11-07 21:15:32 +00:00
|
|
|
specs.Process
|
2016-05-26 00:42:37 +00:00
|
|
|
Exec bool `json:"exec"`
|
2016-10-06 18:39:12 +00:00
|
|
|
Stdin string `json:"stdin"`
|
|
|
|
Stdout string `json:"stdout"`
|
|
|
|
Stderr string `json:"stderr"`
|
2016-05-26 00:42:37 +00:00
|
|
|
RuntimeArgs []string `json:"runtimeArgs"`
|
|
|
|
NoPivotRoot bool `json:"noPivotRoot"`
|
|
|
|
CheckpointPath string `json:"checkpoint"`
|
|
|
|
RootUID int `json:"rootUID"`
|
|
|
|
RootGID int `json:"rootGID"`
|
2016-04-04 18:23:40 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 19:02:41 +00:00
|
|
|
type process struct {
|
2016-03-11 22:26:07 +00:00
|
|
|
sync.WaitGroup
|
2016-05-26 00:42:37 +00:00
|
|
|
id string
|
|
|
|
bundle string
|
|
|
|
stdio *stdio
|
|
|
|
exec bool
|
|
|
|
containerPid int
|
|
|
|
checkpoint *checkpoint
|
|
|
|
checkpointPath string
|
|
|
|
shimIO *IO
|
|
|
|
stdinCloser io.Closer
|
|
|
|
console *os.File
|
|
|
|
consolePath string
|
|
|
|
state *processState
|
|
|
|
runtime string
|
2016-02-01 19:02:41 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 20:59:34 +00:00
|
|
|
func newProcess(id, bundle, runtimeName string) (*process, error) {
|
2016-02-01 19:02:41 +00:00
|
|
|
p := &process{
|
2016-02-25 20:59:34 +00:00
|
|
|
id: id,
|
|
|
|
bundle: bundle,
|
|
|
|
runtime: runtimeName,
|
2016-02-01 19:02:41 +00:00
|
|
|
}
|
2016-02-01 23:07:02 +00:00
|
|
|
s, err := loadProcess()
|
|
|
|
if err != nil {
|
2016-10-06 18:39:12 +00:00
|
|
|
return nil, fmt.Errorf("load process from json %s", err)
|
2016-02-01 19:02:41 +00:00
|
|
|
}
|
2016-02-03 21:56:15 +00:00
|
|
|
p.state = s
|
2016-05-26 00:42:37 +00:00
|
|
|
if s.CheckpointPath != "" {
|
|
|
|
cpt, err := loadCheckpoint(s.CheckpointPath)
|
2016-02-01 23:07:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.checkpoint = cpt
|
2016-05-26 00:42:37 +00:00
|
|
|
p.checkpointPath = s.CheckpointPath
|
2016-02-01 23:07:02 +00:00
|
|
|
}
|
2016-02-01 19:02:41 +00:00
|
|
|
if err := p.openIO(); err != nil {
|
2016-10-06 18:39:12 +00:00
|
|
|
return nil, fmt.Errorf("open IO for container %s", err)
|
2016-02-01 19:02:41 +00:00
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2016-04-04 18:23:40 +00:00
|
|
|
func loadProcess() (*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-04-04 18:23:40 +00:00
|
|
|
var s processState
|
2016-02-01 23:07:02 +00:00
|
|
|
if err := json.NewDecoder(f).Decode(&s); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &s, nil
|
|
|
|
}
|
|
|
|
|
2016-05-26 00:42:37 +00:00
|
|
|
func loadCheckpoint(checkpointPath string) (*checkpoint, error) {
|
|
|
|
f, err := os.Open(filepath.Join(checkpointPath, "config.json"))
|
2016-02-01 23:07:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2016-04-04 18:23:40 +00:00
|
|
|
var cpt checkpoint
|
2016-02-01 23:07:02 +00:00
|
|
|
if err := json.NewDecoder(f).Decode(&cpt); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &cpt, nil
|
|
|
|
}
|
|
|
|
|
2016-06-09 20:33:26 +00:00
|
|
|
func (p *process) create() error {
|
2016-02-01 19:02:41 +00:00
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-11 18:11:42 +00:00
|
|
|
logPath := filepath.Join(cwd, "log.json")
|
2016-03-24 20:30:27 +00:00
|
|
|
args := append([]string{
|
2016-03-11 18:11:42 +00:00
|
|
|
"--log", logPath,
|
|
|
|
"--log-format", "json",
|
2016-03-24 20:30:27 +00:00
|
|
|
}, p.state.RuntimeArgs...)
|
2016-02-11 19:30:25 +00:00
|
|
|
if p.state.Exec {
|
2016-02-01 19:02:41 +00:00
|
|
|
args = append(args, "exec",
|
2016-06-09 20:33:26 +00:00
|
|
|
"-d",
|
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",
|
2016-09-07 16:26:37 +00:00
|
|
|
"-d",
|
2016-06-07 21:05:22 +00:00
|
|
|
"--image-path", p.checkpointPath,
|
|
|
|
"--work-path", filepath.Join(p.checkpointPath, "criu.work", "restore-"+time.Now().Format(time.RFC3339)),
|
2016-02-01 23:07:02 +00:00
|
|
|
)
|
|
|
|
add := func(flags ...string) {
|
|
|
|
args = append(args, flags...)
|
|
|
|
}
|
|
|
|
if p.checkpoint.Shell {
|
|
|
|
add("--shell-job")
|
|
|
|
}
|
2016-06-03 22:00:49 +00:00
|
|
|
if p.checkpoint.TCP {
|
2016-02-01 23:07:02 +00:00
|
|
|
add("--tcp-established")
|
|
|
|
}
|
|
|
|
if p.checkpoint.UnixSockets {
|
|
|
|
add("--ext-unix-sk")
|
|
|
|
}
|
2016-03-30 21:25:42 +00:00
|
|
|
if p.state.NoPivotRoot {
|
|
|
|
add("--no-pivot")
|
|
|
|
}
|
2016-06-07 20:25:54 +00:00
|
|
|
for _, ns := range p.checkpoint.EmptyNS {
|
|
|
|
add("--empty-ns", ns)
|
|
|
|
}
|
|
|
|
|
2016-02-01 19:02:41 +00:00
|
|
|
} else {
|
2016-06-09 20:33:26 +00:00
|
|
|
args = append(args, "create",
|
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-03-30 21:25:42 +00:00
|
|
|
if p.state.NoPivotRoot {
|
|
|
|
args = append(args, "--no-pivot")
|
|
|
|
}
|
2016-02-01 19:02:41 +00:00
|
|
|
}
|
|
|
|
args = append(args,
|
|
|
|
"--pid-file", filepath.Join(cwd, "pid"),
|
2016-02-11 00:48:04 +00:00
|
|
|
p.id,
|
2016-02-01 19:02:41 +00:00
|
|
|
)
|
2016-02-25 20:59:34 +00:00
|
|
|
cmd := exec.Command(p.runtime, 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
|
2016-05-19 17:12:50 +00:00
|
|
|
// Call out to setPDeathSig to set SysProcAttr as elements are platform specific
|
|
|
|
cmd.SysProcAttr = setPDeathSig()
|
|
|
|
|
2016-03-11 22:26:07 +00:00
|
|
|
if err := cmd.Start(); err != nil {
|
2016-03-17 21:37:26 +00:00
|
|
|
if exErr, ok := err.(*exec.Error); ok {
|
|
|
|
if exErr.Err == exec.ErrNotFound || exErr.Err == os.ErrNotExist {
|
2016-03-22 16:01:21 +00:00
|
|
|
return fmt.Errorf("%s not installed on system", p.runtime)
|
2016-03-17 21:37:26 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-11 22:26:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.stdio.stdout.Close()
|
|
|
|
p.stdio.stderr.Close()
|
|
|
|
if err := cmd.Wait(); err != nil {
|
2016-03-17 23:32:35 +00:00
|
|
|
if _, ok := err.(*exec.ExitError); ok {
|
|
|
|
return errRuntime
|
|
|
|
}
|
2016-02-01 19:02:41 +00:00
|
|
|
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 {
|
2016-02-11 19:30:25 +00:00
|
|
|
if !p.state.Exec {
|
2016-06-16 21:38:28 +00:00
|
|
|
cmd := exec.Command(p.runtime, append(p.state.RuntimeArgs, "delete", p.id)...)
|
|
|
|
cmd.SysProcAttr = setPDeathSig()
|
|
|
|
out, err := cmd.CombinedOutput()
|
2016-02-25 20:59:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %v", out, err)
|
|
|
|
}
|
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 (
|
2016-02-11 19:23:35 +00:00
|
|
|
uid = p.state.RootUID
|
|
|
|
gid = p.state.RootGID
|
2016-02-03 21:56:15 +00:00
|
|
|
)
|
2016-03-23 17:26:05 +00:00
|
|
|
go func() {
|
|
|
|
if stdinCloser, err := os.OpenFile(p.state.Stdin, syscall.O_WRONLY, 0); err == nil {
|
|
|
|
p.stdinCloser = stdinCloser
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-02-03 21:56:15 +00:00
|
|
|
if p.state.Terminal {
|
2016-04-04 18:23:40 +00:00
|
|
|
master, console, err := newConsole(uid, gid)
|
2016-02-01 19:02:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-04 18:23:40 +00:00
|
|
|
p.console = master
|
|
|
|
p.consolePath = console
|
2016-03-23 17:26:05 +00:00
|
|
|
stdin, err := os.OpenFile(p.state.Stdin, syscall.O_RDONLY, 0)
|
2016-02-01 19:02:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-04 18:23:40 +00:00
|
|
|
go io.Copy(master, 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
|
|
|
|
}
|
2016-03-11 22:26:07 +00:00
|
|
|
p.Add(1)
|
2016-02-01 19:02:41 +00:00
|
|
|
go func() {
|
2016-04-04 18:23:40 +00:00
|
|
|
io.Copy(stdout, master)
|
|
|
|
master.Close()
|
2016-03-11 22:26:07 +00:00
|
|
|
p.Done()
|
2016-02-01 19:02:41 +00:00
|
|
|
}()
|
|
|
|
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.Stdout: func(f *os.File) {
|
2016-03-11 22:26:07 +00:00
|
|
|
p.Add(1)
|
|
|
|
go func() {
|
|
|
|
io.Copy(f, i.Stdout)
|
|
|
|
p.Done()
|
|
|
|
}()
|
2016-02-01 23:07:02 +00:00
|
|
|
},
|
2016-02-03 21:56:15 +00:00
|
|
|
p.state.Stderr: func(f *os.File) {
|
2016-03-11 22:26:07 +00:00
|
|
|
p.Add(1)
|
|
|
|
go func() {
|
|
|
|
io.Copy(f, i.Stderr)
|
|
|
|
p.Done()
|
|
|
|
}()
|
2016-02-01 23:07:02 +00:00
|
|
|
},
|
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
|
|
|
}
|
2016-03-23 17:26:05 +00:00
|
|
|
|
|
|
|
f, err := os.OpenFile(p.state.Stdin, syscall.O_RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
io.Copy(i.Stdin, f)
|
|
|
|
i.Stdin.Close()
|
|
|
|
}()
|
|
|
|
|
2016-02-01 19:02:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
// IO holds all 3 standard io Reader/Writer (stdin,stdout,stderr)
|
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
|
2016-06-30 15:13:06 +00:00
|
|
|
// change ownership of the pipes in case we are in a user namespace
|
2016-02-01 23:07:02 +00:00
|
|
|
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
|
|
|
|
}
|