exec.go 8.9 KB

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