exec.go 8.6 KB

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