Skip to content

feat: keep descriptors above the standard streams - #76

Merged
HaoZeke merged 1 commit into
mainfrom
feat/keep-above-stdio
Nov 20, 2026
Merged

HaoZeke merged 1 commit into
mainfrom
feat/keep-above-stdio

Conversation

@HaoZeke

@HaoZeke HaoZeke commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

When standard input, standard output, or standard error starts closed,
the next openat or pipe2 call takes descriptor slot 0, 1, or
2. Subsequent child setup or standard-stream writes can clobber that
descriptor. Both places in proc moved low descriptors by hand,
childExec with F_DUPFD and run with F_DUPFD_CLOEXEC on its
exec-failure pipe, while front ends holding flock descriptors need
the same primitive.

Add sys.keepAboveStdio to duplicate any open descriptor below 3 to
3 or above with F_DUPFD_CLOEXEC, retrying interrupted calls, and
close the original low slot. Descriptors already at 3 or above return
unchanged, and failed calls leave the original descriptor open. Switch
both sites in src/proc.zig onto sys.keepAboveStdio, updating shared
slots in childExec when standard input and standard error share
/dev/null.

When standard input, output, or error starts closed, the next open or
pipe takes slot 0, 1, or 2. Later child setup or standard-stream writes
can clobber that descriptor. Both places in proc moved low descriptors
by hand, childExec with F_DUPFD and run's exec-failure pipe with
F_DUPFD_CLOEXEC, and front ends holding flock files need the same step.

Add sys.keepAboveStdio to duplicate a descriptor below 3 to 3 or above
with F_DUPFD_CLOEXEC, retrying EINTR, and close the low slot. A
descriptor already at 3 or above returns unchanged, and a failed call
leaves the original descriptor open. Both sites in proc call
keepAboveStdio, and childExec updates shared slots when standard input
and standard error share /dev/null.

Tests cover moving slots 0, 1, and 2 with FD_CLOEXEC set, leaving high
descriptors unchanged, reporting EBADF on closed slots, and running a
child with all three standard streams closed.
@HaoZeke
HaoZeke merged commit 3c3b172 into main Nov 20, 2026
8 checks passed
HaoZeke added a commit to stackhands/eb-local that referenced this pull request Nov 21, 2026
Update the zig-common dependency in build.zig.zon from v0.12.0 to
3c3b1720de00577612f834a23c246c34fbf990a0 on main. That commit merges
stackhands/zig-common#76, which adds sys.keepAboveStdio and switches
proc onto it.
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.

1 participant