daemon_unix.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // +build !windows
  2. package main
  3. import (
  4. "fmt"
  5. "net"
  6. "os"
  7. "os/signal"
  8. "path/filepath"
  9. "strconv"
  10. "github.com/containerd/containerd/linux"
  11. "github.com/docker/docker/cmd/dockerd/hack"
  12. "github.com/docker/docker/daemon"
  13. "github.com/docker/docker/libcontainerd"
  14. "github.com/docker/docker/pkg/parsers/kernel"
  15. "github.com/docker/libnetwork/portallocator"
  16. "golang.org/x/sys/unix"
  17. )
  18. const defaultDaemonConfigFile = "/etc/docker/daemon.json"
  19. // setDefaultUmask sets the umask to 0022 to avoid problems
  20. // caused by custom umask
  21. func setDefaultUmask() error {
  22. desiredUmask := 0022
  23. unix.Umask(desiredUmask)
  24. if umask := unix.Umask(desiredUmask); umask != desiredUmask {
  25. return fmt.Errorf("failed to set umask: expected %#o, got %#o", desiredUmask, umask)
  26. }
  27. return nil
  28. }
  29. func getDaemonConfDir(_ string) string {
  30. return "/etc/docker"
  31. }
  32. func (cli *DaemonCli) getPlatformRemoteOptions() ([]libcontainerd.RemoteOption, error) {
  33. // On older kernel, letting putting the containerd-shim in its own
  34. // namespace will effectively prevent operations such as unlink, rename
  35. // and remove on mountpoints that were present at the time the shim
  36. // namespace was created. This would led to a famous EBUSY will trying to
  37. // remove shm mounts.
  38. var noNewNS bool
  39. if !kernel.CheckKernelVersion(3, 18, 0) {
  40. noNewNS = true
  41. }
  42. opts := []libcontainerd.RemoteOption{
  43. libcontainerd.WithOOMScore(cli.Config.OOMScoreAdjust),
  44. libcontainerd.WithPlugin("linux", &linux.Config{
  45. Shim: daemon.DefaultShimBinary,
  46. Runtime: daemon.DefaultRuntimeBinary,
  47. RuntimeRoot: filepath.Join(cli.Config.Root, "runc"),
  48. ShimDebug: cli.Config.Debug,
  49. ShimNoMountNS: noNewNS,
  50. }),
  51. }
  52. if cli.Config.Debug {
  53. opts = append(opts, libcontainerd.WithLogLevel("debug"))
  54. }
  55. if cli.Config.ContainerdAddr != "" {
  56. opts = append(opts, libcontainerd.WithRemoteAddr(cli.Config.ContainerdAddr))
  57. } else {
  58. opts = append(opts, libcontainerd.WithStartDaemon(true))
  59. }
  60. return opts, nil
  61. }
  62. // setupConfigReloadTrap configures the USR2 signal to reload the configuration.
  63. func (cli *DaemonCli) setupConfigReloadTrap() {
  64. c := make(chan os.Signal, 1)
  65. signal.Notify(c, unix.SIGHUP)
  66. go func() {
  67. for range c {
  68. cli.reloadConfig()
  69. }
  70. }()
  71. }
  72. // getSwarmRunRoot gets the root directory for swarm to store runtime state
  73. // For example, the control socket
  74. func (cli *DaemonCli) getSwarmRunRoot() string {
  75. return filepath.Join(cli.Config.ExecRoot, "swarm")
  76. }
  77. // allocateDaemonPort ensures that there are no containers
  78. // that try to use any port allocated for the docker server.
  79. func allocateDaemonPort(addr string) error {
  80. host, port, err := net.SplitHostPort(addr)
  81. if err != nil {
  82. return err
  83. }
  84. intPort, err := strconv.Atoi(port)
  85. if err != nil {
  86. return err
  87. }
  88. var hostIPs []net.IP
  89. if parsedIP := net.ParseIP(host); parsedIP != nil {
  90. hostIPs = append(hostIPs, parsedIP)
  91. } else if hostIPs, err = net.LookupIP(host); err != nil {
  92. return fmt.Errorf("failed to lookup %s address in host specification", host)
  93. }
  94. pa := portallocator.Get()
  95. for _, hostIP := range hostIPs {
  96. if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
  97. return fmt.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
  98. }
  99. }
  100. return nil
  101. }
  102. // notifyShutdown is called after the daemon shuts down but before the process exits.
  103. func notifyShutdown(err error) {
  104. }
  105. func wrapListeners(proto string, ls []net.Listener) []net.Listener {
  106. switch proto {
  107. case "unix":
  108. ls[0] = &hack.MalformedHostHeaderOverride{ls[0]}
  109. case "fd":
  110. for i := range ls {
  111. ls[i] = &hack.MalformedHostHeaderOverride{ls[i]}
  112. }
  113. }
  114. return ls
  115. }