]> git.sur5r.net Git - openldap/blob - servers/slapd/lock.c
Merge in all devel changes since 2.0-alpha2.
[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-1999 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 #ifdef HAVE_SYS_PARAM_H
21 #include <sys/param.h>
22 #endif
23 #include "slap.h"
24
25 FILE *
26 lock_fopen( const char *fname, const char *type, FILE **lfp )
27 {
28         FILE    *fp;
29         char    buf[MAXPATHLEN];
30
31         /* open the lock file */
32         strcpy( buf, fname );
33         strcat( buf, ".lock" );
34         if ( (*lfp = fopen( buf, "w" )) == NULL ) {
35                 Debug( LDAP_DEBUG_ANY, "could not open \"%s\"\n", buf, 0, 0 );
36                 return( NULL );
37         }
38
39         /* acquire the lock */
40         ldap_lockf( fileno(*lfp) );
41
42         /* open the log file */
43         if ( (fp = fopen( fname, type )) == NULL ) {
44                 Debug( LDAP_DEBUG_ANY, "could not open \"%s\"\n", fname, 0, 0 );
45                 ldap_unlockf( fileno(*lfp) );
46                 fclose( *lfp );
47                 *lfp = NULL;
48                 return( NULL );
49         }
50
51         return( fp );
52 }
53
54 int
55 lock_fclose( FILE *fp, FILE *lfp )
56 {
57         /* unlock */
58         ldap_unlockf( fileno(lfp) );
59         fclose( lfp );
60
61         return( fclose( fp ) );
62 }