daemon_unix.go 3.0 KB

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