On Tue, Oct 11, 2011 at 03:16:38PM +0200, Raphael Hertzog wrote: > On Tue, 11 Oct 2011, Guillem Jover wrote: > > The easy solution to this would seem to be to package something like > > the perl File::FcntlLock module: > > > > <http://search.cpan.org/~jtt/File-FcntlLock-0.12/> > > Dear Debian perl team members, it would be nice if one of you could > package this module. :-)
The ITP was filed[0], so that may soon be a viable option. > > Besides the loop being somewhat suboptimal, usage of “O_CREAT | O_EXCL” > > on NFS might be unreliable. > > The documentation mentions a work around: > > Portable programs that want to perform atomic file locking using a lockfile, > and need to avoid reliance on NFS support for O_EXCL, can create a unique > file on the same file system (e.g., incorporating hostname and PID), and use > link(2) to make a link to the lockfile. If link(2) returns 0, the lock is > successful. Otherwise, use stat(2) on the unique file to check if its link > count has increased to 2, in which case the lock is also successful. > > Would this be better? > > If I get this right it's: > while (1) { > open(LOCK, ">", "debian/files.new.$$") > my $ok = link("debian/files.new.$$", "debian/files.new") > if (!ok) { > my @st = stat("debian/files.new.$$") || syserr(""); > $ok = 1 if $st[3] == 2; > } > unlink("debian/files.new.$$") || syserr(""); > last if $ok; > sleep(1); # Or rather usleep from Time::HiRes > } > > I'm not sure what case it covers to check for the link count on a failure... From link(2): BUGS On NFS file systems, the return code may be wrong in case the NFS server performs the link creation and dies before it can say so. Use stat(2) to find out if the link got created. Cheers, James [0]: http://bugs.debian.org/645014 -- James GPG Key: 1024D/61326D40 2003-09-02 James Vega <james...@debian.org>
signature.asc
Description: Digital signature