pkg/systemd: use sync.Once for systemd detection
This brings over the enhancement from a506630e57
.
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 <github@gone.nl>
This commit is contained in:
parent
7d0ab4fc2c
commit
5d31e93787
@ -32,16 +32,27 @@
|
|||||||
|
|
||||||
package systemd
|
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
|
// IsRunningSystemd checks whether the host was booted with systemd as its init
|
||||||
// system. This functions similarly to systemd's `sd_booted(3)`: internally, it
|
// system. This functions similarly to systemd's `sd_booted(3)`: internally, it
|
||||||
// checks whether /run/systemd/system/ exists and is a directory.
|
// checks whether /run/systemd/system/ exists and is a directory.
|
||||||
// https://github.com/coreos/go-systemd/blob/d843340ab4bd3815fda02e648f9b09ae2dc722a7/util/util.go#L68-L78
|
// https://github.com/coreos/go-systemd/blob/d843340ab4bd3815fda02e648f9b09ae2dc722a7/util/util.go#L68-L78
|
||||||
func IsRunningSystemd() bool {
|
func IsRunningSystemd() bool {
|
||||||
|
detectSystemd.Do(func() {
|
||||||
fi, err := os.Lstat("/run/systemd/system")
|
fi, err := os.Lstat("/run/systemd/system")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return
|
||||||
}
|
}
|
||||||
return fi.IsDir()
|
runningSystemd = fi.IsDir()
|
||||||
|
})
|
||||||
|
return runningSystemd
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user