Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are POSIX file locks reentrant?

I am using POSIX mandatory file locks through fcntl. I'm wondering if those locks are reentrant, ie. can a process acquire a lock it already owns ?

like image 703
paradigmatic Avatar asked Oct 06 '22 02:10

paradigmatic


1 Answers

Advisory locks through fcntl are on a per process base and just accumulate locked intervals on the file for the given process. That is, it is up to the application to keep track of the intervals and any unlock call for an interval will unlock it, regardless on how many lock calls had been made for that interval.

Even worse, the closing of any file descriptor for the file cancels all locks on the file:

As well as being removed by an explicit F_UNLCK, record locks are automatically released when the process terminates or if it closes any file descriptor referring to a file on which locks are held. This is bad: it means that a process can lose the locks on a file like /etc/passwd or /etc/mtab when for some reason a library function decides to open, read and close it.

like image 134
Jens Gustedt Avatar answered Oct 10 '22 08:10

Jens Gustedt