1

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?

Makoto
  • 104,088
  • 27
  • 192
  • 230
sdeber
  • 21
  • 1

1 Answers1

1

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).

kestasx
  • 1,091
  • 8
  • 15
  • Sorry, I did not make myself clear. The solution you gave does not solve my problem. I was passing file descriptors around multiple processes and what I wanted is given a specific FD in ANY of these processes, I should be able to count the number FDs in the system that are referencing the same underlying kernel structure. – sdeber Dec 24 '14 at 00:25