Skip to content

Commit

Permalink
daemon: listeners: make systemd listener optional
Browse files Browse the repository at this point in the history
On non systemd systems, the systemd-based activation is not needed at all,
thus make it build-time optional. It won't be built in if the 'no_systemd'
tag is set.

changes v2: renamed 'nosystemd' tag to 'no_systemd'
changes v3: fixed some obsolete imports
            use new style tags

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
  • Loading branch information
metux committed Aug 25, 2023
1 parent 3e5b2a6 commit dd37233
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 50 deletions.
50 changes: 0 additions & 50 deletions daemon/listeners/listeners_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"crypto/tls"
"net"
"os"
"strconv"

"github.com/containerd/containerd/log"
"github.com/coreos/go-systemd/v22/activation"
"github.com/docker/docker/pkg/homedir"
"github.com/docker/go-connections/sockets"
"github.com/pkg/errors"
Expand Down Expand Up @@ -58,51 +56,3 @@ func Init(proto, addr, socketGroup string, tlsConfig *tls.Config) ([]net.Listene

return ls, 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, tlsConfig *tls.Config) ([]net.Listener, error) {
var (
err error
listeners []net.Listener
)
// socket activation
if tlsConfig != nil {
listeners, err = activation.TLSListeners(tlsConfig)
} else {
listeners, err = activation.Listeners()
}
if err != nil {
return nil, err
}

if len(listeners) == 0 {
return nil, errors.New("no sockets found via socket activation: make sure the service was started by systemd")
}

// 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, errors.Errorf("failed to parse systemd fd address: should be a number: %v", addr)
}
fdOffset := fdNum - 3
if len(listeners) < fdOffset+1 {
return nil, errors.New("too few socket activated files passed in by systemd")
}
if listeners[fdOffset] == nil {
return nil, errors.Errorf("failed to listen on systemd activated file: fd %d", fdOffset+3)
}
for i, ls := range listeners {
if i == fdOffset || ls == nil {
continue
}
if err := ls.Close(); err != nil {
return nil, errors.Wrapf(err, "failed to close systemd activated file: fd %d", fdOffset+3)
}
}
return []net.Listener{listeners[fdOffset]}, nil
}
13 changes: 13 additions & 0 deletions daemon/listeners/listenfd_nosystemd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build linux && no_systemd

package listeners // import "github.com/docker/docker/daemon/listeners"

import (
"crypto/tls"
"errors"
"net"
)

func listenFD(addr string, tlsConfig *tls.Config) ([]net.Listener, error) {
return nil, errors.New("listenFD not implemented")
}
60 changes: 60 additions & 0 deletions daemon/listeners/listenfd_systemd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//go:build linux && !no_systemd

package listeners // import "github.com/docker/docker/daemon/listeners"

import (
"crypto/tls"
"net"
"strconv"

"github.com/coreos/go-systemd/v22/activation"
"github.com/pkg/errors"
)

// 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, tlsConfig *tls.Config) ([]net.Listener, error) {
var (
err error
listeners []net.Listener
)
// socket activation
if tlsConfig != nil {
listeners, err = activation.TLSListeners(tlsConfig)
} else {
listeners, err = activation.Listeners()
}
if err != nil {
return nil, err
}

if len(listeners) == 0 {
return nil, errors.New("no sockets found via socket activation: make sure the service was started by systemd")
}

// 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, errors.Errorf("failed to parse systemd fd address: should be a number: %v", addr)
}
fdOffset := fdNum - 3
if len(listeners) < fdOffset+1 {
return nil, errors.New("too few socket activated files passed in by systemd")
}
if listeners[fdOffset] == nil {
return nil, errors.Errorf("failed to listen on systemd activated file: fd %d", fdOffset+3)
}
for i, ls := range listeners {
if i == fdOffset || ls == nil {
continue
}
if err := ls.Close(); err != nil {
return nil, errors.Wrapf(err, "failed to close systemd activated file: fd %d", fdOffset+3)
}
}
return []net.Listener{listeners[fdOffset]}, nil
}

0 comments on commit dd37233

Please sign in to comment.