From 1c8080fe9dcb3713bf1231cf4b455e80c6f0c697 Mon Sep 17 00:00:00 2001 From: Derrick Brashear Date: Sat, 16 Sep 2006 00:23:18 +0000 Subject: [PATCH] namei-emulate-flock-20060913 FIXES 39797 lockf when not locking and unlocking the whole file is fraught with peril --- acinclude.m4 | 2 +- src/vol/namei_ops.c | 63 +++++++++++++++++++++++---------------------- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/acinclude.m4 b/acinclude.m4 index a5a6da9c0..4663ceb38 100644 --- a/acinclude.m4 +++ b/acinclude.m4 @@ -1074,7 +1074,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 diff --git a/src/vol/namei_ops.c b/src/vol/namei_ops.c index 0f3d5faa3..22dfade1a 100644 --- a/src/vol/namei_ops.c +++ b/src/vol/namei_ops.c @@ -27,9 +27,6 @@ RCSID #include #include #include -#ifdef AFS_AIX_ENV -#include -#endif #if defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV) #include #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 + +/* + * 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; } -- 2.39.5