Repository navigation
fix5 slice-fdinherit: register process-inherited fds >= 3 at startup (#1557) - #1562
Merged
Merged
Conversation
… 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.
Member
Author
集成树验证账本(master b326994 + 2605544,门控窗口、净化环境剔除 JARVIS_*)拣入零冲突:与基线 73d0a1b 以来 master 增量的 35 个文件零重叠,delta 与簇 commit 逐项一致(5 文件 +564/−0),作者/提交者均为曹梦轩;远端 sha 经 gh api 核实 = 本地 HEAD。
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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,纯增)
Executor::new_inner入口拍快照(src/executor/init.rs):第一行调用crate::fd::inherited_open_fds()采集 fd≥3 集合——抢在构造器自身打开任何描述符(信号邮箱线程、env 导入等)之前,避免误登记壳的瞬时 fd。__RUBASH_FD_*respawn 恢复之后):FdTable::register_inherited_process_fds(&snapshot);env 通道已认领的槽位跳过,不覆盖恢复的端点。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)。src/fd/unix.rs):优先/proc/self/fd列表精确枚举(目录流自身 fd 在列表内,候选一律在流关闭后经 F_GETFL 复验、自动剔除);无 /proc 时回退 fcntl 扫描 3..=1023(唯一有意收窄处)。src/fd/windows_impl.rs):HANDLE 无可枚举的小整数命名空间、继承按句柄 opt-in,跨进程 fd 状态走既有 env 通道——三个新符号为带注释的空桩,保持crate::fd::*跨平台表面一致;本片验证以 Linux 实测为准,Windows 行为未实测。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 个文件零重叠)复验进行中,完整账本以验证评论为准。
残余
$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]钉,待其修复时激活。read的诊断为3: Bad file descriptor,GNU 为read: 0: read error: Bad file descriptor;(b) 外部cat <&3(只写 fd)由壳在装配期报错,GNU 是 dup 成功后 cat 自报——两者 rc 均为 1。