From 65c02b540d616b3752a2eb5da110541f728d6cbe Mon Sep 17 00:00:00 2001 From: Ophestra Umiker Date: Thu, 11 Jul 2024 01:13:41 +0900 Subject: [PATCH] util: port sd_booted function Manpage provided by systemd states that the sd_booted function internally "checks whether the directory /run/systemd/system/ exists", as well as that "a simple check like this can also be implemented trivially in shell or any other language". This implies the behaviour of this function can be expected to be stable. Signed-off-by: Ophestra Umiker --- util.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 util.go diff --git a/util.go b/util.go new file mode 100644 index 0000000..4c8484b --- /dev/null +++ b/util.go @@ -0,0 +1,28 @@ +package main + +import ( + "errors" + "fmt" + "io/fs" + "os" +) + +const ( + systemdCheckPath = "/run/systemd/system" +) + +// https://www.freedesktop.org/software/systemd/man/sd_booted.html +func sdBooted() bool { + _, err := os.Stat(systemdCheckPath) + if err != nil { + if verbose { + if errors.Is(err, fs.ErrNotExist) { + fmt.Println("System not booted through systemd") + } else { + fmt.Println("Error accessing", systemdCheckPath+":", err.Error()) + } + } + return false + } + return true +}