2016-01-06 21:32:46 +00:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
2016-02-01 19:02:41 +00:00
|
|
|
"encoding/json"
|
2016-02-02 22:21:25 +00:00
|
|
|
"fmt"
|
2016-02-01 19:02:41 +00:00
|
|
|
"io"
|
2016-01-06 21:32:46 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-04-27 19:00:29 +00:00
|
|
|
"os/exec"
|
2016-01-06 21:32:46 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2016-09-07 22:20:30 +00:00
|
|
|
"strings"
|
2016-07-13 18:01:07 +00:00
|
|
|
"sync"
|
2016-03-14 20:57:28 +00:00
|
|
|
"syscall"
|
2016-09-07 22:20:30 +00:00
|
|
|
"time"
|
2016-02-29 18:48:39 +00:00
|
|
|
|
2016-09-01 18:29:24 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-02-29 18:48:39 +00:00
|
|
|
"github.com/docker/containerd/specs"
|
2016-05-19 17:12:50 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2016-01-06 21:32:46 +00:00
|
|
|
)
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
// Process holds the operation allowed on a container's process
|
2016-02-01 19:02:41 +00:00
|
|
|
type Process interface {
|
|
|
|
io.Closer
|
|
|
|
|
|
|
|
// ID of the process.
|
|
|
|
// This is either "init" when it is the container's init process or
|
|
|
|
// it is a user provided id for the process similar to the container id
|
|
|
|
ID() string
|
2016-06-09 20:33:26 +00:00
|
|
|
// Start unblocks the associated container init process.
|
|
|
|
// This should only be called on the process with ID "init"
|
|
|
|
Start() error
|
2016-02-02 22:21:25 +00:00
|
|
|
CloseStdin() error
|
|
|
|
Resize(int, int) error
|
2016-02-01 19:02:41 +00:00
|
|
|
// ExitFD returns the fd the provides an event when the process exits
|
|
|
|
ExitFD() int
|
|
|
|
// ExitStatus returns the exit status of the process or an error if it
|
|
|
|
// has not exited
|
|
|
|
ExitStatus() (int, error)
|
2016-02-03 21:56:15 +00:00
|
|
|
// Spec returns the process spec that created the process
|
2016-02-29 18:48:39 +00:00
|
|
|
Spec() specs.ProcessSpec
|
2016-02-01 19:02:41 +00:00
|
|
|
// Signal sends the provided signal to the process
|
|
|
|
Signal(os.Signal) error
|
|
|
|
// Container returns the container that the process belongs to
|
|
|
|
Container() Container
|
2016-02-03 22:30:45 +00:00
|
|
|
// Stdio of the container
|
|
|
|
Stdio() Stdio
|
|
|
|
// SystemPid is the pid on the system
|
|
|
|
SystemPid() int
|
2016-03-14 20:57:28 +00:00
|
|
|
// State returns if the process is running or not
|
|
|
|
State() State
|
2016-04-27 19:00:29 +00:00
|
|
|
// Wait reaps the shim process if avaliable
|
|
|
|
Wait()
|
2016-02-01 19:02:41 +00:00
|
|
|
}
|
|
|
|
|
2016-02-11 19:23:35 +00:00
|
|
|
type processConfig struct {
|
|
|
|
id string
|
|
|
|
root string
|
2016-02-29 18:48:39 +00:00
|
|
|
processSpec specs.ProcessSpec
|
2016-03-15 18:31:32 +00:00
|
|
|
spec *specs.Spec
|
2016-02-11 19:23:35 +00:00
|
|
|
c *container
|
|
|
|
stdio Stdio
|
2016-02-11 19:30:25 +00:00
|
|
|
exec bool
|
|
|
|
checkpoint string
|
2016-02-11 19:23:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newProcess(config *processConfig) (*process, error) {
|
2016-01-06 21:32:46 +00:00
|
|
|
p := &process{
|
2016-02-11 19:23:35 +00:00
|
|
|
root: config.root,
|
|
|
|
id: config.id,
|
|
|
|
container: config.c,
|
|
|
|
spec: config.processSpec,
|
|
|
|
stdio: config.stdio,
|
2016-07-01 16:11:29 +00:00
|
|
|
cmdDoneCh: make(chan struct{}),
|
2016-07-13 18:01:07 +00:00
|
|
|
state: Running,
|
2016-02-11 19:23:35 +00:00
|
|
|
}
|
|
|
|
uid, gid, err := getRootIDs(config.spec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-01-06 21:32:46 +00:00
|
|
|
}
|
2016-02-11 19:23:35 +00:00
|
|
|
f, err := os.Create(filepath.Join(config.root, "process.json"))
|
2016-02-01 19:02:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2016-02-26 02:39:03 +00:00
|
|
|
|
2016-04-26 20:29:35 +00:00
|
|
|
ps := ProcessState{
|
|
|
|
ProcessSpec: config.processSpec,
|
|
|
|
Exec: config.exec,
|
|
|
|
PlatformProcessState: PlatformProcessState{
|
|
|
|
Checkpoint: config.checkpoint,
|
|
|
|
RootUID: uid,
|
|
|
|
RootGID: gid,
|
|
|
|
},
|
|
|
|
Stdin: config.stdio.Stdin,
|
|
|
|
Stdout: config.stdio.Stdout,
|
|
|
|
Stderr: config.stdio.Stderr,
|
|
|
|
RuntimeArgs: config.c.runtimeArgs,
|
|
|
|
NoPivotRoot: config.c.noPivotRoot,
|
|
|
|
}
|
|
|
|
|
2016-02-26 02:39:03 +00:00
|
|
|
if err := json.NewEncoder(f).Encode(ps); err != nil {
|
2016-02-01 19:02:41 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-11 19:23:35 +00:00
|
|
|
exit, err := getExitPipe(filepath.Join(config.root, ExitFile))
|
2016-01-06 21:32:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-11 19:23:35 +00:00
|
|
|
control, err := getControlPipe(filepath.Join(config.root, ControlFile))
|
2016-02-02 22:21:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-06 21:32:46 +00:00
|
|
|
p.exitPipe = exit
|
2016-02-02 22:21:25 +00:00
|
|
|
p.controlPipe = control
|
2016-01-06 21:32:46 +00:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2016-02-03 21:56:15 +00:00
|
|
|
func loadProcess(root, id string, c *container, s *ProcessState) (*process, error) {
|
2016-01-06 21:32:46 +00:00
|
|
|
p := &process{
|
|
|
|
root: root,
|
|
|
|
id: id,
|
|
|
|
container: c,
|
2016-02-26 02:39:03 +00:00
|
|
|
spec: s.ProcessSpec,
|
2016-02-03 21:56:15 +00:00
|
|
|
stdio: Stdio{
|
|
|
|
Stdin: s.Stdin,
|
|
|
|
Stdout: s.Stdout,
|
|
|
|
Stderr: s.Stderr,
|
|
|
|
},
|
2016-07-13 18:01:07 +00:00
|
|
|
state: Stopped,
|
2016-01-06 21:32:46 +00:00
|
|
|
}
|
2016-09-07 22:20:30 +00:00
|
|
|
|
|
|
|
startTime, err := ioutil.ReadFile(filepath.Join(p.root, StartTimeFile))
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.startTime = string(startTime)
|
|
|
|
|
2016-02-25 20:59:34 +00:00
|
|
|
if _, err := p.getPidFromFile(); err != nil {
|
2016-02-03 22:30:45 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-06 21:32:46 +00:00
|
|
|
if _, err := p.ExitStatus(); err != nil {
|
|
|
|
if err == ErrProcessNotExited {
|
|
|
|
exit, err := getExitPipe(filepath.Join(root, ExitFile))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.exitPipe = exit
|
2016-07-04 08:12:51 +00:00
|
|
|
|
|
|
|
control, err := getControlPipe(filepath.Join(root, ControlFile))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.controlPipe = control
|
|
|
|
|
2016-07-13 18:01:07 +00:00
|
|
|
p.state = Running
|
2016-01-06 21:32:46 +00:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-01 19:02:41 +00:00
|
|
|
return p, nil
|
2016-01-06 21:32:46 +00:00
|
|
|
}
|
|
|
|
|
2016-09-07 22:20:30 +00:00
|
|
|
func readProcStatField(pid int, field int) (string, error) {
|
|
|
|
data, err := ioutil.ReadFile(filepath.Join(string(filepath.Separator), "proc", strconv.Itoa(pid), "stat"))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if field > 2 {
|
|
|
|
// First, split out the name since he could contains spaces.
|
|
|
|
parts := strings.Split(string(data), ") ")
|
|
|
|
// Now split out the rest, we end up with 2 fields less
|
|
|
|
parts = strings.Split(parts[1], " ")
|
|
|
|
return parts[field-2-1], nil // field count start at 1 in manual
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(string(data), " (")
|
|
|
|
|
|
|
|
if field == 1 {
|
|
|
|
return parts[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
parts = strings.Split(parts[1], ") ")
|
|
|
|
return parts[0], nil
|
|
|
|
}
|
|
|
|
|
2016-01-06 21:32:46 +00:00
|
|
|
type process struct {
|
2016-02-03 21:56:15 +00:00
|
|
|
root string
|
|
|
|
id string
|
|
|
|
pid int
|
2016-02-02 22:21:25 +00:00
|
|
|
exitPipe *os.File
|
|
|
|
controlPipe *os.File
|
|
|
|
container *container
|
2016-02-29 18:48:39 +00:00
|
|
|
spec specs.ProcessSpec
|
2016-02-03 21:56:15 +00:00
|
|
|
stdio Stdio
|
2016-04-27 19:00:29 +00:00
|
|
|
cmd *exec.Cmd
|
2016-07-01 16:11:29 +00:00
|
|
|
cmdSuccess bool
|
|
|
|
cmdDoneCh chan struct{}
|
2016-07-13 18:01:07 +00:00
|
|
|
state State
|
|
|
|
stateLock sync.Mutex
|
2016-09-07 22:20:30 +00:00
|
|
|
startTime string
|
2016-01-06 21:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *process) ID() string {
|
|
|
|
return p.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *process) Container() Container {
|
|
|
|
return p.container
|
|
|
|
}
|
|
|
|
|
2016-02-03 22:30:45 +00:00
|
|
|
func (p *process) SystemPid() int {
|
|
|
|
return p.pid
|
|
|
|
}
|
|
|
|
|
2016-01-06 21:32:46 +00:00
|
|
|
// ExitFD returns the fd of the exit pipe
|
|
|
|
func (p *process) ExitFD() int {
|
|
|
|
return int(p.exitPipe.Fd())
|
|
|
|
}
|
|
|
|
|
2016-02-02 22:21:25 +00:00
|
|
|
func (p *process) CloseStdin() error {
|
|
|
|
_, err := fmt.Fprintf(p.controlPipe, "%d %d %d\n", 0, 0, 0)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *process) Resize(w, h int) error {
|
|
|
|
_, err := fmt.Fprintf(p.controlPipe, "%d %d %d\n", 1, w, h)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-01 18:29:24 +00:00
|
|
|
func (p *process) handleSigkilledShim(rst int, rerr error) (int, error) {
|
2016-09-07 22:20:30 +00:00
|
|
|
if p.cmd == nil || p.cmd.Process == nil {
|
|
|
|
e := unix.Kill(p.pid, 0)
|
|
|
|
if e == syscall.ESRCH {
|
|
|
|
return rst, rerr
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's not the same process, just mark it stopped and set
|
|
|
|
// the status to 255
|
|
|
|
if same, err := p.isSameProcess(); !same {
|
|
|
|
logrus.Warnf("containerd: %s:%s (pid %d) is not the same process anymore (%v)", p.container.id, p.id, p.pid, err)
|
|
|
|
p.stateLock.Lock()
|
|
|
|
p.state = Stopped
|
|
|
|
p.stateLock.Unlock()
|
|
|
|
// Create the file so we get the exit event generated once monitor kicks in
|
|
|
|
// without going to this all process again
|
|
|
|
rerr = ioutil.WriteFile(filepath.Join(p.root, ExitStatusFile), []byte("255"), 0644)
|
|
|
|
return 255, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ppid, err := readProcStatField(p.pid, 4)
|
|
|
|
if err != nil {
|
|
|
|
return rst, fmt.Errorf("could not check process ppid: %v (%v)", err, rerr)
|
|
|
|
}
|
|
|
|
if ppid == "1" {
|
|
|
|
logrus.Warnf("containerd: %s:%s shim died, killing associated process", p.container.id, p.id)
|
|
|
|
unix.Kill(p.pid, syscall.SIGKILL)
|
|
|
|
// wait for the process to die
|
|
|
|
for {
|
|
|
|
e := unix.Kill(p.pid, 0)
|
|
|
|
if e == syscall.ESRCH {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
rst = 128 + int(syscall.SIGKILL)
|
|
|
|
// Create the file so we get the exit event generated once monitor kicks in
|
|
|
|
// without going to this all process again
|
|
|
|
rerr = ioutil.WriteFile(filepath.Join(p.root, ExitStatusFile), []byte(fmt.Sprintf("%d", rst)), 0644)
|
|
|
|
}
|
|
|
|
|
2016-09-01 18:29:24 +00:00
|
|
|
return rst, rerr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Possible that the shim was SIGKILLED
|
|
|
|
e := unix.Kill(p.cmd.Process.Pid, 0)
|
|
|
|
if e != syscall.ESRCH {
|
|
|
|
return rst, rerr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we got the shim ProcessState
|
|
|
|
<-p.cmdDoneCh
|
|
|
|
|
|
|
|
shimStatus := p.cmd.ProcessState.Sys().(syscall.WaitStatus)
|
|
|
|
if shimStatus.Signaled() && shimStatus.Signal() == syscall.SIGKILL {
|
|
|
|
logrus.Debugf("containerd: ExitStatus(container: %s, process: %s): shim was SIGKILL'ed reaping its child with pid %d", p.container.id, p.id, p.pid)
|
|
|
|
|
|
|
|
var (
|
|
|
|
status unix.WaitStatus
|
|
|
|
rusage unix.Rusage
|
|
|
|
wpid int
|
|
|
|
)
|
|
|
|
|
2016-09-07 22:20:30 +00:00
|
|
|
// Some processes change their PR_SET_PDEATHSIG, so force kill them
|
|
|
|
unix.Kill(p.pid, syscall.SIGKILL)
|
|
|
|
|
2016-09-01 18:29:24 +00:00
|
|
|
for wpid == 0 {
|
|
|
|
wpid, e = unix.Wait4(p.pid, &status, unix.WNOHANG, &rusage)
|
|
|
|
if e != nil {
|
|
|
|
logrus.Debugf("containerd: ExitStatus(container: %s, process: %s): Wait4(%d): %v", p.container.id, p.id, p.pid, rerr)
|
|
|
|
return rst, rerr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if wpid == p.pid {
|
|
|
|
rerr = nil
|
|
|
|
rst = 128 + int(shimStatus.Signal())
|
|
|
|
} else {
|
|
|
|
logrus.Errorf("containerd: ExitStatus(container: %s, process: %s): unexpected returned pid from wait4 %v (expected %v)", p.container.id, p.id, wpid, p.pid)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.stateLock.Lock()
|
|
|
|
p.state = Stopped
|
|
|
|
p.stateLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
return rst, rerr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *process) ExitStatus() (rst int, rerr error) {
|
2016-01-06 21:32:46 +00:00
|
|
|
data, err := ioutil.ReadFile(filepath.Join(p.root, ExitStatusFile))
|
2016-09-01 18:29:24 +00:00
|
|
|
defer func() {
|
2016-09-07 22:20:30 +00:00
|
|
|
if rerr != nil {
|
|
|
|
rst, rerr = p.handleSigkilledShim(rst, rerr)
|
|
|
|
}
|
2016-09-01 18:29:24 +00:00
|
|
|
}()
|
2016-01-06 21:32:46 +00:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return -1, ErrProcessNotExited
|
|
|
|
}
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
if len(data) == 0 {
|
|
|
|
return -1, ErrProcessNotExited
|
|
|
|
}
|
2016-07-13 18:01:07 +00:00
|
|
|
p.stateLock.Lock()
|
|
|
|
p.state = Stopped
|
|
|
|
p.stateLock.Unlock()
|
2016-02-02 22:21:25 +00:00
|
|
|
return strconv.Atoi(string(data))
|
2016-01-06 21:32:46 +00:00
|
|
|
}
|
|
|
|
|
2016-02-29 18:48:39 +00:00
|
|
|
func (p *process) Spec() specs.ProcessSpec {
|
2016-01-06 21:32:46 +00:00
|
|
|
return p.spec
|
|
|
|
}
|
|
|
|
|
2016-02-03 22:30:45 +00:00
|
|
|
func (p *process) Stdio() Stdio {
|
|
|
|
return p.stdio
|
|
|
|
}
|
|
|
|
|
2016-01-06 21:32:46 +00:00
|
|
|
// Close closes any open files and/or resouces on the process
|
|
|
|
func (p *process) Close() error {
|
2016-06-28 07:04:38 +00:00
|
|
|
err := p.exitPipe.Close()
|
|
|
|
if cerr := p.controlPipe.Close(); err == nil {
|
|
|
|
err = cerr
|
|
|
|
}
|
|
|
|
return err
|
2016-01-06 21:32:46 +00:00
|
|
|
}
|
2016-02-02 22:21:25 +00:00
|
|
|
|
2016-03-14 20:57:28 +00:00
|
|
|
func (p *process) State() State {
|
2016-07-13 18:01:07 +00:00
|
|
|
p.stateLock.Lock()
|
|
|
|
defer p.stateLock.Unlock()
|
|
|
|
return p.state
|
2016-03-14 20:57:28 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 20:59:34 +00:00
|
|
|
func (p *process) getPidFromFile() (int, error) {
|
|
|
|
data, err := ioutil.ReadFile(filepath.Join(p.root, "pid"))
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
i, err := strconv.Atoi(string(data))
|
|
|
|
if err != nil {
|
2016-03-18 06:53:23 +00:00
|
|
|
return -1, errInvalidPidInt
|
2016-02-02 22:21:25 +00:00
|
|
|
}
|
2016-02-25 20:59:34 +00:00
|
|
|
p.pid = i
|
|
|
|
return i, nil
|
2016-02-02 22:21:25 +00:00
|
|
|
}
|
2016-04-26 20:29:35 +00:00
|
|
|
|
2016-09-07 22:20:30 +00:00
|
|
|
func (p *process) readStartTime() (string, error) {
|
|
|
|
return readProcStatField(p.pid, 22)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *process) saveStartTime() error {
|
|
|
|
startTime, err := p.readStartTime()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.startTime = startTime
|
|
|
|
return ioutil.WriteFile(filepath.Join(p.root, StartTimeFile), []byte(startTime), 0644)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *process) isSameProcess() (bool, error) {
|
|
|
|
// for backward compat assume it's the same if startTime wasn't set
|
|
|
|
if p.startTime == "" {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if p.pid == 0 {
|
|
|
|
_, err := p.getPidFromFile()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
startTime, err := p.readStartTime()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return startTime == p.startTime, nil
|
|
|
|
}
|
|
|
|
|
2016-04-27 19:00:29 +00:00
|
|
|
// Wait will reap the shim process
|
|
|
|
func (p *process) Wait() {
|
2016-07-01 16:11:29 +00:00
|
|
|
if p.cmdDoneCh != nil {
|
|
|
|
<-p.cmdDoneCh
|
2016-04-27 19:00:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-26 20:29:35 +00:00
|
|
|
func getExitPipe(path string) (*os.File, error) {
|
2016-05-19 17:12:50 +00:00
|
|
|
if err := unix.Mkfifo(path, 0755); err != nil && !os.IsExist(err) {
|
2016-04-26 20:29:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// add NONBLOCK in case the other side has already closed or else
|
|
|
|
// this function would never return
|
|
|
|
return os.OpenFile(path, syscall.O_RDONLY|syscall.O_NONBLOCK, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getControlPipe(path string) (*os.File, error) {
|
2016-05-19 17:12:50 +00:00
|
|
|
if err := unix.Mkfifo(path, 0755); err != nil && !os.IsExist(err) {
|
2016-04-26 20:29:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return os.OpenFile(path, syscall.O_RDWR|syscall.O_NONBLOCK, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signal sends the provided signal to the process
|
|
|
|
func (p *process) Signal(s os.Signal) error {
|
|
|
|
return syscall.Kill(p.pid, s.(syscall.Signal))
|
|
|
|
}
|
2016-06-09 20:33:26 +00:00
|
|
|
|
|
|
|
// Start unblocks the associated container init process.
|
|
|
|
// This should only be called on the process with ID "init"
|
|
|
|
func (p *process) Start() error {
|
|
|
|
if p.ID() == InitProcessID {
|
2016-06-24 19:02:53 +00:00
|
|
|
var (
|
2016-07-01 16:11:29 +00:00
|
|
|
errC = make(chan error, 1)
|
|
|
|
args = append(p.container.runtimeArgs, "start", p.container.id)
|
|
|
|
cmd = exec.Command(p.container.runtime, args...)
|
2016-06-24 19:02:53 +00:00
|
|
|
)
|
|
|
|
go func() {
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
errC <- fmt.Errorf("%s: %q", err.Error(), out)
|
|
|
|
}
|
|
|
|
errC <- nil
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case err := <-errC:
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-01 16:11:29 +00:00
|
|
|
case <-p.cmdDoneCh:
|
|
|
|
if !p.cmdSuccess {
|
2016-08-26 17:56:18 +00:00
|
|
|
if cmd.Process != nil {
|
|
|
|
cmd.Process.Kill()
|
|
|
|
}
|
2016-07-01 16:11:29 +00:00
|
|
|
cmd.Wait()
|
|
|
|
return ErrShimExited
|
|
|
|
}
|
|
|
|
err := <-errC
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-24 19:02:53 +00:00
|
|
|
}
|
2016-06-09 20:33:26 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|