mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Refactor listenFD function
* 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>
This commit is contained in:
parent
ce0457a2c9
commit
fb04043ca5
1 changed files with 22 additions and 12 deletions
|
@ -8,6 +8,7 @@ import (
|
|||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/docker/docker/pkg/sockets"
|
||||
"github.com/docker/libnetwork/portallocator"
|
||||
|
||||
|
@ -90,24 +91,33 @@ func listenFD(addr string) ([]net.Listener, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if listeners == nil || len(listeners) == 0 {
|
||||
if len(listeners) == 0 {
|
||||
return nil, fmt.Errorf("No sockets found")
|
||||
}
|
||||
|
||||
// default to all fds just like unix:// and tcp://
|
||||
if addr == "" {
|
||||
addr = "*"
|
||||
}
|
||||
|
||||
fdNum, _ := strconv.Atoi(addr)
|
||||
fdOffset := fdNum - 3
|
||||
if (addr != "*") && (len(listeners) < int(fdOffset)+1) {
|
||||
return nil, fmt.Errorf("Too few socket activated files passed in")
|
||||
}
|
||||
|
||||
if addr == "*" {
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue