add OLOCK

This commit is contained in:
rsc 2004-06-11 14:38:56 +00:00
parent d2ffec702e
commit e750400529
2 changed files with 20 additions and 4 deletions

View file

@ -1,6 +1,7 @@
#define _GNU_SOURCE /* for Linux O_DIRECT */ #define _GNU_SOURCE /* for Linux O_DIRECT */
#include <u.h> #include <u.h>
#define NOPLAN9DEFINES #define NOPLAN9DEFINES
#include <sys/file.h>
#include <libc.h> #include <libc.h>
#include <sys/stat.h> #include <sys/stat.h>
#ifndef O_DIRECT #ifndef O_DIRECT
@ -10,11 +11,12 @@
int int
p9create(char *path, int mode, ulong perm) p9create(char *path, int mode, ulong perm)
{ {
int fd, cexec, umode, rclose; int fd, cexec, umode, rclose, lock;
lock = mode&OLOCK;
cexec = mode&OCEXEC; cexec = mode&OCEXEC;
rclose = mode&ORCLOSE; rclose = mode&ORCLOSE;
mode &= ~(ORCLOSE|OCEXEC); mode &= ~(ORCLOSE|OCEXEC|OLOCK);
/* XXX should get mode mask right? */ /* XXX should get mode mask right? */
fd = -1; fd = -1;
@ -45,6 +47,12 @@ p9create(char *path, int mode, ulong perm)
} }
out: out:
if(fd >= 0){ if(fd >= 0){
if(lock){
if(flock(fd, (mode==OREAD) ? LOCK_SH : LOCK_EX) < 0){
close(fd);
return -1;
}
}
if(cexec) if(cexec)
fcntl(fd, F_SETFL, FD_CLOEXEC); fcntl(fd, F_SETFL, FD_CLOEXEC);
if(rclose) if(rclose)

View file

@ -1,6 +1,7 @@
#define _GNU_SOURCE /* for Linux O_DIRECT */ #define _GNU_SOURCE /* for Linux O_DIRECT */
#include <u.h> #include <u.h>
#define NOPLAN9DEFINES #define NOPLAN9DEFINES
#include <sys/file.h>
#include <libc.h> #include <libc.h>
#ifndef O_DIRECT #ifndef O_DIRECT
#define O_DIRECT 0 #define O_DIRECT 0
@ -10,12 +11,13 @@ int
p9open(char *name, int mode) p9open(char *name, int mode)
{ {
int cexec, rclose; int cexec, rclose;
int fd, umode; int fd, umode, lock;
umode = mode&3; umode = mode&3;
cexec = mode&OCEXEC; cexec = mode&OCEXEC;
rclose = mode&ORCLOSE; rclose = mode&ORCLOSE;
mode &= ~(3|OCEXEC|ORCLOSE); lock = mode&OLOCK;
mode &= ~(3|OCEXEC|ORCLOSE|OLOCK);
if(mode&OTRUNC){ if(mode&OTRUNC){
umode |= O_TRUNC; umode |= O_TRUNC;
mode ^= OTRUNC; mode ^= OTRUNC;
@ -30,6 +32,12 @@ p9open(char *name, int mode)
} }
fd = open(name, umode); fd = open(name, umode);
if(fd >= 0){ if(fd >= 0){
if(lock){
if(flock(fd, (mode==OREAD) ? LOCK_SH : LOCK_EX) < 0){
close(fd);
return -1;
}
}
if(cexec) if(cexec)
fcntl(fd, F_SETFL, FD_CLOEXEC); fcntl(fd, F_SETFL, FD_CLOEXEC);
if(rclose) if(rclose)