Get reference count value (counter value of file using) for file by descriptor?
Asked Answered
D

0

6

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?

Disavow answered 20/2, 2013 at 9:0 Comment(4)
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.Alarice
@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?Disavow
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.Alarice
If 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.Obverse

© 2022 - 2024 — McMap. All rights reserved.