]> git.sur5r.net Git - openldap/blob - servers/slapd/lock.c
b23aa9028c32ff68c00196ced538938af70271e1
[openldap] / servers / slapd / lock.c
1 /* lock.c - routines to open and apply an advisory lock to a file */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/string.h>
8 #include <ac/socket.h>
9 #include <ac/time.h>
10 #include <ac/unistd.h>
11
12 #ifdef HAVE_SYS_FILE_H
13 #include <sys/file.h>
14 #endif
15 #include <sys/param.h>
16 #include "slap.h"
17
18 FILE *
19 lock_fopen( char *fname, char *type, FILE **lfp )
20 {
21         FILE    *fp;
22         char    buf[MAXPATHLEN];
23
24         /* open the lock file */
25         strcpy( buf, fname );
26         strcat( buf, ".lock" );
27         if ( (*lfp = fopen( buf, "w" )) == NULL ) {
28                 Debug( LDAP_DEBUG_ANY, "could not open \"%s\"\n", buf, 0, 0 );
29                 return( NULL );
30         }
31
32         /* acquire the lock */
33         while ( ldap_lockf( fileno(*lfp) ) != 0 ) {
34                 ;       /* NULL */
35         }
36
37         /* open the log file */
38         if ( (fp = fopen( fname, type )) == NULL ) {
39                 Debug( LDAP_DEBUG_ANY, "could not open \"%s\"\n", fname, 0, 0 );
40                 ldap_unlockf( fileno(*lfp) );
41                 fclose( *lfp );
42                 *lfp = NULL;
43                 return( NULL );
44         }
45
46         return( fp );
47 }
48
49 int
50 lock_fclose( FILE *fp, FILE *lfp )
51 {
52         /* unlock */
53         ldap_unlockf( fileno(lfp) );
54         fclose( lfp );
55
56         return( fclose( fp ) );
57 }