daemon_unix.go 2.7 KB

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