PR422261 platform selection fails for unqualified client name
[valgrind.git] / none / tests / rlimit_nofile.c
blob1f45612b9737df9d8d9d1188f06c9c5e4f92fe14
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <sys/resource.h>
6 #include <unistd.h>
7 #include "fdleak.h"
9 int main(int argc, char **argv)
11 struct rlimit oldrlim;
12 struct rlimit newrlim;
13 int fd;
15 CLOSE_INHERITED_FDS;
17 if (getrlimit(RLIMIT_NOFILE, &oldrlim) < 0)
19 perror("getrlimit");
20 exit(1);
23 newrlim.rlim_cur = oldrlim.rlim_max+1;
24 newrlim.rlim_max = oldrlim.rlim_max;
25 if (setrlimit(RLIMIT_NOFILE, &newrlim) == -1)
27 if (errno != EINVAL) {
28 fprintf(stderr, "setrlimit exceeding hardlimit must set errno=EINVAL\n");
29 exit(1);
32 else
34 fprintf(stderr, "setrlimit exceeding hardlimit must return -1\n");
35 exit(1);
38 newrlim.rlim_cur = oldrlim.rlim_max;
39 newrlim.rlim_max = oldrlim.rlim_max+1;
40 if (setrlimit(RLIMIT_NOFILE, &newrlim) == -1)
42 if (errno != EPERM) {
43 fprintf(stderr, "setrlimit changing hardlimit must set errno=EPERM\n");
44 exit(1);
47 else
49 fprintf(stderr, "setrlimit changing hardlimit must return -1\n");
50 exit(1);
53 newrlim.rlim_cur = oldrlim.rlim_cur / 2;
54 newrlim.rlim_max = oldrlim.rlim_max;
56 if (setrlimit(RLIMIT_NOFILE, &newrlim) < 0)
58 perror("setrlimit");
59 exit(1);
62 if (getrlimit(RLIMIT_NOFILE, &newrlim) < 0)
64 perror("getrlimit");
65 exit(1);
68 if (newrlim.rlim_cur != oldrlim.rlim_cur / 2)
70 fprintf(stderr, "rlim_cur is %llu (should be %llu)\n",
71 (unsigned long long)newrlim.rlim_cur,
72 (unsigned long long)oldrlim.rlim_cur / 2);
75 if (newrlim.rlim_max != oldrlim.rlim_max)
77 fprintf(stderr, "rlim_max is %llu (should be %llu)\n",
78 (unsigned long long)newrlim.rlim_max,
79 (unsigned long long)oldrlim.rlim_max);
82 newrlim.rlim_cur -= 3; /* allow for stdin, stdout and stderr */
84 while (newrlim.rlim_cur-- > 0)
86 if (open("/dev/null", O_RDONLY) < 0)
88 perror("open");
92 if ((fd = open("/dev/null", O_RDONLY)) >= 0)
94 fprintf(stderr, "open succeeded with fd %d - it should have failed!\n", fd);
96 else if (errno != EMFILE)
98 perror("open");
101 /* We used to test setrlimit(RLIMIT_NOFILE, NULL) -1 || errno != EFAULT,
102 but glibc doesn't give any guarantees that won't just crash, in
103 newer versions it just silently succeeds... See bug #385912. */
105 exit(0);