diff options
author | Igor Pashev <pashev.igor@gmail.com> | 2012-01-28 20:02:04 +0300 |
---|---|---|
committer | Igor Pashev <pashev.igor@gmail.com> | 2012-01-28 20:02:04 +0300 |
commit | d2964a998745db6e27c1339d44a5ab94ce56c71f (patch) | |
tree | 7f3bf4d3f1ef37e4e06343ea1babef1bfe072f4c | |
parent | bfcc507a16b041056986947afd2ac32c9c00d16d (diff) | |
download | libbsd-d2964a998745db6e27c1339d44a5ab94ce56c71f.tar.gz |
flock() by fcntl()
-rw-r--r-- | src/flopen.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/flopen.c b/src/flopen.c index f5f7338..e93ac7f 100644 --- a/src/flopen.c +++ b/src/flopen.c @@ -36,6 +36,50 @@ __FBSDID("$FreeBSD$"); #include <unistd.h> #include <libutil.h> +#ifndef LOCK_EX +#include <fcntl.h> +#include <memory.h> +#define LOCK_SH 1 +#define LOCK_EX 2 +#define LOCK_NB 4 +#define LOCK_UN 8 + +/* Assume there is not flock() at all */ +static int flock (int fd, int op) +{ + struct flock fl; + int c, r; + + if (op & LOCK_NB) { + c = F_SETLK; + } else { + c = F_SETLKW; + op &= ~LOCK_NB; + } + + memset(&fl, 0, sizeof(fl)); + fl.l_whence = SEEK_SET; + + switch (op) { + case LOCK_SH: + fl.l_type = F_RDLCK; break; + case LOCK_EX: + fl.l_type = F_WRLCK; break; + case LOCK_UN: + fl.l_type = F_UNLCK; break; + default: + errno = EINVAL; + return (-1); + } + + r = fcntl(fd, c, &fl); + if ((r == -1) && (errno == EACCES)) { + errno = EAGAIN; + } + return (r); +} +#endif + int flopen(const char *path, int flags, ...) { |