server_unix.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // +build freebsd linux
  2. package server
  3. import (
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "strconv"
  8. "github.com/docker/docker/daemon"
  9. "github.com/docker/docker/pkg/sockets"
  10. "github.com/docker/docker/pkg/systemd"
  11. "github.com/docker/libnetwork/portallocator"
  12. )
  13. // newServer sets up the required serverClosers and does protocol specific checking.
  14. func (s *Server) newServer(proto, addr string) ([]serverCloser, error) {
  15. var (
  16. err error
  17. ls []net.Listener
  18. )
  19. switch proto {
  20. case "fd":
  21. ls, err = systemd.ListenFD(addr)
  22. if err != nil {
  23. return nil, err
  24. }
  25. // We don't want to start serving on these sockets until the
  26. // daemon is initialized and installed. Otherwise required handlers
  27. // won't be ready.
  28. <-s.start
  29. case "tcp":
  30. l, err := s.initTCPSocket(addr)
  31. if err != nil {
  32. return nil, err
  33. }
  34. ls = append(ls, l)
  35. case "unix":
  36. l, err := sockets.NewUnixSocket(addr, s.cfg.SocketGroup, s.start)
  37. if err != nil {
  38. return nil, err
  39. }
  40. ls = append(ls, l)
  41. default:
  42. return nil, fmt.Errorf("Invalid protocol format: %q", proto)
  43. }
  44. var res []serverCloser
  45. for _, l := range ls {
  46. res = append(res, &HTTPServer{
  47. &http.Server{
  48. Addr: addr,
  49. Handler: s.router,
  50. },
  51. l,
  52. })
  53. }
  54. return res, nil
  55. }
  56. // AcceptConnections allows clients to connect to the API server.
  57. // Referenced Daemon is notified about this server, and waits for the
  58. // daemon acknowledgement before the incoming connections are accepted.
  59. func (s *Server) AcceptConnections(d *daemon.Daemon) {
  60. // Tell the init daemon we are accepting requests
  61. s.daemon = d
  62. s.registerSubRouter()
  63. go systemd.SdNotify("READY=1")
  64. // close the lock so the listeners start accepting connections
  65. select {
  66. case <-s.start:
  67. default:
  68. close(s.start)
  69. }
  70. }
  71. func allocateDaemonPort(addr string) error {
  72. host, port, err := net.SplitHostPort(addr)
  73. if err != nil {
  74. return err
  75. }
  76. intPort, err := strconv.Atoi(port)
  77. if err != nil {
  78. return err
  79. }
  80. var hostIPs []net.IP
  81. if parsedIP := net.ParseIP(host); parsedIP != nil {
  82. hostIPs = append(hostIPs, parsedIP)
  83. } else if hostIPs, err = net.LookupIP(host); err != nil {
  84. return fmt.Errorf("failed to lookup %s address in host specification", host)
  85. }
  86. pa := portallocator.Get()
  87. for _, hostIP := range hostIPs {
  88. if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
  89. return fmt.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
  90. }
  91. }
  92. return nil
  93. }
  94. // getContainersByNameDownlevel performs processing for pre 1.20 APIs. This
  95. // is only relevant on non-Windows daemons.
  96. func getContainersByNameDownlevel(w http.ResponseWriter, s *Server, namevar string) error {
  97. containerJSONRaw, err := s.daemon.ContainerInspectPre120(namevar)
  98. if err != nil {
  99. return err
  100. }
  101. return writeJSON(w, http.StatusOK, containerJSONRaw)
  102. }