with POSIX locks, we gotta have the file open for write if we want to flock
authorJohn Kohl <jtkohl@mit.edu>
Fri, 14 Jun 1991 15:21:49 +0000 (15:21 +0000)
committerJohn Kohl <jtkohl@mit.edu>
Fri, 14 Jun 1991 15:21:49 +0000 (15:21 +0000)
git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@2180 dc483132-0cff-0310-8789-dd5450dbe970

src/lib/kdb/kdb_dbm.c

index 8991d0a39814de6e9ec0bfb2bd4547680aba7b51..640b6016dae283d03336d9ccb93c043ee4765424 100644 (file)
@@ -179,9 +179,21 @@ krb5_dbm_db_init()
        char *filename = gen_dbsuffix (current_db_name, ".ok");
        if (!filename)
            return ENOMEM;
-       if ((dblfd = open(filename, 0, 0)) < 0) {
+#ifdef POSIX_FILE_LOCKS
+       /* needs be open read/write so that write locking can work with
+          POSIX systems */
+       if ((dblfd = open(filename, O_RDWR, 0)) == -1) {
+           if (errno == EACCES) {
+               if ((dblfd = open(filename, O_RDONLY, 0)) == -1)
+                   return errno;
+           } else
+               return errno;
+       }
+#else
+       if ((dblfd = open(filename, 0, 0)) == -1) {
            return errno;
        }
+#endif
        free(filename);
        inited++;
     }
@@ -601,8 +613,14 @@ int mode;
     fl.l_whence = 0;
     fl.l_start = 0;
     fl.l_len = 0;
-    if (fcntl(dblfd, non_blocking ? F_SETLK : F_SETLKW, &fl) == -1)
+    if (fcntl(dblfd, non_blocking ? F_SETLK : F_SETLKW, &fl) == -1) {
+       if (errno == EBADF && mode == KRB5_DBM_EXCLUSIVE) {
+           /* tried to exclusive-lock something we don't have write access
+              to. */
+           return KRB5_KDB_CANTLOCK_DB;
+       }
        return errno;
+    }
 #else
     switch (mode) {
     case KRB5_DBM_EXCLUSIVE: