*BSD News Article 85813


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.carno.net.au!harbinger.cc.monash.edu.au!news.rmit.EDU.AU!news.unimelb.EDU.AU!munnari.OZ.AU!news.ecn.uoknor.edu!feed1.news.erols.com!howland.erols.net!www.nntp.primenet.com!nntp.primenet.com!news1.best.com!nntp1.best.com!not-for-mail
From: dillon@flea.best.net (Matt Dillon)
Newsgroups: comp.unix.bsd.freebsd.misc
Subject: Re: Locking master.passwd correctly
Date: 26 Dec 1996 16:47:33 -0800
Organization: BEST Internet Communications, Inc.
Lines: 42
Message-ID: <59v6b5$ri7@flea.best.net>
References: <59sro8$l1a@atlas.jcpenney.com>
NNTP-Posting-Host: flea.best.net
Xref: euryale.cc.adfa.oz.au comp.unix.bsd.freebsd.misc:33286

:In article <59sro8$l1a@atlas.jcpenney.com>,
:drown  <drown@chaos.taylored.com> wrote:
:>Hi folks,
:>
:>I've had some weird problems lately with chfn stomping over my account
:>creation software.  It seems that chfn doesn't "see" my lock on the 
:>master.passwd file sometimes, even though at other times it does.  It seems 
:>to be a race condition.  Here's the relivant perl code for locking the
:>password file:
:>
:>open(MPW, "</etc/master.passwd") || die "cant locate $masterpw?!?!\n";
:>if (!flock(MPW, 6)) {
:>        close(MPW);
:>        while(1) {
:>                open(MPW, "</etc/master.passwd");
:>                if (flock(MPW, 6)) { # Got it
:>                       last;
:>                }
:>                close(MPW);
:>                sleep(5);
:>        }
:>}
:>
:>Seems like it should work.  The strange thing is that it usually _does_ work,
:>it's just that every so often a user edits their info via chfn and boom,
:>problems.  I've confirmed this by patching pwd_mkdb to log when it's called.
:>
:>Any ideas what could be causing this?  Am I locking the file incorrectly?
:>
:>Thanks!
:>
:>drown@chaos.taylored.com

   Oh, p.s., you have to fix your code as well... once you get the 
   exclusive lock, you must do an fstat() call on the file descriptor
   and check to see if st_nlink is 0.  If it IS 0, then somebody renamed
   the file out from under you and you must close() and loop back to your
   open.

						-Matt