container_operations_unix.go 7.9 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/runc/libcontainer/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. c, err := daemon.GetContainer(containerID)
  51. if err != nil {
  52. return nil, err
  53. }
  54. if !c.IsRunning() {
  55. return nil, fmt.Errorf("cannot join IPC of a non running container: %s", containerID)
  56. }
  57. if c.IsRestarting() {
  58. return nil, errContainerIsRestarting(container.ID)
  59. }
  60. return c, nil
  61. }
  62. func (daemon *Daemon) getPidContainer(container *container.Container) (*container.Container, error) {
  63. containerID := container.HostConfig.PidMode.Container()
  64. c, err := daemon.GetContainer(containerID)
  65. if err != nil {
  66. return nil, err
  67. }
  68. if !c.IsRunning() {
  69. return nil, fmt.Errorf("cannot join PID of a non running container: %s", containerID)
  70. }
  71. if c.IsRestarting() {
  72. return nil, errContainerIsRestarting(container.ID)
  73. }
  74. return c, 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 := container.DefaultSHMSize
  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. targetPath := filepath.Clean(s.File.Name)
  152. // ensure that the target is a filename only; no paths allowed
  153. if targetPath != filepath.Base(targetPath) {
  154. return fmt.Errorf("error creating secret: secret must not be a path")
  155. }
  156. fPath := filepath.Join(localMountPath, targetPath)
  157. if err := idtools.MkdirAllAs(filepath.Dir(fPath), 0700, rootUID, rootGID); err != nil {
  158. return errors.Wrap(err, "error creating secret mount path")
  159. }
  160. logrus.WithFields(logrus.Fields{
  161. "name": s.File.Name,
  162. "path": fPath,
  163. }).Debug("injecting secret")
  164. secret := c.SecretStore.Get(s.SecretID)
  165. if secret == nil {
  166. return fmt.Errorf("unable to get secret from secret store")
  167. }
  168. if err := ioutil.WriteFile(fPath, secret.Spec.Data, s.File.Mode); err != nil {
  169. return errors.Wrap(err, "error injecting secret")
  170. }
  171. uid, err := strconv.Atoi(s.File.UID)
  172. if err != nil {
  173. return err
  174. }
  175. gid, err := strconv.Atoi(s.File.GID)
  176. if err != nil {
  177. return err
  178. }
  179. if err := os.Chown(fPath, rootUID+uid, rootGID+gid); err != nil {
  180. return errors.Wrap(err, "error setting ownership for secret")
  181. }
  182. }
  183. // remount secrets ro
  184. if err := mount.Mount("tmpfs", localMountPath, "tmpfs", "remount,ro,"+tmpfsOwnership); err != nil {
  185. return errors.Wrap(err, "unable to remount secret dir as readonly")
  186. }
  187. return nil
  188. }
  189. func killProcessDirectly(container *container.Container) error {
  190. if _, err := container.WaitStop(10 * time.Second); err != nil {
  191. // Ensure that we don't kill ourselves
  192. if pid := container.GetPID(); pid != 0 {
  193. logrus.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", stringid.TruncateID(container.ID))
  194. if err := syscall.Kill(pid, 9); err != nil {
  195. if err != syscall.ESRCH {
  196. return err
  197. }
  198. e := errNoSuchProcess{pid, 9}
  199. logrus.Debug(e)
  200. return e
  201. }
  202. }
  203. }
  204. return nil
  205. }
  206. func detachMounted(path string) error {
  207. return syscall.Unmount(path, syscall.MNT_DETACH)
  208. }
  209. func isLinkable(child *container.Container) bool {
  210. // A container is linkable only if it belongs to the default network
  211. _, ok := child.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
  212. return ok
  213. }
  214. func enableIPOnPredefinedNetwork() bool {
  215. return false
  216. }
  217. func (daemon *Daemon) isNetworkHotPluggable() bool {
  218. return true
  219. }
  220. func setupPathsAndSandboxOptions(container *container.Container, sboxOptions *[]libnetwork.SandboxOption) error {
  221. var err error
  222. container.HostsPath, err = container.GetRootResourcePath("hosts")
  223. if err != nil {
  224. return err
  225. }
  226. *sboxOptions = append(*sboxOptions, libnetwork.OptionHostsPath(container.HostsPath))
  227. container.ResolvConfPath, err = container.GetRootResourcePath("resolv.conf")
  228. if err != nil {
  229. return err
  230. }
  231. *sboxOptions = append(*sboxOptions, libnetwork.OptionResolvConfPath(container.ResolvConfPath))
  232. return nil
  233. }
  234. func initializeNetworkingPaths(container *container.Container, nc *container.Container) {
  235. container.HostnamePath = nc.HostnamePath
  236. container.HostsPath = nc.HostsPath
  237. container.ResolvConfPath = nc.ResolvConfPath
  238. }