FS#27691 - [pacman] better locking mechanism

Attached to Project: Pacman
Opened by Davorin Učakar (phantom) - Friday, 23 December 2011, 20:25 GMT
Last edited by Dave Reisner (falconindy) - Tuesday, 21 February 2012, 04:46 GMT
Task Type Feature Request
Category Backend/Core
Status Closed
Assigned To Dave Reisner (falconindy)
Architecture All
Severity Low
Priority Normal
Reported Version 3.5.4
Due in Version Undecided
Due Date Undecided
Percent Complete 100%
Votes 0
Private No

Details

Instead of creating /var/lib/pacman/db.lck before transactions and deleting it when pacman finishes, a better approach would be to make an exclusive lock on that file (db.lck would be present in the filesystem all the time). Lock is released when a process finishes (or crashes), so there would be no need to manually delete db.lck after pacman crashes or is killed.

#include <fcntl.h>
#include <sys/file.h>
#include <unistd.h>

int fd = open( "/var/lib/pacman/db.lck", O_RDONLY | O_NONBLOCK );
if( fd == -1 ) ... /// failed
int ret = flock( fd, LOCK_EX | LOCK_NB );
if( ret == -1 ) ... // failed

... DB transactions ...

close( fd );
This task depends upon

Closed by  Dave Reisner (falconindy)
Tuesday, 21 February 2012, 04:46 GMT
Reason for closing:  Duplicate
Additional comments about closing:  FS#23501
Comment by Dave Reisner (falconindy) - Sunday, 01 January 2012, 00:16 GMT
+1 for the idea. At a glance, it looks like its portable enough (which makes sense, since linux stole flock semantics from BSD). I can't seem to find anything decisive about cygwin supporting it. As far back as 1999 they only supported the fcntl interface as flock wasn't part of POSIX. Some RedHat patchwork was offered up for inclusion in cygwin in 2003 to implement flock, but I haven't been able to discern whether that ended up being merged, nor can I really find any documentation about flock in cygwin recently.

Regardless, this isn't hard to do. I'll throw together something for a ML and we can bikeshed it there.
Comment by Dave Reisner (falconindy) - Friday, 06 January 2012, 00:07 GMT
And of course, Dan douses my early celebration by pointing out that we need to support NFS. NFSv4 can support locking via flock, but NFS3 seems to only have (sketchy) support via fcntl, which isn't portable. I'll leave it up to Dan to decide whether or not it's acceptable to go ahead with this.
Comment by Dan McGee (toofishes) - Tuesday, 31 January 2012, 19:53 GMT
Can we close this as I opened FS#23501 to cover any number of locking changes?

Loading...