Skip to content

Commit 008703b

Browse files
committed
allow building without systemd dependency
Signed-off-by: Tibor Vass <[email protected]>
1 parent 242fb7c commit 008703b

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

sdk/unix_listener.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import (
1010
"github.com/docker/go-connections/sockets"
1111
)
1212

13-
const (
14-
pluginSockDir = "/run/docker/plugins"
15-
)
13+
const pluginSockDir = "/run/docker/plugins"
1614

1715
func newUnixListener(pluginName string, gid int) (net.Listener, string, error) {
1816
path, err := fullSocketAddress(pluginName)

sdk/unix_listener_nosystemd.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// +build linux freebsd
2+
// +build nosystemd
3+
4+
package sdk
5+
6+
import "net"
7+
8+
func setupSocketActivation() (net.Listener, error) {
9+
return nil, nil
10+
}

sdk/unix_listener_systemd.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// +build linux freebsd
2+
// +build !nosystemd
3+
4+
package sdk
5+
6+
import (
7+
"fmt"
8+
"net"
9+
"os"
10+
11+
"github.com/coreos/go-systemd/activation"
12+
)
13+
14+
// isRunningSystemd checks whether the host was booted with systemd as its init
15+
// system. This functions similarly to systemd's `sd_booted(3)`: internally, it
16+
// checks whether /run/systemd/system/ exists and is a directory.
17+
// http://www.freedesktop.org/software/systemd/man/sd_booted.html
18+
//
19+
// Copied from github.com/coreos/go-systemd/util.IsRunningSystemd
20+
func isRunningSystemd() bool {
21+
fi, err := os.Lstat("/run/systemd/system")
22+
if err != nil {
23+
return false
24+
}
25+
return fi.IsDir()
26+
}
27+
28+
func setupSocketActivation() (net.Listener, error) {
29+
if !isRunningSystemd() {
30+
return nil, nil
31+
}
32+
listenFds := activation.Files(false)
33+
if len(listenFds) > 1 {
34+
return nil, fmt.Errorf("expected only one socket from systemd, got %d", len(listenFds))
35+
}
36+
var listener net.Listener
37+
if len(listenFds) == 1 {
38+
l, err := net.FileListener(listenFds[0])
39+
if err != nil {
40+
return nil, err
41+
}
42+
listener = l
43+
}
44+
return listener, nil
45+
}

0 commit comments

Comments
 (0)