daemon_unix.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/runtime/v1/linux"
  11. "github.com/docker/docker/cmd/dockerd/hack"
  12. "github.com/docker/docker/daemon"
  13. "github.com/docker/docker/daemon/config"
  14. "github.com/docker/docker/libcontainerd/supervisor"
  15. "github.com/docker/docker/pkg/homedir"
  16. "github.com/docker/docker/rootless"
  17. "github.com/docker/libnetwork/portallocator"
  18. "golang.org/x/sys/unix"
  19. )
  20. func getDefaultDaemonConfigDir() (string, error) {
  21. if !rootless.RunningWithNonRootUsername() {
  22. return "/etc/docker", nil
  23. }
  24. // NOTE: CLI uses ~/.docker while the daemon uses ~/.config/docker, because
  25. // ~/.docker was not designed to store daemon configurations.
  26. // In future, the daemon directory may be renamed to ~/.config/moby-engine (?).
  27. configHome, err := homedir.GetConfigHome()
  28. if err != nil {
  29. return "", nil
  30. }
  31. return filepath.Join(configHome, "docker"), nil
  32. }
  33. func getDefaultDaemonConfigFile() (string, error) {
  34. dir, err := getDefaultDaemonConfigDir()
  35. if err != nil {
  36. return "", err
  37. }
  38. return filepath.Join(dir, "daemon.json"), nil
  39. }
  40. // setDefaultUmask sets the umask to 0022 to avoid problems
  41. // caused by custom umask
  42. func setDefaultUmask() error {
  43. desiredUmask := 0022
  44. unix.Umask(desiredUmask)
  45. if umask := unix.Umask(desiredUmask); umask != desiredUmask {
  46. return fmt.Errorf("failed to set umask: expected %#o, got %#o", desiredUmask, umask)
  47. }
  48. return nil
  49. }
  50. func (cli *DaemonCli) getPlatformContainerdDaemonOpts() ([]supervisor.DaemonOpt, error) {
  51. opts := []supervisor.DaemonOpt{
  52. supervisor.WithOOMScore(cli.Config.OOMScoreAdjust),
  53. supervisor.WithPlugin("linux", &linux.Config{
  54. Shim: daemon.DefaultShimBinary,
  55. Runtime: daemon.DefaultRuntimeBinary,
  56. RuntimeRoot: filepath.Join(cli.Config.Root, "runc"),
  57. ShimDebug: cli.Config.Debug,
  58. }),
  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. func wrapListeners(proto string, ls []net.Listener) []net.Listener {
  103. switch proto {
  104. case "unix":
  105. ls[0] = &hack.MalformedHostHeaderOverride{Listener: ls[0]}
  106. case "fd":
  107. for i := range ls {
  108. ls[i] = &hack.MalformedHostHeaderOverride{Listener: ls[i]}
  109. }
  110. }
  111. return ls
  112. }
  113. func newCgroupParent(config *config.Config) string {
  114. cgroupParent := "docker"
  115. useSystemd := daemon.UsingSystemd(config)
  116. if useSystemd {
  117. cgroupParent = "system.slice"
  118. }
  119. if config.CgroupParent != "" {
  120. cgroupParent = config.CgroupParent
  121. }
  122. if useSystemd {
  123. cgroupParent = cgroupParent + ":" + "docker" + ":"
  124. }
  125. return cgroupParent
  126. }