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 */
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
* 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
+
+#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
-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
}
/* 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 );
lock_fclose( FILE *fp, FILE *lfp )
{
/* unlock */
- ldap_unlockf( lfp );
+ ldap_unlockf( fileno(lfp) );
fclose( lfp );
return( fclose( fp ) );
}
/* acquire the lock */
- while ( ldap_lockf( *lfp ) != 0 )
+ while ( ldap_lockf( fileno(*lfp) ) != 0 )
{
; /* NULL */
}
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 );
)
{
/* unlock */
- ldap_unlockf( lfp );
+ ldap_unlockf( fileno(lfp) );
fclose( lfp );
return( fclose( fp ) );