]> git.sur5r.net Git - openldap/blob - servers/slapd/lock.c
filter_candidate tweaks, search_stack tweaks
[openldap] / servers / slapd / lock.c
1 /* lock.c - routines to open and apply an advisory lock to a file */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/socket.h>
14 #include <ac/time.h>
15 #include <ac/unistd.h>
16
17 #ifdef HAVE_SYS_FILE_H
18 #include <sys/file.h>
19 #endif
20
21 #include "slap.h"
22 #include <lutil.h>
23
24 FILE *
25 lock_fopen( const char *fname, const char *type, FILE **lfp )
26 {
27         FILE    *fp;
28         char    buf[MAXPATHLEN];
29
30         /* open the lock file */
31         snprintf( buf, sizeof buf, "%s.lock", fname );
32
33         if ( (*lfp = fopen( buf, "w" )) == NULL ) {
34 #ifdef NEW_LOGGING
35                 LDAP_LOG( OPERATION, ERR, 
36                         "lock_fopen: could not open lock file \"%s\".\n", buf, 0, 0);
37 #else
38                 Debug( LDAP_DEBUG_ANY, "could not open \"%s\"\n", buf, 0, 0 );
39 #endif
40
41                 return( NULL );
42         }
43
44         /* acquire the lock */
45         ldap_lockf( fileno(*lfp) );
46
47         /* open the log file */
48         if ( (fp = fopen( fname, type )) == NULL ) {
49 #ifdef NEW_LOGGING
50                 LDAP_LOG( OPERATION, ERR, 
51                         "lock_fopen: could not open log file \"%s\".\n", buf, 0, 0);
52 #else
53                 Debug( LDAP_DEBUG_ANY, "could not open \"%s\"\n", fname, 0, 0 );
54 #endif
55
56                 ldap_unlockf( fileno(*lfp) );
57                 fclose( *lfp );
58                 *lfp = NULL;
59                 return( NULL );
60         }
61
62         return( fp );
63 }
64
65 int
66 lock_fclose( FILE *fp, FILE *lfp )
67 {
68         /* unlock */
69         ldap_unlockf( fileno(lfp) );
70         fclose( lfp );
71
72         return( fclose( fp ) );
73 }