2014-09-04 05:29:19 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2015-03-05 09:55:14 -08:00
|
|
|
"syscall"
|
2014-09-04 05:29:19 +00:00
|
|
|
|
2014-09-09 04:19:32 +00:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2015-07-16 16:00:55 -07:00
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
2015-07-28 08:43:22 +08:00
|
|
|
// Blank import 'nsenter' so that init in that package will call c
|
|
|
|
// function 'nsexec()' to do 'setns' before Go runtime take over,
|
|
|
|
// it's used for join to exist ns like 'docker exec' command.
|
2015-07-16 16:00:55 -07:00
|
|
|
_ "github.com/opencontainers/runc/libcontainer/nsenter"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/utils"
|
2014-09-04 05:29:19 +00:00
|
|
|
)
|
|
|
|
|
2015-07-28 08:43:22 +08:00
|
|
|
// Exec implements the exec driver Driver interface,
|
|
|
|
// it calls libcontainer APIs to execute a container.
|
2015-09-11 12:05:57 -07:00
|
|
|
func (d *Driver) Exec(c *execdriver.Command, processConfig *execdriver.ProcessConfig, pipes *execdriver.Pipes, hooks execdriver.Hooks) (int, error) {
|
2014-09-04 05:29:19 +00:00
|
|
|
active := d.activeContainers[c.ID]
|
|
|
|
if active == nil {
|
|
|
|
return -1, fmt.Errorf("No active container exists with ID %s", c.ID)
|
|
|
|
}
|
|
|
|
|
2015-03-05 09:55:14 -08:00
|
|
|
p := &libcontainer.Process{
|
|
|
|
Args: append([]string{processConfig.Entrypoint}, processConfig.Arguments...),
|
|
|
|
Env: c.ProcessConfig.Env,
|
|
|
|
Cwd: c.WorkingDir,
|
2015-04-11 11:04:24 +08:00
|
|
|
User: processConfig.User,
|
2015-03-05 09:55:14 -08:00
|
|
|
}
|
2014-09-04 05:29:19 +00:00
|
|
|
|
2015-06-19 16:01:50 +10:00
|
|
|
if processConfig.Privileged {
|
|
|
|
p.Capabilities = execdriver.GetAllCapabilities()
|
|
|
|
}
|
|
|
|
|
2015-04-22 23:37:15 +00:00
|
|
|
config := active.Config()
|
|
|
|
if err := setupPipes(&config, processConfig, p, pipes); err != nil {
|
2015-03-05 09:55:14 -08:00
|
|
|
return -1, err
|
2014-09-04 05:29:19 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 09:55:14 -08:00
|
|
|
if err := active.Start(p); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2015-09-11 12:05:57 -07:00
|
|
|
if hooks.Start != nil {
|
2015-03-05 09:55:14 -08:00
|
|
|
pid, err := p.Pid()
|
|
|
|
if err != nil {
|
|
|
|
p.Signal(os.Kill)
|
|
|
|
p.Wait()
|
|
|
|
return -1, err
|
|
|
|
}
|
2015-09-11 18:01:47 +08:00
|
|
|
|
|
|
|
// A closed channel for OOM is returned here as it will be
|
|
|
|
// non-blocking and return the correct result when read.
|
|
|
|
chOOM := make(chan struct{})
|
|
|
|
close(chOOM)
|
|
|
|
hooks.Start(&c.ProcessConfig, pid, chOOM)
|
2015-03-05 09:55:14 -08:00
|
|
|
}
|
2014-09-04 05:29:19 +00:00
|
|
|
|
2015-03-05 09:55:14 -08:00
|
|
|
ps, err := p.Wait()
|
|
|
|
if err != nil {
|
|
|
|
exitErr, ok := err.(*exec.ExitError)
|
|
|
|
if !ok {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
ps = exitErr.ProcessState
|
|
|
|
}
|
|
|
|
return utils.ExitStatus(ps.Sys().(syscall.WaitStatus)), nil
|
2014-09-04 05:29:19 +00:00
|
|
|
}
|