-
-
Notifications
You must be signed in to change notification settings - Fork 609
fs: implement close_range(2) #1436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gburd
wants to merge
1
commit into
cloudius-systems:master
Choose a base branch
from
gburd:pr/close-range
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,6 +78,7 @@ clock_getres | |
| clock_gettime | ||
| clock_nanosleep | ||
| close | ||
| close_range | ||
| closedir | ||
| closelog | ||
| confstr | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,7 @@ clock_getres | |
| clock_gettime | ||
| clock_nanosleep | ||
| close | ||
| close_range | ||
| closedir | ||
| closelog | ||
| confstr | ||
|
|
||
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
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * Copyright (C) 2026 Greg Burd | ||
| * | ||
| * This work is open source software, licensed under the terms of the | ||
| * BSD license as described in the LICENSE file in the top-level directory. | ||
| */ | ||
|
|
||
| // Exercises close_range(2). Built and run as part of the OSv test image. | ||
|
|
||
| #ifndef _GNU_SOURCE | ||
| #define _GNU_SOURCE | ||
| #endif | ||
| #include <unistd.h> | ||
| #include <fcntl.h> | ||
|
|
||
| #include <errno.h> | ||
|
|
||
| #include <cassert> | ||
| #include <iostream> | ||
|
|
||
| #ifndef CLOSE_RANGE_CLOEXEC | ||
| #define CLOSE_RANGE_CLOEXEC (1U << 2) | ||
| #endif | ||
|
|
||
| // Open n fds pointing at /dev/null and return the lowest one; they are | ||
| // contiguous because the fd table hands out the lowest free slots. | ||
| static int open_run(int n, int *fds) | ||
| { | ||
| for (int i = 0; i < n; i++) { | ||
| fds[i] = open("/dev/null", O_RDONLY); | ||
| assert(fds[i] >= 0); | ||
| } | ||
| return fds[0]; | ||
| } | ||
|
|
||
| int main() | ||
| { | ||
| std::cerr << "Running close_range tests\n"; | ||
|
|
||
| // Close a contiguous run of fds. | ||
| int fds[5]; | ||
| open_run(5, fds); | ||
| assert(close_range(fds[0], fds[4], 0) == 0); | ||
| // All of them are now closed: fcntl must report EBADF. | ||
| for (int i = 0; i < 5; i++) { | ||
| errno = 0; | ||
| assert(fcntl(fds[i], F_GETFD) == -1 && errno == EBADF); | ||
| } | ||
|
|
||
| // A range that includes not-open fds is fine (Linux ignores them). | ||
| int a = open("/dev/null", O_RDONLY); | ||
| assert(a >= 0); | ||
| assert(close_range(a, a + 100, 0) == 0); | ||
| errno = 0; | ||
| assert(fcntl(a, F_GETFD) == -1 && errno == EBADF); | ||
|
|
||
| // CLOSE_RANGE_CLOEXEC sets close-on-exec instead of closing. | ||
| int b = open("/dev/null", O_RDONLY); | ||
| assert(b >= 0); | ||
| int flags = fcntl(b, F_GETFD); | ||
| assert(flags != -1 && (flags & FD_CLOEXEC) == 0); // not set yet | ||
| assert(close_range(b, b, CLOSE_RANGE_CLOEXEC) == 0); | ||
| flags = fcntl(b, F_GETFD); | ||
| assert(flags != -1); // still open | ||
| assert(flags & FD_CLOEXEC); // now set | ||
| close(b); | ||
|
|
||
| // first > last is rejected; an unknown flag is rejected. | ||
| errno = 0; | ||
| assert(close_range(10, 5, 0) == -1 && errno == EINVAL); | ||
| errno = 0; | ||
| assert(close_range(0, 3, 0x40) == -1 && errno == EINVAL); | ||
|
|
||
| std::cerr << "close_range tests PASSED\n"; | ||
| return 0; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.