daemon_unix.go 3.2 KB

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