2016-02-11 18:30:23 +00:00
|
|
|
// +build !windows
|
2015-02-13 19:46:14 +00:00
|
|
|
|
2016-02-11 18:30:23 +00:00
|
|
|
package listeners
|
2015-02-13 19:46:14 +00:00
|
|
|
|
|
|
|
import (
|
2015-11-03 10:03:35 +00:00
|
|
|
"crypto/tls"
|
2015-02-13 19:46:14 +00:00
|
|
|
"fmt"
|
2015-03-31 20:37:49 +00:00
|
|
|
"net"
|
2015-05-15 20:05:35 +00:00
|
|
|
"strconv"
|
2015-02-13 19:46:14 +00:00
|
|
|
|
2015-10-07 21:23:53 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-02-11 18:30:23 +00:00
|
|
|
"github.com/coreos/go-systemd/activation"
|
2015-12-30 00:27:12 +00:00
|
|
|
"github.com/docker/go-connections/sockets"
|
2015-05-15 20:05:35 +00:00
|
|
|
"github.com/docker/libnetwork/portallocator"
|
2015-02-13 19:46:14 +00:00
|
|
|
)
|
|
|
|
|
2016-02-11 18:30:23 +00:00
|
|
|
// Init creates new listeners for the server.
|
|
|
|
func Init(proto, addr, socketGroup string, tlsConfig *tls.Config) (ls []net.Listener, err error) {
|
2015-02-13 19:46:14 +00:00
|
|
|
switch proto {
|
|
|
|
case "fd":
|
2016-02-11 18:30:23 +00:00
|
|
|
ls, err = listenFD(addr, tlsConfig)
|
2015-03-31 20:37:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-13 19:46:14 +00:00
|
|
|
case "tcp":
|
2016-02-11 18:30:23 +00:00
|
|
|
l, err := initTCPSocket(addr, tlsConfig)
|
2015-05-20 23:48:39 +00:00
|
|
|
if err != nil {
|
2015-03-31 20:37:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-28 19:15:03 +00:00
|
|
|
ls = append(ls, l)
|
2015-02-13 19:46:14 +00:00
|
|
|
case "unix":
|
2016-02-11 18:30:23 +00:00
|
|
|
l, err := sockets.NewUnixSocket(addr, socketGroup)
|
2015-05-28 19:15:03 +00:00
|
|
|
if err != nil {
|
2015-12-09 14:18:45 +00:00
|
|
|
return nil, fmt.Errorf("can't create unix socket %s: %v", addr, err)
|
2015-02-13 23:08:42 +00:00
|
|
|
}
|
2015-05-28 19:15:03 +00:00
|
|
|
ls = append(ls, l)
|
2015-03-31 20:37:49 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Invalid protocol format: %q", proto)
|
2015-02-13 23:08:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-11 18:30:23 +00:00
|
|
|
return
|
2015-05-15 20:05:35 +00:00
|
|
|
}
|
2015-06-03 19:01:53 +00:00
|
|
|
|
2015-08-06 12:35:00 +00:00
|
|
|
// listenFD returns the specified socket activated files as a slice of
|
|
|
|
// net.Listeners or all of the activated files if "*" is given.
|
2015-11-03 10:03:35 +00:00
|
|
|
func listenFD(addr string, tlsConfig *tls.Config) ([]net.Listener, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
listeners []net.Listener
|
|
|
|
)
|
2015-08-06 12:35:00 +00:00
|
|
|
// socket activation
|
2015-11-03 10:03:35 +00:00
|
|
|
if tlsConfig != nil {
|
2016-02-11 18:30:23 +00:00
|
|
|
listeners, err = activation.TLSListeners(false, tlsConfig)
|
2015-11-03 10:03:35 +00:00
|
|
|
} else {
|
2016-02-11 18:30:23 +00:00
|
|
|
listeners, err = activation.Listeners(false)
|
2015-11-03 10:03:35 +00:00
|
|
|
}
|
2015-08-06 12:35:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-07 21:23:53 +00:00
|
|
|
if len(listeners) == 0 {
|
2015-08-06 12:35:00 +00:00
|
|
|
return nil, fmt.Errorf("No sockets found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// default to all fds just like unix:// and tcp://
|
2015-10-07 21:23:53 +00:00
|
|
|
if addr == "" || addr == "*" {
|
|
|
|
return listeners, nil
|
2015-08-06 12:35:00 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 21:23:53 +00: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 12:35:00 +00:00
|
|
|
fdOffset := fdNum - 3
|
2015-10-07 21:23:53 +00:00
|
|
|
if len(listeners) < int(fdOffset)+1 {
|
2015-08-06 12:35:00 +00:00
|
|
|
return nil, fmt.Errorf("Too few socket activated files passed in")
|
|
|
|
}
|
2015-10-07 21:23:53 +00: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 12:35:00 +00:00
|
|
|
}
|
|
|
|
return []net.Listener{listeners[fdOffset]}, nil
|
|
|
|
}
|
2016-02-11 18:30:23 +00:00
|
|
|
|
|
|
|
// allocateDaemonPort ensures that there are no containers
|
|
|
|
// that try to use any port allocated for the docker server.
|
|
|
|
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
|
|
|
|
}
|