fs: implement close_range(2) - #1417
Closed
gburd wants to merge 1 commit into
Closed
Conversation
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.
Contributor
Author
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.
What
close_range(2)was missing entirely. It is the standard way to close a rangeof 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_CLOEXECon) every open fd in the inclusive range[first, last],clamping
lastto the fd-table size and ignoring fds that are not open, as Linuxdoes.
CLOSE_RANGE_UNSHAREis a no-op in a single-process unikernel (there isnothing to unshare).
first > lastand unknown flags returnEINVAL.Wired as syscall
SYS_close_range(adding__NR_close_range/SYS_close_rangefor x86-64 and aarch64) with a tracepoint, declared inunistd.hunder_GNU_SOURCE, and the symbol exported fromlibc.so.6andld-musl.so.1.Testing
tests/tst-close-range.cccovers closing a contiguous run, a range that spansnot-open fds, the
CLOSE_RANGE_CLOEXECvariant, and theEINVALpaths. Passeson OSv under KVM.