daemon_unix.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/libcontainerd"
  14. "github.com/docker/libnetwork/portallocator"
  15. "golang.org/x/sys/unix"
  16. )
  17. const defaultDaemonConfigFile = "/etc/docker/daemon.json"
  18. // setDefaultUmask sets the umask to 0022 to avoid problems
  19. // caused by custom umask
  20. func setDefaultUmask() error {
  21. desiredUmask := 0022
  22. unix.Umask(desiredUmask)
  23. if umask := unix.Umask(desiredUmask); umask != desiredUmask {
  24. return fmt.Errorf("failed to set umask: expected %#o, got %#o", desiredUmask, umask)
  25. }
  26. return nil
  27. }
  28. func getDaemonConfDir(_ string) string {
  29. return "/etc/docker"
  30. }
  31. func (cli *DaemonCli) getPlatformRemoteOptions() ([]libcontainerd.RemoteOption, error) {
  32. opts := []libcontainerd.RemoteOption{
  33. libcontainerd.WithOOMScore(cli.Config.OOMScoreAdjust),
  34. libcontainerd.WithPlugin("linux", &linux.Config{
  35. Shim: daemon.DefaultShimBinary,
  36. Runtime: daemon.DefaultRuntimeBinary,
  37. RuntimeRoot: filepath.Join(cli.Config.Root, "runc"),
  38. ShimDebug: cli.Config.Debug,
  39. }),
  40. }
  41. if cli.Config.Debug {
  42. opts = append(opts, libcontainerd.WithLogLevel("debug"))
  43. } else if cli.Config.LogLevel != "" {
  44. opts = append(opts, libcontainerd.WithLogLevel(cli.Config.LogLevel))
  45. }
  46. if cli.Config.ContainerdAddr != "" {
  47. opts = append(opts, libcontainerd.WithRemoteAddr(cli.Config.ContainerdAddr))
  48. } else {
  49. opts = append(opts, libcontainerd.WithStartDaemon(true))
  50. }
  51. if !cli.Config.CriContainerd {
  52. opts = append(opts, libcontainerd.WithPlugin("cri", nil))
  53. }
  54. return opts, nil
  55. }
  56. // setupConfigReloadTrap configures the USR2 signal to reload the configuration.
  57. func (cli *DaemonCli) setupConfigReloadTrap() {
  58. c := make(chan os.Signal, 1)
  59. signal.Notify(c, unix.SIGHUP)
  60. go func() {
  61. for range c {
  62. cli.reloadConfig()
  63. }
  64. }()
  65. }
  66. // getSwarmRunRoot gets the root directory for swarm to store runtime state
  67. // For example, the control socket
  68. func (cli *DaemonCli) getSwarmRunRoot() string {
  69. return filepath.Join(cli.Config.ExecRoot, "swarm")
  70. }
  71. // allocateDaemonPort ensures that there are no containers
  72. // that try to use any port allocated for the docker server.
  73. func allocateDaemonPort(addr string) error {
  74. host, port, err := net.SplitHostPort(addr)
  75. if err != nil {
  76. return err
  77. }
  78. intPort, err := strconv.Atoi(port)
  79. if err != nil {
  80. return err
  81. }
  82. var hostIPs []net.IP
  83. if parsedIP := net.ParseIP(host); parsedIP != nil {
  84. hostIPs = append(hostIPs, parsedIP)
  85. } else if hostIPs, err = net.LookupIP(host); err != nil {
  86. return fmt.Errorf("failed to lookup %s address in host specification", host)
  87. }
  88. pa := portallocator.Get()
  89. for _, hostIP := range hostIPs {
  90. if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
  91. return fmt.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
  92. }
  93. }
  94. return nil
  95. }
  96. func wrapListeners(proto string, ls []net.Listener) []net.Listener {
  97. switch proto {
  98. case "unix":
  99. ls[0] = &hack.MalformedHostHeaderOverride{Listener: ls[0]}
  100. case "fd":
  101. for i := range ls {
  102. ls[i] = &hack.MalformedHostHeaderOverride{Listener: ls[i]}
  103. }
  104. }
  105. return ls
  106. }