2014-09-09 04:19:32 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"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"
|
|
|
|
"github.com/docker/docker/pkg/broadcastwriter"
|
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2015-06-29 20:27:54 +00:00
|
|
|
"github.com/docker/docker/pkg/pools"
|
2015-03-24 11:25:26 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2014-09-09 04:19:32 +00:00
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
)
|
|
|
|
|
2014-09-15 22:56:47 +00:00
|
|
|
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
|
2014-09-09 17:36:13 +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 {
|
|
|
|
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 {
|
|
|
|
return &execStore{s: make(map[string]*execConfig, 0)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *execStore) Add(id string, execConfig *execConfig) {
|
|
|
|
e.Lock()
|
|
|
|
e.s[id] = execConfig
|
|
|
|
e.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-09-15 22:56:47 +00:00
|
|
|
func (execConfig *execConfig) Resize(h, w int) error {
|
2015-07-23 11:34:40 +00:00
|
|
|
select {
|
|
|
|
case <-execConfig.waitStart:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
return fmt.Errorf("Exec %s is not running, so it can not be resized.", execConfig.ID)
|
|
|
|
}
|
2014-09-15 22:56:47 +00:00
|
|
|
return execConfig.ProcessConfig.Terminal.Resize(h, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2014-09-15 22:56:47 +00:00
|
|
|
execConfig.Container.execCommands.Add(execConfig.ID, execConfig)
|
|
|
|
// Storing execs in daemon for easy access via remote API.
|
|
|
|
d.execCommands.Add(execConfig.ID, execConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Daemon) getExecConfig(name string) (*execConfig, error) {
|
2015-07-10 02:39:57 +00:00
|
|
|
execConfig := d.execCommands.Get(name)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
if execConfig != nil && d.containers.Get(execConfig.Container.ID) != nil {
|
|
|
|
|
2014-09-15 22:56:47 +00:00
|
|
|
if !execConfig.Container.IsRunning() {
|
2014-09-25 21:23:27 +00:00
|
|
|
return nil, fmt.Errorf("Container %s is not running", execConfig.Container.ID)
|
2014-09-15 22:56:47 +00:00
|
|
|
}
|
|
|
|
return execConfig, nil
|
2014-09-09 04:19:32 +00:00
|
|
|
}
|
|
|
|
|
2014-09-17 18:36:51 +00:00
|
|
|
return nil, fmt.Errorf("No such exec instance '%s' found in daemon", name)
|
2014-09-15 22:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Daemon) unregisterExecCommand(execConfig *execConfig) {
|
|
|
|
execConfig.Container.execCommands.Delete(execConfig.ID)
|
|
|
|
d.execCommands.Delete(execConfig.ID)
|
|
|
|
}
|
2014-09-09 04:19:32 +00:00
|
|
|
|
2014-09-15 22:56:47 +00:00
|
|
|
func (d *Daemon) getActiveContainer(name string) (*Container, error) {
|
2014-12-16 23:06:35 +00:00
|
|
|
container, err := d.Get(name)
|
|
|
|
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() {
|
2014-09-25 21:23:27 +00:00
|
|
|
return nil, fmt.Errorf("Container %s is not running", name)
|
2014-09-09 04:19:32 +00:00
|
|
|
}
|
2014-11-27 16:08:39 +00:00
|
|
|
if container.IsPaused() {
|
|
|
|
return nil, fmt.Errorf("Container %s is paused, unpause the container before exec", name)
|
|
|
|
}
|
2014-09-15 22:56:47 +00:00
|
|
|
return container, nil
|
|
|
|
}
|
2014-09-09 04:19:32 +00:00
|
|
|
|
2015-04-17 05:36:23 +00:00
|
|
|
func (d *Daemon) ContainerExecCreate(config *runconfig.ExecConfig) (string, error) {
|
2015-04-24 18:12:56 +00:00
|
|
|
// Not all drivers support Exec (LXC for example)
|
|
|
|
if err := checkExecSupport(d.execDriver.Name()); err != nil {
|
|
|
|
return "", err
|
2014-09-09 04:19:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 05:36:23 +00:00
|
|
|
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-04-11 00:05:21 +00:00
|
|
|
cmd := runconfig.NewCommand(config.Cmd...)
|
|
|
|
entrypoint, args := d.getEntrypointAndArgs(runconfig.NewEntrypoint(), cmd)
|
2014-09-09 04:19:32 +00:00
|
|
|
|
2015-06-26 22:06:37 +00:00
|
|
|
user := config.User
|
|
|
|
if len(user) == 0 {
|
|
|
|
user = container.Config.User
|
|
|
|
}
|
|
|
|
|
2015-07-08 17:55:42 +00:00
|
|
|
processConfig := &execdriver.ProcessConfig{
|
2014-09-09 04:19:32 +00:00
|
|
|
Tty: config.Tty,
|
|
|
|
Entrypoint: entrypoint,
|
|
|
|
Arguments: args,
|
2015-06-26 22:06:37 +00:00
|
|
|
User: user,
|
2015-06-19 06:01:50 +00:00
|
|
|
Privileged: config.Privileged,
|
2014-09-09 04:19:32 +00:00
|
|
|
}
|
|
|
|
|
2014-09-15 22:56:47 +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,
|
2014-09-09 04:19:32 +00:00
|
|
|
StreamConfig: StreamConfig{},
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
d.registerExecCommand(execConfig)
|
|
|
|
|
2015-05-24 18:19:39 +00:00
|
|
|
container.LogEvent("exec_create: " + execConfig.ProcessConfig.Entrypoint + " " + strings.Join(execConfig.ProcessConfig.Arguments, " "))
|
|
|
|
|
2015-04-17 05:36:23 +00:00
|
|
|
return execConfig.ID, nil
|
2014-09-15 22:56:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-17 05:36:23 +00:00
|
|
|
func (d *Daemon) ContainerExecStart(execName 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
|
|
|
|
)
|
|
|
|
|
2014-09-15 23:14:04 +00:00
|
|
|
execConfig, err := d.getExecConfig(execName)
|
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-09-15 22:56:47 +00:00
|
|
|
}
|
|
|
|
|
2014-09-15 23:14:04 +00:00
|
|
|
func() {
|
|
|
|
execConfig.Lock()
|
|
|
|
defer execConfig.Unlock()
|
|
|
|
if execConfig.Running {
|
|
|
|
err = fmt.Errorf("Error: Exec command %s is already running", execName)
|
|
|
|
}
|
|
|
|
execConfig.Running = true
|
|
|
|
}()
|
2014-09-15 22:56:47 +00:00
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-09-15 22:56:47 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("starting exec command %s in container %s", execConfig.ID, execConfig.Container.ID)
|
2014-09-15 22:56:47 +00:00
|
|
|
container := execConfig.Container
|
|
|
|
|
2014-11-14 11:34:59 +00:00
|
|
|
container.LogEvent("exec_start: " + execConfig.ProcessConfig.Entrypoint + " " + strings.Join(execConfig.ProcessConfig.Arguments, " "))
|
|
|
|
|
2014-09-15 22:56:47 +00:00
|
|
|
if execConfig.OpenStdin {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if execConfig.OpenStdout {
|
2015-04-17 05:36:23 +00:00
|
|
|
cStdout = stdout
|
2014-09-15 22:56:47 +00:00
|
|
|
}
|
|
|
|
if execConfig.OpenStderr {
|
2015-04-17 05:36:23 +00:00
|
|
|
cStderr = stderr
|
2014-09-09 04:19:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
execConfig.StreamConfig.stderr = broadcastwriter.New()
|
|
|
|
execConfig.StreamConfig.stdout = broadcastwriter.New()
|
|
|
|
// Attach to stdin
|
|
|
|
if execConfig.OpenStdin {
|
|
|
|
execConfig.StreamConfig.stdin, execConfig.StreamConfig.stdinPipe = io.Pipe()
|
|
|
|
} else {
|
|
|
|
execConfig.StreamConfig.stdinPipe = ioutils.NopWriteCloser(ioutil.Discard) // Silently drop stdin
|
|
|
|
}
|
|
|
|
|
2015-04-06 19:19:38 +00:00
|
|
|
attachErr := attach(&execConfig.StreamConfig, execConfig.OpenStdin, true, execConfig.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
|
|
|
|
2014-11-17 23:50:09 +00:00
|
|
|
// Note, the execConfig data will be removed when the container
|
|
|
|
// 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-04-26 16:50:25 +00:00
|
|
|
if err := container.Exec(execConfig); err != nil {
|
2014-09-15 22:56:47 +00:00
|
|
|
execErr <- fmt.Errorf("Cannot run exec command %s in container %s: %s", execName, container.ID, err)
|
2014-09-09 04:19:32 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case err := <-attachErr:
|
2014-09-10 07:20:05 +00:00
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return fmt.Errorf("attach failed with error: %s", 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-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() {
|
|
|
|
return fmt.Errorf("container stopped while running exec")
|
|
|
|
}
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-09-09 04:19:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 22:56:47 +00:00
|
|
|
func (d *Daemon) Exec(c *Container, execConfig *execConfig, pipes *execdriver.Pipes, startCallback execdriver.StartCallback) (int, error) {
|
2015-07-08 17:55:42 +00:00
|
|
|
exitStatus, err := d.execDriver.Exec(c.command, execConfig.ProcessConfig, pipes, startCallback)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
execConfig.ExitCode = exitStatus
|
|
|
|
execConfig.Running = false
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|