lock: reopen lockfile after changing mode to make lock effective

lock is acquired on open/create when file has DMEXCL set in mode,
so we need to reopen the file after setting the bit with wstat.
front
cinap_lenrek 2015-06-13 15:01:02 +02:00
parent 1473e5d437
commit 9c1dff3fa9
1 changed files with 10 additions and 6 deletions

View File

@ -41,24 +41,28 @@ waitfor(int pid)
static int static int
openlock(char *lock) openlock(char *lock)
{ {
int lckfd; int lckfd, didwstat = 0;
Dir *dir; Dir *dir;
if (lockwait) Reopen:
while ((lckfd = open(lock, ORDWR)) < 0) while ((lckfd = open(lock, ORDWR)) < 0 && lockwait)
sleep(1000); sleep(1000);
else
lckfd = open(lock, ORDWR);
if (lckfd < 0) if (lckfd < 0)
sysfatal("can't open %s read/write: %r", lock); sysfatal("can't open %s read/write: %r", lock);
dir = dirfstat(lckfd); dir = dirfstat(lckfd);
if (dir == nil) if (dir == nil)
sysfatal("can't fstat %s: %r", lock); sysfatal("can't fstat %s: %r", lock);
if (!(dir->mode & DMEXCL)) { if (!(dir->mode & DMEXCL)) {
if(didwstat++)
sysfatal("exclusive bit does not stick for %s", lock);
dir->mode |= DMEXCL; dir->mode |= DMEXCL;
dir->qid.type |= QTEXCL; dir->qid.type |= QTEXCL;
if (dirfwstat(lckfd, dir) < 0) if (dirfwstat(lckfd, dir) < 0)
sysfatal("can't make %s exclusive access: %r", lock); sysfatal("can't make %s exclusive access: %r", lock);
/* reopen for lock to be effective */
free(dir);
close(lckfd);
goto Reopen;
} }
free(dir); free(dir);
return lckfd; return lckfd;