container_operations_unix.go 8.0 KB

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