This is the mail archive of the
mailing list for the Cygwin project.
Re: Mandatory file locking semantics (was: sqlite3: bug with monotone)
- From: Corinna Vinschen <corinna-cygwin at cygwin dot com>
- To: cygwin at cygwin dot com
- Date: Wed, 5 Jun 2013 19:02:46 +0200
- Subject: Re: Mandatory file locking semantics (was: sqlite3: bug with monotone)
- References: <51A7862F dot 1070507 at etr-usa dot com> <51A7D47E dot 3050502 at users dot sourceforge dot net> <51A7F547 dot 6020509 at etr-usa dot com> <20130531092228 dot GB30659 at calimero dot vinschen dot de> <51A900EF dot 2020606 at etr-usa dot com> <20130601105741 dot GC30659 at calimero dot vinschen dot de> <20130602103125 dot GE13934 at calimero dot vinschen dot de> <51ACD415 dot 2090709 at etr-usa dot com> <20130604084815 dot GD19572 at calimero dot vinschen dot de> <51AF6433 dot 5050104 at etr-usa dot com>
- Reply-to: cygwin at cygwin dot com
On Jun 5 10:15, Warren Young wrote:
> On 6/4/2013 02:48, Corinna Vinschen wrote:
> >On Jun 3 11:36, Warren Young wrote:
> >>Could you add an O_MAND open(2) option as well to turn on the same
> >>feature? This will avoid a race condition.
> >If you call F_LCK_MANDATORY right after open in the opening routine,
> >there won't be (much of) a race. I know what you mean (it's the
> >O_CLOEXEC dilemma)
> As long as everyone plays by the rules, there actually isn't a race.
> For this style of locking to work at all, every program involved has
> to enable the mandatory locking flag before doing any I/O on the
> file. In that sense, locking is already atomic.
> Adding O_MAND changes the call pattern from:
> fd = open(..., O_RDWR);
> fcntl(fd, F_LCK_MANDATORY, 1);
> fcntl(fd, F_SETLK, &lock);
> fd = open(..., O_RDWR | O_MAND);
> fcntl(fd, F_SETLK, &lock);
> Since there are two required calls due to the POSIX design, there's
> still a hole here, where an uncooperative program could call open(2)
> for writing on the same file without acquiring the lock. To fix
> *that* race condition, you'd need an extension to open(2) that lets
> it acquire the lock on open. That just isn't in the cards.
Advisory locking is *based* on cooperative behaviour. That means,
the assumption is that all applications accessing certain files
behave like they should and not write to a file before asking for the
lock. In your case, there's the sqlite lib. If every application
accessing an sqlite db uses the sqlite lib, there's no reason to
assume that there's an uncooperative application.
> Or am I missing something? Does mandatory locking on Linux or SysV
> somehow avoid the "uncooperative program" race?
Not at all. Did you read Section 0, "Why you should avoid mandatory
locking" in Linux Documentation/filesystems/mandatory-locking.txt file?
It's all about races.
> That having been said, I still think O_MAND works better.
Again, to be clear: Mandatory locking is not for the every day use of a
Cygwin application. I provided it for the sake of a *very* limited
scenario, where Cygwin applications have to cooperate with native Windows
applications using record locking. The burden to use it correctly is
on the application developer.
Corinna Vinschen Please, send mails regarding Cygwin to
Cygwin Maintainer cygwin AT cygwin DOT com
Problem reports: http://cygwin.com/problems.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple