daemon_unix.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/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. return opts, nil
  52. }
  53. // setupConfigReloadTrap configures the USR2 signal to reload the configuration.
  54. func (cli *DaemonCli) setupConfigReloadTrap() {
  55. c := make(chan os.Signal, 1)
  56. signal.Notify(c, unix.SIGHUP)
  57. go func() {
  58. for range c {
  59. cli.reloadConfig()
  60. }
  61. }()
  62. }
  63. // getSwarmRunRoot gets the root directory for swarm to store runtime state
  64. // For example, the control socket
  65. func (cli *DaemonCli) getSwarmRunRoot() string {
  66. return filepath.Join(cli.Config.ExecRoot, "swarm")
  67. }
  68. // allocateDaemonPort ensures that there are no containers
  69. // that try to use any port allocated for the docker server.
  70. func allocateDaemonPort(addr string) error {
  71. host, port, err := net.SplitHostPort(addr)
  72. if err != nil {
  73. return err
  74. }
  75. intPort, err := strconv.Atoi(port)
  76. if err != nil {
  77. return err
  78. }
  79. var hostIPs []net.IP
  80. if parsedIP := net.ParseIP(host); parsedIP != nil {
  81. hostIPs = append(hostIPs, parsedIP)
  82. } else if hostIPs, err = net.LookupIP(host); err != nil {
  83. return fmt.Errorf("failed to lookup %s address in host specification", host)
  84. }
  85. pa := portallocator.Get()
  86. for _, hostIP := range hostIPs {
  87. if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
  88. return fmt.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
  89. }
  90. }
  91. return nil
  92. }
  93. func wrapListeners(proto string, ls []net.Listener) []net.Listener {
  94. switch proto {
  95. case "unix":
  96. ls[0] = &hack.MalformedHostHeaderOverride{Listener: ls[0]}
  97. case "fd":
  98. for i := range ls {
  99. ls[i] = &hack.MalformedHostHeaderOverride{Listener: ls[i]}
  100. }
  101. }
  102. return ls
  103. }