]> git.sur5r.net Git - openldap/blob - servers/slapd/lock.c
9325389d121d9be56b8a6d9e520bb7663b9472da
[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         ldap_lockf( fileno(*lfp) );
34
35         /* open the log file */
36         if ( (fp = fopen( fname, type )) == NULL ) {
37                 Debug( LDAP_DEBUG_ANY, "could not open \"%s\"\n", fname, 0, 0 );
38                 ldap_unlockf( fileno(*lfp) );
39                 fclose( *lfp );
40                 *lfp = NULL;
41                 return( NULL );
42         }
43
44         return( fp );
45 }
46
47 int
48 lock_fclose( FILE *fp, FILE *lfp )
49 {
50         /* unlock */
51         ldap_unlockf( fileno(lfp) );
52         fclose( lfp );
53
54         return( fclose( fp ) );
55 }