fix(device): Windows 上命令起不来——不依赖 os/exec 打开空设备 NUL
设备 pc-gd(windows/amd64)执行任何命令都返回
device=pc-gd exit=127 启动失败:open NUL: The system cannot find the file specified.
os/exec 在 Stdin 为 nil 时会自己 os.Open(os.DevNull),Windows 上那是相对路径的
设备名 "NUL",CreateFile 解析它要先拼进程的当前目录;cwd 失效(快捷方式起始位置
指向已删目录、启动目录被改名等)时直接 ERROR_FILE_NOT_FOUND,命令还没到 cmd.exe
就死了。旁证:同一台设备 hello 里 shell_ver 为空——启动时探测 `cmd /c ver` 走的
是同一条路,一样失败。
- run(): 显式给 cmd.Stdin 一个空 reader,绕开 os/exec 内部的 open("NUL")。
空 reader 与 NUL 语义一致(子进程读到立即 EOF),且不依赖进程 cwd。
- main(): MkdirAll 工作根之后 os.Chdir(r.root),把 cwd 钉在一个确定存在的绝对
路径上(失败只记日志,不致命),顺带让 shellInfo 的版本探测能跑通。
This commit is contained in:
@@ -177,6 +177,13 @@ func main() {
|
|||||||
if err := os.MkdirAll(r.root, 0o755); err != nil {
|
if err := os.MkdirAll(r.root, 0o755); err != nil {
|
||||||
log.Fatalf("工作根不可用:%v", err)
|
log.Fatalf("工作根不可用:%v", err)
|
||||||
}
|
}
|
||||||
|
// 进程 cwd 失效时(快捷方式的「起始位置」指向已删目录、启动目录被改名等),
|
||||||
|
// Windows 上 os/exec 给 nil Stdin 打开空设备 "NUL" 是个相对路径调用,会直接报
|
||||||
|
// 「找不到文件」,任何命令都起不来(含下面的 shellInfo 探测)。工作根刚 MkdirAll 过,
|
||||||
|
// 钉在这里;失败也不致命,后面 run() 还有一层不依赖 NUL 的兵底。
|
||||||
|
if err := os.Chdir(r.root); err != nil {
|
||||||
|
log.Printf("切换工作目录失败(不影响命令执行):%v", err)
|
||||||
|
}
|
||||||
if r.shell == "" {
|
if r.shell == "" {
|
||||||
r.shell = defaultShell()
|
r.shell = defaultShell()
|
||||||
}
|
}
|
||||||
@@ -278,6 +285,10 @@ func (r *runner) run(m proto.Msg) {
|
|||||||
sh, shArgs := shellCmd(r.shell)
|
sh, shArgs := shellCmd(r.shell)
|
||||||
cmd := exec.CommandContext(ctx, sh, append(shArgs, m.Cmd)...)
|
cmd := exec.CommandContext(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)...)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user