server_linux.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // +build linux
  2. package server
  3. import (
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "strconv"
  8. "github.com/Sirupsen/logrus"
  9. "github.com/docker/docker/daemon"
  10. "github.com/docker/docker/pkg/sockets"
  11. "github.com/docker/docker/pkg/systemd"
  12. "github.com/docker/docker/pkg/version"
  13. "github.com/docker/docker/runconfig"
  14. "github.com/docker/libnetwork/portallocator"
  15. )
  16. const (
  17. // See http://git.kernel.org/cgit/linux/kernel/git/tip/tip.git/tree/kernel/sched/sched.h?id=8cd9234c64c584432f6992fe944ca9e46ca8ea76#n269
  18. linuxMinCpuShares = 2
  19. linuxMaxCpuShares = 262144
  20. )
  21. // newServer sets up the required serverClosers and does protocol specific checking.
  22. func (s *Server) newServer(proto, addr string) ([]serverCloser, error) {
  23. var (
  24. err error
  25. ls []net.Listener
  26. )
  27. switch proto {
  28. case "fd":
  29. ls, err = systemd.ListenFD(addr)
  30. if err != nil {
  31. return nil, err
  32. }
  33. // We don't want to start serving on these sockets until the
  34. // daemon is initialized and installed. Otherwise required handlers
  35. // won't be ready.
  36. <-s.start
  37. case "tcp":
  38. l, err := s.initTcpSocket(addr)
  39. if err != nil {
  40. return nil, err
  41. }
  42. ls = append(ls, l)
  43. case "unix":
  44. l, err := sockets.NewUnixSocket(addr, s.cfg.SocketGroup, s.start)
  45. if err != nil {
  46. return nil, err
  47. }
  48. ls = append(ls, l)
  49. default:
  50. return nil, fmt.Errorf("Invalid protocol format: %q", proto)
  51. }
  52. var res []serverCloser
  53. for _, l := range ls {
  54. res = append(res, &HttpServer{
  55. &http.Server{
  56. Addr: addr,
  57. Handler: s.router,
  58. },
  59. l,
  60. })
  61. }
  62. return res, nil
  63. }
  64. func (s *Server) AcceptConnections(d *daemon.Daemon) {
  65. // Tell the init daemon we are accepting requests
  66. s.daemon = d
  67. go systemd.SdNotify("READY=1")
  68. // close the lock so the listeners start accepting connections
  69. select {
  70. case <-s.start:
  71. default:
  72. close(s.start)
  73. }
  74. }
  75. func allocateDaemonPort(addr string) error {
  76. host, port, err := net.SplitHostPort(addr)
  77. if err != nil {
  78. return err
  79. }
  80. intPort, err := strconv.Atoi(port)
  81. if err != nil {
  82. return err
  83. }
  84. var hostIPs []net.IP
  85. if parsedIP := net.ParseIP(host); parsedIP != nil {
  86. hostIPs = append(hostIPs, parsedIP)
  87. } else if hostIPs, err = net.LookupIP(host); err != nil {
  88. return fmt.Errorf("failed to lookup %s address in host specification", host)
  89. }
  90. pa := portallocator.Get()
  91. for _, hostIP := range hostIPs {
  92. if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
  93. return fmt.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
  94. }
  95. }
  96. return nil
  97. }
  98. func adjustCpuShares(version version.Version, hostConfig *runconfig.HostConfig) {
  99. if version.LessThan("1.19") {
  100. if hostConfig.CpuShares > 0 {
  101. // Handle unsupported CpuShares
  102. if hostConfig.CpuShares < linuxMinCpuShares {
  103. logrus.Warnf("Changing requested CpuShares of %d to minimum allowed of %d", hostConfig.CpuShares, linuxMinCpuShares)
  104. hostConfig.CpuShares = linuxMinCpuShares
  105. } else if hostConfig.CpuShares > linuxMaxCpuShares {
  106. logrus.Warnf("Changing requested CpuShares of %d to maximum allowed of %d", hostConfig.CpuShares, linuxMaxCpuShares)
  107. hostConfig.CpuShares = linuxMaxCpuShares
  108. }
  109. }
  110. }
  111. }