6 #include <sys/resource.h>
10 /* For the linux kernel the CLOSE_RANGE_* constants are in a separate header. */
11 #if defined(__linux__)
12 #include <linux/close_range.h>
15 /* It looks like close_range was initially implemented for FreeBSD 13
16 * but without CLOSE_RANGE_CLOEXEC
17 * That implementation got back ported to FreeBSD 12.2
18 * And then CLOSE_RANGE_CLOEXEC added to 13 but not backported
19 * so 12 has close_range but not CLOSE_RANGE_CLOEXEC */
20 #if !defined(CLOSE_RANGE_CLOEXEC)
21 #define CLOSE_RANGE_CLOEXEC 1<<2
27 // I'm assuming opens start at 3 and get recycled
28 int fd1
= open("close_range.c", O_RDONLY
);
29 int fd2
= open("close_range.vgtest", O_RDONLY
);
30 int fd3
= open("close_range.stderr.exp", O_RDONLY
);
33 close_range(fd1
, fd3
, 0);
35 close_range(fd1
, fd3
, 0);
37 fd1
= open("close_range.c", O_RDONLY
);
38 fd2
= open("close_range.vgtest", O_RDONLY
);
40 // 3 and 4 open 5 closed
41 close_range(fd1
, fd3
, 0);
43 fd1
= open("close_range.c", O_RDONLY
);
44 fd3
= open("close_range.stderr.exp", O_RDONLY
);
46 // 3 and 5 open 4 closed
47 close_range(fd1
, fd3
, 0);
49 fd1
= open("close_range.c", O_RDONLY
);
50 fd2
= open("close_range.vgtest", O_RDONLY
);
51 fd3
= open("close_range.stderr.exp", O_RDONLY
);
54 close_range(fd1
, fd3
, CLOSE_RANGE_CLOEXEC
);
55 close_range(fd1
, fd3
, 0);
57 fd1
= open("close_range.c", O_RDONLY
);
58 fd2
= open("close_range.vgtest", O_RDONLY
);
59 fd3
= open("close_range.stderr.exp", O_RDONLY
);
63 close_range(fd1
, fd3
, 2);
64 assert(errno
= EINVAL
);
68 close_range(fd3
, fd1
, 2);
69 assert(errno
= EINVAL
);
72 getrlimit(RLIMIT_NOFILE
, &rl
);
75 close_range(rl
.rlim_cur
+100, rl
.rlim_cur
+200, 0);
77 close_range(3, rl
.rlim_cur
, 0);
79 fd1
= open("close_range.c", O_RDONLY
);
80 fd2
= open("close_range.vgtest", O_RDONLY
);
81 fd3
= open("close_range.stderr.exp", O_RDONLY
);
83 close_range(3, rl
.rlim_cur
+1, 0);