[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

lockfiles in pwdb



Either I'm not reading it right, or there's a problem with the locking
code in pwdb. Specifically in the do_lock_file() function.

The general outline of do_lock_file is like this:

Make a scratch file with our PID in it.
Attempt to link the scratch file to the lockfile
	(i.e. /etc/passwd.lock or whatever)
If the link fails, open the existing lockfile and check the PID it
	contains.
If that PID doesn't exist unlink the stale lockfile and try to link
	our file again.
After a successful link, check the link count of the lockfile to
	prevent a race condition. It should be exactly 2.

This omits a few branches in the code but it fleshes out the avenue
that's going to lead to trouble. Let's say there are two processes
that want to lock /etc/passwd, and a stale lockfile exists.

#1 goes first. It tries to make a link, fails, checks the old PID, and
decides the lock is stale and should be removed. Then, #1 is forced to
relinquish the processor.

#2 comes up before #1 can go again. #2 tries to make a link, fails,
checks the old PID (which remember is not #1's PID yet!) and decides
the lock is stale. Then #2's time is up.

#1 comes up again, removes the stale lock, links it's own scratch
file, checks the link count (which is now 2) and decides everything is
okay. It's somewhere in its critical section, doing critical things,
when its time runs out again.

#2 comes up. It has already decided to remove the lockfile, so it
does, even though the lock now belongs to #1. It links it's own
scratch file, checks the link count (still 2) and decides everything
is okay. Whoops, both processes are now in the critical section.

I understand this is pretty damn unlikely. The do_lock_file() code is
safe. Just not completely safe.

I like to be a fixer instead of a whiner, so I'm trying to think of a
solution. Unfortunately the stuff I'm thinking of applies fcntl() and
won't work over NFS. Any ideas?

	- A





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index] []