exec.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // build linux
  2. package daemon
  3. import (
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "strings"
  8. "sync"
  9. log "github.com/Sirupsen/logrus"
  10. "github.com/docker/docker/daemon/execdriver"
  11. "github.com/docker/docker/daemon/execdriver/lxc"
  12. "github.com/docker/docker/engine"
  13. "github.com/docker/docker/pkg/broadcastwriter"
  14. "github.com/docker/docker/pkg/ioutils"
  15. "github.com/docker/docker/pkg/promise"
  16. "github.com/docker/docker/runconfig"
  17. "github.com/docker/docker/utils"
  18. )
  19. type execConfig struct {
  20. sync.Mutex
  21. ID string
  22. Running bool
  23. ExitCode int
  24. ProcessConfig execdriver.ProcessConfig
  25. StreamConfig
  26. OpenStdin bool
  27. OpenStderr bool
  28. OpenStdout bool
  29. Container *Container
  30. }
  31. type execStore struct {
  32. s map[string]*execConfig
  33. sync.Mutex
  34. }
  35. func newExecStore() *execStore {
  36. return &execStore{s: make(map[string]*execConfig, 0)}
  37. }
  38. func (e *execStore) Add(id string, execConfig *execConfig) {
  39. e.Lock()
  40. e.s[id] = execConfig
  41. e.Unlock()
  42. }
  43. func (e *execStore) Get(id string) *execConfig {
  44. e.Lock()
  45. res := e.s[id]
  46. e.Unlock()
  47. return res
  48. }
  49. func (e *execStore) Delete(id string) {
  50. e.Lock()
  51. delete(e.s, id)
  52. e.Unlock()
  53. }
  54. func (execConfig *execConfig) Resize(h, w int) error {
  55. return execConfig.ProcessConfig.Terminal.Resize(h, w)
  56. }
  57. func (d *Daemon) registerExecCommand(execConfig *execConfig) {
  58. // Storing execs in container inorder to kill them gracefully whenever the container is stopped or removed.
  59. execConfig.Container.execCommands.Add(execConfig.ID, execConfig)
  60. // Storing execs in daemon for easy access via remote API.
  61. d.execCommands.Add(execConfig.ID, execConfig)
  62. }
  63. func (d *Daemon) getExecConfig(name string) (*execConfig, error) {
  64. if execConfig := d.execCommands.Get(name); execConfig != nil {
  65. if !execConfig.Container.IsRunning() {
  66. return nil, fmt.Errorf("Container %s is not running", execConfig.Container.ID)
  67. }
  68. return execConfig, nil
  69. }
  70. return nil, fmt.Errorf("No such exec instance '%s' found in daemon", name)
  71. }
  72. func (d *Daemon) unregisterExecCommand(execConfig *execConfig) {
  73. execConfig.Container.execCommands.Delete(execConfig.ID)
  74. d.execCommands.Delete(execConfig.ID)
  75. }
  76. func (d *Daemon) getActiveContainer(name string) (*Container, error) {
  77. container := d.Get(name)
  78. if container == nil {
  79. return nil, fmt.Errorf("No such container: %s", name)
  80. }
  81. if !container.IsRunning() {
  82. return nil, fmt.Errorf("Container %s is not running", name)
  83. }
  84. if container.IsPaused() {
  85. return nil, fmt.Errorf("Container %s is paused, unpause the container before exec", name)
  86. }
  87. return container, nil
  88. }
  89. func (d *Daemon) ContainerExecCreate(job *engine.Job) engine.Status {
  90. if len(job.Args) != 1 {
  91. return job.Errorf("Usage: %s [options] container command [args]", job.Name)
  92. }
  93. if strings.HasPrefix(d.execDriver.Name(), lxc.DriverName) {
  94. return job.Error(lxc.ErrExec)
  95. }
  96. var name = job.Args[0]
  97. container, err := d.getActiveContainer(name)
  98. if err != nil {
  99. return job.Error(err)
  100. }
  101. config, err := runconfig.ExecConfigFromJob(job)
  102. if err != nil {
  103. return job.Error(err)
  104. }
  105. entrypoint, args := d.getEntrypointAndArgs(nil, config.Cmd)
  106. processConfig := execdriver.ProcessConfig{
  107. Tty: config.Tty,
  108. Entrypoint: entrypoint,
  109. Arguments: args,
  110. }
  111. execConfig := &execConfig{
  112. ID: utils.GenerateRandomID(),
  113. OpenStdin: config.AttachStdin,
  114. OpenStdout: config.AttachStdout,
  115. OpenStderr: config.AttachStderr,
  116. StreamConfig: StreamConfig{},
  117. ProcessConfig: processConfig,
  118. Container: container,
  119. Running: false,
  120. }
  121. d.registerExecCommand(execConfig)
  122. job.Printf("%s\n", execConfig.ID)
  123. return engine.StatusOK
  124. }
  125. func (d *Daemon) ContainerExecStart(job *engine.Job) engine.Status {
  126. if len(job.Args) != 1 {
  127. return job.Errorf("Usage: %s [options] exec", job.Name)
  128. }
  129. var (
  130. cStdin io.ReadCloser
  131. cStdout, cStderr io.Writer
  132. execName = job.Args[0]
  133. )
  134. execConfig, err := d.getExecConfig(execName)
  135. if err != nil {
  136. return job.Error(err)
  137. }
  138. func() {
  139. execConfig.Lock()
  140. defer execConfig.Unlock()
  141. if execConfig.Running {
  142. err = fmt.Errorf("Error: Exec command %s is already running", execName)
  143. }
  144. execConfig.Running = true
  145. }()
  146. if err != nil {
  147. return job.Error(err)
  148. }
  149. log.Debugf("starting exec command %s in container %s", execConfig.ID, execConfig.Container.ID)
  150. container := execConfig.Container
  151. if execConfig.OpenStdin {
  152. r, w := io.Pipe()
  153. go func() {
  154. defer w.Close()
  155. defer log.Debugf("Closing buffered stdin pipe")
  156. io.Copy(w, job.Stdin)
  157. }()
  158. cStdin = r
  159. }
  160. if execConfig.OpenStdout {
  161. cStdout = job.Stdout
  162. }
  163. if execConfig.OpenStderr {
  164. cStderr = job.Stderr
  165. }
  166. execConfig.StreamConfig.stderr = broadcastwriter.New()
  167. execConfig.StreamConfig.stdout = broadcastwriter.New()
  168. // Attach to stdin
  169. if execConfig.OpenStdin {
  170. execConfig.StreamConfig.stdin, execConfig.StreamConfig.stdinPipe = io.Pipe()
  171. } else {
  172. execConfig.StreamConfig.stdinPipe = ioutils.NopWriteCloser(ioutil.Discard) // Silently drop stdin
  173. }
  174. attachErr := d.attach(&execConfig.StreamConfig, execConfig.OpenStdin, true, execConfig.ProcessConfig.Tty, cStdin, cStdout, cStderr)
  175. execErr := make(chan error)
  176. // Note, the execConfig data will be removed when the container
  177. // itself is deleted. This allows us to query it (for things like
  178. // the exitStatus) even after the cmd is done running.
  179. go func() {
  180. err := container.Exec(execConfig)
  181. if err != nil {
  182. execErr <- fmt.Errorf("Cannot run exec command %s in container %s: %s", execName, container.ID, err)
  183. }
  184. }()
  185. select {
  186. case err := <-attachErr:
  187. if err != nil {
  188. return job.Errorf("attach failed with error: %s", err)
  189. }
  190. break
  191. case err := <-execErr:
  192. return job.Error(err)
  193. }
  194. return engine.StatusOK
  195. }
  196. func (d *Daemon) Exec(c *Container, execConfig *execConfig, pipes *execdriver.Pipes, startCallback execdriver.StartCallback) (int, error) {
  197. exitStatus, err := d.execDriver.Exec(c.command, &execConfig.ProcessConfig, pipes, startCallback)
  198. // On err, make sure we don't leave ExitCode at zero
  199. if err != nil && exitStatus == 0 {
  200. exitStatus = 128
  201. }
  202. execConfig.ExitCode = exitStatus
  203. execConfig.Running = false
  204. return exitStatus, err
  205. }
  206. func (container *Container) Exec(execConfig *execConfig) error {
  207. container.Lock()
  208. defer container.Unlock()
  209. waitStart := make(chan struct{})
  210. callback := func(processConfig *execdriver.ProcessConfig, pid int) {
  211. if processConfig.Tty {
  212. // The callback is called after the process Start()
  213. // so we are in the parent process. In TTY mode, stdin/out/err is the PtySlave
  214. // which we close here.
  215. if c, ok := processConfig.Stdout.(io.Closer); ok {
  216. c.Close()
  217. }
  218. }
  219. close(waitStart)
  220. }
  221. // We use a callback here instead of a goroutine and an chan for
  222. // syncronization purposes
  223. cErr := promise.Go(func() error { return container.monitorExec(execConfig, callback) })
  224. // Exec should not return until the process is actually running
  225. select {
  226. case <-waitStart:
  227. case err := <-cErr:
  228. return err
  229. }
  230. return nil
  231. }
  232. func (container *Container) monitorExec(execConfig *execConfig, callback execdriver.StartCallback) error {
  233. var (
  234. err error
  235. exitCode int
  236. )
  237. pipes := execdriver.NewPipes(execConfig.StreamConfig.stdin, execConfig.StreamConfig.stdout, execConfig.StreamConfig.stderr, execConfig.OpenStdin)
  238. exitCode, err = container.daemon.Exec(container, execConfig, pipes, callback)
  239. if err != nil {
  240. log.Errorf("Error running command in existing container %s: %s", container.ID, err)
  241. }
  242. log.Debugf("Exec task in container %s exited with code %d", container.ID, exitCode)
  243. if execConfig.OpenStdin {
  244. if err := execConfig.StreamConfig.stdin.Close(); err != nil {
  245. log.Errorf("Error closing stdin while running in %s: %s", container.ID, err)
  246. }
  247. }
  248. if err := execConfig.StreamConfig.stdout.Clean(); err != nil {
  249. log.Errorf("Error closing stdout while running in %s: %s", container.ID, err)
  250. }
  251. if err := execConfig.StreamConfig.stderr.Clean(); err != nil {
  252. log.Errorf("Error closing stderr while running in %s: %s", container.ID, err)
  253. }
  254. if execConfig.ProcessConfig.Terminal != nil {
  255. if err := execConfig.ProcessConfig.Terminal.Close(); err != nil {
  256. log.Errorf("Error closing terminal while running in container %s: %s", container.ID, err)
  257. }
  258. }
  259. return err
  260. }