daemon_unix.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // +build !windows,!solaris
  2. package main
  3. import (
  4. "fmt"
  5. "net"
  6. "os"
  7. "os/signal"
  8. "path/filepath"
  9. "strconv"
  10. "syscall"
  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. )
  16. const defaultDaemonConfigFile = "/etc/docker/daemon.json"
  17. // setDefaultUmask sets the umask to 0022 to avoid problems
  18. // caused by custom umask
  19. func setDefaultUmask() error {
  20. desiredUmask := 0022
  21. syscall.Umask(desiredUmask)
  22. if umask := syscall.Umask(desiredUmask); umask != desiredUmask {
  23. return fmt.Errorf("failed to set umask: expected %#o, got %#o", desiredUmask, umask)
  24. }
  25. return nil
  26. }
  27. func getDaemonConfDir(_ string) string {
  28. return "/etc/docker"
  29. }
  30. // setupConfigReloadTrap configures the USR2 signal to reload the configuration.
  31. func (cli *DaemonCli) setupConfigReloadTrap() {
  32. c := make(chan os.Signal, 1)
  33. signal.Notify(c, syscall.SIGHUP)
  34. go func() {
  35. for range c {
  36. cli.reloadConfig()
  37. }
  38. }()
  39. }
  40. func (cli *DaemonCli) getPlatformRemoteOptions() []libcontainerd.RemoteOption {
  41. opts := []libcontainerd.RemoteOption{
  42. libcontainerd.WithDebugLog(cli.Config.Debug),
  43. libcontainerd.WithOOMScore(cli.Config.OOMScoreAdjust),
  44. }
  45. if cli.Config.ContainerdAddr != "" {
  46. opts = append(opts, libcontainerd.WithRemoteAddr(cli.Config.ContainerdAddr))
  47. } else {
  48. opts = append(opts, libcontainerd.WithStartDaemon(true))
  49. }
  50. if daemon.UsingSystemd(cli.Config) {
  51. args := []string{"--systemd-cgroup=true"}
  52. opts = append(opts, libcontainerd.WithRuntimeArgs(args))
  53. }
  54. if cli.Config.LiveRestoreEnabled {
  55. opts = append(opts, libcontainerd.WithLiveRestore(true))
  56. }
  57. opts = append(opts, libcontainerd.WithRuntimePath(daemon.DefaultRuntimeBinary))
  58. return opts
  59. }
  60. // getLibcontainerdRoot gets the root directory for libcontainerd/containerd to
  61. // store their state.
  62. func (cli *DaemonCli) getLibcontainerdRoot() string {
  63. return filepath.Join(cli.Config.ExecRoot, "libcontainerd")
  64. }
  65. // getSwarmRunRoot gets the root directory for swarm to store runtime state
  66. // For example, the control socket
  67. func (cli *DaemonCli) getSwarmRunRoot() string {
  68. return filepath.Join(cli.Config.ExecRoot, "swarm")
  69. }
  70. // allocateDaemonPort ensures that there are no containers
  71. // that try to use any port allocated for the docker server.
  72. func allocateDaemonPort(addr string) error {
  73. host, port, err := net.SplitHostPort(addr)
  74. if err != nil {
  75. return err
  76. }
  77. intPort, err := strconv.Atoi(port)
  78. if err != nil {
  79. return err
  80. }
  81. var hostIPs []net.IP
  82. if parsedIP := net.ParseIP(host); parsedIP != nil {
  83. hostIPs = append(hostIPs, parsedIP)
  84. } else if hostIPs, err = net.LookupIP(host); err != nil {
  85. return fmt.Errorf("failed to lookup %s address in host specification", host)
  86. }
  87. pa := portallocator.Get()
  88. for _, hostIP := range hostIPs {
  89. if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
  90. return fmt.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
  91. }
  92. }
  93. return nil
  94. }
  95. // notifyShutdown is called after the daemon shuts down but before the process exits.
  96. func notifyShutdown(err error) {
  97. }
  98. func wrapListeners(proto string, ls []net.Listener) []net.Listener {
  99. switch proto {
  100. case "unix":
  101. ls[0] = &hack.MalformedHostHeaderOverride{ls[0]}
  102. case "fd":
  103. for i := range ls {
  104. ls[i] = &hack.MalformedHostHeaderOverride{ls[i]}
  105. }
  106. }
  107. return ls
  108. }