container_operations_unix.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // +build linux freebsd
  2. package daemon // import "github.com/docker/docker/daemon"
  3. import (
  4. "context"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "strconv"
  10. "time"
  11. "github.com/docker/docker/container"
  12. "github.com/docker/docker/daemon/links"
  13. "github.com/docker/docker/errdefs"
  14. "github.com/docker/docker/pkg/idtools"
  15. "github.com/docker/docker/pkg/mount"
  16. "github.com/docker/docker/pkg/stringid"
  17. "github.com/docker/docker/runconfig"
  18. "github.com/docker/libnetwork"
  19. "github.com/docker/swarmkit/template"
  20. "github.com/opencontainers/selinux/go-selinux/label"
  21. "github.com/pkg/errors"
  22. "github.com/sirupsen/logrus"
  23. "golang.org/x/sys/unix"
  24. )
  25. func (daemon *Daemon) setupLinkedContainers(container *container.Container) ([]string, error) {
  26. var env []string
  27. children := daemon.children(container)
  28. bridgeSettings := container.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
  29. if bridgeSettings == nil || bridgeSettings.EndpointSettings == nil {
  30. return nil, nil
  31. }
  32. for linkAlias, child := range children {
  33. if !child.IsRunning() {
  34. return nil, fmt.Errorf("Cannot link to a non running container: %s AS %s", child.Name, linkAlias)
  35. }
  36. childBridgeSettings := child.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
  37. if childBridgeSettings == nil || childBridgeSettings.EndpointSettings == nil {
  38. return nil, fmt.Errorf("container %s not attached to default bridge network", child.ID)
  39. }
  40. link := links.NewLink(
  41. bridgeSettings.IPAddress,
  42. childBridgeSettings.IPAddress,
  43. linkAlias,
  44. child.Config.Env,
  45. child.Config.ExposedPorts,
  46. )
  47. env = append(env, link.ToEnv()...)
  48. }
  49. return env, nil
  50. }
  51. func (daemon *Daemon) getIpcContainer(id string) (*container.Container, error) {
  52. errMsg := "can't join IPC of container " + id
  53. // Check the container exists
  54. container, err := daemon.GetContainer(id)
  55. if err != nil {
  56. return nil, errors.Wrap(err, errMsg)
  57. }
  58. // Check the container is running and not restarting
  59. if err := daemon.checkContainer(container, containerIsRunning, containerIsNotRestarting); err != nil {
  60. return nil, errors.Wrap(err, errMsg)
  61. }
  62. // Check the container ipc is shareable
  63. if st, err := os.Stat(container.ShmPath); err != nil || !st.IsDir() {
  64. if err == nil || os.IsNotExist(err) {
  65. return nil, errors.New(errMsg + ": non-shareable IPC")
  66. }
  67. // stat() failed?
  68. return nil, errors.Wrap(err, errMsg+": unexpected error from stat "+container.ShmPath)
  69. }
  70. return container, nil
  71. }
  72. func (daemon *Daemon) getPidContainer(container *container.Container) (*container.Container, error) {
  73. containerID := container.HostConfig.PidMode.Container()
  74. container, err := daemon.GetContainer(containerID)
  75. if err != nil {
  76. return nil, errors.Wrapf(err, "cannot join PID of a non running container: %s", containerID)
  77. }
  78. return container, daemon.checkContainer(container, containerIsRunning, containerIsNotRestarting)
  79. }
  80. func containerIsRunning(c *container.Container) error {
  81. if !c.IsRunning() {
  82. return errdefs.Conflict(errors.Errorf("container %s is not running", c.ID))
  83. }
  84. return nil
  85. }
  86. func containerIsNotRestarting(c *container.Container) error {
  87. if c.IsRestarting() {
  88. return errContainerIsRestarting(c.ID)
  89. }
  90. return nil
  91. }
  92. func (daemon *Daemon) setupIpcDirs(c *container.Container) error {
  93. ipcMode := c.HostConfig.IpcMode
  94. switch {
  95. case ipcMode.IsContainer():
  96. ic, err := daemon.getIpcContainer(ipcMode.Container())
  97. if err != nil {
  98. return err
  99. }
  100. c.ShmPath = ic.ShmPath
  101. case ipcMode.IsHost():
  102. if _, err := os.Stat("/dev/shm"); err != nil {
  103. return fmt.Errorf("/dev/shm is not mounted, but must be for --ipc=host")
  104. }
  105. c.ShmPath = "/dev/shm"
  106. case ipcMode.IsPrivate(), ipcMode.IsNone():
  107. // c.ShmPath will/should not be used, so make it empty.
  108. // Container's /dev/shm mount comes from OCI spec.
  109. c.ShmPath = ""
  110. case ipcMode.IsEmpty():
  111. // A container was created by an older version of the daemon.
  112. // The default behavior used to be what is now called "shareable".
  113. fallthrough
  114. case ipcMode.IsShareable():
  115. rootIDs := daemon.idMappings.RootPair()
  116. if !c.HasMountFor("/dev/shm") {
  117. shmPath, err := c.ShmResourcePath()
  118. if err != nil {
  119. return err
  120. }
  121. if err := idtools.MkdirAllAndChown(shmPath, 0700, rootIDs); err != nil {
  122. return err
  123. }
  124. shmproperty := "mode=1777,size=" + strconv.FormatInt(c.HostConfig.ShmSize, 10)
  125. if err := unix.Mount("shm", shmPath, "tmpfs", uintptr(unix.MS_NOEXEC|unix.MS_NOSUID|unix.MS_NODEV), label.FormatMountLabel(shmproperty, c.GetMountLabel())); err != nil {
  126. return fmt.Errorf("mounting shm tmpfs: %s", err)
  127. }
  128. if err := os.Chown(shmPath, rootIDs.UID, rootIDs.GID); err != nil {
  129. return err
  130. }
  131. c.ShmPath = shmPath
  132. }
  133. default:
  134. return fmt.Errorf("invalid IPC mode: %v", ipcMode)
  135. }
  136. return nil
  137. }
  138. func (daemon *Daemon) setupSecretDir(c *container.Container, hasSecretDir *bool) (setupErr error) {
  139. if len(c.SecretReferences) == 0 {
  140. return nil
  141. }
  142. if !*hasSecretDir {
  143. if err := daemon.createSecretDir(c); err != nil {
  144. return err
  145. }
  146. *hasSecretDir = true
  147. }
  148. if c.DependencyStore == nil {
  149. return fmt.Errorf("secret store is not initialized")
  150. }
  151. // retrieve possible remapped range start for root UID, GID
  152. rootIDs := daemon.idMappings.RootPair()
  153. for _, s := range c.SecretReferences {
  154. // TODO (ehazlett): use type switch when more are supported
  155. if s.File == nil {
  156. logrus.Error("secret target type is not a file target")
  157. continue
  158. }
  159. // secrets are created in the SecretMountPath on the host, at a
  160. // single level
  161. fPath, err := c.SecretFilePath(*s)
  162. if err != nil {
  163. return errors.Wrap(err, "error getting secret file path")
  164. }
  165. if err := idtools.MkdirAllAndChown(filepath.Dir(fPath), 0700, rootIDs); err != nil {
  166. return errors.Wrap(err, "error creating secret mount path")
  167. }
  168. logrus.WithFields(logrus.Fields{
  169. "name": s.File.Name,
  170. "path": fPath,
  171. }).Debug("injecting secret")
  172. secret, err := c.DependencyStore.Secrets().Get(s.SecretID)
  173. if err != nil {
  174. return errors.Wrap(err, "unable to get secret from secret store")
  175. }
  176. if err := ioutil.WriteFile(fPath, secret.Spec.Data, s.File.Mode); err != nil {
  177. return errors.Wrap(err, "error injecting secret")
  178. }
  179. uid, err := strconv.Atoi(s.File.UID)
  180. if err != nil {
  181. return err
  182. }
  183. gid, err := strconv.Atoi(s.File.GID)
  184. if err != nil {
  185. return err
  186. }
  187. if err := os.Chown(fPath, rootIDs.UID+uid, rootIDs.GID+gid); err != nil {
  188. return errors.Wrap(err, "error setting ownership for secret")
  189. }
  190. if err := os.Chmod(fPath, s.File.Mode); err != nil {
  191. return errors.Wrap(err, "error setting file mode for secret")
  192. }
  193. }
  194. return nil
  195. }
  196. func (daemon *Daemon) createSecretDir(c *container.Container) error {
  197. localMountPath, err := c.SecretMountPath()
  198. if err != nil {
  199. return err
  200. }
  201. logrus.Debugf("secrets: setting up secret dir: %s", localMountPath)
  202. // retrieve possible remapped range start for root UID, GID
  203. rootIDs := daemon.idMappings.RootPair()
  204. // create tmpfs
  205. if err := idtools.MkdirAllAndChown(localMountPath, 0700, rootIDs); err != nil {
  206. return errors.Wrap(err, "error creating secret local mount path")
  207. }
  208. tmpfsOwnership := fmt.Sprintf("uid=%d,gid=%d", rootIDs.UID, rootIDs.GID)
  209. if err := mount.Mount("tmpfs", localMountPath, "tmpfs", "nodev,nosuid,noexec,"+tmpfsOwnership); err != nil {
  210. return errors.Wrap(err, "unable to setup secret mount")
  211. }
  212. return nil
  213. }
  214. func (daemon *Daemon) remountSecretDir(c *container.Container) error {
  215. localMountPath, err := c.SecretMountPath()
  216. if err != nil {
  217. return err
  218. }
  219. label.Relabel(localMountPath, c.MountLabel, false)
  220. rootIDs := daemon.idMappings.RootPair()
  221. tmpfsOwnership := fmt.Sprintf("uid=%d,gid=%d", rootIDs.UID, rootIDs.GID)
  222. // remount secrets ro
  223. if err := mount.Mount("tmpfs", localMountPath, "tmpfs", "remount,ro,"+tmpfsOwnership); err != nil {
  224. return errors.Wrap(err, "unable to remount secret dir as readonly")
  225. }
  226. return nil
  227. }
  228. func (daemon *Daemon) cleanupSecretDir(c *container.Container) {
  229. localMountPath, err := c.SecretMountPath()
  230. if err != nil {
  231. logrus.WithError(err).WithField("container", c.ID).Errorf("error getting secrets mounth path for cleanup")
  232. }
  233. detachMounted(localMountPath)
  234. if err := os.RemoveAll(localMountPath); err != nil {
  235. logrus.Errorf("error cleaning up secret mount: %s", err)
  236. }
  237. }
  238. func (daemon *Daemon) setupConfigDir(c *container.Container, hasSecretDir *bool) (setupErr error) {
  239. if len(c.ConfigReferences) == 0 {
  240. return nil
  241. }
  242. localPath, err := c.ConfigsDirPath()
  243. if err != nil {
  244. return err
  245. }
  246. logrus.Debugf("configs: setting up config dir: %s", localPath)
  247. // retrieve possible remapped range start for root UID, GID
  248. rootIDs := daemon.idMappings.RootPair()
  249. // create tmpfs
  250. if err := idtools.MkdirAllAndChown(localPath, 0700, rootIDs); err != nil {
  251. return errors.Wrap(err, "error creating config dir")
  252. }
  253. defer func() {
  254. if setupErr != nil {
  255. if err := os.RemoveAll(localPath); err != nil {
  256. logrus.Errorf("error cleaning up config dir: %s", err)
  257. }
  258. }
  259. }()
  260. if c.DependencyStore == nil {
  261. return fmt.Errorf("config store is not initialized")
  262. }
  263. for _, configRef := range c.ConfigReferences {
  264. // TODO (ehazlett): use type switch when more are supported
  265. if configRef.File == nil {
  266. logrus.Error("config target type is not a file target")
  267. continue
  268. }
  269. getter := c.DependencyStore.Configs().(template.TemplatedConfigGetter)
  270. config, sensitive, err := getter.GetAndFlagSecretData(configRef.ConfigID)
  271. if err != nil {
  272. return errors.Wrap(err, "unable to get config from config store")
  273. }
  274. var fPath string
  275. if sensitive {
  276. configRef.Sensitive = true
  277. fPath, err = c.SensitiveConfigFilePath(*configRef.ConfigReference)
  278. if !*hasSecretDir {
  279. if err := daemon.createSecretDir(c); err != nil {
  280. return err
  281. }
  282. *hasSecretDir = true
  283. }
  284. } else {
  285. fPath, err = c.ConfigFilePath(*configRef.ConfigReference)
  286. }
  287. if err != nil {
  288. return errors.Wrap(err, "error getting config file path")
  289. }
  290. log := logrus.WithFields(logrus.Fields{"name": configRef.File.Name, "path": fPath})
  291. log.Debug("injecting config")
  292. if err := idtools.MkdirAllAndChown(filepath.Dir(fPath), 0700, rootIDs); err != nil {
  293. return errors.Wrap(err, "error creating config path")
  294. }
  295. if err := ioutil.WriteFile(fPath, config.Spec.Data, configRef.File.Mode); err != nil {
  296. return errors.Wrap(err, "error injecting config")
  297. }
  298. uid, err := strconv.Atoi(configRef.File.UID)
  299. if err != nil {
  300. return err
  301. }
  302. gid, err := strconv.Atoi(configRef.File.GID)
  303. if err != nil {
  304. return err
  305. }
  306. if err := os.Chown(fPath, rootIDs.UID+uid, rootIDs.GID+gid); err != nil {
  307. return errors.Wrap(err, "error setting ownership for config")
  308. }
  309. if err := os.Chmod(fPath, configRef.File.Mode); err != nil {
  310. return errors.Wrap(err, "error setting file mode for config")
  311. }
  312. label.Relabel(fPath, c.MountLabel, false)
  313. }
  314. return nil
  315. }
  316. func killProcessDirectly(cntr *container.Container) error {
  317. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  318. defer cancel()
  319. // Block until the container to stops or timeout.
  320. status := <-cntr.Wait(ctx, container.WaitConditionNotRunning)
  321. if status.Err() != nil {
  322. // Ensure that we don't kill ourselves
  323. if pid := cntr.GetPID(); pid != 0 {
  324. logrus.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", stringid.TruncateID(cntr.ID))
  325. if err := unix.Kill(pid, 9); err != nil {
  326. if err != unix.ESRCH {
  327. return err
  328. }
  329. e := errNoSuchProcess{pid, 9}
  330. logrus.Debug(e)
  331. return e
  332. }
  333. }
  334. }
  335. return nil
  336. }
  337. func detachMounted(path string) error {
  338. return unix.Unmount(path, unix.MNT_DETACH)
  339. }
  340. func isLinkable(child *container.Container) bool {
  341. // A container is linkable only if it belongs to the default network
  342. _, ok := child.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
  343. return ok
  344. }
  345. func enableIPOnPredefinedNetwork() bool {
  346. return false
  347. }
  348. func (daemon *Daemon) isNetworkHotPluggable() bool {
  349. return true
  350. }
  351. func setupPathsAndSandboxOptions(container *container.Container, sboxOptions *[]libnetwork.SandboxOption) error {
  352. var err error
  353. container.HostsPath, err = container.GetRootResourcePath("hosts")
  354. if err != nil {
  355. return err
  356. }
  357. *sboxOptions = append(*sboxOptions, libnetwork.OptionHostsPath(container.HostsPath))
  358. container.ResolvConfPath, err = container.GetRootResourcePath("resolv.conf")
  359. if err != nil {
  360. return err
  361. }
  362. *sboxOptions = append(*sboxOptions, libnetwork.OptionResolvConfPath(container.ResolvConfPath))
  363. return nil
  364. }
  365. func (daemon *Daemon) initializeNetworkingPaths(container *container.Container, nc *container.Container) error {
  366. container.HostnamePath = nc.HostnamePath
  367. container.HostsPath = nc.HostsPath
  368. container.ResolvConfPath = nc.ResolvConfPath
  369. return nil
  370. }
  371. func (daemon *Daemon) setupContainerMountsRoot(c *container.Container) error {
  372. // get the root mount path so we can make it unbindable
  373. p, err := c.MountsResourcePath("")
  374. if err != nil {
  375. return err
  376. }
  377. if err := idtools.MkdirAllAndChown(p, 0700, daemon.idMappings.RootPair()); err != nil {
  378. return err
  379. }
  380. if err := mount.MakeUnbindable(p); err != nil {
  381. // Setting unbindable is a precaution and is not neccessary for correct operation.
  382. // Do not error out if this fails.
  383. logrus.WithError(err).WithField("resource", p).WithField("container", c.ID).Warn("Error setting container resource mounts to unbindable, this may cause mount leakages, preventing removal of this container.")
  384. }
  385. return nil
  386. }