fix(device): 所有 exec 统一走 newCmd,不再让 os/exec 去打开 NUL

上一版只给 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 钉在工作根上没坏处(相对路径的设备名和相对路径文件都更稳)。
This commit is contained in:
2026-11-20 14:26:31 +08:00
parent 47a0b9e50d
commit c9dd5e48bd
2 changed files with 20 additions and 11 deletions
+18 -10
View File
@@ -283,12 +283,8 @@ func (r *runner) run(m proto.Msg) {
defer cancel() defer cancel()
sh, shArgs := shellCmd(r.shell) 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 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 { if len(m.Env) > 0 {
cmd.Env = append(os.Environ(), flatten(m.Env)...) cmd.Env = append(os.Environ(), flatten(m.Env)...)
} }
@@ -433,6 +429,17 @@ func shellKind(p string) string {
return p 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) { func shellCmd(name string) (string, []string) {
switch name { switch name {
case "cmd": case "cmd":
@@ -472,16 +479,17 @@ func shellInfo(name string) (string, string) {
ctx, cancel := context.WithTimeout(context.Background(), 800*time.Millisecond) ctx, cancel := context.WithTimeout(context.Background(), 800*time.Millisecond)
defer cancel() defer cancel()
var out []byte var c *exec.Cmd
switch name { switch name {
case "cmd": case "cmd":
out, _ = exec.CommandContext(ctx, "cmd", "/c", "ver").Output() c = newCmd(ctx, "cmd", "/c", "ver")
case "powershell", "pwsh": case "powershell", "pwsh":
out, _ = exec.CommandContext(ctx, bin, "-NoProfile", "-NonInteractive", "-Command", c = newCmd(ctx, bin, "-NoProfile", "-NonInteractive", "-Command",
"$PSVersionTable.PSVersion.ToString()").Output() "$PSVersionTable.PSVersion.ToString()")
default: default:
out, _ = exec.CommandContext(ctx, p, "--version").Output() c = newCmd(ctx, p, "--version")
} }
out, _ := c.Output()
ver := "" ver := ""
if s := strings.TrimSpace(string(out)); s != "" { if s := strings.TrimSpace(string(out)); s != "" {
+2 -1
View File
@@ -3,6 +3,7 @@
package main package main
import ( import (
"context"
"os/exec" "os/exec"
"strconv" "strconv"
"syscall" "syscall"
@@ -19,6 +20,6 @@ func killProc(cmd *exec.Cmd) {
if cmd == nil || cmd.Process == nil { if cmd == nil || cmd.Process == nil {
return 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() _ = cmd.Process.Kill()
} }