container_operations_unix.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // +build linux freebsd
  2. package daemon
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "syscall"
  11. "time"
  12. "github.com/Sirupsen/logrus"
  13. "github.com/cloudflare/cfssl/log"
  14. containertypes "github.com/docker/docker/api/types/container"
  15. "github.com/docker/docker/container"
  16. "github.com/docker/docker/daemon/links"
  17. "github.com/docker/docker/pkg/idtools"
  18. "github.com/docker/docker/pkg/mount"
  19. "github.com/docker/docker/pkg/stringid"
  20. "github.com/docker/docker/runconfig"
  21. "github.com/docker/libnetwork"
  22. "github.com/opencontainers/runc/libcontainer/configs"
  23. "github.com/opencontainers/runc/libcontainer/devices"
  24. "github.com/opencontainers/runc/libcontainer/label"
  25. "github.com/opencontainers/runtime-spec/specs-go"
  26. "github.com/pkg/errors"
  27. )
  28. func u32Ptr(i int64) *uint32 { u := uint32(i); return &u }
  29. func fmPtr(i int64) *os.FileMode { fm := os.FileMode(i); return &fm }
  30. func (daemon *Daemon) setupLinkedContainers(container *container.Container) ([]string, error) {
  31. var env []string
  32. children := daemon.children(container)
  33. bridgeSettings := container.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
  34. if bridgeSettings == nil || bridgeSettings.EndpointSettings == nil {
  35. return nil, nil
  36. }
  37. for linkAlias, child := range children {
  38. if !child.IsRunning() {
  39. return nil, fmt.Errorf("Cannot link to a non running container: %s AS %s", child.Name, linkAlias)
  40. }
  41. childBridgeSettings := child.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
  42. if childBridgeSettings == nil || childBridgeSettings.EndpointSettings == nil {
  43. return nil, fmt.Errorf("container %s not attached to default bridge network", child.ID)
  44. }
  45. link := links.NewLink(
  46. bridgeSettings.IPAddress,
  47. childBridgeSettings.IPAddress,
  48. linkAlias,
  49. child.Config.Env,
  50. child.Config.ExposedPorts,
  51. )
  52. env = append(env, link.ToEnv()...)
  53. }
  54. return env, nil
  55. }
  56. func (daemon *Daemon) getIpcContainer(container *container.Container) (*container.Container, error) {
  57. containerID := container.HostConfig.IpcMode.Container()
  58. c, err := daemon.GetContainer(containerID)
  59. if err != nil {
  60. return nil, err
  61. }
  62. if !c.IsRunning() {
  63. return nil, fmt.Errorf("cannot join IPC of a non running container: %s", containerID)
  64. }
  65. if c.IsRestarting() {
  66. return nil, errContainerIsRestarting(container.ID)
  67. }
  68. return c, nil
  69. }
  70. func (daemon *Daemon) getPidContainer(container *container.Container) (*container.Container, error) {
  71. containerID := container.HostConfig.PidMode.Container()
  72. c, err := daemon.GetContainer(containerID)
  73. if err != nil {
  74. return nil, err
  75. }
  76. if !c.IsRunning() {
  77. return nil, fmt.Errorf("cannot join PID of a non running container: %s", containerID)
  78. }
  79. if c.IsRestarting() {
  80. return nil, errContainerIsRestarting(container.ID)
  81. }
  82. return c, nil
  83. }
  84. func (daemon *Daemon) setupIpcDirs(c *container.Container) error {
  85. var err error
  86. c.ShmPath, err = c.ShmResourcePath()
  87. if err != nil {
  88. return err
  89. }
  90. if c.HostConfig.IpcMode.IsContainer() {
  91. ic, err := daemon.getIpcContainer(c)
  92. if err != nil {
  93. return err
  94. }
  95. c.ShmPath = ic.ShmPath
  96. } else if c.HostConfig.IpcMode.IsHost() {
  97. if _, err := os.Stat("/dev/shm"); err != nil {
  98. return fmt.Errorf("/dev/shm is not mounted, but must be for --ipc=host")
  99. }
  100. c.ShmPath = "/dev/shm"
  101. } else {
  102. rootUID, rootGID := daemon.GetRemappedUIDGID()
  103. if !c.HasMountFor("/dev/shm") {
  104. shmPath, err := c.ShmResourcePath()
  105. if err != nil {
  106. return err
  107. }
  108. if err := idtools.MkdirAllAs(shmPath, 0700, rootUID, rootGID); err != nil {
  109. return err
  110. }
  111. shmSize := container.DefaultSHMSize
  112. if c.HostConfig.ShmSize != 0 {
  113. shmSize = c.HostConfig.ShmSize
  114. }
  115. shmproperty := "mode=1777,size=" + strconv.FormatInt(shmSize, 10)
  116. if err := syscall.Mount("shm", shmPath, "tmpfs", uintptr(syscall.MS_NOEXEC|syscall.MS_NOSUID|syscall.MS_NODEV), label.FormatMountLabel(shmproperty, c.GetMountLabel())); err != nil {
  117. return fmt.Errorf("mounting shm tmpfs: %s", err)
  118. }
  119. if err := os.Chown(shmPath, rootUID, rootGID); err != nil {
  120. return err
  121. }
  122. }
  123. }
  124. return nil
  125. }
  126. func (daemon *Daemon) setupSecretDir(c *container.Container) (setupErr error) {
  127. if len(c.SecretReferences) == 0 {
  128. return nil
  129. }
  130. localMountPath := c.SecretMountPath()
  131. logrus.Debugf("secrets: setting up secret dir: %s", localMountPath)
  132. defer func() {
  133. if setupErr != nil {
  134. // cleanup
  135. _ = detachMounted(localMountPath)
  136. if err := os.RemoveAll(localMountPath); err != nil {
  137. log.Errorf("error cleaning up secret mount: %s", err)
  138. }
  139. }
  140. }()
  141. // retrieve possible remapped range start for root UID, GID
  142. rootUID, rootGID := daemon.GetRemappedUIDGID()
  143. // create tmpfs
  144. if err := idtools.MkdirAllAs(localMountPath, 0700, rootUID, rootGID); err != nil {
  145. return errors.Wrap(err, "error creating secret local mount path")
  146. }
  147. tmpfsOwnership := fmt.Sprintf("uid=%d,gid=%d", rootUID, rootGID)
  148. if err := mount.Mount("tmpfs", localMountPath, "tmpfs", "nodev,nosuid,noexec,"+tmpfsOwnership); err != nil {
  149. return errors.Wrap(err, "unable to setup secret mount")
  150. }
  151. for _, s := range c.SecretReferences {
  152. if c.SecretStore == nil {
  153. return fmt.Errorf("secret store is not initialized")
  154. }
  155. // TODO (ehazlett): use type switch when more are supported
  156. if s.File == nil {
  157. return fmt.Errorf("secret target type is not a file target")
  158. }
  159. targetPath := filepath.Clean(s.File.Name)
  160. // ensure that the target is a filename only; no paths allowed
  161. if targetPath != filepath.Base(targetPath) {
  162. return fmt.Errorf("error creating secret: secret must not be a path")
  163. }
  164. fPath := filepath.Join(localMountPath, targetPath)
  165. if err := idtools.MkdirAllAs(filepath.Dir(fPath), 0700, rootUID, rootGID); 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 := c.SecretStore.Get(s.SecretID)
  173. if secret == nil {
  174. return fmt.Errorf("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, rootUID+uid, rootGID+gid); err != nil {
  188. return errors.Wrap(err, "error setting ownership for secret")
  189. }
  190. }
  191. // remount secrets ro
  192. if err := mount.Mount("tmpfs", localMountPath, "tmpfs", "remount,ro,"+tmpfsOwnership); err != nil {
  193. return errors.Wrap(err, "unable to remount secret dir as readonly")
  194. }
  195. return nil
  196. }
  197. func killProcessDirectly(container *container.Container) error {
  198. if _, err := container.WaitStop(10 * time.Second); err != nil {
  199. // Ensure that we don't kill ourselves
  200. if pid := container.GetPID(); pid != 0 {
  201. logrus.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", stringid.TruncateID(container.ID))
  202. if err := syscall.Kill(pid, 9); err != nil {
  203. if err != syscall.ESRCH {
  204. return err
  205. }
  206. e := errNoSuchProcess{pid, 9}
  207. logrus.Debug(e)
  208. return e
  209. }
  210. }
  211. }
  212. return nil
  213. }
  214. func specDevice(d *configs.Device) specs.Device {
  215. return specs.Device{
  216. Type: string(d.Type),
  217. Path: d.Path,
  218. Major: d.Major,
  219. Minor: d.Minor,
  220. FileMode: fmPtr(int64(d.FileMode)),
  221. UID: u32Ptr(int64(d.Uid)),
  222. GID: u32Ptr(int64(d.Gid)),
  223. }
  224. }
  225. func specDeviceCgroup(d *configs.Device) specs.DeviceCgroup {
  226. t := string(d.Type)
  227. return specs.DeviceCgroup{
  228. Allow: true,
  229. Type: &t,
  230. Major: &d.Major,
  231. Minor: &d.Minor,
  232. Access: &d.Permissions,
  233. }
  234. }
  235. func getDevicesFromPath(deviceMapping containertypes.DeviceMapping) (devs []specs.Device, devPermissions []specs.DeviceCgroup, err error) {
  236. resolvedPathOnHost := deviceMapping.PathOnHost
  237. // check if it is a symbolic link
  238. if src, e := os.Lstat(deviceMapping.PathOnHost); e == nil && src.Mode()&os.ModeSymlink == os.ModeSymlink {
  239. if linkedPathOnHost, e := filepath.EvalSymlinks(deviceMapping.PathOnHost); e == nil {
  240. resolvedPathOnHost = linkedPathOnHost
  241. }
  242. }
  243. device, err := devices.DeviceFromPath(resolvedPathOnHost, deviceMapping.CgroupPermissions)
  244. // if there was no error, return the device
  245. if err == nil {
  246. device.Path = deviceMapping.PathInContainer
  247. return append(devs, specDevice(device)), append(devPermissions, specDeviceCgroup(device)), nil
  248. }
  249. // if the device is not a device node
  250. // try to see if it's a directory holding many devices
  251. if err == devices.ErrNotADevice {
  252. // check if it is a directory
  253. if src, e := os.Stat(resolvedPathOnHost); e == nil && src.IsDir() {
  254. // mount the internal devices recursively
  255. filepath.Walk(resolvedPathOnHost, func(dpath string, f os.FileInfo, e error) error {
  256. childDevice, e := devices.DeviceFromPath(dpath, deviceMapping.CgroupPermissions)
  257. if e != nil {
  258. // ignore the device
  259. return nil
  260. }
  261. // add the device to userSpecified devices
  262. childDevice.Path = strings.Replace(dpath, resolvedPathOnHost, deviceMapping.PathInContainer, 1)
  263. devs = append(devs, specDevice(childDevice))
  264. devPermissions = append(devPermissions, specDeviceCgroup(childDevice))
  265. return nil
  266. })
  267. }
  268. }
  269. if len(devs) > 0 {
  270. return devs, devPermissions, nil
  271. }
  272. return devs, devPermissions, fmt.Errorf("error gathering device information while adding custom device %q: %s", deviceMapping.PathOnHost, err)
  273. }
  274. func detachMounted(path string) error {
  275. return syscall.Unmount(path, syscall.MNT_DETACH)
  276. }
  277. func isLinkable(child *container.Container) bool {
  278. // A container is linkable only if it belongs to the default network
  279. _, ok := child.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
  280. return ok
  281. }
  282. func enableIPOnPredefinedNetwork() bool {
  283. return false
  284. }
  285. func (daemon *Daemon) isNetworkHotPluggable() bool {
  286. return true
  287. }
  288. func setupPathsAndSandboxOptions(container *container.Container, sboxOptions *[]libnetwork.SandboxOption) error {
  289. var err error
  290. container.HostsPath, err = container.GetRootResourcePath("hosts")
  291. if err != nil {
  292. return err
  293. }
  294. *sboxOptions = append(*sboxOptions, libnetwork.OptionHostsPath(container.HostsPath))
  295. container.ResolvConfPath, err = container.GetRootResourcePath("resolv.conf")
  296. if err != nil {
  297. return err
  298. }
  299. *sboxOptions = append(*sboxOptions, libnetwork.OptionResolvConfPath(container.ResolvConfPath))
  300. return nil
  301. }
  302. func initializeNetworkingPaths(container *container.Container, nc *container.Container) {
  303. container.HostnamePath = nc.HostnamePath
  304. container.HostsPath = nc.HostsPath
  305. container.ResolvConfPath = nc.ResolvConfPath
  306. }