Repository navigation
fix(env): launch #! scripts through the shared childproc resolver (Refs #1623) - #1628
Conversation
…historical snapshot (#1629) The checkpoint section records the acceptance state as audited by the lead review: current main had 50 commands wired (not the historical 76/42 numbers), A1/tail/ABI-v3 are not accepted yet, I18N is an acceptance prerequisite, and the safety record for preserve-root (chmod/chgrp/chown) blocks wiring until the listed gates pass. Captain addendum records same-day movement: #1627 merged, PRs #1624/#1625/#1626/#1628 open, and the fix/batch-1595-1616-1617 trio.
…busybox verb (Refs #1623) The three failing x64-default ctest cases copied the multicall winuxcmd.exe to interp.exe and assumed argv[1] verb dispatch. The multicall dispatches by exe stem (main.cpp:478 wmain -> path.cppm:16 get_executable_name; Mode 2 stem dispatch, no argv[1] fallback), so 'interp' fell to 'command not found' 127. Stage the interpreter under its own registered name winuxcmd.exe (Mode 1 verb form) — the [interpreter, opt-arg, script, argv] shape the cases verify is unchanged. Test-only; product code untouched. Local default-edition build clean; childproc.* 26/26, env.*/timeout.* 80/80; the 20 remaining local gtest failures are host-environment-only (nice/nohup PATH resolution, NIU_ROOT layout pins) and all pass on CI x64 default.
9f2e589 to
4336897
Compare
队长复核 — WinuxCmd#1628(Refs #1623):机制 PASS,两处 shebang 语义需改后合入
需要改的语义(GNU oracle 实测)用 WSL GNU(kernel
结论:“256 字节内没有换行就不是脚本(126)”不是当前内核 binfmt_script 的模型。内核在 256 字节缓冲内解析,解释器取得到的就启动(参数截断),所以
要求的修改
另外记录
|
…r (Refs #1628 #1623) (#1664) The merged #1628 modeled an overlong first line as ENOEXEC (126), reading "the line fills BINPRM_BUF_SIZE with no newline" as "not a script". GNU does not reject it: exec.c zero-fills a BINPRM_BUF_SIZE (256) buffer but reads only BINPRM_BUF_SIZE - 1 (255) bytes into it, and binfmt_script.c parses the line inside that window. Past the window the line is truncated at the edge and the script still launches. Parse within the 255-byte window in both implementations. The only remaining nullopt/None cases are a `#!` with no interpreter token and the recursion guard. The bare `#!` stays 126 as a product BadFormat boundary: GNU env reaches 0 only through the libc execvp /bin/sh fallback, while raw exec gets ENOEXEC. Pins: - tests/unit/childproc: the overlong-line 126 pin becomes a positive launch pin that asserts the optional argument is cut to exactly the 255-byte window (240 x's after "#!./interp.cmd "); a long line whose interpreter is missing stays 127. The bare-`#!` pin is reworded as the product boundary. - rust2 childproc: the same replacement, exact argv pinned to SHEBANG_LINE_BYTES - 10, plus the missing-interpreter 127 case. Probed WSL GNU 6.18 / GNU env 9.4: `#!/bin/echo ` + 300 x's -> rc 0 with a 243-byte argument; `#!/nonexistent/interp` + 300 x's -> 127; bare `#!` -> env 0 (execvp fallback) vs raw exec ENOEXEC.
根因
niubash #1002 的第二断点:即使
env.exe已在分发里(WinuxCmd #1622),env <hook>仍无法启动#!/usr/bin/env bash的 hook——git for Windows 的
parse_interpreter()(compat/mingw.c:1873-1907)会剥掉shebang 的可选参数,所以 git 实际 spawn 的是
env <hook>;MSYS 的 env经 execve 重读 shebang。niugit 没有 MSYS 层,所以 env 自身必须做 shebang
重读。
缺口在共享 helper 内,不在 env 的调用点:
env.cpp早已调winux::childproc::plan_child_launch,但src/utils/childproc.cppm的plan_child_launch只处理原生镜像与.cmd/.bat(经 COMSPEC),没有任何shebang 处理——
scan_candidates对任意常规文件都报 Found,随后CreateProcessW以 126 死掉。修法
src/utils/childproc.cppm(共享 helper,env/timeout/nohup/nice/stdbuf/watch/sdiff/diff3/diff/find/xargs/install 共用)按
rust2/crates/winux-cmds/src/childproc.rs的既定语义补齐:classify_file:batch 扩展 → COMSPEC;MZ→ 原生镜像;#!→resolve_shebang_script递归解析;其余文件保持原样(仍交CreateProcessW,126——Rust 的 BadFormat "Exec format error" 不在本单范围)。
parse_shebang_line:首个 space/tab 切 interpreter,余部为 ONE 可选参数,尾 space/tab 修剪、CR 保留(CRLF shebang 命中不到 interpreter → 127);
#!为空或首行超 256 字节无换行 → 126;递归上限 3。resolve_interpreter:裸名改写./name(cwd 相对,永不 PATH 搜索,GNU 实测
"#!env sh"→ 127);A 原生链 → B shell-namespace mapper(
shell_tree_to_native_w,#!/usr/bin/env必经此路)→ C 叶名走 PATH。plan_child_launch增加 script 分支:lpApplicationName= 解析后的interpreter 路径(interpreter 是
.cmd/.bat时走 COMSPEC),lpCommandLine=interpreter [opt-arg] script args...;链式脚本只追加外层
[arg?, script](内层脚本恰出现一次,childproc: shebang interpreter that is itself a script gets a duplicated argv token #1595);env -a对脚本无效(内核模型丢弃 argv[0]),
command_line_override对脚本忽略(与batch 分支一致)。
src/utils/native_path.cppm:shell_tree_to_native_w加export(第二位消费者;Rust 侧对应函数本就公开)。
comspec_path(),batch 分支行为不变。非
#!子进程的代码路径逐字节不变(classify 对其返回 Image,走原有native 分支);新增开销仅为每次 spawn 一次 256 字节头读取。
钉
tests/unit/childproc/childproc_unit_test.cpp新增 9 个(Pipeline 实 spawn):env_shebang_script_launches_through_interpreter(原生 interpreter +可选参数,argv 顺序由 marker 证明)
env_shebang_script_with_posix_interpreter_resolves_via_shell_root(
#!/usr/bin/env echo+ NIU_ROOT 桩根——即 [gap] factor: 0 and 1 are printed with themselves as factors #1002 hook 形态)env_shebang_script_missing_interpreter_reports_127env_shebang_script_crlf_line_misses_interpreter_reports_127env_shebang_script_chain_appends_outer_tokens_once(childproc: shebang interpreter that is itself a script gets a duplicated argv token #1595)env_shebang_script_with_batch_interpreter_routes_through_comspecenv_shebang_script_bare_hashbang_reports_126env_shebang_script_overlong_first_line_reports_126timeout_shebang_script_launches_through_interpreter(第二调用方)验收
winuxcmd env ./s1.sh a/./s2.sh arc 0([bug] env 启动 #! 脚本失败(Permission denied/126):未接入 execvp 形 childproc 解析,#!/usr/bin/env 的 git hook 依赖它(niubash#1002) #1623 验收用例)。pin bump 后,在发版烟囱断言里跑。
本 PR 删除了什么
plan_child_launch的 batch 分支内联 COMSPEC 取值改为调
comspec_path()(行为不变);其余为新增代码与测试。#!子进程的解析/启动路径;env.cpp调用点(无需改);Rust
childproc.rs(已是参考实现,未动)。UNVERIFIED-BY-CAPTAIN