2014-09-09 00:19:32 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-09-23 17:47:33 -04:00
|
|
|
"strings"
|
2014-09-15 18:56:47 -04:00
|
|
|
"sync"
|
2015-07-08 14:13:47 -04:00
|
|
|
"time"
|
2014-09-09 00:19:32 -04:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-09-10 18:01:18 -04:00
|
|
|
"github.com/docker/docker/context"
|
2014-09-09 00:19:32 -04:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2015-09-18 13:48:16 -04:00
|
|
|
derr "github.com/docker/docker/errors"
|
2014-09-09 00:19:32 -04:00
|
|
|
"github.com/docker/docker/pkg/broadcastwriter"
|
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2015-06-29 16:27:54 -04:00
|
|
|
"github.com/docker/docker/pkg/pools"
|
2015-03-24 07:25:26 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-08-28 11:29:10 -04:00
|
|
|
"github.com/docker/docker/pkg/stringutils"
|
2014-09-09 00:19:32 -04:00
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
)
|
|
|
|
|
2015-07-30 17:01:53 -04: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 19:14:04 -04:00
|
|
|
sync.Mutex
|
2014-09-15 18:56:47 -04:00
|
|
|
ID string
|
2014-09-16 02:43:43 -04:00
|
|
|
Running bool
|
2014-11-17 18:50:09 -05:00
|
|
|
ExitCode int
|
2015-07-08 13:55:42 -04:00
|
|
|
ProcessConfig *execdriver.ProcessConfig
|
2015-07-30 17:01:53 -04:00
|
|
|
streamConfig
|
2014-09-15 18:56:47 -04:00
|
|
|
OpenStdin bool
|
|
|
|
OpenStderr bool
|
|
|
|
OpenStdout bool
|
|
|
|
Container *Container
|
2015-07-09 17:51:10 -04:00
|
|
|
canRemove bool
|
2015-07-23 07:34:40 -04:00
|
|
|
|
|
|
|
// waitStart will be closed immediately after the exec is really started.
|
|
|
|
waitStart chan struct{}
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
|
2014-09-15 18:56:47 -04:00
|
|
|
type execStore struct {
|
2015-07-30 17:01:53 -04:00
|
|
|
s map[string]*ExecConfig
|
2014-12-23 17:03:20 -05:00
|
|
|
sync.RWMutex
|
2014-09-15 18:56:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func newExecStore() *execStore {
|
2015-07-30 17:01:53 -04:00
|
|
|
return &execStore{s: make(map[string]*ExecConfig, 0)}
|
2014-09-15 18:56:47 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
func (e *execStore) Add(id string, ExecConfig *ExecConfig) {
|
2014-09-15 18:56:47 -04:00
|
|
|
e.Lock()
|
2015-07-30 17:01:53 -04:00
|
|
|
e.s[id] = ExecConfig
|
2014-09-15 18:56:47 -04:00
|
|
|
e.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
func (e *execStore) Get(id string) *ExecConfig {
|
2014-12-23 17:03:20 -05:00
|
|
|
e.RLock()
|
2014-09-15 18:56:47 -04:00
|
|
|
res := e.s[id]
|
2014-12-23 17:03:20 -05:00
|
|
|
e.RUnlock()
|
2014-09-15 18:56:47 -04:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *execStore) Delete(id string) {
|
|
|
|
e.Lock()
|
|
|
|
delete(e.s, id)
|
|
|
|
e.Unlock()
|
|
|
|
}
|
|
|
|
|
2014-12-23 17:03:20 -05:00
|
|
|
func (e *execStore) List() []string {
|
|
|
|
var IDs []string
|
|
|
|
e.RLock()
|
2014-12-23 19:03:24 -05:00
|
|
|
for id := range e.s {
|
2014-12-23 17:03:20 -05:00
|
|
|
IDs = append(IDs, id)
|
|
|
|
}
|
|
|
|
e.RUnlock()
|
|
|
|
return IDs
|
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
func (ExecConfig *ExecConfig) resize(h, w int) error {
|
2015-07-23 07:34:40 -04:00
|
|
|
select {
|
2015-07-30 17:01:53 -04:00
|
|
|
case <-ExecConfig.waitStart:
|
2015-07-23 07:34:40 -04:00
|
|
|
case <-time.After(time.Second):
|
2015-09-18 13:48:16 -04:00
|
|
|
return derr.ErrorCodeExecResize.WithArgs(ExecConfig.ID)
|
2015-07-23 07:34:40 -04:00
|
|
|
}
|
2015-07-30 17:01:53 -04:00
|
|
|
return ExecConfig.ProcessConfig.Terminal.Resize(h, w)
|
2014-09-15 18:56:47 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
func (d *Daemon) registerExecCommand(ExecConfig *ExecConfig) {
|
2015-02-26 06:03:44 -05:00
|
|
|
// Storing execs in container in order to kill them gracefully whenever the container is stopped or removed.
|
2015-07-30 17:01:53 -04:00
|
|
|
ExecConfig.Container.execCommands.Add(ExecConfig.ID, ExecConfig)
|
2014-09-15 18:56:47 -04:00
|
|
|
// Storing execs in daemon for easy access via remote API.
|
2015-07-30 17:01:53 -04:00
|
|
|
d.execCommands.Add(ExecConfig.ID, ExecConfig)
|
2014-09-15 18:56:47 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
func (d *Daemon) getExecConfig(name string) (*ExecConfig, error) {
|
|
|
|
ExecConfig := d.execCommands.Get(name)
|
2015-07-09 22:39:57 -04:00
|
|
|
|
|
|
|
// If the exec is found but its container is not in the daemon's list of
|
|
|
|
// containers then it must have been delete, in which case instead of
|
|
|
|
// 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-07-30 17:01:53 -04:00
|
|
|
if ExecConfig != nil && d.containers.Get(ExecConfig.Container.ID) != nil {
|
2015-07-09 22:39:57 -04:00
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
if !ExecConfig.Container.IsRunning() {
|
2015-09-18 13:48:16 -04:00
|
|
|
return nil, derr.ErrorCodeContainerNotRunning.WithArgs(ExecConfig.Container.ID)
|
2014-09-15 18:56:47 -04:00
|
|
|
}
|
2015-07-30 17:01:53 -04:00
|
|
|
return ExecConfig, nil
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
|
2015-09-18 13:48:16 -04:00
|
|
|
return nil, derr.ErrorCodeNoExecID.WithArgs(name)
|
2014-09-15 18:56:47 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
func (d *Daemon) unregisterExecCommand(ExecConfig *ExecConfig) {
|
|
|
|
ExecConfig.Container.execCommands.Delete(ExecConfig.ID)
|
|
|
|
d.execCommands.Delete(ExecConfig.ID)
|
2014-09-15 18:56:47 -04:00
|
|
|
}
|
2014-09-09 00:19:32 -04:00
|
|
|
|
2015-09-10 18:01:18 -04:00
|
|
|
func (d *Daemon) getActiveContainer(ctx context.Context, name string) (*Container, error) {
|
|
|
|
container, err := d.Get(ctx, name)
|
2014-12-16 18:06:35 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
|
2014-09-10 20:06:59 -04:00
|
|
|
if !container.IsRunning() {
|
2015-09-18 13:48:16 -04:00
|
|
|
return nil, derr.ErrorCodeNotRunning.WithArgs(name)
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
2015-07-30 17:01:53 -04:00
|
|
|
if container.isPaused() {
|
2015-09-18 13:48:16 -04:00
|
|
|
return nil, derr.ErrorCodeExecPaused.WithArgs(name)
|
2014-11-27 11:08:39 -05:00
|
|
|
}
|
2014-09-15 18:56:47 -04:00
|
|
|
return container, nil
|
|
|
|
}
|
2014-09-09 00:19:32 -04:00
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// ContainerExecCreate sets up an exec in a running container.
|
2015-09-10 18:01:18 -04:00
|
|
|
func (d *Daemon) ContainerExecCreate(ctx context.Context, config *runconfig.ExecConfig) (string, error) {
|
2015-04-24 14:12:56 -04:00
|
|
|
// Not all drivers support Exec (LXC for example)
|
|
|
|
if err := checkExecSupport(d.execDriver.Name()); err != nil {
|
|
|
|
return "", err
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
|
2015-09-10 18:01:18 -04:00
|
|
|
container, err := d.getActiveContainer(ctx, config.Container)
|
2014-12-01 12:16:49 -05:00
|
|
|
if err != nil {
|
2015-04-17 01:36:23 -04:00
|
|
|
return "", err
|
2014-12-01 12:16:49 -05:00
|
|
|
}
|
2014-09-15 18:56:47 -04:00
|
|
|
|
2015-08-28 11:29:10 -04:00
|
|
|
cmd := stringutils.NewStrSlice(config.Cmd...)
|
|
|
|
entrypoint, args := d.getEntrypointAndArgs(stringutils.NewStrSlice(), cmd)
|
2014-09-09 00:19:32 -04:00
|
|
|
|
2015-06-26 18:06:37 -04:00
|
|
|
user := config.User
|
|
|
|
if len(user) == 0 {
|
|
|
|
user = container.Config.User
|
|
|
|
}
|
|
|
|
|
2015-07-08 13:55:42 -04:00
|
|
|
processConfig := &execdriver.ProcessConfig{
|
2014-09-09 00:19:32 -04:00
|
|
|
Tty: config.Tty,
|
|
|
|
Entrypoint: entrypoint,
|
|
|
|
Arguments: args,
|
2015-06-26 18:06:37 -04:00
|
|
|
User: user,
|
2015-06-19 02:01:50 -04:00
|
|
|
Privileged: config.Privileged,
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
ExecConfig := &ExecConfig{
|
2015-07-28 20:19:17 -04:00
|
|
|
ID: stringid.GenerateNonCryptoID(),
|
2014-09-09 00:19:32 -04:00
|
|
|
OpenStdin: config.AttachStdin,
|
2014-09-15 18:56:47 -04:00
|
|
|
OpenStdout: config.AttachStdout,
|
|
|
|
OpenStderr: config.AttachStderr,
|
2015-07-30 17:01:53 -04:00
|
|
|
streamConfig: streamConfig{},
|
2014-09-09 00:19:32 -04:00
|
|
|
ProcessConfig: processConfig,
|
2014-09-15 18:56:47 -04:00
|
|
|
Container: container,
|
2014-09-16 02:43:43 -04:00
|
|
|
Running: false,
|
2015-07-23 07:34:40 -04:00
|
|
|
waitStart: make(chan struct{}),
|
2014-09-15 18:56:47 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
d.registerExecCommand(ExecConfig)
|
2014-09-15 18:56:47 -04:00
|
|
|
|
2015-09-10 18:01:18 -04:00
|
|
|
container.logEvent(ctx, "exec_create: "+ExecConfig.ProcessConfig.Entrypoint+" "+strings.Join(ExecConfig.ProcessConfig.Arguments, " "))
|
2014-09-15 18:56:47 -04:00
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
return ExecConfig.ID, nil
|
2014-09-15 18:56:47 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// ContainerExecStart starts a previously set up exec instance. The
|
|
|
|
// std streams are set up.
|
2015-09-10 18:01:18 -04:00
|
|
|
func (d *Daemon) ContainerExecStart(ctx context.Context, execName string, stdin io.ReadCloser, stdout io.Writer, stderr io.Writer) error {
|
2014-09-15 18:56:47 -04:00
|
|
|
var (
|
|
|
|
cStdin io.ReadCloser
|
|
|
|
cStdout, cStderr io.Writer
|
|
|
|
)
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
ExecConfig, err := d.getExecConfig(execName)
|
2014-09-15 19:14:04 -04:00
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-09-15 18:56:47 -04:00
|
|
|
}
|
|
|
|
|
2014-09-15 19:14:04 -04:00
|
|
|
func() {
|
2015-07-30 17:01:53 -04:00
|
|
|
ExecConfig.Lock()
|
|
|
|
defer ExecConfig.Unlock()
|
|
|
|
if ExecConfig.Running {
|
2015-09-18 13:48:16 -04:00
|
|
|
err = derr.ErrorCodeExecRunning.WithArgs(execName)
|
2014-09-15 19:14:04 -04:00
|
|
|
}
|
2015-07-30 17:01:53 -04:00
|
|
|
ExecConfig.Running = true
|
2014-09-15 19:14:04 -04:00
|
|
|
}()
|
2014-09-15 18:56:47 -04:00
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-09-15 18:56:47 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
logrus.Debugf("starting exec command %s in container %s", ExecConfig.ID, ExecConfig.Container.ID)
|
|
|
|
container := ExecConfig.Container
|
2014-09-15 18:56:47 -04:00
|
|
|
|
2015-09-10 18:01:18 -04:00
|
|
|
container.logEvent(ctx, "exec_start: "+ExecConfig.ProcessConfig.Entrypoint+" "+strings.Join(ExecConfig.ProcessConfig.Arguments, " "))
|
2014-11-14 06:34:59 -05:00
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
if ExecConfig.OpenStdin {
|
2014-09-15 18:56:47 -04:00
|
|
|
r, w := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
defer w.Close()
|
2015-03-26 18:22:04 -04:00
|
|
|
defer logrus.Debugf("Closing buffered stdin pipe")
|
2015-06-29 16:27:54 -04:00
|
|
|
pools.Copy(w, stdin)
|
2014-09-15 18:56:47 -04:00
|
|
|
}()
|
|
|
|
cStdin = r
|
|
|
|
}
|
2015-07-30 17:01:53 -04:00
|
|
|
if ExecConfig.OpenStdout {
|
2015-04-17 01:36:23 -04:00
|
|
|
cStdout = stdout
|
2014-09-15 18:56:47 -04:00
|
|
|
}
|
2015-07-30 17:01:53 -04:00
|
|
|
if ExecConfig.OpenStderr {
|
2015-04-17 01:36:23 -04:00
|
|
|
cStderr = stderr
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
ExecConfig.streamConfig.stderr = broadcastwriter.New()
|
|
|
|
ExecConfig.streamConfig.stdout = broadcastwriter.New()
|
2014-09-09 00:19:32 -04:00
|
|
|
// Attach to stdin
|
2015-07-30 17:01:53 -04:00
|
|
|
if ExecConfig.OpenStdin {
|
|
|
|
ExecConfig.streamConfig.stdin, ExecConfig.streamConfig.stdinPipe = io.Pipe()
|
2014-09-09 00:19:32 -04:00
|
|
|
} else {
|
2015-07-30 17:01:53 -04:00
|
|
|
ExecConfig.streamConfig.stdinPipe = ioutils.NopWriteCloser(ioutil.Discard) // Silently drop stdin
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
attachErr := attach(&ExecConfig.streamConfig, ExecConfig.OpenStdin, true, ExecConfig.ProcessConfig.Tty, cStdin, cStdout, cStderr)
|
2014-09-09 00:19:32 -04:00
|
|
|
|
2014-09-10 03:20:05 -04:00
|
|
|
execErr := make(chan error)
|
2014-09-15 18:56:47 -04:00
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// Note, the ExecConfig data will be removed when the container
|
2014-11-17 18:50:09 -05: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 18:56:47 -04:00
|
|
|
|
2014-09-09 00:19:32 -04:00
|
|
|
go func() {
|
2015-09-10 18:01:18 -04:00
|
|
|
if err := container.exec(ctx, ExecConfig); err != nil {
|
2015-09-18 13:48:16 -04:00
|
|
|
execErr <- derr.ErrorCodeExecCantRun.WithArgs(execName, container.ID, err)
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case err := <-attachErr:
|
2014-09-10 03:20:05 -04:00
|
|
|
if err != nil {
|
2015-09-18 13:48:16 -04:00
|
|
|
return derr.ErrorCodeExecAttach.WithArgs(err)
|
2014-09-10 03:20:05 -04:00
|
|
|
}
|
2015-08-10 09:48:39 -04:00
|
|
|
return nil
|
2014-09-09 00:19:32 -04:00
|
|
|
case err := <-execErr:
|
2015-08-10 09:48:39 -04:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maybe the container stopped while we were trying to exec
|
|
|
|
if !container.IsRunning() {
|
2015-09-18 13:48:16 -04:00
|
|
|
return derr.ErrorCodeExecContainerStopped
|
2015-08-10 09:48:39 -04:00
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// Exec calls the underlying exec driver to run
|
2015-09-10 18:01:18 -04:00
|
|
|
func (d *Daemon) Exec(ctx context.Context, c *Container, ExecConfig *ExecConfig, pipes *execdriver.Pipes, startCallback execdriver.DriverCallback) (int, error) {
|
2015-09-11 15:05:57 -04:00
|
|
|
hooks := execdriver.Hooks{
|
|
|
|
Start: startCallback,
|
|
|
|
}
|
2015-09-10 18:01:18 -04:00
|
|
|
exitStatus, err := d.execDriver.Exec(ctx, c.command, ExecConfig.ProcessConfig, pipes, hooks)
|
2014-11-17 18:50:09 -05:00
|
|
|
|
|
|
|
// On err, make sure we don't leave ExitCode at zero
|
|
|
|
if err != nil && exitStatus == 0 {
|
|
|
|
exitStatus = 128
|
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
ExecConfig.ExitCode = exitStatus
|
|
|
|
ExecConfig.Running = false
|
2014-11-17 18:50:09 -05:00
|
|
|
|
|
|
|
return exitStatus, err
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
2015-07-08 14:13:47 -04: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 17:51:10 -04:00
|
|
|
for id, config := range d.execCommands.s {
|
|
|
|
if config.canRemove {
|
2015-07-08 14:13:47 -04:00
|
|
|
cleaned++
|
|
|
|
d.execCommands.Delete(id)
|
2015-07-09 17:51:10 -04:00
|
|
|
} else {
|
|
|
|
if _, exists := liveExecCommands[id]; !exists {
|
|
|
|
config.canRemove = true
|
|
|
|
}
|
2015-07-08 14:13:47 -04:00
|
|
|
}
|
|
|
|
}
|
2015-07-13 13:36:36 -04:00
|
|
|
if cleaned > 0 {
|
|
|
|
logrus.Debugf("clean %d unused exec commands", cleaned)
|
|
|
|
}
|
2015-07-08 14:13:47 -04: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
|
|
|
|
}
|