Skip to content

Commit a506630

Browse files
committed
daemon: use sync.Once for systemd detection
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent e7ba5ca commit a506630

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

daemon/daemon_unix.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"runtime/debug"
1515
"strconv"
1616
"strings"
17+
"sync"
1718
"time"
1819

1920
"github.com/containerd/cgroups"
@@ -644,6 +645,11 @@ func UsingSystemd(config *config.Config) bool {
644645
return false
645646
}
646647

648+
var (
649+
runningSystemd bool
650+
detectSystemd sync.Once
651+
)
652+
647653
// isRunningSystemd checks whether the host was booted with systemd as its init
648654
// system. This functions similarly to systemd's `sd_booted(3)`: internally, it
649655
// checks whether /run/systemd/system/ exists and is a directory.
@@ -652,11 +658,14 @@ func UsingSystemd(config *config.Config) bool {
652658
// NOTE: This function comes from package github.com/coreos/go-systemd/util
653659
// It was borrowed here to avoid a dependency on cgo.
654660
func isRunningSystemd() bool {
655-
fi, err := os.Lstat("/run/systemd/system")
656-
if err != nil {
657-
return false
658-
}
659-
return fi.IsDir()
661+
detectSystemd.Do(func() {
662+
fi, err := os.Lstat("/run/systemd/system")
663+
if err != nil {
664+
return
665+
}
666+
runningSystemd = fi.IsDir()
667+
})
668+
return runningSystemd
660669
}
661670

662671
// verifyPlatformContainerSettings performs platform-specific validation of the

0 commit comments

Comments
 (0)