Find number of file descriptor per process (initially 3 usually)
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
int rm_fd_cache_get_fds_per_process(RmFdCache *self) { | |
g_assert(self); | |
int n = -1; | |
struct rlimit rl; | |
if(getrlimit(RLIMIT_NOFILE, &rl) == -1) { | |
return -1; | |
} | |
struct pollfd *fds = g_malloc0(rl.rlim_cur * sizeof(struct pollfd)); | |
for(rlim_t i = 0; i < rl.rlim_cur; ++i) { | |
fds[i].fd = i; | |
fds[i].events = 0; | |
} | |
if(poll(fds, rl.rlim_cur, 0) == -1) { | |
goto cleanup; | |
} | |
n = rl.rlim_cur; | |
for(rlim_t i = 0; i < rl.rlim_cur; ++i) { | |
n -= !!(fds[i].revents & POLLNVAL); | |
} | |
cleanup: | |
g_free(fds); | |
return n; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment