Add missing zstd.h to coregrind Makefile.am noinst_HEADERS
[valgrind.git] / memcheck / tests / close_range.c
blob6d8b5a0ba842cfe0fcf44074b659441217484bd7
1 #define _GNU_SOURCE
2 #include <unistd.h>
3 #include <errno.h>
4 #include <sys/types.h>
5 #include <sys/time.h>
6 #include <sys/resource.h>
7 #include <fcntl.h>
8 #include <assert.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>
13 #endif
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
22 #endif
24 int main(void)
26 struct rlimit rl;
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);
32 // all open
33 close_range(fd1, fd3, 0);
34 // all closed
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);
53 // good flag
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);
61 errno = 0;
62 // bad flag
63 close_range(fd1, fd3, 2);
64 assert(errno = EINVAL);
66 errno = 0;
67 // wrong order
68 close_range(fd3, fd1, 2);
69 assert(errno = EINVAL);
71 errno = 0;
72 getrlimit(RLIMIT_NOFILE, &rl);
74 // should do nothing
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);
86 unsigned a;
87 unsigned b;
88 int c;
89 close_range(a, b, c);