daemon_unix.go 3.1 KB

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