Bug 497723 - forgot to restore callgrind output cleanup
[valgrind.git] / none / tests / rlimit64_nofile.c
blob54c3edf73ab781d0c4a33a4e4dcb851985b71dd4
1 #define _LARGEFILE_SOURCE
2 #define _LARGEFILE64_SOURCE
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <sys/resource.h>
9 #include <unistd.h>
10 #include "fdleak.h"
12 int main(int argc, char **argv)
14 struct rlimit64 oldrlim;
15 struct rlimit64 newrlim;
16 int fd;
18 CLOSE_INHERITED_FDS;
20 if (getrlimit64(RLIMIT_NOFILE, &oldrlim) < 0)
22 perror("getrlimit");
23 exit(1);
26 newrlim.rlim_cur = oldrlim.rlim_max+1;
27 newrlim.rlim_max = oldrlim.rlim_max;
28 if (setrlimit64(RLIMIT_NOFILE, &newrlim) == -1)
30 if (errno != EINVAL) {
31 fprintf(stderr, "setrlimit64 exceeding hardlimit must set errno=EINVAL\n");
32 exit(1);
35 else
37 fprintf(stderr, "setrlimit64 exceeding hardlimit must return -1\n");
38 exit(1);
41 newrlim.rlim_cur = oldrlim.rlim_max;
42 newrlim.rlim_max = oldrlim.rlim_max+1;
43 if (setrlimit64(RLIMIT_NOFILE, &newrlim) == -1)
45 if (errno != EPERM) {
46 fprintf(stderr, "setrlimit64 changing hardlimit must set errno=EPERM\n");
47 exit(1);
50 else
52 fprintf(stderr, "setrlimit64 changing hardlimit must return -1\n");
53 exit(1);
56 newrlim.rlim_cur = oldrlim.rlim_cur / 2;
57 newrlim.rlim_max = oldrlim.rlim_max;
59 if (setrlimit64(RLIMIT_NOFILE, &newrlim) < 0)
61 perror("setrlimit64");
62 exit(1);
65 if (getrlimit64(RLIMIT_NOFILE, &newrlim) < 0)
67 perror("getrlimit");
68 exit(1);
71 if (newrlim.rlim_cur != oldrlim.rlim_cur / 2)
73 fprintf(stderr, "rlim_cur is %llu (should be %llu)\n",
74 (unsigned long long)newrlim.rlim_cur,
75 (unsigned long long)oldrlim.rlim_cur / 2);
78 if (newrlim.rlim_max != oldrlim.rlim_max)
80 fprintf(stderr, "rlim_max is %llu (should be %llu)\n",
81 (unsigned long long)newrlim.rlim_max,
82 (unsigned long long)oldrlim.rlim_max);
85 newrlim.rlim_cur -= 3; /* allow for stdin, stdout and stderr */
87 while (newrlim.rlim_cur-- > 0)
89 if (open("/dev/null", O_RDONLY) < 0)
91 perror("open");
95 if ((fd = open("/dev/null", O_RDONLY)) >= 0)
97 fprintf(stderr, "open succeeded with fd %d - it should have failed!\n", fd);
99 else if (errno != EMFILE)
101 perror("open");
104 exit(0);