]> git.sur5r.net Git - openldap/commitdiff
Update lutil_lockf (aka: ldap_lockf) to hide implementation in
authorKurt Zeilenga <kurt@openldap.org>
Sun, 28 Mar 1999 21:39:02 +0000 (21:39 +0000)
committerKurt Zeilenga <kurt@openldap.org>
Sun, 28 Mar 1999 21:39:02 +0000 (21:39 +0000)
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.

include/ac/unistd.h
include/lutil_lockf.h
libraries/liblutil/lockf.c
servers/slapd/lock.c
servers/slurpd/lock.c

index e5b3eb3300b2ebc2a3d26e5c619ee3529de1bc1a..61ad9172b18043a2ee8a7025286901de09b4a5e2 100644 (file)
@@ -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 */
index 166cbe1a409bd53dc607db44dad5aad590c8ecc6..49d2c01e050a189687fc5106ec1056a5249d79dc 100644 (file)
@@ -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
 
index 13226bbf590471e6ef9fea5dd1ca97b75e2d8c29..c21d2a8dfc1577b49fbf7d03980d3ccc7d27001c 100644 (file)
@@ -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
+
+#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
index 391dd4639a6595036a18becc37b98294754aeb9e..b23aa9028c32ff68c00196ced538938af70271e1 100644 (file)
@@ -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 ) );
index e4d7ddaffb056beffed599fd19a40624382221eb..cca1bed3f8ad9aa04e66be56b8fcd194eee46fa5 100644 (file)
@@ -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 ) );