container_operations_unix.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // +build linux freebsd
  2. package 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/pkg/idtools"
  14. "github.com/docker/docker/pkg/mount"
  15. "github.com/docker/docker/pkg/stringid"
  16. "github.com/docker/docker/runconfig"
  17. "github.com/docker/libnetwork"
  18. "github.com/opencontainers/selinux/go-selinux/label"
  19. "github.com/pkg/errors"
  20. "github.com/sirupsen/logrus"
  21. "golang.org/x/sys/unix"
  22. )
  23. func (daemon *Daemon) setupLinkedContainers(container *container.Container) ([]string, error) {
  24. var env []string
  25. children := daemon.children(container)
  26. bridgeSettings := container.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
  27. if bridgeSettings == nil || bridgeSettings.EndpointSettings == nil {
  28. return nil, nil
  29. }
  30. for linkAlias, child := range children {
  31. if !child.IsRunning() {
  32. return nil, fmt.Errorf("Cannot link to a non running container: %s AS %s", child.Name, linkAlias)
  33. }
  34. childBridgeSettings := child.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
  35. if childBridgeSettings == nil || childBridgeSettings.EndpointSettings == nil {
  36. return nil, fmt.Errorf("container %s not attached to default bridge network", child.ID)
  37. }
  38. link := links.NewLink(
  39. bridgeSettings.IPAddress,
  40. childBridgeSettings.IPAddress,
  41. linkAlias,
  42. child.Config.Env,
  43. child.Config.ExposedPorts,
  44. )
  45. env = append(env, link.ToEnv()...)
  46. }
  47. return env, nil
  48. }
  49. func (daemon *Daemon) getIpcContainer(container *container.Container) (*container.Container, error) {
  50. containerID := container.HostConfig.IpcMode.Container()
  51. container, err := daemon.GetContainer(containerID)
  52. if err != nil {
  53. return nil, errors.Wrapf(err, "cannot join IPC of a non running container: %s", container.ID)
  54. }
  55. return container, daemon.checkContainer(container, containerIsRunning, containerIsNotRestarting)
  56. }
  57. func (daemon *Daemon) getPidContainer(container *container.Container) (*container.Container, error) {
  58. containerID := container.HostConfig.PidMode.Container()
  59. container, err := daemon.GetContainer(containerID)
  60. if err != nil {
  61. return nil, errors.Wrapf(err, "cannot join PID of a non running container: %s", container.ID)
  62. }
  63. return container, daemon.checkContainer(container, containerIsRunning, containerIsNotRestarting)
  64. }
  65. func containerIsRunning(c *container.Container) error {
  66. if !c.IsRunning() {
  67. return errors.Errorf("container %s is not running", c.ID)
  68. }
  69. return nil
  70. }
  71. func containerIsNotRestarting(c *container.Container) error {
  72. if c.IsRestarting() {
  73. return errContainerIsRestarting(c.ID)
  74. }
  75. return nil
  76. }
  77. func (daemon *Daemon) setupIpcDirs(c *container.Container) error {
  78. var err error
  79. c.ShmPath, err = c.ShmResourcePath()
  80. if err != nil {
  81. return err
  82. }
  83. if c.HostConfig.IpcMode.IsContainer() {
  84. ic, err := daemon.getIpcContainer(c)
  85. if err != nil {
  86. return err
  87. }
  88. c.ShmPath = ic.ShmPath
  89. } else if c.HostConfig.IpcMode.IsHost() {
  90. if _, err := os.Stat("/dev/shm"); err != nil {
  91. return fmt.Errorf("/dev/shm is not mounted, but must be for --ipc=host")
  92. }
  93. c.ShmPath = "/dev/shm"
  94. } else {
  95. rootIDs := daemon.idMappings.RootPair()
  96. if !c.HasMountFor("/dev/shm") {
  97. shmPath, err := c.ShmResourcePath()
  98. if err != nil {
  99. return err
  100. }
  101. if err := idtools.MkdirAllAndChown(shmPath, 0700, rootIDs); err != nil {
  102. return err
  103. }
  104. shmproperty := "mode=1777,size=" + strconv.FormatInt(c.HostConfig.ShmSize, 10)
  105. if err := unix.Mount("shm", shmPath, "tmpfs", uintptr(unix.MS_NOEXEC|unix.MS_NOSUID|unix.MS_NODEV), label.FormatMountLabel(shmproperty, c.GetMountLabel())); err != nil {
  106. return fmt.Errorf("mounting shm tmpfs: %s", err)
  107. }
  108. if err := os.Chown(shmPath, rootIDs.UID, rootIDs.GID); err != nil {
  109. return err
  110. }
  111. }
  112. }
  113. return nil
  114. }
  115. func (daemon *Daemon) setupSecretDir(c *container.Container) (setupErr error) {
  116. if len(c.SecretReferences) == 0 {
  117. return nil
  118. }
  119. localMountPath := c.SecretMountPath()
  120. logrus.Debugf("secrets: setting up secret dir: %s", localMountPath)
  121. // retrieve possible remapped range start for root UID, GID
  122. rootIDs := daemon.idMappings.RootPair()
  123. // create tmpfs
  124. if err := idtools.MkdirAllAndChown(localMountPath, 0700, rootIDs); err != nil {
  125. return errors.Wrap(err, "error creating secret local mount path")
  126. }
  127. defer func() {
  128. if setupErr != nil {
  129. // cleanup
  130. _ = detachMounted(localMountPath)
  131. if err := os.RemoveAll(localMountPath); err != nil {
  132. logrus.Errorf("error cleaning up secret mount: %s", err)
  133. }
  134. }
  135. }()
  136. tmpfsOwnership := fmt.Sprintf("uid=%d,gid=%d", rootIDs.UID, rootIDs.GID)
  137. if err := mount.Mount("tmpfs", localMountPath, "tmpfs", "nodev,nosuid,noexec,"+tmpfsOwnership); err != nil {
  138. return errors.Wrap(err, "unable to setup secret mount")
  139. }
  140. if c.DependencyStore == nil {
  141. return fmt.Errorf("secret store is not initialized")
  142. }
  143. for _, s := range c.SecretReferences {
  144. // TODO (ehazlett): use type switch when more are supported
  145. if s.File == nil {
  146. logrus.Error("secret target type is not a file target")
  147. continue
  148. }
  149. // secrets are created in the SecretMountPath on the host, at a
  150. // single level
  151. fPath := c.SecretFilePath(*s)
  152. if err := idtools.MkdirAllAndChown(filepath.Dir(fPath), 0700, rootIDs); err != nil {
  153. return errors.Wrap(err, "error creating secret mount path")
  154. }
  155. logrus.WithFields(logrus.Fields{
  156. "name": s.File.Name,
  157. "path": fPath,
  158. }).Debug("injecting secret")
  159. secret, err := c.DependencyStore.Secrets().Get(s.SecretID)
  160. if err != nil {
  161. return errors.Wrap(err, "unable to get secret from secret store")
  162. }
  163. if err := ioutil.WriteFile(fPath, secret.Spec.Data, s.File.Mode); err != nil {
  164. return errors.Wrap(err, "error injecting secret")
  165. }
  166. uid, err := strconv.Atoi(s.File.UID)
  167. if err != nil {
  168. return err
  169. }
  170. gid, err := strconv.Atoi(s.File.GID)
  171. if err != nil {
  172. return err
  173. }
  174. if err := os.Chown(fPath, rootIDs.UID+uid, rootIDs.GID+gid); err != nil {
  175. return errors.Wrap(err, "error setting ownership for secret")
  176. }
  177. }
  178. label.Relabel(localMountPath, c.MountLabel, false)
  179. // remount secrets ro
  180. if err := mount.Mount("tmpfs", localMountPath, "tmpfs", "remount,ro,"+tmpfsOwnership); err != nil {
  181. return errors.Wrap(err, "unable to remount secret dir as readonly")
  182. }
  183. return nil
  184. }
  185. func (daemon *Daemon) setupConfigDir(c *container.Container) (setupErr error) {
  186. if len(c.ConfigReferences) == 0 {
  187. return nil
  188. }
  189. localPath := c.ConfigsDirPath()
  190. logrus.Debugf("configs: setting up config dir: %s", localPath)
  191. // retrieve possible remapped range start for root UID, GID
  192. rootIDs := daemon.idMappings.RootPair()
  193. // create tmpfs
  194. if err := idtools.MkdirAllAndChown(localPath, 0700, rootIDs); err != nil {
  195. return errors.Wrap(err, "error creating config dir")
  196. }
  197. defer func() {
  198. if setupErr != nil {
  199. if err := os.RemoveAll(localPath); err != nil {
  200. logrus.Errorf("error cleaning up config dir: %s", err)
  201. }
  202. }
  203. }()
  204. if c.DependencyStore == nil {
  205. return fmt.Errorf("config store is not initialized")
  206. }
  207. for _, configRef := range c.ConfigReferences {
  208. // TODO (ehazlett): use type switch when more are supported
  209. if configRef.File == nil {
  210. logrus.Error("config target type is not a file target")
  211. continue
  212. }
  213. fPath := c.ConfigFilePath(*configRef)
  214. log := logrus.WithFields(logrus.Fields{"name": configRef.File.Name, "path": fPath})
  215. if err := idtools.MkdirAllAndChown(filepath.Dir(fPath), 0700, rootIDs); err != nil {
  216. return errors.Wrap(err, "error creating config path")
  217. }
  218. log.Debug("injecting config")
  219. config, err := c.DependencyStore.Configs().Get(configRef.ConfigID)
  220. if err != nil {
  221. return errors.Wrap(err, "unable to get config from config store")
  222. }
  223. if err := ioutil.WriteFile(fPath, config.Spec.Data, configRef.File.Mode); err != nil {
  224. return errors.Wrap(err, "error injecting config")
  225. }
  226. uid, err := strconv.Atoi(configRef.File.UID)
  227. if err != nil {
  228. return err
  229. }
  230. gid, err := strconv.Atoi(configRef.File.GID)
  231. if err != nil {
  232. return err
  233. }
  234. if err := os.Chown(fPath, rootIDs.UID+uid, rootIDs.GID+gid); err != nil {
  235. return errors.Wrap(err, "error setting ownership for config")
  236. }
  237. }
  238. return nil
  239. }
  240. func killProcessDirectly(cntr *container.Container) error {
  241. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  242. defer cancel()
  243. // Block until the container to stops or timeout.
  244. status := <-cntr.Wait(ctx, container.WaitConditionNotRunning)
  245. if status.Err() != nil {
  246. // Ensure that we don't kill ourselves
  247. if pid := cntr.GetPID(); pid != 0 {
  248. logrus.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", stringid.TruncateID(cntr.ID))
  249. if err := unix.Kill(pid, 9); err != nil {
  250. if err != unix.ESRCH {
  251. return err
  252. }
  253. e := errNoSuchProcess{pid, 9}
  254. logrus.Debug(e)
  255. return e
  256. }
  257. }
  258. }
  259. return nil
  260. }
  261. func detachMounted(path string) error {
  262. return unix.Unmount(path, unix.MNT_DETACH)
  263. }
  264. func isLinkable(child *container.Container) bool {
  265. // A container is linkable only if it belongs to the default network
  266. _, ok := child.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
  267. return ok
  268. }
  269. func enableIPOnPredefinedNetwork() bool {
  270. return false
  271. }
  272. func (daemon *Daemon) isNetworkHotPluggable() bool {
  273. return true
  274. }
  275. func setupPathsAndSandboxOptions(container *container.Container, sboxOptions *[]libnetwork.SandboxOption) error {
  276. var err error
  277. container.HostsPath, err = container.GetRootResourcePath("hosts")
  278. if err != nil {
  279. return err
  280. }
  281. *sboxOptions = append(*sboxOptions, libnetwork.OptionHostsPath(container.HostsPath))
  282. container.ResolvConfPath, err = container.GetRootResourcePath("resolv.conf")
  283. if err != nil {
  284. return err
  285. }
  286. *sboxOptions = append(*sboxOptions, libnetwork.OptionResolvConfPath(container.ResolvConfPath))
  287. return nil
  288. }
  289. func (daemon *Daemon) initializeNetworkingPaths(container *container.Container, nc *container.Container) error {
  290. container.HostnamePath = nc.HostnamePath
  291. container.HostsPath = nc.HostsPath
  292. container.ResolvConfPath = nc.ResolvConfPath
  293. return nil
  294. }