Skip to content

Commit

Permalink
sdk: check systemd running for socket activation
Browse files Browse the repository at this point in the history
Signed-off-by: Antonio Murdaca <[email protected]>
  • Loading branch information
runcom committed Mar 9, 2016
1 parent c9f224a commit f0fe1a9
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions sdk/unix_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"

"github.com/coreos/go-systemd/activation"
"github.com/coreos/go-systemd/util"
"github.com/docker/go-connections/sockets"
)

Expand All @@ -21,20 +22,15 @@ func newUnixListener(pluginName string, group string) (net.Listener, string, err
if err != nil {
return nil, "", err
}
listenFds := activation.Files(false)
if len(listenFds) > 1 {
return nil, "", fmt.Errorf("expected only one socket from systemd, got %d", len(listenFds))
listener, err := setupSocketActivation()
if err != nil {
return nil, "", err
}
if len(listenFds) == 1 {
l, err := net.FileListener(listenFds[0])
if listener == nil {
listener, err = sockets.NewUnixSocket(path, group)
if err != nil {
return nil, "", err
}
return l, path, nil
}
listener, err := sockets.NewUnixSocket(path, group)
if err != nil {
return nil, "", err
}
return listener, path, nil
}
Expand All @@ -48,3 +44,22 @@ func fullSocketAddress(address string) (string, error) {
}
return filepath.Join(pluginSockDir, address+".sock"), nil
}

func setupSocketActivation() (net.Listener, error) {
if !util.IsRunningSystemd() {
return nil, nil
}
listenFds := activation.Files(false)
if len(listenFds) > 1 {
return nil, fmt.Errorf("expected only one socket from systemd, got %d", len(listenFds))
}
var listener net.Listener
if len(listenFds) == 1 {
l, err := net.FileListener(listenFds[0])
if err != nil {
return nil, err
}
listener = l
}
return listener, nil
}

0 comments on commit f0fe1a9

Please sign in to comment.