fix(agent): verify systemd on dbus before starting service monitoring (#2573)

This commit is contained in:
Sven van Ginkel
2026-10-09 11:39:09 -04:00
committed by GitHub
parent e30e980854
commit 5529d2e15e
2 changed files with 15 additions and 5 deletions
+7
View File
@@ -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(),
+8 -5
View File
@@ -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")