Embed exec.Cmd on Process

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
Michael Crosby 2014-01-10 11:44:35 -08:00
parent 25a2697717
commit 5573c744e4
3 changed files with 25 additions and 69 deletions

View File

@ -733,10 +733,10 @@ func (container *Container) Start() (err error) {
container.process = &execdriver.Process{ container.process = &execdriver.Process{
Name: container.ID, Name: container.ID,
Privileged: container.hostConfig.Privileged, Privileged: container.hostConfig.Privileged,
Dir: root, Rootfs: root,
InitPath: "/.dockerinit", InitPath: "/.dockerinit",
Entrypoint: container.Path, Entrypoint: container.Path,
Args: container.Args, Arguments: container.Args,
WorkingDir: workingDir, WorkingDir: workingDir,
ConfigPath: container.lxcConfigPath(), ConfigPath: container.lxcConfigPath(),
Network: en, Network: en,

View File

@ -2,7 +2,6 @@ package execdriver
import ( import (
"errors" "errors"
"io"
"os/exec" "os/exec"
"syscall" "syscall"
"time" "time"
@ -28,59 +27,26 @@ type Network struct {
} }
type Process struct { type Process struct {
exec.Cmd
Name string // unique name for the conatienr Name string // unique name for the conatienr
Privileged bool Privileged bool
User string User string
Dir string // root fs of the container Rootfs string // root fs of the container
InitPath string // dockerinit InitPath string // dockerinit
Entrypoint string Entrypoint string
Args []string Arguments []string
// Environment map[string]string // we don't use this right now because we use an env file
WorkingDir string WorkingDir string
ConfigPath string ConfigPath string
Tty bool Tty bool
Network *Network // if network is nil then networking is disabled Network *Network // if network is nil then networking is disabled
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
cmd *exec.Cmd
}
func (c *Process) SetCmd(cmd *exec.Cmd) error {
c.cmd = cmd
cmd.Stdout = c.Stdout
cmd.Stderr = c.Stderr
cmd.Stdin = c.Stdin
return nil
} }
func (c *Process) Pid() int { func (c *Process) Pid() int {
return c.cmd.Process.Pid return c.Process.Pid
}
func (c *Process) StdinPipe() (io.WriteCloser, error) {
return c.cmd.StdinPipe()
}
func (c *Process) StderrPipe() (io.ReadCloser, error) {
return c.cmd.StderrPipe()
}
func (c *Process) StdoutPipe() (io.ReadCloser, error) {
return c.cmd.StdoutPipe()
} }
func (c *Process) GetExitCode() int { func (c *Process) GetExitCode() int {
if c.cmd != nil { return c.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
return c.cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
}
return -1 return -1
} }
func (c *Process) Wait() error {
if c.cmd != nil {
return c.cmd.Wait()
}
return ErrCommandIsNil
}

View File

@ -67,22 +67,28 @@ func (d *driver) Start(c *execdriver.Process) error {
} }
params = append(params, "--", c.Entrypoint) params = append(params, "--", c.Entrypoint)
params = append(params, c.Args...) params = append(params, c.Arguments...)
cmd := exec.Command(params[0], params[1:]...) var (
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true} name = params[0]
cmd.SysProcAttr.Setctty = true arg = params[1:]
)
if err := c.SetCmd(cmd); err != nil { aname, err := exec.LookPath(name)
return err if err != nil {
aname = name
} }
c.Path = aname
c.Args = append([]string{name}, arg...)
if err := cmd.Start(); err != nil { c.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
c.SysProcAttr.Setctty = true
if err := c.Start(); err != nil {
return err return err
} }
// Poll for running // Poll for running
if err := d.waitForStart(cmd, c); err != nil { if err := d.waitForStart(c); err != nil {
return err return err
} }
return nil return nil
@ -113,11 +119,11 @@ func (d *driver) Wait(c *execdriver.Process, duration time.Duration) error {
// If seconds < 0 then wait forever // If seconds < 0 then wait forever
func (d *driver) wait(c *execdriver.Process, duration time.Duration) error { func (d *driver) wait(c *execdriver.Process, duration time.Duration) error {
begin:
var ( var (
killer bool killer bool
done = d.waitCmd(c) done = d.waitCmd(c)
) )
begin:
if duration > 0 { if duration > 0 {
select { select {
case err := <-done: case err := <-done:
@ -146,23 +152,7 @@ func (d *driver) kill(c *execdriver.Process, sig int) error {
return exec.Command("lxc-kill", "-n", c.Name, strconv.Itoa(sig)).Run() return exec.Command("lxc-kill", "-n", c.Name, strconv.Itoa(sig)).Run()
} }
/* Generate the lxc configuration and return the path to the file func (d *driver) waitForStart(c *execdriver.Process) error {
func (d *driver) generateConfig(c *execdriver.Process) (string, error) {
p := path.Join(d.root, c.Name)
f, err := os.Create(p)
if err != nil {
return "", nil
}
defer f.Close()
if err := LxcTemplateCompiled.Execute(f, c.Context); err != nil {
return "", err
}
return p, nil
}
*/
func (d *driver) waitForStart(cmd *exec.Cmd, c *execdriver.Process) error {
// We wait for the container to be fully running. // We wait for the container to be fully running.
// Timeout after 5 seconds. In case of broken pipe, just retry. // Timeout after 5 seconds. In case of broken pipe, just retry.
// Note: The container can run and finish correctly before // Note: The container can run and finish correctly before