mirror of
https://git.openldap.org/openldap/openldap.git
synced 2024-12-09 02:52:04 +08:00
Update lutil_lockf (aka: ldap_lockf) to hide implementation in
library, not header. Eliminate need for <ac/unistd.h> to sometimes include <fcntl.h> and/or <sys/file.h>. 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.
This commit is contained in:
parent
a45bbea505
commit
7adc0a65d0
@ -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 <fcntl.h>
|
||||
# endif
|
||||
|
||||
# ifdef F_WRLCK
|
||||
# ifndef NEED_FCNTL_LOCKING
|
||||
# define NEED_FCNTL_LOCKING
|
||||
# endif
|
||||
# include <lutil_lockf.h>
|
||||
# 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 <sys/file.h>
|
||||
# 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 <lutil_lockf.h>
|
||||
# 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 <lutil_lockf.h>
|
||||
|
||||
#endif /* _AC_UNISTD_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
|
||||
|
||||
|
@ -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 <stdio.h>
|
||||
#include <ac/unistd.h>
|
||||
|
||||
#ifdef NEED_SIMPLE_LOCKING
|
||||
#undef LOCK_API
|
||||
|
||||
int lutil_lockf ( FILE *fp ) {
|
||||
#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 <fcntl.h>
|
||||
# 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 <sys/file.h>
|
||||
# endif
|
||||
# define USE_FLOCK 1
|
||||
# define LOCK_API "flock"
|
||||
#endif
|
||||
|
||||
#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
|
||||
|
||||
#ifdef USE_FLOCK
|
||||
int lutil_lockf ( int fd ) {
|
||||
return flock( fd, LOCK_EX );
|
||||
}
|
||||
|
||||
#endif /* NEED_SIMPLE_LOCKING */
|
||||
int lutil_unlockf ( int fd ) {
|
||||
return flock( fd, LOCK_UN );
|
||||
}
|
||||
#endif
|
||||
|
@ -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 ) );
|
||||
|
@ -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 ) );
|
||||
|
Loading…
Reference in New Issue
Block a user