From ad3fce9801d04c52d83029b1c8b36be0ad7579fd Mon Sep 17 00:00:00 2001 From: lpf Date: Sat, 21 Feb 2026 18:19:46 +0800 Subject: [PATCH] fix gateway --- cmd/clawgo/cmd_gateway.go | 47 ++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/cmd/clawgo/cmd_gateway.go b/cmd/clawgo/cmd_gateway.go index 0dad0f1..5d8725a 100644 --- a/cmd/clawgo/cmd_gateway.go +++ b/cmd/clawgo/cmd_gateway.go @@ -386,16 +386,51 @@ func userGatewayUnitPath() (string, string, error) { func detectInstalledGatewayService() (string, string, error) { systemPath := "/etc/systemd/system/" + gatewayServiceName - if info, err := os.Stat(systemPath); err == nil && !info.IsDir() { - return "system", systemPath, nil - } - - scope, userPath, err := userGatewayUnitPath() + userScope, userPath, err := userGatewayUnitPath() if err != nil { return "", "", err } + + systemExists := false + if info, err := os.Stat(systemPath); err == nil && !info.IsDir() { + systemExists = true + } + + userExists := false if info, err := os.Stat(userPath); err == nil && !info.IsDir() { - return scope, userPath, nil + userExists = true + } + + preferredScope := strings.ToLower(strings.TrimSpace(os.Getenv("CLAWGO_GATEWAY_SCOPE"))) + switch preferredScope { + case "system": + if systemExists { + return "system", systemPath, nil + } + return "", "", fmt.Errorf("gateway service unit not found in system scope: %s", systemPath) + case "user": + if userExists { + return userScope, userPath, nil + } + return "", "", fmt.Errorf("gateway service unit not found in user scope: %s", userPath) + } + + // Auto-pick scope by current privilege to avoid non-root users accidentally + // selecting system scope when both unit files exist. + if os.Geteuid() == 0 { + if systemExists { + return "system", systemPath, nil + } + if userExists { + return userScope, userPath, nil + } + } else { + if userExists { + return userScope, userPath, nil + } + if systemExists { + return "system", systemPath, nil + } } return "", "", fmt.Errorf("gateway service not registered. Run: clawgo gateway")