In Linux, is it possible to get the reference count value (counter value of using a file) for a specified file by descriptor by using non-kernel API?
Asked
Active
Viewed 2,706 times
6
-
The /proc-filesystem offers you information about the current running processes and files. But apart from that, C is not capable of doing anything like this without specific functions provided for such means. – bash.d Feb 20 '13 at 09:22
-
@bash.d, if I understood your comment correctly it is meant that C standard runtime libraries is not contains any API calls for this purpose? – ramb0tn1k Feb 20 '13 at 11:06
-
The C stdlib is not meant to do this. You'll need to include Linux-specific libraries to perform this. In the C stdlib you'll find things like I/O, memory management and stuff. – bash.d Feb 20 '13 at 11:12
-
2If you mean link count, use `lstat(fd, &buf)` and check `buf.st_nlink`. If you mean number of open descriptors, you can use `fcntl(fd, F_SETLEASE, F_RDLCK)` (if `fd` opened read-only) or `fcntl(fd, F_SETLEASE, F_WRLCK)` to place a lease on the file to check if the file is open for writing (`F_RDLCK`) or at all (`F_WRLCK`) -- failing in those cases --, and getting a signal (plus a grace lease-break-time to release the lease) if such open occurs while the lease is held. However, this is not a counter, is very Linux-specific, and is quite limited otherwise too; see `man 2 fcntl` for details. – Nominal Animal Feb 21 '13 at 04:36