diff --git a/agent/systemd.go b/agent/systemd.go index d731ded84..7b3c29647 100644 --- a/agent/systemd.go +++ b/agent/systemd.go @@ -114,6 +114,13 @@ func newSystemdManager() (*systemdManager, error) { return nil, err } + // dbus may be present without systemd (e.g. runit, OpenRC), so verify systemd is on the bus (#2555) + if _, err := conn.GetManagerProperty("Version"); err != nil { + conn.Close() + slog.Debug("Systemd not available on dbus", "err", err) + return nil, nil + } + manager := &systemdManager{ serviceStatsMap: make(map[string]*systemd.Service), logsEnabled: systemdLogsEnabled(), diff --git a/agent/systemd_test.go b/agent/systemd_test.go index 4947805a3..d8c6a7353 100644 --- a/agent/systemd_test.go +++ b/agent/systemd_test.go @@ -180,10 +180,13 @@ func TestIsSystemdAvailable(t *testing.T) { // On systems without systemd, it should return false result := isSystemdAvailable() - // Check if either the /run/systemd/system directory exists or PID 1 is systemd - runSystemdExists := false - if _, err := os.Stat("/run/systemd/system"); err == nil { - runSystemdExists = true + // Check if the systemd directory or a dbus socket exists, or PID 1 is systemd + pathExists := false + for _, path := range []string{"/run/systemd/system", "/run/dbus/system_bus_socket", "/var/run/dbus/system_bus_socket"} { + if _, err := os.Stat(path); err == nil { + pathExists = true + break + } } pid1IsSystemd := false @@ -191,7 +194,7 @@ func TestIsSystemdAvailable(t *testing.T) { pid1IsSystemd = strings.TrimSpace(string(data)) == "systemd" } - expected := runSystemdExists || pid1IsSystemd + expected := pathExists || pid1IsSystemd assert.Equal(t, expected, result, "isSystemdAvailable should correctly detect systemd presence")