per file lock list

Matthew Dillon dillon at
Thu Jul 21 19:26:05 GMT 2005

:We have a question: how to get all POSIX locks for a given file?
:As far as I know, existing API does not allow to retrieve all file
:locks. Therefore, we need to use kernel internal structures to get all
:So the question: is there an elegant way to get the lock list for a given file?
:Thank you in advance.

    You can use F_GETLK to iterate through all posix locks held on a file.
    From man fcntl:

     F_GETLK    Get the first lock that blocks the lock description pointed to
                by the third argument, arg, taken as a pointer to a struct
                flock (see above).  The information retrieved overwrites the
                information passed to fcntl() in the flock structure.  If no
                lock is found that would prevent this lock from being created,
                the structure is left unchanged by this function call except
                for the lock type which is set to F_UNLCK.

    So what you do is you specify a lock description that covers the whole 
    file and call F_GETLK.  You then use the results to modify the lock
    description to a range that starts just past the returned lock
    for the next call.  You continue iterating until F_GETLK tells you that
    there are no more locks.

					Matthew Dillon 
					<dillon at>

More information about the freebsd-hackers mailing list