#include #include #include #include #include int main (void) { #define N1 100 struct rlimit r; r.rlim_cur = N1; r.rlim_max = N1; setrlimit (RLIMIT_NOFILE, &r); int i; int fd[N1]; for (i = 0; ; ++i) if ((fd[i] = open ("/dev/null", 0)) < 0) { if (i == N1 - 3) printf ("fine, %d file descriptors open\n", N1); else { puts ("*** make sure the parent doesn't open any descriptors other than 0, 1, 2"); return 1; } break; } #define N2 50 r.rlim_cur = N2; r.rlim_max = N2; int e = setrlimit (RLIMIT_NOFILE, &r); if (e == EINVAL) { puts ("good, setrlimit sees the open descriptors"); return 0; } getrlimit (RLIMIT_NOFILE, &r); if (r.rlim_cur != N2 || r.rlim_max != N2) { puts ("getrlimit returned different values"); return 1; } close (fd[N2 - 4]); fd[N2 - 4] = open ("/dev/null", 0); if (fd[N2 - 4] != -1) { printf ("opening a new descriptor succeeded even though %d are open and the limit is %d\n", N1 - 1, N2); return 1; } return 0; }