aboutsummaryrefslogtreecommitdiff
path: root/tests/sys
diff options
context:
space:
mode:
authorKyle Evans <kevans@FreeBSD.org>2020-04-12 21:23:19 +0000
committerKyle Evans <kevans@FreeBSD.org>2020-04-12 21:23:19 +0000
commit472ced39efb537374068f06b348fe5dac389c45a (patch)
tree30f4cf81fcfd1a7f955265b34c52d3265a977d9a /tests/sys
parent1d3500e0658360e7475fbf0e3bdf9fe499c2c18a (diff)
downloadsrc-472ced39efb537374068f06b348fe5dac389c45a.tar.gz
src-472ced39efb537374068f06b348fe5dac389c45a.zip
Implement a close_range(2) syscall
close_range(min, max, flags) allows for a range of descriptors to be closed. The Python folk have indicated that they would much prefer this interface to closefrom(2), as the case may be that they/someone have special fds dup'd to higher in the range and they can't necessarily closefrom(min) because they don't want to hit the upper range, but relocating them to lower isn't necessarily feasible. sys_closefrom has been rewritten to use kern_close_range() using ~0U to indicate closing to the end of the range. This was chosen rather than requiring callers of kern_close_range() to hold FILEDESC_SLOCK across the call to kern_close_range for simplicity. The flags argument of close_range(2) is currently unused, so any flags set is currently EINVAL. It was added to the interface in Linux so that future flags could be added for, e.g., "halt on first error" and things of this nature. This patch is based on a syscall of the same design that is expected to be merged into Linux. Reviewed by: kib, markj, vangyzen (all slightly earlier revisions) Differential Revision: https://reviews.freebsd.org/D21627
Notes
Notes: svn path=/head/; revision=359836
Diffstat (limited to 'tests/sys')
-rw-r--r--tests/sys/file/closefrom_test.c40
1 files changed, 39 insertions, 1 deletions
diff --git a/tests/sys/file/closefrom_test.c b/tests/sys/file/closefrom_test.c
index 78cfeecae026..7ce93415519e 100644
--- a/tests/sys/file/closefrom_test.c
+++ b/tests/sys/file/closefrom_test.c
@@ -146,7 +146,7 @@ main(void)
pid_t pid;
int fd, i, start;
- printf("1..15\n");
+ printf("1..19\n");
/* We better start up with fd's 0, 1, and 2 open. */
start = devnull();
@@ -271,5 +271,43 @@ main(void)
fail("closefrom", "highest fd %d", fd);
ok("closefrom");
+ /* Chew up another 8 fd */
+ for (i = 0; i < 8; i++)
+ (void)devnull();
+ fd = highest_fd();
+ start = fd - 7;
+
+ /* close_range() a hole in the middle */
+ close_range(start + 3, start + 5, 0);
+ for (i = start + 3; i < start + 6; ++i) {
+ if (close(i) == 0 || errno != EBADF) {
+ --i;
+ break;
+ }
+ }
+ if (i != start + 6)
+ fail("close_range", "failed to close at %d in %d - %d", i + 1,
+ start + 3, start + 6);
+ ok("close_range");
+
+ /* close_range from the middle of the hole */
+ close_range(start + 4, start + 6, 0);
+ if ((i = highest_fd()) != fd)
+ fail("close_range", "highest fd %d", i);
+ ok("close_range");
+
+ /* close_range to the end; effectively closefrom(2) */
+ close_range(start + 3, ~0L, 0);
+ if ((i = highest_fd()) != start + 2)
+ fail("close_range", "highest fd %d", i);
+ ok("close_range");
+
+ /* Now close the rest */
+ close_range(start, start + 4, 0);
+ fd = highest_fd();
+ if (fd != 3)
+ fail("close_range", "highest fd %d", fd);
+ ok("close_range");
+
return (0);
}