2015-02-13 14:46:14 -05:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-03-31 16:37:49 -04:00
|
|
|
"net"
|
2015-02-13 14:46:14 -05:00
|
|
|
"net/http"
|
2015-05-15 16:05:35 -04:00
|
|
|
"strconv"
|
2015-02-13 14:46:14 -05:00
|
|
|
|
2015-04-17 17:32:18 -04:00
|
|
|
"github.com/docker/docker/daemon"
|
2015-05-20 19:48:39 -04:00
|
|
|
"github.com/docker/docker/pkg/sockets"
|
2015-02-13 18:08:42 -05:00
|
|
|
"github.com/docker/docker/pkg/systemd"
|
2015-05-15 16:05:35 -04:00
|
|
|
"github.com/docker/libnetwork/portallocator"
|
2015-02-13 14:46:14 -05:00
|
|
|
)
|
|
|
|
|
2015-05-28 15:15:03 -04:00
|
|
|
// newServer sets up the required serverClosers and does protocol specific checking.
|
|
|
|
func (s *Server) newServer(proto, addr string) ([]serverCloser, error) {
|
2015-03-31 16:37:49 -04:00
|
|
|
var (
|
|
|
|
err error
|
2015-05-28 15:15:03 -04:00
|
|
|
ls []net.Listener
|
2015-03-31 16:37:49 -04:00
|
|
|
)
|
2015-02-13 14:46:14 -05:00
|
|
|
switch proto {
|
|
|
|
case "fd":
|
2015-05-28 15:15:03 -04:00
|
|
|
ls, err = systemd.ListenFD(addr)
|
2015-03-31 16:37:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// We don't want to start serving on these sockets until the
|
|
|
|
// daemon is initialized and installed. Otherwise required handlers
|
|
|
|
// won't be ready.
|
2015-04-17 17:32:18 -04:00
|
|
|
<-s.start
|
2015-02-13 14:46:14 -05:00
|
|
|
case "tcp":
|
2015-05-28 15:15:03 -04:00
|
|
|
l, err := s.initTcpSocket(addr)
|
2015-05-20 19:48:39 -04:00
|
|
|
if err != nil {
|
2015-03-31 16:37:49 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-28 15:15:03 -04:00
|
|
|
ls = append(ls, l)
|
2015-02-13 14:46:14 -05:00
|
|
|
case "unix":
|
2015-05-28 15:15:03 -04:00
|
|
|
l, err := sockets.NewUnixSocket(addr, s.cfg.SocketGroup, s.start)
|
|
|
|
if err != nil {
|
2015-03-31 16:37:49 -04:00
|
|
|
return nil, err
|
2015-02-13 18:08:42 -05:00
|
|
|
}
|
2015-05-28 15:15:03 -04:00
|
|
|
ls = append(ls, l)
|
2015-03-31 16:37:49 -04:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Invalid protocol format: %q", proto)
|
2015-02-13 18:08:42 -05:00
|
|
|
}
|
2015-05-28 15:15:03 -04:00
|
|
|
var res []serverCloser
|
|
|
|
for _, l := range ls {
|
|
|
|
res = append(res, &HttpServer{
|
|
|
|
&http.Server{
|
|
|
|
Addr: addr,
|
|
|
|
Handler: s.router,
|
|
|
|
},
|
|
|
|
l,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return res, nil
|
2015-02-13 18:08:42 -05:00
|
|
|
}
|
|
|
|
|
2015-04-17 17:32:18 -04:00
|
|
|
func (s *Server) AcceptConnections(d *daemon.Daemon) {
|
2015-02-13 18:08:42 -05:00
|
|
|
// Tell the init daemon we are accepting requests
|
2015-04-17 17:32:18 -04:00
|
|
|
s.daemon = d
|
2015-02-13 18:08:42 -05:00
|
|
|
go systemd.SdNotify("READY=1")
|
|
|
|
// close the lock so the listeners start accepting connections
|
2015-03-31 18:12:15 -04:00
|
|
|
select {
|
2015-04-17 17:32:18 -04:00
|
|
|
case <-s.start:
|
2015-03-31 18:12:15 -04:00
|
|
|
default:
|
2015-04-17 17:32:18 -04:00
|
|
|
close(s.start)
|
2015-02-13 18:08:42 -05:00
|
|
|
}
|
|
|
|
}
|
2015-05-15 16:05:35 -04:00
|
|
|
|
|
|
|
func allocateDaemonPort(addr string) error {
|
|
|
|
host, port, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
intPort, err := strconv.Atoi(port)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var hostIPs []net.IP
|
|
|
|
if parsedIP := net.ParseIP(host); parsedIP != nil {
|
|
|
|
hostIPs = append(hostIPs, parsedIP)
|
|
|
|
} else if hostIPs, err = net.LookupIP(host); err != nil {
|
|
|
|
return fmt.Errorf("failed to lookup %s address in host specification", host)
|
|
|
|
}
|
|
|
|
|
|
|
|
pa := portallocator.Get()
|
|
|
|
for _, hostIP := range hostIPs {
|
|
|
|
if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
|
|
|
|
return fmt.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|