diff --git a/pkg/daemon/agent/daemon.go b/pkg/daemon/agent/daemon.go index e99c42958f..c8f16cc6e8 100644 --- a/pkg/daemon/agent/daemon.go +++ b/pkg/daemon/agent/daemon.go @@ -143,10 +143,23 @@ func isServiceInstalled() bool { return err == nil } +//nolint:gosec // systemctl args are internal constants, never user input +func runSystemctl(args ...string) (string, error) { + out, err := exec.Command("systemctl", args...).CombinedOutput() + return string(out), err +} + +func runServiceAction(action string) (string, error) { + return runSystemctl(action, pkgconfig.BinaryName) +} + +func systemctlErr(action, out string, err error) error { + return fmt.Errorf("systemctl %s: %s: %w", action, out, err) +} + func isServiceRunning() bool { - //nolint:gosec // BinaryName is a compile-time constant, not tainted input - out, err := exec.Command("systemctl", "is-active", pkgconfig.BinaryName).CombinedOutput() - return err == nil && strings.TrimSpace(string(out)) == "active" + out, err := runServiceAction("is-active") + return err == nil && strings.TrimSpace(out) == "active" } // quoteSystemdArg wraps an argument in double quotes if it contains characters @@ -341,10 +354,8 @@ func installOrUpdateUnit(unitContent string) (bool, error) { return false, fmt.Errorf("write service file: %w", err) } - if out, err := exec.Command( - "systemctl", "daemon-reload", - ).CombinedOutput(); err != nil { - return false, fmt.Errorf("systemctl daemon-reload: %s: %w", string(out), err) + if out, err := runSystemctl("daemon-reload"); err != nil { + return false, systemctlErr("daemon-reload", out, err) } return true, nil @@ -354,27 +365,19 @@ func installOrUpdateUnit(unitContent string) (bool, error) { // needed. If systemctl fails, it falls back to a background process. func ensureServiceRunning(needsReload bool, executable string, args []string) error { // Always enable so the service starts on boot, even if it was previously disabled. - //nolint:gosec // BinaryName is a compile-time constant, not tainted input - if out, err := exec.Command("systemctl", "enable", pkgconfig.BinaryName). - CombinedOutput(); err != nil { - return fmt.Errorf("systemctl enable: %s: %w", string(out), err) + if out, err := runServiceAction("enable"); err != nil { + return systemctlErr("enable", out, err) } // Restart if the unit file changed, otherwise just ensure it's running. if needsReload && isServiceRunning() { - //nolint:gosec // BinaryName is a compile-time constant - if out, err := exec.Command( - "systemctl", "restart", pkgconfig.BinaryName, - ).CombinedOutput(); err != nil { + if out, err := runServiceAction("restart"); err != nil { log.Warnf("Error restarting service: %s: %v", string(out), err) return startFallbackDaemon(executable, args, machinediagnostics.SystemRuntimePaths()) } log.Infof("restarted Devsy daemon with updated config") } else if !isServiceRunning() { - //nolint:gosec // BinaryName is a compile-time constant, not tainted input - if out, err := exec.Command( - "systemctl", "start", pkgconfig.BinaryName, - ).CombinedOutput(); err != nil { + if out, err := runServiceAction("start"); err != nil { log.Warnf("Error starting service: %s: %v", string(out), err) return startFallbackDaemon(executable, args, machinediagnostics.SystemRuntimePaths()) } @@ -462,25 +465,27 @@ func RemoveDaemon() error { // systemctlIgnoringNotLoaded runs a systemctl action against the daemon service, // treating a "not loaded" result (the unit does not exist) as a no-op. func systemctlIgnoringNotLoaded(action string) error { - //nolint:gosec // BinaryName is a compile-time constant - out, err := exec.Command("systemctl", action, pkgconfig.BinaryName).CombinedOutput() - // A missing unit reports "not loaded" (stop) or "does not exist" (disable); - // both mean there is nothing to act on, so treat them as a no-op. - outStr := string(out) - if err != nil && !strings.Contains(outStr, "not loaded") && - !strings.Contains(outStr, "does not exist") { - return fmt.Errorf("systemctl %s: %s: %w", action, outStr, err) + out, err := runServiceAction(action) + if err != nil && !isMissingUnitOutput(out) { + return systemctlErr(action, out, err) } return nil } +// isMissingUnitOutput reports whether systemctl output says the daemon unit does +// not exist. A missing unit reports "not loaded" (stop) or "does not exist" +// (disable); both mean there is nothing to act on. +func isMissingUnitOutput(out string) bool { + return strings.Contains(out, "not loaded") || strings.Contains(out, "does not exist") +} + func removeServiceUnit() error { if err := os.Remove(serviceFilePath()); err != nil && !os.IsNotExist(err) { return fmt.Errorf("remove service file: %w", err) } - if out, err := exec.Command("systemctl", "daemon-reload").CombinedOutput(); err != nil { - return fmt.Errorf("systemctl daemon-reload: %s: %w", string(out), err) + if out, err := runSystemctl("daemon-reload"); err != nil { + return systemctlErr("daemon-reload", out, err) } return nil diff --git a/pkg/daemon/agent/daemon_test.go b/pkg/daemon/agent/daemon_test.go index cecb605215..a19c3b10f5 100644 --- a/pkg/daemon/agent/daemon_test.go +++ b/pkg/daemon/agent/daemon_test.go @@ -88,6 +88,35 @@ func TestDaemonUnitStateLocationSupportsQuotedRoot(t *testing.T) { assert.Equal(t, StateLocation{Root: "/state/dev sy", Layout: StateLayoutAgentHome}, location) } +func TestIsMissingUnitOutput(t *testing.T) { + tests := map[string]struct { + out string + want bool + }{ + "stop on unloaded unit": { + out: "Failed to stop devsy.service: Unit devsy.service not loaded.", + want: true, + }, + "disable on absent unit": { + out: "Failed to disable unit: Unit file devsy.service does not exist.", + want: true, + }, + "other failure": { + out: "Failed to start devsy.service: Access denied", + want: false, + }, + "empty output": { + out: "", + want: false, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + assert.Equal(t, tc.want, isMissingUnitOutput(tc.out)) + }) + } +} + func TestBuildWorkspaceDaemonConfig_ShutdownAction(t *testing.T) { tests := []struct { name string