]> git.sur5r.net Git - openldap/blob - servers/slapd/lock.c
const'fication
[openldap] / servers / slapd / lock.c
1 /* lock.c - routines to open and apply an advisory lock to a file */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/string.h>
12 #include <ac/socket.h>
13 #include <ac/time.h>
14 #include <ac/unistd.h>
15
16 #ifdef HAVE_SYS_FILE_H
17 #include <sys/file.h>
18 #endif
19 #ifdef HAVE_SYS_PARAM_H
20 #include <sys/param.h>
21 #endif
22 #include "slap.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         strcpy( buf, fname );
32         strcat( buf, ".lock" );
33         if ( (*lfp = fopen( buf, "w" )) == NULL ) {
34                 Debug( LDAP_DEBUG_ANY, "could not open \"%s\"\n", buf, 0, 0 );
35                 return( NULL );
36         }
37
38         /* acquire the lock */
39         ldap_lockf( fileno(*lfp) );
40
41         /* open the log file */
42         if ( (fp = fopen( fname, type )) == NULL ) {
43                 Debug( LDAP_DEBUG_ANY, "could not open \"%s\"\n", fname, 0, 0 );
44                 ldap_unlockf( fileno(*lfp) );
45                 fclose( *lfp );
46                 *lfp = NULL;
47                 return( NULL );
48         }
49
50         return( fp );
51 }
52
53 int
54 lock_fclose( FILE *fp, FILE *lfp )
55 {
56         /* unlock */
57         ldap_unlockf( fileno(lfp) );
58         fclose( lfp );
59
60         return( fclose( fp ) );
61 }