server_unix.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // +build freebsd 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. // AcceptConnections allows clients to connect to the API server.
  65. // Referenced Daemon is notified about this server, and waits for the
  66. // daemon acknowledgement before the incoming connections are accepted.
  67. func (s *Server) AcceptConnections(d *daemon.Daemon) {
  68. // Tell the init daemon we are accepting requests
  69. s.daemon = d
  70. s.registerSubRouter()
  71. go systemd.SdNotify("READY=1")
  72. // close the lock so the listeners start accepting connections
  73. select {
  74. case <-s.start:
  75. default:
  76. close(s.start)
  77. }
  78. }
  79. func allocateDaemonPort(addr string) error {
  80. host, port, err := net.SplitHostPort(addr)
  81. if err != nil {
  82. return err
  83. }
  84. intPort, err := strconv.Atoi(port)
  85. if err != nil {
  86. return err
  87. }
  88. var hostIPs []net.IP
  89. if parsedIP := net.ParseIP(host); parsedIP != nil {
  90. hostIPs = append(hostIPs, parsedIP)
  91. } else if hostIPs, err = net.LookupIP(host); err != nil {
  92. return fmt.Errorf("failed to lookup %s address in host specification", host)
  93. }
  94. pa := portallocator.Get()
  95. for _, hostIP := range hostIPs {
  96. if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
  97. return fmt.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
  98. }
  99. }
  100. return nil
  101. }
  102. func adjustCPUShares(version version.Version, hostConfig *runconfig.HostConfig) {
  103. if version.LessThan("1.19") {
  104. if hostConfig != nil && hostConfig.CPUShares > 0 {
  105. // Handle unsupported CpuShares
  106. if hostConfig.CPUShares < linuxMinCPUShares {
  107. logrus.Warnf("Changing requested CpuShares of %d to minimum allowed of %d", hostConfig.CPUShares, linuxMinCPUShares)
  108. hostConfig.CPUShares = linuxMinCPUShares
  109. } else if hostConfig.CPUShares > linuxMaxCPUShares {
  110. logrus.Warnf("Changing requested CpuShares of %d to maximum allowed of %d", hostConfig.CPUShares, linuxMaxCPUShares)
  111. hostConfig.CPUShares = linuxMaxCPUShares
  112. }
  113. }
  114. }
  115. }
  116. // getContainersByNameDownlevel performs processing for pre 1.20 APIs. This
  117. // is only relevant on non-Windows daemons.
  118. func getContainersByNameDownlevel(w http.ResponseWriter, s *Server, namevar string) error {
  119. containerJSONRaw, err := s.daemon.ContainerInspectPre120(namevar)
  120. if err != nil {
  121. return err
  122. }
  123. return writeJSON(w, http.StatusOK, containerJSONRaw)
  124. }