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