mirror of
https://github.com/henrygd/beszel.git
synced 2026-10-21 22:02:34 +00:00
fix(agent): verify systemd on dbus before starting service monitoring (#2573)
This commit is contained in:
@@ -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(),
|
||||
|
||||
@@ -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")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user