exec.go 9.9 KB

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