mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
fb04043ca5
* fixed weird logic with "*" * return error if fdNum is failed to parse * check if listener at offset is nil * close unused listeners Signed-off-by: Alexander Morozov <lk4d4@docker.com>
123 lines
2.9 KiB
Go
123 lines
2.9 KiB
Go
// +build freebsd linux
|
|
|
|
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
"github.com/docker/docker/pkg/sockets"
|
|
"github.com/docker/libnetwork/portallocator"
|
|
|
|
systemdActivation "github.com/coreos/go-systemd/activation"
|
|
)
|
|
|
|
// 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) {
|
|
var (
|
|
err error
|
|
ls []net.Listener
|
|
)
|
|
switch proto {
|
|
case "fd":
|
|
ls, err = listenFD(addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
case "tcp":
|
|
l, err := s.initTCPSocket(addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ls = append(ls, l)
|
|
case "unix":
|
|
l, err := sockets.NewUnixSocket(addr, s.cfg.SocketGroup, s.start)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ls = append(ls, l)
|
|
default:
|
|
return nil, fmt.Errorf("Invalid protocol format: %q", proto)
|
|
}
|
|
var res []*HTTPServer
|
|
for _, l := range ls {
|
|
res = append(res, &HTTPServer{
|
|
&http.Server{
|
|
Addr: addr,
|
|
},
|
|
l,
|
|
})
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
if len(listeners) == 0 {
|
|
return nil, fmt.Errorf("No sockets found")
|
|
}
|
|
|
|
// default to all fds just like unix:// and tcp://
|
|
if addr == "" || addr == "*" {
|
|
return listeners, nil
|
|
}
|
|
|
|
fdNum, err := strconv.Atoi(addr)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse systemd address, should be number: %v", err)
|
|
}
|
|
fdOffset := fdNum - 3
|
|
if len(listeners) < int(fdOffset)+1 {
|
|
return nil, fmt.Errorf("Too few socket activated files passed in")
|
|
}
|
|
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)
|
|
}
|
|
}
|
|
return []net.Listener{listeners[fdOffset]}, nil
|
|
}
|