daemon_unix.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // +build !windows
  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/daemon"
  12. "github.com/docker/docker/libcontainerd"
  13. "github.com/docker/docker/pkg/system"
  14. "github.com/docker/libnetwork/portallocator"
  15. )
  16. const defaultDaemonConfigFile = "/etc/docker/daemon.json"
  17. // currentUserIsOwner checks whether the current user is the owner of the given
  18. // file.
  19. func currentUserIsOwner(f string) bool {
  20. if fileInfo, err := system.Stat(f); err == nil && fileInfo != nil {
  21. if int(fileInfo.UID()) == os.Getuid() {
  22. return true
  23. }
  24. }
  25. return false
  26. }
  27. // setDefaultUmask sets the umask to 0022 to avoid problems
  28. // caused by custom umask
  29. func setDefaultUmask() error {
  30. desiredUmask := 0022
  31. syscall.Umask(desiredUmask)
  32. if umask := syscall.Umask(desiredUmask); umask != desiredUmask {
  33. return fmt.Errorf("failed to set umask: expected %#o, got %#o", desiredUmask, umask)
  34. }
  35. return nil
  36. }
  37. func getDaemonConfDir() string {
  38. return "/etc/docker"
  39. }
  40. // setupConfigReloadTrap configures the USR2 signal to reload the configuration.
  41. func (cli *DaemonCli) setupConfigReloadTrap() {
  42. c := make(chan os.Signal, 1)
  43. signal.Notify(c, syscall.SIGHUP)
  44. go func() {
  45. for range c {
  46. cli.reloadConfig()
  47. }
  48. }()
  49. }
  50. func (cli *DaemonCli) getPlatformRemoteOptions() []libcontainerd.RemoteOption {
  51. opts := []libcontainerd.RemoteOption{
  52. libcontainerd.WithDebugLog(cli.Config.Debug),
  53. }
  54. if cli.Config.ContainerdAddr != "" {
  55. opts = append(opts, libcontainerd.WithRemoteAddr(cli.Config.ContainerdAddr))
  56. } else {
  57. opts = append(opts, libcontainerd.WithStartDaemon(true))
  58. }
  59. if daemon.UsingSystemd(cli.Config) {
  60. args := []string{"--systemd-cgroup=true"}
  61. opts = append(opts, libcontainerd.WithRuntimeArgs(args))
  62. }
  63. return opts
  64. }
  65. // getLibcontainerdRoot gets the root directory for libcontainerd/containerd to
  66. // store their state.
  67. func (cli *DaemonCli) getLibcontainerdRoot() string {
  68. return filepath.Join(cli.Config.ExecRoot, "libcontainerd")
  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. }