5 #include <sys/resource.h>
9 int main(int argc
, char **argv
)
11 struct rlimit oldrlim
;
12 struct rlimit newrlim
;
17 if (getrlimit(RLIMIT_NOFILE
, &oldrlim
) < 0)
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");
34 fprintf(stderr
, "setrlimit exceeding hardlimit must return -1\n");
38 newrlim
.rlim_cur
= oldrlim
.rlim_max
;
39 newrlim
.rlim_max
= oldrlim
.rlim_max
+1;
40 if (setrlimit(RLIMIT_NOFILE
, &newrlim
) == -1)
43 fprintf(stderr
, "setrlimit changing hardlimit must set errno=EPERM\n");
49 fprintf(stderr
, "setrlimit changing hardlimit must return -1\n");
53 newrlim
.rlim_cur
= oldrlim
.rlim_cur
/ 2;
54 newrlim
.rlim_max
= oldrlim
.rlim_max
;
56 if (setrlimit(RLIMIT_NOFILE
, &newrlim
) < 0)
62 if (getrlimit(RLIMIT_NOFILE
, &newrlim
) < 0)
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)
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
)
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. */