Skip to content

Commit

Permalink
Refactor listenFD function
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
LK4D4 committed Oct 7, 2015
1 parent ce0457a commit fb04043
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions api/server/server_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"strconv"

"github.com/Sirupsen/logrus"
"github.com/docker/docker/pkg/sockets"
"github.com/docker/libnetwork/portallocator"

Expand Down Expand Up @@ -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 = "*"
if addr == "" || addr == "*" {
return listeners, nil
}

fdNum, _ := strconv.Atoi(addr)
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 (addr != "*") && (len(listeners) < int(fdOffset)+1) {
if len(listeners) < int(fdOffset)+1 {
return nil, fmt.Errorf("Too few socket activated files passed in")
}

if addr == "*" {
return listeners, nil
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
}

0 comments on commit fb04043

Please sign in to comment.