From 5d31e9378758dadfe6b137b15ba875c9484255d2 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 1 Sep 2023 12:14:56 +0200 Subject: [PATCH] pkg/systemd: use sync.Once for systemd detection This brings over the enhancement from https://github.com/moby/moby/commit/a506630e570735d13c587a9d755f3d5729658f28. We don't expect the systemd state to change while containerd is running, so we can use a `sync.Once` for this, to prevent stat'ing each time. Signed-off-by: Sebastiaan van Stijn --- pkg/systemd/util.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/pkg/systemd/util.go b/pkg/systemd/util.go index 95300d59c..1bec94baf 100644 --- a/pkg/systemd/util.go +++ b/pkg/systemd/util.go @@ -32,16 +32,27 @@ package systemd -import "os" +import ( + "os" + "sync" +) + +var ( + runningSystemd bool + detectSystemd sync.Once +) // IsRunningSystemd checks whether the host was booted with systemd as its init // system. This functions similarly to systemd's `sd_booted(3)`: internally, it // checks whether /run/systemd/system/ exists and is a directory. // https://github.com/coreos/go-systemd/blob/d843340ab4bd3815fda02e648f9b09ae2dc722a7/util/util.go#L68-L78 func IsRunningSystemd() bool { - fi, err := os.Lstat("/run/systemd/system") - if err != nil { - return false - } - return fi.IsDir() + detectSystemd.Do(func() { + fi, err := os.Lstat("/run/systemd/system") + if err != nil { + return + } + runningSystemd = fi.IsDir() + }) + return runningSystemd }