Browse Source

Plan 9 from Bell Labs 2012-10-26

David du Colombier 11 years ago
parent
commit
55e94a846a
1 changed files with 12 additions and 10 deletions
  1. 12 10
      sys/src/cmd/lock.c

+ 12 - 10
sys/src/cmd/lock.c

@@ -44,23 +44,25 @@ openlock(char *lock)
 	int lckfd;
 	Dir *dir;
 
-	if (lockwait)
-		while ((lckfd = open(lock, ORDWR)) < 0)
-			sleep(1000);
-	else
-		lckfd = open(lock, ORDWR);
-	if (lckfd < 0)
-		sysfatal("can't open %s read/write: %r", lock);
-	dir = dirfstat(lckfd);
+	/* first ensure that the lock file has the lock bit set */
+	dir = dirstat(lock);
 	if (dir == nil)
-		sysfatal("can't fstat %s: %r", lock);
+		sysfatal("can't stat %s: %r", lock);
 	if (!(dir->mode & DMEXCL)) {
 		dir->mode |= DMEXCL;
 		dir->qid.type |= QTEXCL;
-		if (dirfwstat(lckfd, dir) < 0)
+		if (dirwstat(lock, dir) < 0)
 			sysfatal("can't make %s exclusive access: %r", lock);
 	}
 	free(dir);
+
+	if (lockwait)
+		while ((lckfd = open(lock, ORDWR)) < 0)
+			sleep(1000);
+	else
+		lckfd = open(lock, ORDWR);
+	if (lckfd < 0)
+		sysfatal("can't open %s read/write: %r", lock);
 	return lckfd;
 }