|
| 1 | +// build linux |
| 2 | + |
| 3 | +package daemon |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "io/ioutil" |
| 9 | + |
| 10 | + "github.com/docker/docker/daemon/execdriver" |
| 11 | + "github.com/docker/docker/engine" |
| 12 | + "github.com/docker/docker/pkg/broadcastwriter" |
| 13 | + "github.com/docker/docker/pkg/ioutils" |
| 14 | + "github.com/docker/docker/pkg/log" |
| 15 | + "github.com/docker/docker/runconfig" |
| 16 | + "github.com/docker/docker/utils" |
| 17 | +) |
| 18 | + |
| 19 | +type ExecConfig struct { |
| 20 | + ProcessConfig execdriver.ProcessConfig |
| 21 | + StreamConfig StreamConfig |
| 22 | + OpenStdin bool |
| 23 | +} |
| 24 | + |
| 25 | +func (d *Daemon) ContainerExec(job *engine.Job) engine.Status { |
| 26 | + if len(job.Args) != 1 { |
| 27 | + return job.Errorf("Usage: %s container_id command", job.Name) |
| 28 | + } |
| 29 | + |
| 30 | + var ( |
| 31 | + cStdin io.ReadCloser |
| 32 | + cStdout, cStderr io.Writer |
| 33 | + cStdinCloser io.Closer |
| 34 | + name = job.Args[0] |
| 35 | + ) |
| 36 | + |
| 37 | + container := d.Get(name) |
| 38 | + |
| 39 | + if container == nil { |
| 40 | + return job.Errorf("No such container: %s", name) |
| 41 | + } |
| 42 | + |
| 43 | + if !container.State.IsRunning() { |
| 44 | + return job.Errorf("Container %s is not not running", name) |
| 45 | + } |
| 46 | + |
| 47 | + config := runconfig.ExecConfigFromJob(job) |
| 48 | + |
| 49 | + if config.AttachStdin { |
| 50 | + r, w := io.Pipe() |
| 51 | + go func() { |
| 52 | + defer w.Close() |
| 53 | + io.Copy(w, job.Stdin) |
| 54 | + }() |
| 55 | + cStdin = r |
| 56 | + cStdinCloser = job.Stdin |
| 57 | + } |
| 58 | + if config.AttachStdout { |
| 59 | + cStdout = job.Stdout |
| 60 | + } |
| 61 | + if config.AttachStderr { |
| 62 | + cStderr = job.Stderr |
| 63 | + } |
| 64 | + |
| 65 | + entrypoint, args := d.getEntrypointAndArgs(nil, config.Cmd) |
| 66 | + |
| 67 | + processConfig := execdriver.ProcessConfig{ |
| 68 | + Privileged: config.Privileged, |
| 69 | + User: config.User, |
| 70 | + Tty: config.Tty, |
| 71 | + Entrypoint: entrypoint, |
| 72 | + Arguments: args, |
| 73 | + } |
| 74 | + |
| 75 | + execConfig := &ExecConfig{ |
| 76 | + OpenStdin: config.AttachStdin, |
| 77 | + StreamConfig: StreamConfig{}, |
| 78 | + ProcessConfig: processConfig, |
| 79 | + } |
| 80 | + |
| 81 | + execConfig.StreamConfig.stderr = broadcastwriter.New() |
| 82 | + execConfig.StreamConfig.stdout = broadcastwriter.New() |
| 83 | + // Attach to stdin |
| 84 | + if execConfig.OpenStdin { |
| 85 | + execConfig.StreamConfig.stdin, execConfig.StreamConfig.stdinPipe = io.Pipe() |
| 86 | + } else { |
| 87 | + execConfig.StreamConfig.stdinPipe = ioutils.NopWriteCloser(ioutil.Discard) // Silently drop stdin |
| 88 | + } |
| 89 | + |
| 90 | + var execErr, attachErr chan error |
| 91 | + go func() { |
| 92 | + attachErr = d.Attach(&execConfig.StreamConfig, config.AttachStdin, false, config.Tty, cStdin, cStdinCloser, cStdout, cStderr) |
| 93 | + }() |
| 94 | + |
| 95 | + go func() { |
| 96 | + err := container.Exec(execConfig) |
| 97 | + if err != nil { |
| 98 | + err = fmt.Errorf("Cannot run in container %s: %s", name, err) |
| 99 | + } |
| 100 | + execErr <- err |
| 101 | + }() |
| 102 | + |
| 103 | + select { |
| 104 | + case err := <-attachErr: |
| 105 | + return job.Errorf("attach failed with error: %s", err) |
| 106 | + case err := <-execErr: |
| 107 | + return job.Error(err) |
| 108 | + } |
| 109 | + |
| 110 | + return engine.StatusOK |
| 111 | +} |
| 112 | + |
| 113 | +func (daemon *Daemon) Exec(c *Container, execConfig *ExecConfig, pipes *execdriver.Pipes, startCallback execdriver.StartCallback) (int, error) { |
| 114 | + return daemon.execDriver.Exec(c.command, &execConfig.ProcessConfig, pipes, startCallback) |
| 115 | +} |
| 116 | + |
| 117 | +func (container *Container) Exec(execConfig *ExecConfig) error { |
| 118 | + container.Lock() |
| 119 | + defer container.Unlock() |
| 120 | + |
| 121 | + waitStart := make(chan struct{}) |
| 122 | + |
| 123 | + callback := func(processConfig *execdriver.ProcessConfig, pid int) { |
| 124 | + if processConfig.Tty { |
| 125 | + // The callback is called after the process Start() |
| 126 | + // so we are in the parent process. In TTY mode, stdin/out/err is the PtySlace |
| 127 | + // which we close here. |
| 128 | + if c, ok := processConfig.Stdout.(io.Closer); ok { |
| 129 | + c.Close() |
| 130 | + } |
| 131 | + } |
| 132 | + close(waitStart) |
| 133 | + } |
| 134 | + |
| 135 | + // We use a callback here instead of a goroutine and an chan for |
| 136 | + // syncronization purposes |
| 137 | + cErr := utils.Go(func() error { return container.monitorExec(execConfig, callback) }) |
| 138 | + |
| 139 | + // Exec should not return until the process is actually running |
| 140 | + select { |
| 141 | + case <-waitStart: |
| 142 | + case err := <-cErr: |
| 143 | + return err |
| 144 | + } |
| 145 | + |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +func (container *Container) monitorExec(execConfig *ExecConfig, callback execdriver.StartCallback) error { |
| 150 | + var ( |
| 151 | + err error |
| 152 | + exitCode int |
| 153 | + ) |
| 154 | + |
| 155 | + pipes := execdriver.NewPipes(execConfig.StreamConfig.stdin, execConfig.StreamConfig.stdout, execConfig.StreamConfig.stderr, execConfig.OpenStdin) |
| 156 | + exitCode, err = container.daemon.Exec(container, execConfig, pipes, callback) |
| 157 | + if err != nil { |
| 158 | + log.Errorf("Error running command in existing container %s: %s", container.ID, err) |
| 159 | + } |
| 160 | + |
| 161 | + log.Debugf("Exec task in container %s exited with code %d", container.ID, exitCode) |
| 162 | + if execConfig.OpenStdin { |
| 163 | + if err := execConfig.StreamConfig.stdin.Close(); err != nil { |
| 164 | + log.Errorf("Error closing stdin while running in %s: %s", container.ID, err) |
| 165 | + } |
| 166 | + } |
| 167 | + if err := execConfig.StreamConfig.stdout.Clean(); err != nil { |
| 168 | + log.Errorf("Error closing stdout while running in %s: %s", container.ID, err) |
| 169 | + } |
| 170 | + if err := execConfig.StreamConfig.stderr.Clean(); err != nil { |
| 171 | + log.Errorf("Error closing stderr while running in %s: %s", container.ID, err) |
| 172 | + } |
| 173 | + if execConfig.ProcessConfig.Terminal != nil { |
| 174 | + if err := execConfig.ProcessConfig.Terminal.Close(); err != nil { |
| 175 | + log.Errorf("Error closing terminal while running in container %s: %s", container.ID, err) |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + return err |
| 180 | +} |
0 commit comments