DEVEL15-namei-emulate-flock-20060913
authorDerrick Brashear <shadow@dementia.org>
Sat, 16 Sep 2006 01:08:59 +0000 (01:08 +0000)
committerDerrick Brashear <shadow@dementia.org>
Sat, 16 Sep 2006 01:08:59 +0000 (01:08 +0000)
FIXES 39797

lockf when not locking and unlocking the whole file is fraught with peril

(cherry picked from commit 1c8080fe9dcb3713bf1231cf4b455e80c6f0c697)

acinclude.m4
src/vol/namei_ops.c

index 4f79c56..8a88f9c 100644 (file)
@@ -1083,7 +1083,7 @@ else
 fi
 AC_SUBST(BUILD_LOGIN)
 
-AC_CHECK_FUNCS(utimes random srandom getdtablesize snprintf strlcat strlcpy re_comp re_exec)
+AC_CHECK_FUNCS(utimes random srandom getdtablesize snprintf strlcat strlcpy re_comp re_exec flock)
 AC_CHECK_FUNCS(setprogname getprogname sigaction mkstemp vsnprintf strerror strcasestr)
 AC_CHECK_FUNCS(setvbuf)
 AC_FUNC_SETVBUF_REVERSED
index 0f3d5fa..22dfade 100644 (file)
@@ -27,9 +27,6 @@ RCSID
 #include <sys/file.h>
 #include <sys/param.h>
 #include <lock.h>
-#ifdef AFS_AIX_ENV
-#include <sys/lockf.h>
-#endif
 #if defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
 #include <unistd.h>
 #endif
@@ -69,6 +66,38 @@ extern off_t afs_lseek(int FD, off_t O, int F);
 
 /*@printflike@*/ extern void Log(const char *format, ...);
 
+#ifndef LOCK_SH
+#define   LOCK_SH   1    /* shared lock */
+#define   LOCK_EX   2    /* exclusive lock */
+#define   LOCK_NB   4    /* don't block when locking */
+#define   LOCK_UN   8    /* unlock */
+#endif
+
+#ifndef HAVE_FLOCK
+#include <fcntl.h>
+
+/*
+ * This function emulates a subset of flock()
+ */
+int 
+emul_flock(int fd, int cmd)
+{    struct flock f;
+
+    memset(&f, 0, sizeof (f));
+
+    if (cmd & LOCK_UN)
+        f.l_type = F_UNLCK;
+    if (cmd & LOCK_SH)
+        f.l_type = F_RDLCK;
+    if (cmd & LOCK_EX)
+        f.l_type = F_WRLCK;
+
+    return fcntl(fd, (cmd & LOCK_NB) ? F_SETLK : F_SETLKW, &f);
+}
+
+#define flock(f,c)      emul_flock(f,c)
+#endif
+
 extern char *volutil_PartitionName_r(int volid, char *buf, int buflen);
 int Testing=0;
 
@@ -878,11 +907,7 @@ namei_GetLinkCount2(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrit
     namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
 
     if (lockit) {
-#if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
-       if (lockf(h->fd_fd, F_LOCK, 0) < 0)
-#else
        if (flock(h->fd_fd, LOCK_EX) < 0)
-#endif
            return -1;
     }
 
@@ -917,11 +942,7 @@ namei_GetLinkCount2(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrit
 
   bad_getLinkByte:
     if (lockit)
-#if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
-       lockf(h->fd_fd, F_ULOCK, 0);
-#else
        flock(h->fd_fd, LOCK_UN);
-#endif
     return -1;
 }
 
@@ -948,11 +969,7 @@ GetFreeTag(IHandle_t * ih, int vno)
        return -1;
 
     /* Only one manipulates at a time. */
-#if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
-    if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
-#else
     if (flock(fdP->fd_fd, LOCK_EX) < 0) {
-#endif
        FDH_REALLYCLOSE(fdP);
        return -1;
     }
@@ -988,20 +1005,12 @@ GetFreeTag(IHandle_t * ih, int vno)
        goto badGetFreeTag;
     }
     FDH_SYNC(fdP);
-#if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
-    lockf(fdP->fd_fd, F_ULOCK, 0);
-#else
     flock(fdP->fd_fd, LOCK_UN);
-#endif
     FDH_REALLYCLOSE(fdP);
     return col;;
 
   badGetFreeTag:
-#if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
-    lockf(fdP->fd_fd, F_ULOCK, 0);
-#else
     flock(fdP->fd_fd, LOCK_UN);
-#endif
     FDH_REALLYCLOSE(fdP);
     return -1;
 }
@@ -1024,11 +1033,7 @@ namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
     namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
 
     if (!locked) {
-#if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
-       if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
-#else
        if (flock(fdP->fd_fd, LOCK_EX) < 0) {
-#endif
            return -1;
        }
     }
@@ -1067,11 +1072,7 @@ namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
 
 
   bad_SetLinkCount:
-#if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
-    lockf(fdP->fd_fd, F_ULOCK, 0);
-#else
     flock(fdP->fd_fd, LOCK_UN);
-#endif
 
     return code;
 }