From: John Kohl Date: Thu, 19 Apr 1990 16:31:28 +0000 (+0000) Subject: add POSIX version X-Git-Tag: krb5-1.0-alpha2~872 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=d7410d356bbca0b26342e47f243e13e9a5fcff0f;p=krb5.git add POSIX version git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@523 dc483132-0cff-0310-8789-dd5450dbe970 --- diff --git a/src/lib/krb5/os/lock_file.c b/src/lib/krb5/os/lock_file.c index d57652004..e15e38fdd 100644 --- a/src/lib/krb5/os/lock_file.c +++ b/src/lib/krb5/os/lock_file.c @@ -22,7 +22,18 @@ static char rcsid_lock_file_c [] = #include #include +#ifdef POSIX +#include +#include +#define SHARED_LOCK F_RDLCK +#define EXCLUSIVE_LOCK F_WRLCK +#define UNLOCK_LOCK F_UNLCK +#else #include +#define SHARED_LOCK LOCK_SH +#define EXCLUSIVE_LOCK LOCK_EX +#define UNLOCK_LOCK LOCK_UN +#endif #include #include @@ -36,22 +47,51 @@ FILE *filep; char *pathname; int mode; { - int flock_flag = -1; +#ifdef POSIX + int lock_cmd = F_SETLKW; + struct flock lock_arg; +#define lock_flag lock_arg.l_type + lock_flag = -1; +#else + int lock_flag = -1; +#endif switch (mode & ~KRB5_LOCKMODE_DONTBLOCK) { case KRB5_LOCKMODE_SHARED: - flock_flag = LOCK_SH; + lock_flag = SHARED_LOCK; + break; case KRB5_LOCKMODE_EXCLUSIVE: - flock_flag = LOCK_EX; + lock_flag = EXCLUSIVE_LOCK; + break; case KRB5_LOCKMODE_UNLOCK: - flock_flag = LOCK_UN; + lock_flag = UNLOCK_LOCK; + break; } - if (flock_flag == -1) + + if (lock_flag == -1) return(KRB5_LIBOS_BADLOCKFLAG); - if (mode & KRB5_LOCKMODE_DONTBLOCK) - flock_flag |= LOCK_NB; - if (flock(fileno(filep), flock_flag) == -1) + if (mode & KRB5_LOCKMODE_DONTBLOCK) { +#ifdef POSIX + lock_cmd = F_SETLK; +#else + lock_flag |= LOCK_NB; +#endif + } + +#ifdef POSIX + lock_arg.l_whence = 0; + lock_arg.l_start = 0; + lock_arg.l_len = 0; + if (fcntl(fileno(filep), lock_cmd, &lock_arg) == -1) { + if (errno == EACCES || errno == EAGAIN) /* see POSIX/IEEE 1003.1-1988, + 6.5.2.4 */ + return(EAGAIN); + return(errno); + } +#else + if (flock(fileno(filep), lock_flag) == -1) return(errno); +#endif return 0; }