exec.go 9.8 KB

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