Commit Diff


commit - d2ffec702ecc02ddb00700b713865a7868d9805b
commit + e750400529eb4e655e206cbfba42b156a03c29c3
blob - 6bfb0c1b2e8d77325d9e9d00eb0b76d40ff928c0
blob + a9e796adbcfa4673447e8ae29dc2737cab48df23
--- src/lib9/create.c
+++ src/lib9/create.c
@@ -1,6 +1,7 @@
 #define _GNU_SOURCE	/* for Linux O_DIRECT */
 #include <u.h>
 #define NOPLAN9DEFINES
+#include <sys/file.h>
 #include <libc.h>
 #include <sys/stat.h>
 #ifndef O_DIRECT
@@ -10,11 +11,12 @@
 int
 p9create(char *path, int mode, ulong perm)
 {
-	int fd, cexec, umode, rclose;
+	int fd, cexec, umode, rclose, lock;
 
+	lock = mode&OLOCK;
 	cexec = mode&OCEXEC;
 	rclose = mode&ORCLOSE;
-	mode &= ~(ORCLOSE|OCEXEC);
+	mode &= ~(ORCLOSE|OCEXEC|OLOCK);
 
 	/* XXX should get mode mask right? */
 	fd = -1;
@@ -45,6 +47,12 @@ p9create(char *path, int mode, ulong perm)
 	}
 out:
 	if(fd >= 0){
+		if(lock){
+			if(flock(fd, (mode==OREAD) ? LOCK_SH : LOCK_EX) < 0){
+				close(fd);
+				return -1;
+			}
+		}
 		if(cexec)
 			fcntl(fd, F_SETFL, FD_CLOEXEC);
 		if(rclose)
blob - 9d9ba310ece17cb9df7fe0ba3749fccba15a605f
blob + 5b4935c1a2c5fa3c903bd57fde23dd9d2f2788b1
--- src/lib9/open.c
+++ src/lib9/open.c
@@ -1,6 +1,7 @@
 #define _GNU_SOURCE	/* for Linux O_DIRECT */
 #include <u.h>
 #define NOPLAN9DEFINES
+#include <sys/file.h>
 #include <libc.h>
 #ifndef O_DIRECT
 #define O_DIRECT 0
@@ -10,12 +11,13 @@ int
 p9open(char *name, int mode)
 {
 	int cexec, rclose;
-	int fd, umode;
+	int fd, umode, lock;
 
 	umode = mode&3;
 	cexec = mode&OCEXEC;
 	rclose = mode&ORCLOSE;
-	mode &= ~(3|OCEXEC|ORCLOSE);
+	lock = mode&OLOCK;
+	mode &= ~(3|OCEXEC|ORCLOSE|OLOCK);
 	if(mode&OTRUNC){
 		umode |= O_TRUNC;
 		mode ^= OTRUNC;
@@ -30,6 +32,12 @@ p9open(char *name, int mode)
 	}
 	fd = open(name, umode);
 	if(fd >= 0){
+		if(lock){
+			if(flock(fd, (mode==OREAD) ? LOCK_SH : LOCK_EX) < 0){
+				close(fd);
+				return -1;
+			}
+		}
 		if(cexec)
 			fcntl(fd, F_SETFL, FD_CLOEXEC);
 		if(rclose)