Reputation: 819
In a Jailed Linux environment, I want to get the list of file descriptors of the current userland process in C.
Is there a syscall to get the fdt?
Upvotes: 4
Views: 2661
Reputation: 20392
I'm not aware of any way to get a count of file descriptors, but you could cheat.
Open a new filedescriptor and close it, remember the fd number you got.
Starting from 0 to the largest fd you can open (you can get this number with getdtablesize()) dup2 every filedescriptor into that saved fd. Those that don't return errors are open, rest are closed.
As a simplified example, just counting them:
int
count_fds(void)
{
int maxfd = getdtablesize();
int openfds;
int fd;
int i;
openfds = 0;
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
return maxfd;
close(fd);
for (i = 0; i < maxfd; i++) {
if (dup2(i, fd) != -1) {
openfds++;
close(fd);
}
}
return openfds;
}
I'm not aware of any file descriptors that can have side effects if you dup them, but there are always strange beasts out there, so use at own risk.
Upvotes: 1