lockingfcntl

Why fcntl start=0, len=0, whence=2 works?


According to the fcntl manual, fcntl locking with start=0, len=0, whence=2 should lock the byte range starting from the end of file (whence=2), with offset 0 (start=0) until the end of file (len=0), which in my mind means locking in total 0 bytes from EOF to EOF.

In this case I would expect that locking with those arguments would not lock anything. However, if I try (using python wrapper fcntl), the following code does lock something, and the second copy is waiting for the first to finish:

f = open('some_file', 'a+')
fcntl.lockf(f, fcntl.LOCK_EX, 0, 0, 2)
print('got the lock')
time.sleep(100)

Similarly, the code with parameters whence=2, offset=100, len=0 also works, even those in this case the byte range is backwards [EOF + 100, EOF].

What am I locking?


Solution

  • I did some tests, and the answer seems to be as follows - whence=2 does not lock until the EOF, but until the infinity, which is not how I would read the description in the man page:

    Specifying 0 for l_len has the special meaning: lock all bytes starting at the location specified by l_whence and l_start through to the end of file, no matter how large the file grows.

    😞