This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
#include <poll.h> | |
#include <sys/time.h> | |
#include <sys/resource.h> | |
int open_files(unsigned long *soft, unsigned long *hard, unsigned long *current); | |
int main(int argc, char **argv) | |
{ | |
if (argc == 2) { | |
unsigned int max; | |
if (strcmp(argv[1], "max") == 0) | |
max = 0xffffffff; | |
else | |
max = strtoul(argv[1], NULL, 10); | |
int i; | |
for (i = 0; i < max; i++) | |
if (!tmpfile()) | |
break; | |
} | |
unsigned long soft, hard, current; | |
if (open_files(&soft, &hard, ¤t) != 0) { | |
perror("open_files"); | |
return 1; | |
} | |
printf("%lu fds used (limit %lu/%lu)\n", current, soft, hard); | |
return 0; | |
} | |
int open_files(unsigned long *soft, unsigned long *hard, unsigned long *current) | |
{ | |
int rc, i; | |
struct rlimit lim; | |
errno = EINVAL; | |
if (!soft || !hard || !current) | |
return -1; | |
rc = getrlimit(RLIMIT_NOFILE, &lim); | |
if (rc != 0) | |
return rc; | |
*soft = (unsigned long)lim.rlim_cur; | |
*hard = (unsigned long)lim.rlim_max; | |
struct pollfd *fds = calloc(*soft, sizeof(struct pollfd)); | |
if (!fds) | |
return -1; | |
for (i = 0; i < *soft; i++) | |
fds[i].fd = i; | |
rc = poll(fds, *soft, 0); | |
if (rc < 0) | |
return rc; | |
*current = *soft; | |
for (i = 0; i < lim.rlim_cur; i++) | |
if (fds[i].revents & POLLNVAL) | |
*current--; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment