thughes | ad1c956 | 2004-06-26 11:27:52 +0000 | [diff] [blame] | 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 | |
| 8 | int main(int argc, char **argv) |
| 9 | { |
| 10 | struct rlimit oldrlim; |
| 11 | struct rlimit newrlim; |
| 12 | int fd; |
| 13 | |
| 14 | if (getrlimit(RLIMIT_NOFILE, &oldrlim) < 0) |
| 15 | { |
| 16 | perror("getrlimit"); |
| 17 | exit(1); |
| 18 | } |
| 19 | |
| 20 | newrlim.rlim_cur = oldrlim.rlim_cur / 2; |
| 21 | newrlim.rlim_max = oldrlim.rlim_max; |
| 22 | |
| 23 | if (setrlimit(RLIMIT_NOFILE, &newrlim) < 0) |
| 24 | { |
| 25 | perror("setrlimit"); |
| 26 | exit(1); |
| 27 | } |
| 28 | |
| 29 | if (getrlimit(RLIMIT_NOFILE, &newrlim) < 0) |
| 30 | { |
| 31 | perror("getrlimit"); |
| 32 | exit(1); |
| 33 | } |
| 34 | |
| 35 | if (newrlim.rlim_cur != oldrlim.rlim_cur / 2) |
| 36 | { |
| 37 | fprintf(stderr, "rlim_cur is %lu (should be %lu)\n", |
| 38 | newrlim.rlim_cur, oldrlim.rlim_cur / 2); |
| 39 | } |
| 40 | |
| 41 | if (newrlim.rlim_max != oldrlim.rlim_max) |
| 42 | { |
| 43 | fprintf(stderr, "rlim_max is %lu (should be %lu)\n", |
| 44 | newrlim.rlim_max, oldrlim.rlim_max); |
| 45 | } |
| 46 | |
| 47 | newrlim.rlim_cur -= 3; /* allow for stdin, stdout and stderr */ |
| 48 | |
| 49 | while (newrlim.rlim_cur-- > 0) |
| 50 | { |
| 51 | if (open("/dev/null", O_RDONLY) < 0) |
| 52 | { |
| 53 | perror("open"); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if ((fd = open("/dev/null", O_RDONLY)) >= 0) |
| 58 | { |
| 59 | fprintf(stderr, "open succeeded with fd %d - it should have failed!\n", fd); |
| 60 | } |
| 61 | else if (errno != EMFILE) |
| 62 | { |
| 63 | perror("open"); |
| 64 | } |
| 65 | |
| 66 | exit(0); |
| 67 | } |