From c9dd5e48bd68fbaa3a15608088c047fe9784a0a2 Mon Sep 17 00:00:00 2001 From: kicer Date: Fri, 20 Nov 2026 14:26:31 +0800 Subject: [PATCH] =?UTF-8?q?fix(device):=20=E6=89=80=E6=9C=89=20exec=20?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E8=B5=B0=20newCmd=EF=BC=8C=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E8=AE=A9=20os/exec=20=E5=8E=BB=E6=89=93=E5=BC=80=20NUL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 上一版只给 run() 的命令加了空 stdin,实测命令能跑了,但设备 hello 里 shell_ver 仍然是空——启动时 shellInfo 探测 `cmd /c ver` 走的是同一条 os/exec 路径,照样死在 open NUL 上。同一个坑还有 killProc 里的 taskkill: 超时终止时那一步失败,进程组杀不掉。 - 抽出 newCmd(ctx, name, args...):Stdin 统一给 strings.NewReader(""), 空 reader 与 NUL 语义一致(子进程读到立即 EOF),不依赖进程 cwd。 - run() / shellInfo() / proc_windows.killProc() 三处都改走 newCmd。 - 保留上一版 main() 里的 os.Chdir(r.root):本机 NUL 打不开不是 cwd 的锅, 但把进程 cwd 钉在工作根上没坏处(相对路径的设备名和相对路径文件都更稳)。 --- device/cmd/agent/main.go | 28 ++++++++++++++++++---------- device/cmd/agent/proc_windows.go | 3 ++- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/device/cmd/agent/main.go b/device/cmd/agent/main.go index 97acfc2..765b6a5 100644 --- a/device/cmd/agent/main.go +++ b/device/cmd/agent/main.go @@ -283,12 +283,8 @@ func (r *runner) run(m proto.Msg) { defer cancel() sh, shArgs := shellCmd(r.shell) - cmd := exec.CommandContext(ctx, sh, append(shArgs, m.Cmd)...) + cmd := newCmd(ctx, sh, append(shArgs, m.Cmd)...) cmd.Dir = dir - // 显式给空 stdin:nil 时 os/exec 会去 os.Open(os.DevNull),Windows 上那是相对路径的 - // 设备名 "NUL",cwd 失效时 CreateFile 直接报 ERROR_FILE_NOT_FOUND,命令起不来。 - // 空 reader 与 NUL 语义一致(子进程读到立即 EOF),不依赖进程 cwd。 - cmd.Stdin = strings.NewReader("") if len(m.Env) > 0 { cmd.Env = append(os.Environ(), flatten(m.Env)...) } @@ -433,6 +429,17 @@ func shellKind(p string) string { return p } +// newCmd 统一构造要执行的命令:Stdin 显式给一个空 reader。 +// 不给(nil)时 os/exec 会自己 os.Open(os.DevNull),Windows 上那是相对路径的设备名 +// "NUL"——有的机器上 CreateFile 直接报 ERROR_FILE_NOT_FOUND,命令还没到 shell 就死了 +//(实测 pc-gd:所有 exec 都 exit 127 "open NUL: The system cannot find the file specified")。 +// 空 reader 与 NUL 语义一致(子进程读到立即 EOF),且不依赖进程 cwd。 +func newCmd(ctx context.Context, name string, args ...string) *exec.Cmd { + c := exec.CommandContext(ctx, name, args...) + c.Stdin = strings.NewReader("") + return c +} + func shellCmd(name string) (string, []string) { switch name { case "cmd": @@ -472,16 +479,17 @@ func shellInfo(name string) (string, string) { ctx, cancel := context.WithTimeout(context.Background(), 800*time.Millisecond) defer cancel() - var out []byte + var c *exec.Cmd switch name { case "cmd": - out, _ = exec.CommandContext(ctx, "cmd", "/c", "ver").Output() + c = newCmd(ctx, "cmd", "/c", "ver") case "powershell", "pwsh": - out, _ = exec.CommandContext(ctx, bin, "-NoProfile", "-NonInteractive", "-Command", - "$PSVersionTable.PSVersion.ToString()").Output() + c = newCmd(ctx, bin, "-NoProfile", "-NonInteractive", "-Command", + "$PSVersionTable.PSVersion.ToString()") default: - out, _ = exec.CommandContext(ctx, p, "--version").Output() + c = newCmd(ctx, p, "--version") } + out, _ := c.Output() ver := "" if s := strings.TrimSpace(string(out)); s != "" { diff --git a/device/cmd/agent/proc_windows.go b/device/cmd/agent/proc_windows.go index 6da0d53..a3b9101 100644 --- a/device/cmd/agent/proc_windows.go +++ b/device/cmd/agent/proc_windows.go @@ -3,6 +3,7 @@ package main import ( + "context" "os/exec" "strconv" "syscall" @@ -19,6 +20,6 @@ func killProc(cmd *exec.Cmd) { if cmd == nil || cmd.Process == nil { return } - _ = exec.Command("taskkill", "/T", "/F", "/PID", strconv.Itoa(cmd.Process.Pid)).Run() + _ = newCmd(context.Background(), "taskkill", "/T", "/F", "/PID", strconv.Itoa(cmd.Process.Pid)).Run() _ = cmd.Process.Kill() }