In Linux, one can duplicate a file descriptor by using the dup
command family.
Is there any way to get the count of the number of the duplicates for a file descriptor by way of a system call?
In Linux, one can duplicate a file descriptor by using the dup
command family.
Is there any way to get the count of the number of the duplicates for a file descriptor by way of a system call?
Try using fstat()
on open descriptors. This syscall returns struct stat
. Duplicate descriptors refer to the same i-node on the same device (st_dev
and st_ino
members of struct stat
).
By runing loop through all open descriptors (You can safely run loop from 0
to getrlimit(RLIMIT_NOFILE, ...)
- stat()
will return -1 for closed descriptors) and checking for unique st_dev
and st_ino
You will find duplicates.
UPDATE:
After looking closer into fuser
command on Linux, it seems You can find this info for multiple processes by looking at /proc/PIF/fd/
folders (build summary of files opened by all or "interesting" processes).
on FreeBSD similar trick is done by fstat
command (without mounted /proc
filesystem), but this approach probably won't be applicable to Linux (haven't checked deeper).