From 919678d2f800a47ac7a07292833e015c0f66cdb0 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Mon, 7 Nov 2022 17:57:07 +0100 Subject: [PATCH] fix incorrect systemd booted check Check for the directory /run/systemd/system, this is described in sd_booted(3). Reading /proc/1/comm will fail when /proc is mounted with the `hidepid=2` option. [NO NEW TESTS NEEDED] Fixes #16022 Signed-off-by: Paul Holzinger --- utils/utils.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index 5fb3695ceb..f9f96f2835 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -113,9 +113,9 @@ var ( // RunsOnSystemd returns whether the system is using systemd func RunsOnSystemd() bool { runsOnSystemdOnce.Do(func() { - initCommand, err := os.ReadFile("/proc/1/comm") - // On errors, default to systemd - runsOnSystemd = err != nil || strings.TrimRight(string(initCommand), "\n") == "systemd" + // per sd_booted(3), check for this dir + fd, err := os.Stat("/run/systemd/system") + runsOnSystemd = err == nil && fd.IsDir() }) return runsOnSystemd }