From 7adc0a65d0a3c10daca4352002baadeb76afc7e5 Mon Sep 17 00:00:00 2001 From: Kurt Zeilenga Date: Sun, 28 Mar 1999 21:39:02 +0000 Subject: [PATCH] Update lutil_lockf (aka: ldap_lockf) to hide implementation in library, not header. Eliminate need for to sometimes include and/or . Change lock API to expect fd not FILE*. Allows wider use and eliminates requirement that lutil_lockf.h depencency on stdio.h. Implemented lockf, fcntl, and flock locking in lutil/lockf.c. Additional implementations (including no-op) may be needed. Update slapd/lock.c and slurpd/lock.c to use new API. --- include/ac/unistd.h | 42 +++--------------------- include/lutil_lockf.h | 4 +-- libraries/liblutil/lockf.c | 67 ++++++++++++++++++++++++++++++++++---- servers/slapd/lock.c | 6 ++-- servers/slurpd/lock.c | 6 ++-- 5 files changed, 72 insertions(+), 53 deletions(-) diff --git a/include/ac/unistd.h b/include/ac/unistd.h index e5b3eb3300..61ad9172b1 100644 --- a/include/ac/unistd.h +++ b/include/ac/unistd.h @@ -52,43 +52,9 @@ extern char* getpass LDAP_P((const char *getpass)); extern char *mktemp(char *); #endif -/* Setup file locking macros */ -#if !defined( ldap_lockf ) && HAVE_LOCKF && defined( F_LOCK ) -# define ldap_lockf(x) lockf(fileno(x), F_LOCK, 0) -# define ldap_unlockf(x) lockf(fileno(x), F_ULOCK, 0) -#endif - -#if !defined( ldap_lockf ) && HAVE_FCNTL -# ifdef HAVE_FCNTL_H -# include -# endif - -# ifdef F_WRLCK -# ifndef NEED_FCNTL_LOCKING -# define NEED_FCNTL_LOCKING -# endif -# include -# define ldap_lockf(x) lutil_lockf(x) -# define ldap_unlockf(x) lutil_unlockf(x) -# endif -#endif - -#if !defined( ldap_lockf ) && HAVE_FLOCK -# if HAVE_SYS_FILE_H -# include -# endif -# ifdef LOCK_EX -# define ldap_lockf(x) flock(fileno(x), LOCK_EX) -# define ldap_unlockf(x) flock(fileno(x), LOCK_UN) -# endif -#endif - -#if !defined( ldap_lockf ) - /* use some simplistic locking method */ -# define NEED_SIMPLE_LOCKING -# include -# define ldap_lockf(x) lutil_lockf(x) -# define ldap_unlockf(x) lutil_unlockf(x) -#endif +/* use lutil file locking */ +#define ldap_lockf(x) lutil_lockf(x) +#define ldap_unlockf(x) lutil_unlockf(x) +#include #endif /* _AC_UNISTD_H */ diff --git a/include/lutil_lockf.h b/include/lutil_lockf.h index 166cbe1a40..49d2c01e05 100644 --- a/include/lutil_lockf.h +++ b/include/lutil_lockf.h @@ -16,8 +16,8 @@ LDAP_BEGIN_DECL -LDAP_F int lutil_lockf LDAP_P(( FILE *fs )); -LDAP_F int lutil_unlockf LDAP_P(( FILE *fs )); +LDAP_F int lutil_lockf LDAP_P(( int fd )); +LDAP_F int lutil_unlockf LDAP_P(( int fd )); LDAP_END_DECL diff --git a/libraries/liblutil/lockf.c b/libraries/liblutil/lockf.c index 13226bbf59..c21d2a8dfc 100644 --- a/libraries/liblutil/lockf.c +++ b/libraries/liblutil/lockf.c @@ -7,33 +7,86 @@ * license is available at http://www.OpenLDAP.org/license.html or * in file LICENSE in the top-level directory of the distribution. */ -/* Simple file locking method for systems without */ + +/* + * File Locking Routines + * + * Implementations (in order of preference) + * - lockf + * - fcntl + * - flock + * + * Other implementations will be added as needed. + */ #include "portable.h" #include #include -#ifdef NEED_SIMPLE_LOCKING +#undef LOCK_API + +#if HAVE_LOCKF && defined(F_LOCK) +# define USE_LOCKF 1 +# define LOCK_API "lockf" +#endif + +#if !defined(LOCK_API) && HAVE_FCNTL +# ifdef HAVE_FCNTL_H +# include +# endif +# ifdef F_WRLCK +# define USE_FCNTL 1 +# define LOCK_API "fcntl" +# endif +#endif + +#if !defined(LOCK_API) && HAVE_FLOCK +# if HAVE_SYS_FILE_H +# include +# endif +# define USE_FLOCK 1 +# define LOCK_API "flock" +#endif -int lutil_lockf ( FILE *fp ) { +#ifdef USE_LOCKF +int lutil_lockf ( int fd ) { + return lockf( fd, F_LOCK, 0 ); +} + +int lutil_unlockf ( int fd ) { + return lockf( fd, F_ULOCK, 0 ); +} +#endif + +#ifdef USE_FCNTL +int lutil_lockf ( int fd ) { struct flock file_lock; memset( &file_lock, 0, sizeof( file_lock ) ); file_lock.l_type = F_WRLCK; file_lock.l_whence = SEEK_SET; file_lock.l_start = 0; file_lock.l_len = 0; - return( fcntl( fileno(fp), F_SETLKW, &file_lock ) ); + return( fcntl( fd, F_SETLKW, &file_lock ) ); } -int lutil_unlockf ( FILE *fp ) { +int lutil_unlockf ( int fd ) { struct flock file_lock; memset( &file_lock, 0, sizeof( file_lock ) ); file_lock.l_type = F_UNLCK; file_lock.l_whence = SEEK_SET; file_lock.l_start = 0; file_lock.l_len = 0; - return ( fcntl( fileno(fp), F_SETLK, &file_lock ) ); + return( fcntl ( fd, F_SETLK, &file_lock ) ); } +#endif -#endif /* NEED_SIMPLE_LOCKING */ +#ifdef USE_FLOCK +int lutil_lockf ( int fd ) { + return flock( fd, LOCK_EX ); +} + +int lutil_unlockf ( int fd ) { + return flock( fd, LOCK_UN ); +} +#endif diff --git a/servers/slapd/lock.c b/servers/slapd/lock.c index 391dd4639a..b23aa9028c 100644 --- a/servers/slapd/lock.c +++ b/servers/slapd/lock.c @@ -30,14 +30,14 @@ lock_fopen( char *fname, char *type, FILE **lfp ) } /* acquire the lock */ - while ( ldap_lockf( *lfp ) != 0 ) { + while ( ldap_lockf( fileno(*lfp) ) != 0 ) { ; /* NULL */ } /* open the log file */ if ( (fp = fopen( fname, type )) == NULL ) { Debug( LDAP_DEBUG_ANY, "could not open \"%s\"\n", fname, 0, 0 ); - ldap_unlockf( *lfp ); + ldap_unlockf( fileno(*lfp) ); fclose( *lfp ); *lfp = NULL; return( NULL ); @@ -50,7 +50,7 @@ int lock_fclose( FILE *fp, FILE *lfp ) { /* unlock */ - ldap_unlockf( lfp ); + ldap_unlockf( fileno(lfp) ); fclose( lfp ); return( fclose( fp ) ); diff --git a/servers/slurpd/lock.c b/servers/slurpd/lock.c index e4d7ddaffb..cca1bed3f8 100644 --- a/servers/slurpd/lock.c +++ b/servers/slurpd/lock.c @@ -53,7 +53,7 @@ lock_fopen( } /* acquire the lock */ - while ( ldap_lockf( *lfp ) != 0 ) + while ( ldap_lockf( fileno(*lfp) ) != 0 ) { ; /* NULL */ } @@ -62,7 +62,7 @@ lock_fopen( if ( (fp = fopen( fname, type )) == NULL ) { Debug( LDAP_DEBUG_ANY, "Error: could not open \"%s\"\n", fname, 0, 0 ); - ldap_unlockf( *lfp ); + ldap_unlockf( fileno(*lfp) ); fclose( *lfp ); *lfp = NULL; return( NULL ); @@ -80,7 +80,7 @@ lock_fclose( ) { /* unlock */ - ldap_unlockf( lfp ); + ldap_unlockf( fileno(lfp) ); fclose( lfp ); return( fclose( fp ) ); -- 2.39.5