From 54f4f7a23537cceadf1959e787a8593a29fd8bc3 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Wed, 12 Feb 2025 19:21:48 -0800 Subject: [PATCH] add IsSystemd checker --- helpers.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/helpers.go b/helpers.go index 01cd05c..37d8da6 100644 --- a/helpers.go +++ b/helpers.go @@ -3,6 +3,7 @@ package systemctl import ( "context" "errors" + "os" "strconv" "strings" "time" @@ -112,6 +113,15 @@ func GetMaskedUnits(ctx context.Context, opts Options) ([]string, error) { return units, nil } +// check if systemd is the current init system +func IsSystemd() (bool, error) { + b, err := os.ReadFile("/proc/1/comm") + if err != nil { + return false, err + } + return strings.TrimSpace(string(b)) == "systemd", nil +} + // check if a service is masked func IsMasked(ctx context.Context, unit string, opts Options) (bool, error) { units, err := GetMaskedUnits(ctx, opts)