2015-07-29 14:25:56 -04:00
|
|
|
// +build freebsd linux
|
2015-02-13 14:46:14 -05:00
|
|
|
|
|
|
|
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-10-07 17:23:53 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-05-20 19:48:39 -04:00
|
|
|
"github.com/docker/docker/pkg/sockets"
|
2015-05-15 16:05:35 -04:00
|
|
|
"github.com/docker/libnetwork/portallocator"
|
2015-08-06 08:35:00 -04:00
|
|
|
|
|
|
|
systemdActivation "github.com/coreos/go-systemd/activation"
|
2015-02-13 14:46:14 -05:00
|
|
|
)
|
|
|
|
|
2015-10-05 12:32:08 -04:00
|
|
|
// newServer sets up the required HTTPServers and does protocol specific checking.
|
|
|
|
// newServer does not set any muxers, you should set it later to Handler field
|
|
|
|
func (s *Server) newServer(proto, addr string) ([]*HTTPServer, 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-08-06 08:35:00 -04:00
|
|
|
ls, err = listenFD(addr)
|
2015-03-31 16:37:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-13 14:46:14 -05:00
|
|
|
case "tcp":
|
2015-07-21 01:15:44 -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-10-05 12:32:08 -04:00
|
|
|
var res []*HTTPServer
|
2015-05-28 15:15:03 -04:00
|
|
|
for _, l := range ls {
|
2015-07-21 01:15:44 -04:00
|
|
|
res = append(res, &HTTPServer{
|
2015-05-28 15:15:03 -04:00
|
|
|
&http.Server{
|
2015-10-05 12:32:08 -04:00
|
|
|
Addr: addr,
|
2015-05-28 15:15:03 -04:00
|
|
|
},
|
|
|
|
l,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return res, nil
|
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
|
|
|
|
}
|
2015-06-03 15:01:53 -04:00
|
|
|
|
2015-08-06 08:35:00 -04:00
|
|
|
// listenFD returns the specified socket activated files as a slice of
|
|
|
|
// net.Listeners or all of the activated files if "*" is given.
|
|
|
|
func listenFD(addr string) ([]net.Listener, error) {
|
|
|
|
// socket activation
|
|
|
|
listeners, err := systemdActivation.Listeners(false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-07 17:23:53 -04:00
|
|
|
if len(listeners) == 0 {
|
2015-08-06 08:35:00 -04:00
|
|
|
return nil, fmt.Errorf("No sockets found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// default to all fds just like unix:// and tcp://
|
2015-10-07 17:23:53 -04:00
|
|
|
if addr == "" || addr == "*" {
|
|
|
|
return listeners, nil
|
2015-08-06 08:35:00 -04:00
|
|
|
}
|
|
|
|
|
2015-10-07 17:23:53 -04:00
|
|
|
fdNum, err := strconv.Atoi(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse systemd address, should be number: %v", err)
|
|
|
|
}
|
2015-08-06 08:35:00 -04:00
|
|
|
fdOffset := fdNum - 3
|
2015-10-07 17:23:53 -04:00
|
|
|
if len(listeners) < int(fdOffset)+1 {
|
2015-08-06 08:35:00 -04:00
|
|
|
return nil, fmt.Errorf("Too few socket activated files passed in")
|
|
|
|
}
|
2015-10-07 17:23:53 -04:00
|
|
|
if listeners[fdOffset] == nil {
|
|
|
|
return nil, fmt.Errorf("failed to listen on systemd activated file at fd %d", fdOffset+3)
|
|
|
|
}
|
|
|
|
for i, ls := range listeners {
|
|
|
|
if i == fdOffset || ls == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := ls.Close(); err != nil {
|
|
|
|
logrus.Errorf("Failed to close systemd activated file at fd %d: %v", fdOffset+3, err)
|
|
|
|
}
|
2015-08-06 08:35:00 -04:00
|
|
|
}
|
|
|
|
return []net.Listener{listeners[fdOffset]}, nil
|
|
|
|
}
|