lock_file.c (krb5_lock_file): Move the flock() fallback code from
authorTheodore Tso <tytso@mit.edu>
Tue, 3 Nov 1998 21:27:35 +0000 (21:27 +0000)
committerTheodore Tso <tytso@mit.edu>
Tue, 3 Nov 1998 21:27:35 +0000 (21:27 +0000)
fcc_maybe.c into krb5_lock_file().  This works around the bug that
certain lossy operating systems (mainly from our good friends at
SunSoft) do not support POSIX_FILE_LOCKS on all filesystems (namely
tmpfs) but do support flock on those filesystems.

git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@11009 dc483132-0cff-0310-8789-dd5450dbe970

src/lib/krb5/os/ChangeLog
src/lib/krb5/os/lock_file.c

index 84f0dec670390c780933ea4d95615749eee274e3..c8ea14f65353f8e2d5f62c05bb21a6af06773257 100644 (file)
@@ -1,3 +1,12 @@
+1998-10-31  Theodore Ts'o  <tytso@rsts-11.mit.edu>
+
+       * lock_file.c (krb5_lock_file): Move the flock() fallback code
+               from fcc_maybe.c into krb5_lock_file().  This works around
+               the bug that certain lossy operating systems (mainly from
+               our good friends at SunSoft) do not support
+               POSIX_FILE_LOCKS on all filesystems (namely tmpfs) but do
+               support flock on those filesystems.
+
 1998-10-27  Marc Horowitz  <marc@mit.edu>
 
        * c_ustime.c, localaddr.c: moved here from lib/crypto
index edc4d88d20489fe1ffc50c358f7ee2e122353e3c..eb8e761837e79dcd7596d35f107448fae980cc0b 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * lib/krb5/os/lock_file.c
  *
- * Copyright 1990 by the Massachusetts Institute of Technology.
+ * Copyright 1990, 1998 by the Massachusetts Institute of Technology.
  * All Rights Reserved.
  *
  * Export of this software from the United States of America may
 #include <unistd.h>
 #endif
 
-#ifdef POSIX_FILE_LOCKS
 #include <errno.h>
+
+#ifdef POSIX_FILE_LOCKS
 #include <fcntl.h>
-#define SHARED_LOCK    F_RDLCK
-#define EXCLUSIVE_LOCK F_WRLCK
-#define UNLOCK_LOCK    F_UNLCK
-#else
-#include <sys/file.h>
-#define SHARED_LOCK    LOCK_SH
-#define EXCLUSIVE_LOCK LOCK_EX
-#define UNLOCK_LOCK    LOCK_UN
 #endif
 
-extern int errno;
+#ifdef HAVE_FLOCK
+#ifndef sysvimp
+#include <sys/file.h>
+#endif
+#else
+#ifndef LOCK_SH
+#define LOCK_SH 0
+#define LOCK_EX 0
+#define LOCK_UN 0
+#endif
+#endif
 
 /*ARGSUSED*/
 krb5_error_code
@@ -57,26 +60,34 @@ krb5_lock_file(context, fd, mode)
     int fd;
     int mode;
 {
+    int                lock_flag = -1;
+    krb5_error_code    retval = 0;
 #ifdef POSIX_FILE_LOCKS
     int lock_cmd = F_SETLKW;
     static struct flock flock_zero;
     struct flock lock_arg;
+
     lock_arg = flock_zero;
-#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:
-       lock_flag = SHARED_LOCK;
+#ifdef POSIX_FILE_LOCKS
+       lock_arg.l_type = F_RDLCK;
+#endif
+       lock_flag = LOCK_SH;
        break;
     case KRB5_LOCKMODE_EXCLUSIVE:
-       lock_flag = EXCLUSIVE_LOCK;
+#ifdef POSIX_FILE_LOCKS
+       lock_arg.l_type = F_WRLCK;
+#endif
+       lock_flag = LOCK_EX;
        break;
     case KRB5_LOCKMODE_UNLOCK:
-       lock_flag = UNLOCK_LOCK;
+#ifdef POSIX_FILE_LOCKS
+       lock_arg.l_type = F_UNLCK;
+#endif
+       lock_flag = LOCK_UN;
        break;
     }
 
@@ -86,7 +97,8 @@ krb5_lock_file(context, fd, mode)
     if (mode & KRB5_LOCKMODE_DONTBLOCK) {
 #ifdef POSIX_FILE_LOCKS
        lock_cmd = F_SETLK;
-#else
+#endif
+#ifdef HAVE_FLOCK
        lock_flag |= LOCK_NB;
 #endif
     }
@@ -99,13 +111,18 @@ krb5_lock_file(context, fd, mode)
        if (errno == EACCES || errno == EAGAIN) /* see POSIX/IEEE 1003.1-1988,
                                                   6.5.2.4 */
            return(EAGAIN);
-       return(errno);
+       if (errno != EINVAL)    /* Fall back to flock if we get EINVAL */
+           return(errno);
+       retval = errno;
     }
-#else
+#endif
+    
+#ifdef HAVE_FLOCK
     if (flock(fd, lock_flag) == -1)
-       return(errno);
+       retval = errno;
 #endif
-    return 0;
+    
+    return retval;
 }
 #else   /* MSDOS or Macintosh */