daemon_unix.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. }
  55. if cli.Config.ContainerdAddr != "" {
  56. opts = append(opts, libcontainerd.WithRemoteAddr(cli.Config.ContainerdAddr))
  57. } else {
  58. opts = append(opts, libcontainerd.WithStartDaemon(true))
  59. }
  60. if daemon.UsingSystemd(cli.Config) {
  61. args := []string{"--systemd-cgroup=true"}
  62. opts = append(opts, libcontainerd.WithRuntimeArgs(args))
  63. }
  64. if cli.Config.LiveRestore {
  65. opts = append(opts, libcontainerd.WithLiveRestore(true))
  66. }
  67. opts = append(opts, libcontainerd.WithRuntimePath(daemon.DefaultRuntimeBinary))
  68. return opts
  69. }
  70. // getLibcontainerdRoot gets the root directory for libcontainerd/containerd to
  71. // store their state.
  72. func (cli *DaemonCli) getLibcontainerdRoot() string {
  73. return filepath.Join(cli.Config.ExecRoot, "libcontainerd")
  74. }
  75. // allocateDaemonPort ensures that there are no containers
  76. // that try to use any port allocated for the docker server.
  77. func allocateDaemonPort(addr string) error {
  78. host, port, err := net.SplitHostPort(addr)
  79. if err != nil {
  80. return err
  81. }
  82. intPort, err := strconv.Atoi(port)
  83. if err != nil {
  84. return err
  85. }
  86. var hostIPs []net.IP
  87. if parsedIP := net.ParseIP(host); parsedIP != nil {
  88. hostIPs = append(hostIPs, parsedIP)
  89. } else if hostIPs, err = net.LookupIP(host); err != nil {
  90. return fmt.Errorf("failed to lookup %s address in host specification", host)
  91. }
  92. pa := portallocator.Get()
  93. for _, hostIP := range hostIPs {
  94. if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
  95. return fmt.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
  96. }
  97. }
  98. return nil
  99. }
  100. // notifyShutdown is called after the daemon shuts down but before the process exits.
  101. func notifyShutdown(err error) {
  102. }
  103. func wrapListeners(proto string, ls []net.Listener) []net.Listener {
  104. switch proto {
  105. case "unix":
  106. ls[0] = &hack.MalformedHostHeaderOverride{ls[0]}
  107. case "fd":
  108. for i := range ls {
  109. ls[i] = &hack.MalformedHostHeaderOverride{ls[i]}
  110. }
  111. }
  112. return ls
  113. }