Skip to content

fix5 slice-fdinherit: register process-inherited fds >= 3 at startup (#1557) - #1562

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix5/slice-fdinherit
Oct 25, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix5/slice-fdinherit

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #1557

根因

FdTable 是虚拟表:FdTable::new() 只登记 0/1/2,Executor::new 从不探测进程实际继承的描述符。于是 fd≥3 在内核里明明开着(探针以 /proc/$$/fdinfo/3 证实:rubash 进程内 fd 3 存在且 flags 与 GNU 一致),表里却查无此 fd——所有 dup 源校验、重定向应用、内建读写一律报 3: Bad file descriptor;子 shell/函数/comsub 继承同一张表,同样全灭;外部命令在重定向装配期被壳侧拒绝。

GNU 没有 fd 登记表:重定向时直接对内核校验——redir.c:1160 do_redirection_internal 的 r_dup 臂以 fcntl (redirector, F_GETFD, 0) != -1 判源 fd 有效性(同形校验散见 redir.c:980/1098/1173/1252)。即「有效性 = 内核状态」,继承 fd 天然可用。rubash 的虚拟表必须在启动时以内核状态播种。

修法(单 commit,5 文件 +564/−0,纯增)

  1. Executor::new_inner 入口拍快照(src/executor/init.rs):第一行调用 crate::fd::inherited_open_fds() 采集 fd≥3 集合——抢在构造器自身打开任何描述符(信号邮箱线程、env 导入等)之前,避免误登记壳的瞬时 fd。
  2. 构造末尾登记(init.rs,紧跟既有 __RUBASH_FD_* respawn 恢复之后):FdTable::register_inherited_process_fds(&snapshot);env 通道已认领的槽位跳过,不覆盖恢复的端点。
  3. 登记语义(src/executor/fd_table.rs):每个仍开着的 fd≥3 成为一个 File 端点,handle = 其裸描述符本身,复用全部既有服务面(内建 write_all、外部 spawn 的 duplicate_handle 白名单、dup 的 Rc 共享描述、is_disk_file 的 fstat)。方向按 F_GETFL 访问模式:只读臂/只写臂,O_RDWR 双臂共享同一个 FileFd(单一 open file description,偏移共享)。登记前逐个以 fd_directions 实时复验:快照后已关闭的瞬时 fd 跳过。FileFd 的 Drop 在槽位最后引用消失时关闭裸描述符——正好实现 exec N>&- 契约。路径标签:readlink /proc/self/fd/N 真实路径(pipe/socket/anon 形回退 /dev/fd/N)。
  4. fd 层枚举(src/fd/unix.rs):优先 /proc/self/fd 列表精确枚举(目录流自身 fd 在列表内,候选一律在流关闭后经 F_GETFL 复验、自动剔除);无 /proc 时回退 fcntl 扫描 3..=1023(唯一有意收窄处)。
  5. Windows 只作代码路径标注(src/fd/windows_impl.rs):HANDLE 无可枚举的小整数命名空间、继承按句柄 opt-in,跨进程 fd 状态走既有 env 通道——三个新符号为带注释的空桩,保持 crate::fd::* 跨平台表面一致;本片验证以 Linux 实测为准,Windows 行为未实测。
  6. 落点选 Executor::new 而非 rubash 的 main:niubash-runtime 的会话同样经 Executor::new 构造,一处修复两边生效;子 shell/comsub 走父表克隆,不受影响。

红线:未碰 src/lexer/continuation.rs、未涉载体字节;未动 statelife 的 procsub 收尾队列/漏斗。

验证

簇侧(基线 73d0a1b):16 形主矩阵 master 15 形败 → 修复后可观察行为与 GNU 全等;11 形方向矩阵、6 形 close 矩阵 rc 与效果与 GNU 全等;issue1557 回归 13 过/0 败/1 ignored;issue1523 硬门 6/6;lib 730 过/1 败/1 ignored 与基线逐项一致;246 差分 {gl08, ms10}。

集成树(当前 master b326994 + 本 commit,零冲突拣入——与 master 增量的 35 个文件零重叠)复验进行中,完整账本以验证评论为准。

残余

  • procsub 独立赋值形的 $v 值仍为字面文本(master 既存、与 fd 无关的独立缺口:v=>(echo hi >&2); echo $v 即复现;体照常运行、BODY 已能写入继承 fd,即 [bug] 继承的 fd≥3 写入报 Bad file descriptor(fd_table 启动期继承面) #1557 正文第三形的问题面已闭合)。已单独跟踪([bug] 独立赋值右值为整个 >(…) 时变量保持字面、不回填 /dev/fd 路径 #1561),本片回归文件中留有带 GNU 期望的 #[ignore] 钉,待其修复时激活。
  • 诊断措辞差两处,属既存家族(以壳自建 fd 在 master 上同形复现):(a) 只写 fd 上 read 的诊断为 3: Bad file descriptor,GNU 为 read: 0: read error: Bad file descriptor;(b) 外部 cat <&3(只写 fd)由壳在装配期报错,GNU 是 dup 成功后 cat 自报——两者 rc 均为 1。
  • 无 /proc 平台的回退扫描上限 1023:继承 fd>1023 且无 /proc 时不登记(Linux 有 /proc 故精确无上限)。

… startup (#1557)

GNU keeps no registry of inherited descriptors: redir.c
do_redirection_internal validates a dup source against the kernel
(fcntl (redirector, F_GETFD, 0) != -1, redir.c:1160), so a descriptor
handed to the shell by its parent (3>file, socket activation, CI log
channels) is simply usable. Rubash's virtual FdTable started with
only 0/1/2, so every reference to an inherited fd >= 3 failed with
"N: Bad file descriptor" while the descriptor was open in the
process all along.

Executor::new now snapshots the inherited set (fd >= 3) before the
constructor can open anything of its own, and — after the existing
__RUBASH_FD_* respawn restore — FdTable::register_inherited_process_fds
adopts each still-open descriptor as a File endpoint over its own
raw fd: read/write arms follow the F_GETFL access mode, both arms
sharing one FileFd for O_RDWR (one open file description). Adoption
re-validates live, so transient descriptors are never registered.
The FileFd drop closes the raw descriptor when the slot's last
reference goes away, matching the exec N>&- contract.

The fd enumeration lives in the fd layer: unix probes /proc/self/fd
(exact at any descriptor number; 3..=1023 fcntl scan as fallback);
Windows gets annotated no-op stubs (handle inheritance is opt-in
per handle — cross-process state there flows through the
__RUBASH_FD_* channel instead).

Regression: tests/issue1557_inherited_fd_regressions.rs (13 tests
spawning rubash with real dup2-installed inherited descriptors;
expectations measured against GNU bash 5.3.20). One #[ignore] pin
carries the GNU value expectation for the bare whole-RHS >(…)
assignment form, whose missing /dev/fd back-substitution is a
separate pre-existing gap filed as #1561.
@caomengxuan666

Copy link
Copy Markdown
Member Author

集成树验证账本(master b326994 + 2605544,门控窗口、净化环境剔除 JARVIS_*)

拣入零冲突:与基线 73d0a1b 以来 master 增量的 35 个文件零重叠,delta 与簇 commit 逐项一致(5 文件 +564/−0),作者/提交者均为曹梦轩;远端 sha 经 gh api 核实 = 本地 HEAD。

  • cargo build --locked:rc=0。
  • cargo check --locked --all-targets:rc=0;35 条 warning 全在存量测试文件(issue528/544/555/621/651/705/1184/1520/1548 等),本片 5 个改动文件零 warning。
  • issue1557 回归:13 过 / 0 败 / 1 ignored(ignored = [bug] 独立赋值右值为整个 >(…) 时变量保持字面、不回填 /dev/fd 路径 #1561 的 GNU 期望钉,保持未激活)。
  • issue1523 硬门:6/6(statelife 收尾模型未动)。
  • 相邻面:issue1025_1026_1068_1103_1359 8/8、issue1396 1/1、issue1221 3/3、issue1021 2/2、issue1520 18/18。
  • lib 全量:732 过 / 1 败 / 1 ignored,与当前 master 基线逐项一致;唯一败 = 许可的 root 环境项 ownership_unary_operators_check_existing_file。启动探测零污染。
  • 探针三套对 GNU 5.3.20 复跑(16 形矩阵 + 11 形方向 + 6 形 close):各形态 rc 与可观察效果全等。原始 diff 全部落在已裁定残差集内——①[bug] 独立赋值右值为整个 >(…) 时变量保持字面、不回填 /dev/fd 路径 #1561 的 $v 字面值形;②fdinfo 的 ino(临时文件不同、本应不同);③诊断前缀(oracle 全路径 argv0 vs rubash bash: 既定约定);④两处既存措辞家族(只写 fd 上 read 的诊断形;外部 cat 只写 fd 由壳侧装配期报错,rc 两边均 1);⑤ls /proc/$$/fd 列表的 spawn 暂存 fd 伪影(本次出现在 GNU 侧:矩阵形多列 4、方向形关后槽位被暂存 fd 占回,两壳均可出现、号位随运行变,簇侧在 rubash 侧同形观察过;各形态的继承可见性与 exec N>&- 关闭契约本身两边一致)。
  • 246 差分:244 PASS,失败集恰为 {gl08, ms10} = 基线稳定集(新沙箱、RUBASH 变量、新产 results-rubash.json 已验 mtime)。
  • fmt:5 个改动文件逐个 rustfmt --check 全清;CR 计数全 0。

@caomengxuan666
caomengxuan666 merged commit a4c6c2a into master Oct 25, 2026
7 of 10 checks passed
@caomengxuan666
caomengxuan666 deleted the fix5/slice-fdinherit branch October 30, 2026 01:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] 继承的 fd≥3 写入报 Bad file descriptor(fd_table 启动期继承面)

1 participant