daemon_unix.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 getDaemonConfDir(_ string) (string, error) {
  51. return getDefaultDaemonConfigDir()
  52. }
  53. func (cli *DaemonCli) getPlatformContainerdDaemonOpts() ([]supervisor.DaemonOpt, error) {
  54. opts := []supervisor.DaemonOpt{
  55. supervisor.WithOOMScore(cli.Config.OOMScoreAdjust),
  56. supervisor.WithPlugin("linux", &linux.Config{
  57. Shim: daemon.DefaultShimBinary,
  58. Runtime: daemon.DefaultRuntimeBinary,
  59. RuntimeRoot: filepath.Join(cli.Config.Root, "runc"),
  60. ShimDebug: cli.Config.Debug,
  61. }),
  62. }
  63. return opts, nil
  64. }
  65. // setupConfigReloadTrap configures the USR2 signal to reload the configuration.
  66. func (cli *DaemonCli) setupConfigReloadTrap() {
  67. c := make(chan os.Signal, 1)
  68. signal.Notify(c, unix.SIGHUP)
  69. go func() {
  70. for range c {
  71. cli.reloadConfig()
  72. }
  73. }()
  74. }
  75. // getSwarmRunRoot gets the root directory for swarm to store runtime state
  76. // For example, the control socket
  77. func (cli *DaemonCli) getSwarmRunRoot() string {
  78. return filepath.Join(cli.Config.ExecRoot, "swarm")
  79. }
  80. // allocateDaemonPort ensures that there are no containers
  81. // that try to use any port allocated for the docker server.
  82. func allocateDaemonPort(addr string) error {
  83. host, port, err := net.SplitHostPort(addr)
  84. if err != nil {
  85. return err
  86. }
  87. intPort, err := strconv.Atoi(port)
  88. if err != nil {
  89. return err
  90. }
  91. var hostIPs []net.IP
  92. if parsedIP := net.ParseIP(host); parsedIP != nil {
  93. hostIPs = append(hostIPs, parsedIP)
  94. } else if hostIPs, err = net.LookupIP(host); err != nil {
  95. return fmt.Errorf("failed to lookup %s address in host specification", host)
  96. }
  97. pa := portallocator.Get()
  98. for _, hostIP := range hostIPs {
  99. if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
  100. return fmt.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
  101. }
  102. }
  103. return nil
  104. }
  105. func wrapListeners(proto string, ls []net.Listener) []net.Listener {
  106. switch proto {
  107. case "unix":
  108. ls[0] = &hack.MalformedHostHeaderOverride{Listener: ls[0]}
  109. case "fd":
  110. for i := range ls {
  111. ls[i] = &hack.MalformedHostHeaderOverride{Listener: ls[i]}
  112. }
  113. }
  114. return ls
  115. }
  116. func newCgroupParent(config *config.Config) string {
  117. cgroupParent := "docker"
  118. useSystemd := daemon.UsingSystemd(config)
  119. if useSystemd {
  120. cgroupParent = "system.slice"
  121. }
  122. if config.CgroupParent != "" {
  123. cgroupParent = config.CgroupParent
  124. }
  125. if useSystemd {
  126. cgroupParent = cgroupParent + ":" + "docker" + ":"
  127. }
  128. return cgroupParent
  129. }