exec.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package daemon
  2. import (
  3. "fmt"
  4. "io"
  5. "strings"
  6. "time"
  7. "golang.org/x/net/context"
  8. "github.com/Sirupsen/logrus"
  9. "github.com/docker/docker/container"
  10. "github.com/docker/docker/daemon/exec"
  11. "github.com/docker/docker/errors"
  12. "github.com/docker/docker/libcontainerd"
  13. "github.com/docker/docker/pkg/pools"
  14. "github.com/docker/docker/pkg/signal"
  15. "github.com/docker/docker/pkg/term"
  16. "github.com/docker/engine-api/types"
  17. "github.com/docker/engine-api/types/strslice"
  18. )
  19. // Seconds to wait after sending TERM before trying KILL
  20. const termProcessTimeout = 10
  21. func (d *Daemon) registerExecCommand(container *container.Container, config *exec.Config) {
  22. // Storing execs in container in order to kill them gracefully whenever the container is stopped or removed.
  23. container.ExecCommands.Add(config.ID, config)
  24. // Storing execs in daemon for easy access via remote API.
  25. d.execCommands.Add(config.ID, config)
  26. }
  27. // ExecExists looks up the exec instance and returns a bool if it exists or not.
  28. // It will also return the error produced by `getConfig`
  29. func (d *Daemon) ExecExists(name string) (bool, error) {
  30. if _, err := d.getExecConfig(name); err != nil {
  31. return false, err
  32. }
  33. return true, nil
  34. }
  35. // getExecConfig looks up the exec instance by name. If the container associated
  36. // with the exec instance is stopped or paused, it will return an error.
  37. func (d *Daemon) getExecConfig(name string) (*exec.Config, error) {
  38. ec := d.execCommands.Get(name)
  39. // If the exec is found but its container is not in the daemon's list of
  40. // containers then it must have been deleted, in which case instead of
  41. // saying the container isn't running, we should return a 404 so that
  42. // the user sees the same error now that they will after the
  43. // 5 minute clean-up loop is run which erases old/dead execs.
  44. if ec != nil {
  45. if container := d.containers.Get(ec.ContainerID); container != nil {
  46. if !container.IsRunning() {
  47. return nil, fmt.Errorf("Container %s is not running: %s", container.ID, container.State.String())
  48. }
  49. if container.IsPaused() {
  50. return nil, errExecPaused(container.ID)
  51. }
  52. if container.IsRestarting() {
  53. return nil, errContainerIsRestarting(container.ID)
  54. }
  55. return ec, nil
  56. }
  57. }
  58. return nil, errExecNotFound(name)
  59. }
  60. func (d *Daemon) unregisterExecCommand(container *container.Container, execConfig *exec.Config) {
  61. container.ExecCommands.Delete(execConfig.ID)
  62. d.execCommands.Delete(execConfig.ID)
  63. }
  64. func (d *Daemon) getActiveContainer(name string) (*container.Container, error) {
  65. container, err := d.GetContainer(name)
  66. if err != nil {
  67. return nil, err
  68. }
  69. if !container.IsRunning() {
  70. return nil, errNotRunning{container.ID}
  71. }
  72. if container.IsPaused() {
  73. return nil, errExecPaused(name)
  74. }
  75. if container.IsRestarting() {
  76. return nil, errContainerIsRestarting(container.ID)
  77. }
  78. return container, nil
  79. }
  80. // ContainerExecCreate sets up an exec in a running container.
  81. func (d *Daemon) ContainerExecCreate(name string, config *types.ExecConfig) (string, error) {
  82. container, err := d.getActiveContainer(name)
  83. if err != nil {
  84. return "", err
  85. }
  86. cmd := strslice.StrSlice(config.Cmd)
  87. entrypoint, args := d.getEntrypointAndArgs(strslice.StrSlice{}, cmd)
  88. keys := []byte{}
  89. if config.DetachKeys != "" {
  90. keys, err = term.ToBytes(config.DetachKeys)
  91. if err != nil {
  92. err = fmt.Errorf("Invalid escape keys (%s) provided", config.DetachKeys)
  93. return "", err
  94. }
  95. }
  96. execConfig := exec.NewConfig()
  97. execConfig.OpenStdin = config.AttachStdin
  98. execConfig.OpenStdout = config.AttachStdout
  99. execConfig.OpenStderr = config.AttachStderr
  100. execConfig.ContainerID = container.ID
  101. execConfig.DetachKeys = keys
  102. execConfig.Entrypoint = entrypoint
  103. execConfig.Args = args
  104. execConfig.Tty = config.Tty
  105. execConfig.Privileged = config.Privileged
  106. execConfig.User = config.User
  107. if len(execConfig.User) == 0 {
  108. execConfig.User = container.Config.User
  109. }
  110. d.registerExecCommand(container, execConfig)
  111. d.LogContainerEvent(container, "exec_create: "+execConfig.Entrypoint+" "+strings.Join(execConfig.Args, " "))
  112. return execConfig.ID, nil
  113. }
  114. // ContainerExecStart starts a previously set up exec instance. The
  115. // std streams are set up.
  116. // If ctx is cancelled, the process is terminated.
  117. func (d *Daemon) ContainerExecStart(ctx context.Context, name string, stdin io.ReadCloser, stdout io.Writer, stderr io.Writer) (err error) {
  118. var (
  119. cStdin io.ReadCloser
  120. cStdout, cStderr io.Writer
  121. )
  122. ec, err := d.getExecConfig(name)
  123. if err != nil {
  124. return errExecNotFound(name)
  125. }
  126. ec.Lock()
  127. if ec.ExitCode != nil {
  128. ec.Unlock()
  129. err := fmt.Errorf("Error: Exec command %s has already run", ec.ID)
  130. return errors.NewRequestConflictError(err)
  131. }
  132. if ec.Running {
  133. ec.Unlock()
  134. return fmt.Errorf("Error: Exec command %s is already running", ec.ID)
  135. }
  136. ec.Running = true
  137. defer func() {
  138. if err != nil {
  139. ec.Running = false
  140. exitCode := 126
  141. ec.ExitCode = &exitCode
  142. }
  143. }()
  144. ec.Unlock()
  145. c := d.containers.Get(ec.ContainerID)
  146. logrus.Debugf("starting exec command %s in container %s", ec.ID, c.ID)
  147. d.LogContainerEvent(c, "exec_start: "+ec.Entrypoint+" "+strings.Join(ec.Args, " "))
  148. if ec.OpenStdin && stdin != nil {
  149. r, w := io.Pipe()
  150. go func() {
  151. defer w.Close()
  152. defer logrus.Debug("Closing buffered stdin pipe")
  153. pools.Copy(w, stdin)
  154. }()
  155. cStdin = r
  156. }
  157. if ec.OpenStdout {
  158. cStdout = stdout
  159. }
  160. if ec.OpenStderr {
  161. cStderr = stderr
  162. }
  163. if ec.OpenStdin {
  164. ec.NewInputPipes()
  165. } else {
  166. ec.NewNopInputPipe()
  167. }
  168. p := libcontainerd.Process{
  169. Args: append([]string{ec.Entrypoint}, ec.Args...),
  170. Terminal: ec.Tty,
  171. }
  172. if err := execSetPlatformOpt(c, ec, &p); err != nil {
  173. return err
  174. }
  175. attachErr := container.AttachStreams(ctx, ec.StreamConfig, ec.OpenStdin, true, ec.Tty, cStdin, cStdout, cStderr, ec.DetachKeys)
  176. if err := d.containerd.AddProcess(ctx, c.ID, name, p); err != nil {
  177. return err
  178. }
  179. select {
  180. case <-ctx.Done():
  181. logrus.Debugf("Sending TERM signal to process %v in container %v", name, c.ID)
  182. d.containerd.SignalProcess(c.ID, name, int(signal.SignalMap["TERM"]))
  183. select {
  184. case <-time.After(termProcessTimeout * time.Second):
  185. logrus.Infof("Container %v, process %v failed to exit within %d seconds of signal TERM - using the force", c.ID, name, termProcessTimeout)
  186. d.containerd.SignalProcess(c.ID, name, int(signal.SignalMap["KILL"]))
  187. case <-attachErr:
  188. // TERM signal worked
  189. }
  190. return fmt.Errorf("context cancelled")
  191. case err := <-attachErr:
  192. if err != nil {
  193. if _, ok := err.(container.DetachError); !ok {
  194. return fmt.Errorf("exec attach failed with error: %v", err)
  195. }
  196. d.LogContainerEvent(c, "exec_detach")
  197. }
  198. }
  199. return nil
  200. }
  201. // execCommandGC runs a ticker to clean up the daemon references
  202. // of exec configs that are no longer part of the container.
  203. func (d *Daemon) execCommandGC() {
  204. for range time.Tick(5 * time.Minute) {
  205. var (
  206. cleaned int
  207. liveExecCommands = d.containerExecIds()
  208. )
  209. for id, config := range d.execCommands.Commands() {
  210. if config.CanRemove {
  211. cleaned++
  212. d.execCommands.Delete(id)
  213. } else {
  214. if _, exists := liveExecCommands[id]; !exists {
  215. config.CanRemove = true
  216. }
  217. }
  218. }
  219. if cleaned > 0 {
  220. logrus.Debugf("clean %d unused exec commands", cleaned)
  221. }
  222. }
  223. }
  224. // containerExecIds returns a list of all the current exec ids that are in use
  225. // and running inside a container.
  226. func (d *Daemon) containerExecIds() map[string]struct{} {
  227. ids := map[string]struct{}{}
  228. for _, c := range d.containers.List() {
  229. for _, id := range c.ExecCommands.List() {
  230. ids[id] = struct{}{}
  231. }
  232. }
  233. return ids
  234. }