2014-09-09 04:19:32 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-09-23 21:47:33 +00:00
|
|
|
"strings"
|
2014-09-15 22:56:47 +00:00
|
|
|
"sync"
|
2015-07-08 18:13:47 +00:00
|
|
|
"time"
|
2014-09-09 04:19:32 +00:00
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-09-09 04:19:32 +00:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2015-09-18 17:48:16 +00:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-10-06 17:24:49 +00:00
|
|
|
"github.com/docker/docker/pkg/broadcaster"
|
2014-09-09 04:19:32 +00:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2015-06-29 20:27:54 +00:00
|
|
|
"github.com/docker/docker/pkg/pools"
|
2015-11-03 01:49:36 +00:00
|
|
|
"github.com/docker/docker/pkg/promise"
|
2015-03-24 11:25:26 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-08-28 15:29:10 +00:00
|
|
|
"github.com/docker/docker/pkg/stringutils"
|
2014-09-09 04:19:32 +00:00
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
)
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// ExecConfig holds the configurations for execs. The Daemon keeps
|
|
|
|
// track of both running and finished execs so that they can be
|
|
|
|
// examined both during and after completion.
|
|
|
|
type ExecConfig struct {
|
2014-09-15 23:14:04 +00:00
|
|
|
sync.Mutex
|
2014-09-15 22:56:47 +00:00
|
|
|
ID string
|
2014-09-16 06:43:43 +00:00
|
|
|
Running bool
|
2014-11-17 23:50:09 +00:00
|
|
|
ExitCode int
|
2015-07-08 17:55:42 +00:00
|
|
|
ProcessConfig *execdriver.ProcessConfig
|
2015-07-30 21:01:53 +00:00
|
|
|
streamConfig
|
2014-09-15 22:56:47 +00:00
|
|
|
OpenStdin bool
|
|
|
|
OpenStderr bool
|
|
|
|
OpenStdout bool
|
|
|
|
Container *Container
|
2015-07-09 21:51:10 +00:00
|
|
|
canRemove bool
|
2015-07-23 11:34:40 +00:00
|
|
|
|
|
|
|
// waitStart will be closed immediately after the exec is really started.
|
|
|
|
waitStart chan struct{}
|
2014-09-09 04:19:32 +00:00
|
|
|
}
|
|
|
|
|
2014-09-15 22:56:47 +00:00
|
|
|
type execStore struct {
|
2015-07-30 21:01:53 +00:00
|
|
|
s map[string]*ExecConfig
|
2014-12-23 22:03:20 +00:00
|
|
|
sync.RWMutex
|
2014-09-15 22:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newExecStore() *execStore {
|
2015-07-30 21:01:53 +00:00
|
|
|
return &execStore{s: make(map[string]*ExecConfig, 0)}
|
2014-09-15 22:56:47 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
func (e *execStore) Add(id string, ExecConfig *ExecConfig) {
|
2014-09-15 22:56:47 +00:00
|
|
|
e.Lock()
|
2015-07-30 21:01:53 +00:00
|
|
|
e.s[id] = ExecConfig
|
2014-09-15 22:56:47 +00:00
|
|
|
e.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
func (e *execStore) Get(id string) *ExecConfig {
|
2014-12-23 22:03:20 +00:00
|
|
|
e.RLock()
|
2014-09-15 22:56:47 +00:00
|
|
|
res := e.s[id]
|
2014-12-23 22:03:20 +00:00
|
|
|
e.RUnlock()
|
2014-09-15 22:56:47 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *execStore) Delete(id string) {
|
|
|
|
e.Lock()
|
|
|
|
delete(e.s, id)
|
|
|
|
e.Unlock()
|
|
|
|
}
|
|
|
|
|
2014-12-23 22:03:20 +00:00
|
|
|
func (e *execStore) List() []string {
|
|
|
|
var IDs []string
|
|
|
|
e.RLock()
|
2014-12-24 00:03:24 +00:00
|
|
|
for id := range e.s {
|
2014-12-23 22:03:20 +00:00
|
|
|
IDs = append(IDs, id)
|
|
|
|
}
|
|
|
|
e.RUnlock()
|
|
|
|
return IDs
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
func (ExecConfig *ExecConfig) resize(h, w int) error {
|
2015-07-23 11:34:40 +00:00
|
|
|
select {
|
2015-07-30 21:01:53 +00:00
|
|
|
case <-ExecConfig.waitStart:
|
2015-07-23 11:34:40 +00:00
|
|
|
case <-time.After(time.Second):
|
2015-09-18 17:48:16 +00:00
|
|
|
return derr.ErrorCodeExecResize.WithArgs(ExecConfig.ID)
|
2015-07-23 11:34:40 +00:00
|
|
|
}
|
2015-07-30 21:01:53 +00:00
|
|
|
return ExecConfig.ProcessConfig.Terminal.Resize(h, w)
|
2014-09-15 22:56:47 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
func (d *Daemon) registerExecCommand(ExecConfig *ExecConfig) {
|
2015-02-26 11:03:44 +00:00
|
|
|
// Storing execs in container in order to kill them gracefully whenever the container is stopped or removed.
|
2015-07-30 21:01:53 +00:00
|
|
|
ExecConfig.Container.execCommands.Add(ExecConfig.ID, ExecConfig)
|
2014-09-15 22:56:47 +00:00
|
|
|
// Storing execs in daemon for easy access via remote API.
|
2015-07-30 21:01:53 +00:00
|
|
|
d.execCommands.Add(ExecConfig.ID, ExecConfig)
|
2014-09-15 22:56:47 +00:00
|
|
|
}
|
|
|
|
|
2015-09-12 02:50:21 +00:00
|
|
|
// ExecExists looks up the exec instance and returns a bool if it exists or not.
|
|
|
|
// It will also return the error produced by `getExecConfig`
|
|
|
|
func (d *Daemon) ExecExists(name string) (bool, error) {
|
|
|
|
if _, err := d.getExecConfig(name); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getExecConfig looks up the exec instance by name. If the container associated
|
|
|
|
// with the exec instance is stopped or paused, it will return an error.
|
2015-07-30 21:01:53 +00:00
|
|
|
func (d *Daemon) getExecConfig(name string) (*ExecConfig, error) {
|
2015-09-12 02:50:21 +00:00
|
|
|
ec := d.execCommands.Get(name)
|
2015-07-10 02:39:57 +00:00
|
|
|
|
|
|
|
// If the exec is found but its container is not in the daemon's list of
|
2015-11-19 05:51:26 +00:00
|
|
|
// containers then it must have been deleted, in which case instead of
|
2015-07-10 02:39:57 +00:00
|
|
|
// saying the container isn't running, we should return a 404 so that
|
|
|
|
// the user sees the same error now that they will after the
|
|
|
|
// 5 minute clean-up loop is run which erases old/dead execs.
|
|
|
|
|
2015-09-12 02:50:21 +00:00
|
|
|
if ec != nil && d.containers.Get(ec.Container.ID) != nil {
|
|
|
|
if !ec.Container.IsRunning() {
|
|
|
|
return nil, derr.ErrorCodeContainerNotRunning.WithArgs(ec.Container.ID, ec.Container.State.String())
|
|
|
|
}
|
|
|
|
if ec.Container.isPaused() {
|
|
|
|
return nil, derr.ErrorCodeExecPaused.WithArgs(ec.Container.ID)
|
2014-09-15 22:56:47 +00:00
|
|
|
}
|
2015-09-12 02:50:21 +00:00
|
|
|
return ec, nil
|
2014-09-09 04:19:32 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 17:48:16 +00:00
|
|
|
return nil, derr.ErrorCodeNoExecID.WithArgs(name)
|
2014-09-15 22:56:47 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
func (d *Daemon) unregisterExecCommand(ExecConfig *ExecConfig) {
|
|
|
|
ExecConfig.Container.execCommands.Delete(ExecConfig.ID)
|
|
|
|
d.execCommands.Delete(ExecConfig.ID)
|
2014-09-15 22:56:47 +00:00
|
|
|
}
|
2014-09-09 04:19:32 +00:00
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
func (d *Daemon) getActiveContainer(name string) (*Container, error) {
|
|
|
|
container, err := d.Get(name)
|
2014-12-16 23:06:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-09-09 04:19:32 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 00:06:59 +00:00
|
|
|
if !container.IsRunning() {
|
2015-09-18 17:48:16 +00:00
|
|
|
return nil, derr.ErrorCodeNotRunning.WithArgs(name)
|
2014-09-09 04:19:32 +00:00
|
|
|
}
|
2015-07-30 21:01:53 +00:00
|
|
|
if container.isPaused() {
|
2015-09-18 17:48:16 +00:00
|
|
|
return nil, derr.ErrorCodeExecPaused.WithArgs(name)
|
2014-11-27 16:08:39 +00:00
|
|
|
}
|
2014-09-15 22:56:47 +00:00
|
|
|
return container, nil
|
|
|
|
}
|
2014-09-09 04:19:32 +00:00
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// ContainerExecCreate sets up an exec in a running container.
|
2015-09-29 17:51:40 +00:00
|
|
|
func (d *Daemon) ContainerExecCreate(config *runconfig.ExecConfig) (string, error) {
|
|
|
|
container, err := d.getActiveContainer(config.Container)
|
2014-12-01 17:16:49 +00:00
|
|
|
if err != nil {
|
2015-04-17 05:36:23 +00:00
|
|
|
return "", err
|
2014-12-01 17:16:49 +00:00
|
|
|
}
|
2014-09-15 22:56:47 +00:00
|
|
|
|
2015-08-28 15:29:10 +00:00
|
|
|
cmd := stringutils.NewStrSlice(config.Cmd...)
|
|
|
|
entrypoint, args := d.getEntrypointAndArgs(stringutils.NewStrSlice(), cmd)
|
2014-09-09 04:19:32 +00:00
|
|
|
|
2015-07-08 17:55:42 +00:00
|
|
|
processConfig := &execdriver.ProcessConfig{
|
2015-11-01 15:53:15 +00:00
|
|
|
CommonProcessConfig: execdriver.CommonProcessConfig{
|
|
|
|
Tty: config.Tty,
|
|
|
|
Entrypoint: entrypoint,
|
|
|
|
Arguments: args,
|
|
|
|
},
|
2014-09-09 04:19:32 +00:00
|
|
|
}
|
2015-11-01 15:53:15 +00:00
|
|
|
setPlatformSpecificExecProcessConfig(config, container, processConfig)
|
2014-09-09 04:19:32 +00:00
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
ExecConfig := &ExecConfig{
|
2015-07-29 00:19:17 +00:00
|
|
|
ID: stringid.GenerateNonCryptoID(),
|
2014-09-09 04:19:32 +00:00
|
|
|
OpenStdin: config.AttachStdin,
|
2014-09-15 22:56:47 +00:00
|
|
|
OpenStdout: config.AttachStdout,
|
|
|
|
OpenStderr: config.AttachStderr,
|
2015-07-30 21:01:53 +00:00
|
|
|
streamConfig: streamConfig{},
|
2014-09-09 04:19:32 +00:00
|
|
|
ProcessConfig: processConfig,
|
2014-09-15 22:56:47 +00:00
|
|
|
Container: container,
|
2014-09-16 06:43:43 +00:00
|
|
|
Running: false,
|
2015-07-23 11:34:40 +00:00
|
|
|
waitStart: make(chan struct{}),
|
2014-09-15 22:56:47 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
d.registerExecCommand(ExecConfig)
|
2014-09-15 22:56:47 +00:00
|
|
|
|
2015-11-03 17:33:13 +00:00
|
|
|
d.LogContainerEvent(container, "exec_create: "+ExecConfig.ProcessConfig.Entrypoint+" "+strings.Join(ExecConfig.ProcessConfig.Arguments, " "))
|
2014-09-15 22:56:47 +00:00
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
return ExecConfig.ID, nil
|
2014-09-15 22:56:47 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// ContainerExecStart starts a previously set up exec instance. The
|
|
|
|
// std streams are set up.
|
2015-09-12 02:50:21 +00:00
|
|
|
func (d *Daemon) ContainerExecStart(name string, stdin io.ReadCloser, stdout io.Writer, stderr io.Writer) error {
|
2014-09-15 22:56:47 +00:00
|
|
|
var (
|
|
|
|
cStdin io.ReadCloser
|
|
|
|
cStdout, cStderr io.Writer
|
|
|
|
)
|
|
|
|
|
2015-09-12 02:50:21 +00:00
|
|
|
ec, err := d.getExecConfig(name)
|
2014-09-15 23:14:04 +00:00
|
|
|
if err != nil {
|
2015-09-12 02:50:21 +00:00
|
|
|
return derr.ErrorCodeNoExecID.WithArgs(name)
|
2014-09-15 22:56:47 +00:00
|
|
|
}
|
|
|
|
|
2015-09-12 02:50:21 +00:00
|
|
|
ec.Lock()
|
|
|
|
if ec.Running {
|
|
|
|
ec.Unlock()
|
|
|
|
return derr.ErrorCodeExecRunning.WithArgs(ec.ID)
|
2014-09-15 22:56:47 +00:00
|
|
|
}
|
2015-09-12 02:50:21 +00:00
|
|
|
ec.Running = true
|
|
|
|
ec.Unlock()
|
2014-09-15 22:56:47 +00:00
|
|
|
|
2015-09-12 02:50:21 +00:00
|
|
|
logrus.Debugf("starting exec command %s in container %s", ec.ID, ec.Container.ID)
|
|
|
|
container := ec.Container
|
2015-11-03 17:33:13 +00:00
|
|
|
d.LogContainerEvent(container, "exec_start: "+ec.ProcessConfig.Entrypoint+" "+strings.Join(ec.ProcessConfig.Arguments, " "))
|
2014-11-14 11:34:59 +00:00
|
|
|
|
2015-09-12 02:50:21 +00:00
|
|
|
if ec.OpenStdin {
|
2014-09-15 22:56:47 +00:00
|
|
|
r, w := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
defer w.Close()
|
2015-03-26 22:22:04 +00:00
|
|
|
defer logrus.Debugf("Closing buffered stdin pipe")
|
2015-06-29 20:27:54 +00:00
|
|
|
pools.Copy(w, stdin)
|
2014-09-15 22:56:47 +00:00
|
|
|
}()
|
|
|
|
cStdin = r
|
|
|
|
}
|
2015-09-12 02:50:21 +00:00
|
|
|
if ec.OpenStdout {
|
2015-04-17 05:36:23 +00:00
|
|
|
cStdout = stdout
|
2014-09-15 22:56:47 +00:00
|
|
|
}
|
2015-09-12 02:50:21 +00:00
|
|
|
if ec.OpenStderr {
|
2015-04-17 05:36:23 +00:00
|
|
|
cStderr = stderr
|
2014-09-09 04:19:32 +00:00
|
|
|
}
|
|
|
|
|
2015-10-06 17:24:49 +00:00
|
|
|
ec.streamConfig.stderr = new(broadcaster.Unbuffered)
|
|
|
|
ec.streamConfig.stdout = new(broadcaster.Unbuffered)
|
2014-09-09 04:19:32 +00:00
|
|
|
// Attach to stdin
|
2015-09-12 02:50:21 +00:00
|
|
|
if ec.OpenStdin {
|
|
|
|
ec.streamConfig.stdin, ec.streamConfig.stdinPipe = io.Pipe()
|
2014-09-09 04:19:32 +00:00
|
|
|
} else {
|
2015-09-12 02:50:21 +00:00
|
|
|
ec.streamConfig.stdinPipe = ioutils.NopWriteCloser(ioutil.Discard) // Silently drop stdin
|
2014-09-09 04:19:32 +00:00
|
|
|
}
|
|
|
|
|
2015-09-12 02:50:21 +00:00
|
|
|
attachErr := attach(&ec.streamConfig, ec.OpenStdin, true, ec.ProcessConfig.Tty, cStdin, cStdout, cStderr)
|
2014-09-09 04:19:32 +00:00
|
|
|
|
2014-09-10 07:20:05 +00:00
|
|
|
execErr := make(chan error)
|
2014-09-15 22:56:47 +00:00
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// Note, the ExecConfig data will be removed when the container
|
2014-11-17 23:50:09 +00:00
|
|
|
// itself is deleted. This allows us to query it (for things like
|
|
|
|
// the exitStatus) even after the cmd is done running.
|
2014-09-15 22:56:47 +00:00
|
|
|
|
2014-09-09 04:19:32 +00:00
|
|
|
go func() {
|
2015-11-03 01:49:36 +00:00
|
|
|
execErr <- d.containerExec(container, ec)
|
2014-09-09 04:19:32 +00:00
|
|
|
}()
|
2015-10-06 00:21:50 +00:00
|
|
|
|
2014-09-09 04:19:32 +00:00
|
|
|
select {
|
|
|
|
case err := <-attachErr:
|
2014-09-10 07:20:05 +00:00
|
|
|
if err != nil {
|
2015-09-18 17:48:16 +00:00
|
|
|
return derr.ErrorCodeExecAttach.WithArgs(err)
|
2014-09-10 07:20:05 +00:00
|
|
|
}
|
2015-08-10 13:48:39 +00:00
|
|
|
return nil
|
2014-09-09 04:19:32 +00:00
|
|
|
case err := <-execErr:
|
2015-10-06 00:21:50 +00:00
|
|
|
if aErr := <-attachErr; aErr != nil && err == nil {
|
|
|
|
return derr.ErrorCodeExecAttach.WithArgs(aErr)
|
|
|
|
}
|
2015-08-10 13:48:39 +00:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maybe the container stopped while we were trying to exec
|
|
|
|
if !container.IsRunning() {
|
2015-09-18 17:48:16 +00:00
|
|
|
return derr.ErrorCodeExecContainerStopped
|
2015-08-10 13:48:39 +00:00
|
|
|
}
|
2015-10-06 00:21:50 +00:00
|
|
|
return derr.ErrorCodeExecCantRun.WithArgs(ec.ID, container.ID, err)
|
2014-09-09 04:19:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// Exec calls the underlying exec driver to run
|
2015-09-29 17:51:40 +00:00
|
|
|
func (d *Daemon) Exec(c *Container, ExecConfig *ExecConfig, pipes *execdriver.Pipes, startCallback execdriver.DriverCallback) (int, error) {
|
2015-09-11 19:05:57 +00:00
|
|
|
hooks := execdriver.Hooks{
|
|
|
|
Start: startCallback,
|
|
|
|
}
|
2015-09-29 17:51:40 +00:00
|
|
|
exitStatus, err := d.execDriver.Exec(c.command, ExecConfig.ProcessConfig, pipes, hooks)
|
2014-11-17 23:50:09 +00:00
|
|
|
|
|
|
|
// On err, make sure we don't leave ExitCode at zero
|
|
|
|
if err != nil && exitStatus == 0 {
|
|
|
|
exitStatus = 128
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
ExecConfig.ExitCode = exitStatus
|
|
|
|
ExecConfig.Running = false
|
2014-11-17 23:50:09 +00:00
|
|
|
|
|
|
|
return exitStatus, err
|
2014-09-09 04:19:32 +00:00
|
|
|
}
|
2015-07-08 18:13:47 +00:00
|
|
|
|
|
|
|
// execCommandGC runs a ticker to clean up the daemon references
|
|
|
|
// of exec configs that are no longer part of the container.
|
|
|
|
func (d *Daemon) execCommandGC() {
|
|
|
|
for range time.Tick(5 * time.Minute) {
|
|
|
|
var (
|
|
|
|
cleaned int
|
|
|
|
liveExecCommands = d.containerExecIds()
|
|
|
|
)
|
2015-07-09 21:51:10 +00:00
|
|
|
for id, config := range d.execCommands.s {
|
|
|
|
if config.canRemove {
|
2015-07-08 18:13:47 +00:00
|
|
|
cleaned++
|
|
|
|
d.execCommands.Delete(id)
|
2015-07-09 21:51:10 +00:00
|
|
|
} else {
|
|
|
|
if _, exists := liveExecCommands[id]; !exists {
|
|
|
|
config.canRemove = true
|
|
|
|
}
|
2015-07-08 18:13:47 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-13 17:36:36 +00:00
|
|
|
if cleaned > 0 {
|
|
|
|
logrus.Debugf("clean %d unused exec commands", cleaned)
|
|
|
|
}
|
2015-07-08 18:13:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// containerExecIds returns a list of all the current exec ids that are in use
|
|
|
|
// and running inside a container.
|
|
|
|
func (d *Daemon) containerExecIds() map[string]struct{} {
|
|
|
|
ids := map[string]struct{}{}
|
|
|
|
for _, c := range d.containers.List() {
|
|
|
|
for _, id := range c.execCommands.List() {
|
|
|
|
ids[id] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|
2015-11-03 01:49:36 +00:00
|
|
|
|
2015-11-03 19:25:22 +00:00
|
|
|
func (d *Daemon) containerExec(container *Container, ec *ExecConfig) error {
|
2015-11-03 01:49:36 +00:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
|
|
|
callback := func(processConfig *execdriver.ProcessConfig, pid int, chOOM <-chan struct{}) error {
|
|
|
|
if processConfig.Tty {
|
|
|
|
// The callback is called after the process Start()
|
|
|
|
// so we are in the parent process. In TTY mode, stdin/out/err is the PtySlave
|
|
|
|
// which we close here.
|
|
|
|
if c, ok := processConfig.Stdout.(io.Closer); ok {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(ec.waitStart)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use a callback here instead of a goroutine and an chan for
|
|
|
|
// synchronization purposes
|
2015-11-03 19:25:22 +00:00
|
|
|
cErr := promise.Go(func() error { return d.monitorExec(container, ec, callback) })
|
2015-11-03 01:49:36 +00:00
|
|
|
|
|
|
|
// Exec should not return until the process is actually running
|
|
|
|
select {
|
|
|
|
case <-ec.waitStart:
|
|
|
|
case err := <-cErr:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-03 19:25:22 +00:00
|
|
|
func (d *Daemon) monitorExec(container *Container, ExecConfig *ExecConfig, callback execdriver.DriverCallback) error {
|
2015-11-03 01:49:36 +00:00
|
|
|
pipes := execdriver.NewPipes(ExecConfig.streamConfig.stdin, ExecConfig.streamConfig.stdout, ExecConfig.streamConfig.stderr, ExecConfig.OpenStdin)
|
2015-11-09 23:14:20 +00:00
|
|
|
exitCode, err := d.Exec(container, ExecConfig, pipes, callback)
|
2015-11-03 01:49:36 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Error running command in existing container %s: %s", container.ID, err)
|
|
|
|
}
|
|
|
|
logrus.Debugf("Exec task in container %s exited with code %d", container.ID, exitCode)
|
|
|
|
if ExecConfig.OpenStdin {
|
|
|
|
if err := ExecConfig.streamConfig.stdin.Close(); err != nil {
|
|
|
|
logrus.Errorf("Error closing stdin while running in %s: %s", container.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := ExecConfig.streamConfig.stdout.Clean(); err != nil {
|
|
|
|
logrus.Errorf("Error closing stdout while running in %s: %s", container.ID, err)
|
|
|
|
}
|
|
|
|
if err := ExecConfig.streamConfig.stderr.Clean(); err != nil {
|
|
|
|
logrus.Errorf("Error closing stderr while running in %s: %s", container.ID, err)
|
|
|
|
}
|
|
|
|
if ExecConfig.ProcessConfig.Terminal != nil {
|
|
|
|
if err := ExecConfig.ProcessConfig.Terminal.Close(); err != nil {
|
|
|
|
logrus.Errorf("Error closing terminal while running in container %s: %s", container.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// remove the exec command from the container's store only and not the
|
|
|
|
// daemon's store so that the exec command can be inspected.
|
|
|
|
container.execCommands.Delete(ExecConfig.ID)
|
|
|
|
return err
|
|
|
|
}
|