daemon_unix.go 3.4 KB

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