Skip to content

fs: implement close_range(2) - #1417

Closed
gburd wants to merge 1 commit into
cloudius-systems:masterfrom
gburd:pr/close-range
Closed

fs: implement close_range(2)#1417
gburd wants to merge 1 commit into
cloudius-systems:masterfrom
gburd:pr/close-range

Conversation

@gburd

@gburd gburd commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

What

close_range(2) was missing entirely. It is the standard way to close a range
of file descriptors in one call (commonly used to clean up inherited fds and by
some language runtimes), so programs that call it failed.

How

Implemented over the existing fd table: close (or, with CLOSE_RANGE_CLOEXEC,
set O_CLOEXEC on) every open fd in the inclusive range [first, last],
clamping last to the fd-table size and ignoring fds that are not open, as Linux
does. CLOSE_RANGE_UNSHARE is a no-op in a single-process unikernel (there is
nothing to unshare). first > last and unknown flags return EINVAL.

Wired as syscall SYS_close_range (adding __NR_close_range /
SYS_close_range for x86-64 and aarch64) with a tracepoint, declared in
unistd.h under _GNU_SOURCE, and the symbol exported from libc.so.6 and
ld-musl.so.1.

Testing

tests/tst-close-range.cc covers closing a contiguous run, a range that spans
not-open fds, the CLOSE_RANGE_CLOEXEC variant, and the EINVAL paths. Passes
on OSv under KVM.

close_range() was missing entirely. It is the standard way to close a range
of file descriptors in one call (commonly used to clean up inherited fds and
by some language runtimes), so programs that call it failed.

Implement it over the existing fd table: close (or, with CLOSE_RANGE_CLOEXEC,
set O_CLOEXEC on) every open fd in the inclusive range [first, last], clamping
last to the fd-table size and ignoring fds that are not open, as Linux does.
CLOSE_RANGE_UNSHARE is a no-op in a single-process unikernel (there is nothing
to unshare). first > last and unknown flags return EINVAL.

Wire it as syscall SYS_close_range (adding __NR_close_range / SYS_close_range
for x86-64 and aarch64) with a tracepoint, declare it in unistd.h under
_GNU_SOURCE, and export the symbol from libc.so.6 and ld-musl.so.1.

Add tests/tst-close-range.cc covering closing a contiguous run, a range that
spans not-open fds, the CLOSE_RANGE_CLOEXEC variant, and the EINVAL paths.
Passes on OSv under KVM.
@gburd gburd closed this Jul 13, 2026
@gburd
gburd deleted the pr/close-range branch July 13, 2026 11:19
@gburd

gburd commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #1436 (rebased onto current master; GitHub auto-closed this one when the branch was updated). Please review #1436 instead.

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