FreeBSD syscall: fixes for semctl
[valgrind.git] / memcheck / tests / freebsd / close_range.c
blobba1adf9b5ca315830913d0a2a3e6eed8b26d1516
1 #include <unistd.h>
2 #include <errno.h>
3 #include <sys/types.h>
4 #include <sys/time.h>
5 #include <sys/resource.h>
6 #include <fcntl.h>
7 #include <assert.h>
9 /* It looks like close_range was initially implemented for FreeBSD 13
10 * but without CLOSE_RANGE_CLOEXEC
11 * That implementation got back ported to FreeBSD 12.2
12 * And then CLOSE_RANGE_CLOEXEC added to 13 but not backported
13 * so 12 has close_range but not CLOSE_RANGE_CLOEXEC */
14 #if !defined(CLOSE_RANGE_CLOEXEC)
15 #define CLOSE_RANGE_CLOEXEC 1<<2
16 #endif
18 int main(void)
20 struct rlimit rl;
21 // I'm assuming opens start at 3 and get recycled
22 int fd1 = open("close_range.c", O_RDONLY);
23 int fd2 = open("close_range.vgtest", O_RDONLY);
24 int fd3 = open("close_range.stderr.exp", O_RDONLY);
26 // all open
27 close_range(fd1, fd3, 0);
28 // all closed
29 close_range(fd1, fd3, 0);
31 fd1 = open("close_range.c", O_RDONLY);
32 fd2 = open("close_range.vgtest", O_RDONLY);
34 // 3 and 4 open 5 closed
35 close_range(fd1, fd3, 0);
37 fd1 = open("close_range.c", O_RDONLY);
38 fd3 = open("close_range.stderr.exp", O_RDONLY);
40 // 3 and 5 open 4 closed
41 close_range(fd1, fd3, 0);
43 fd1 = open("close_range.c", O_RDONLY);
44 fd2 = open("close_range.vgtest", O_RDONLY);
45 fd3 = open("close_range.stderr.exp", O_RDONLY);
47 // good flag
48 close_range(fd1, fd3, CLOSE_RANGE_CLOEXEC);
49 close_range(fd1, fd3, 0);
51 fd1 = open("close_range.c", O_RDONLY);
52 fd2 = open("close_range.vgtest", O_RDONLY);
53 fd3 = open("close_range.stderr.exp", O_RDONLY);
55 errno = 0;
56 // bad flag
57 close_range(fd1, fd3, 2);
58 assert(errno = EINVAL);
60 errno = 0;
61 // wrong order
62 close_range(fd3, fd1, 2);
63 assert(errno = EINVAL);
65 errno = 0;
66 getrlimit(RLIMIT_NOFILE, &rl);
68 // should do nothing
69 close_range(rl.rlim_cur+100, rl.rlim_cur+200, 0);
71 close_range(3, rl.rlim_cur, 0);
73 fd1 = open("close_range.c", O_RDONLY);
74 fd2 = open("close_range.vgtest", O_RDONLY);
75 fd3 = open("close_range.stderr.exp", O_RDONLY);
77 close_range(3, rl.rlim_cur+1, 0);
80 unsigned a;
81 unsigned b;
82 int c;
83 close_range(a, b, c);