container_operations_unix.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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/cloudflare/cfssl/log"
  13. "github.com/docker/docker/container"
  14. "github.com/docker/docker/daemon/links"
  15. "github.com/docker/docker/pkg/idtools"
  16. "github.com/docker/docker/pkg/mount"
  17. "github.com/docker/docker/pkg/stringid"
  18. "github.com/docker/docker/runconfig"
  19. "github.com/docker/libnetwork"
  20. "github.com/opencontainers/runc/libcontainer/label"
  21. "github.com/pkg/errors"
  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. c, err := daemon.GetContainer(containerID)
  52. if err != nil {
  53. return nil, err
  54. }
  55. if !c.IsRunning() {
  56. return nil, fmt.Errorf("cannot join IPC of a non running container: %s", containerID)
  57. }
  58. if c.IsRestarting() {
  59. return nil, errContainerIsRestarting(container.ID)
  60. }
  61. return c, nil
  62. }
  63. func (daemon *Daemon) getPidContainer(container *container.Container) (*container.Container, error) {
  64. containerID := container.HostConfig.PidMode.Container()
  65. c, err := daemon.GetContainer(containerID)
  66. if err != nil {
  67. return nil, err
  68. }
  69. if !c.IsRunning() {
  70. return nil, fmt.Errorf("cannot join PID of a non running container: %s", containerID)
  71. }
  72. if c.IsRestarting() {
  73. return nil, errContainerIsRestarting(container.ID)
  74. }
  75. return c, 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. rootUID, rootGID := daemon.GetRemappedUIDGID()
  96. if !c.HasMountFor("/dev/shm") {
  97. shmPath, err := c.ShmResourcePath()
  98. if err != nil {
  99. return err
  100. }
  101. if err := idtools.MkdirAllAs(shmPath, 0700, rootUID, rootGID); err != nil {
  102. return err
  103. }
  104. shmSize := container.DefaultSHMSize
  105. if c.HostConfig.ShmSize != 0 {
  106. shmSize = c.HostConfig.ShmSize
  107. }
  108. shmproperty := "mode=1777,size=" + strconv.FormatInt(shmSize, 10)
  109. if err := syscall.Mount("shm", shmPath, "tmpfs", uintptr(syscall.MS_NOEXEC|syscall.MS_NOSUID|syscall.MS_NODEV), label.FormatMountLabel(shmproperty, c.GetMountLabel())); err != nil {
  110. return fmt.Errorf("mounting shm tmpfs: %s", err)
  111. }
  112. if err := os.Chown(shmPath, rootUID, rootGID); err != nil {
  113. return err
  114. }
  115. }
  116. }
  117. return nil
  118. }
  119. func (daemon *Daemon) setupSecretDir(c *container.Container) (setupErr error) {
  120. if len(c.SecretReferences) == 0 {
  121. return nil
  122. }
  123. localMountPath := c.SecretMountPath()
  124. logrus.Debugf("secrets: setting up secret dir: %s", localMountPath)
  125. defer func() {
  126. if setupErr != nil {
  127. // cleanup
  128. _ = detachMounted(localMountPath)
  129. if err := os.RemoveAll(localMountPath); err != nil {
  130. log.Errorf("error cleaning up secret mount: %s", err)
  131. }
  132. }
  133. }()
  134. // retrieve possible remapped range start for root UID, GID
  135. rootUID, rootGID := daemon.GetRemappedUIDGID()
  136. // create tmpfs
  137. if err := idtools.MkdirAllAs(localMountPath, 0700, rootUID, rootGID); err != nil {
  138. return errors.Wrap(err, "error creating secret local mount path")
  139. }
  140. tmpfsOwnership := fmt.Sprintf("uid=%d,gid=%d", rootUID, rootGID)
  141. if err := mount.Mount("tmpfs", localMountPath, "tmpfs", "nodev,nosuid,noexec,"+tmpfsOwnership); err != nil {
  142. return errors.Wrap(err, "unable to setup secret mount")
  143. }
  144. for _, s := range c.SecretReferences {
  145. if c.SecretStore == nil {
  146. return fmt.Errorf("secret store is not initialized")
  147. }
  148. // TODO (ehazlett): use type switch when more are supported
  149. if s.File == nil {
  150. return fmt.Errorf("secret target type is not a file target")
  151. }
  152. targetPath := filepath.Clean(s.File.Name)
  153. // ensure that the target is a filename only; no paths allowed
  154. if targetPath != filepath.Base(targetPath) {
  155. return fmt.Errorf("error creating secret: secret must not be a path")
  156. }
  157. fPath := filepath.Join(localMountPath, targetPath)
  158. if err := idtools.MkdirAllAs(filepath.Dir(fPath), 0700, rootUID, rootGID); err != nil {
  159. return errors.Wrap(err, "error creating secret mount path")
  160. }
  161. logrus.WithFields(logrus.Fields{
  162. "name": s.File.Name,
  163. "path": fPath,
  164. }).Debug("injecting secret")
  165. secret := c.SecretStore.Get(s.SecretID)
  166. if secret == nil {
  167. return fmt.Errorf("unable to get secret from secret store")
  168. }
  169. if err := ioutil.WriteFile(fPath, secret.Spec.Data, s.File.Mode); err != nil {
  170. return errors.Wrap(err, "error injecting secret")
  171. }
  172. uid, err := strconv.Atoi(s.File.UID)
  173. if err != nil {
  174. return err
  175. }
  176. gid, err := strconv.Atoi(s.File.GID)
  177. if err != nil {
  178. return err
  179. }
  180. if err := os.Chown(fPath, rootUID+uid, rootGID+gid); err != nil {
  181. return errors.Wrap(err, "error setting ownership for secret")
  182. }
  183. }
  184. // remount secrets ro
  185. if err := mount.Mount("tmpfs", localMountPath, "tmpfs", "remount,ro,"+tmpfsOwnership); err != nil {
  186. return errors.Wrap(err, "unable to remount secret dir as readonly")
  187. }
  188. return nil
  189. }
  190. func killProcessDirectly(container *container.Container) error {
  191. if _, err := container.WaitStop(10 * time.Second); err != nil {
  192. // Ensure that we don't kill ourselves
  193. if pid := container.GetPID(); pid != 0 {
  194. logrus.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", stringid.TruncateID(container.ID))
  195. if err := syscall.Kill(pid, 9); err != nil {
  196. if err != syscall.ESRCH {
  197. return err
  198. }
  199. e := errNoSuchProcess{pid, 9}
  200. logrus.Debug(e)
  201. return e
  202. }
  203. }
  204. }
  205. return nil
  206. }
  207. func detachMounted(path string) error {
  208. return syscall.Unmount(path, syscall.MNT_DETACH)
  209. }
  210. func isLinkable(child *container.Container) bool {
  211. // A container is linkable only if it belongs to the default network
  212. _, ok := child.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
  213. return ok
  214. }
  215. func enableIPOnPredefinedNetwork() bool {
  216. return false
  217. }
  218. func (daemon *Daemon) isNetworkHotPluggable() bool {
  219. return true
  220. }
  221. func setupPathsAndSandboxOptions(container *container.Container, sboxOptions *[]libnetwork.SandboxOption) error {
  222. var err error
  223. container.HostsPath, err = container.GetRootResourcePath("hosts")
  224. if err != nil {
  225. return err
  226. }
  227. *sboxOptions = append(*sboxOptions, libnetwork.OptionHostsPath(container.HostsPath))
  228. container.ResolvConfPath, err = container.GetRootResourcePath("resolv.conf")
  229. if err != nil {
  230. return err
  231. }
  232. *sboxOptions = append(*sboxOptions, libnetwork.OptionResolvConfPath(container.ResolvConfPath))
  233. return nil
  234. }
  235. func initializeNetworkingPaths(container *container.Container, nc *container.Container) {
  236. container.HostnamePath = nc.HostnamePath
  237. container.HostsPath = nc.HostsPath
  238. container.ResolvConfPath = nc.ResolvConfPath
  239. }