Skip to content

Instantly share code, notes, and snippets.

@sahib
Created Feb 24, 2015
Embed
What would you like to do?
Find number of file descriptor per process (initially 3 usually)
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