]> git.sur5r.net Git - openldap/blob - servers/slapd/lock.c
Fix IRIX sc_mask conflict
[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-2002 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
23 FILE *
24 lock_fopen( const char *fname, const char *type, FILE **lfp )
25 {
26         FILE    *fp;
27         char    buf[MAXPATHLEN];
28
29         /* open the lock file */
30         strcpy(lutil_strcopy( buf, fname ), ".lock" );
31         if ( (*lfp = fopen( buf, "w" )) == NULL ) {
32 #ifdef NEW_LOGGING
33                 LDAP_LOG( OPERATION, ERR, 
34                         "lock_fopen: could not open lock file \"%s\".\n", buf, 0, 0);
35 #else
36                 Debug( LDAP_DEBUG_ANY, "could not open \"%s\"\n", buf, 0, 0 );
37 #endif
38
39                 return( NULL );
40         }
41
42         /* acquire the lock */
43         ldap_lockf( fileno(*lfp) );
44
45         /* open the log file */
46         if ( (fp = fopen( fname, type )) == NULL ) {
47 #ifdef NEW_LOGGING
48                 LDAP_LOG( OPERATION, ERR, 
49                         "lock_fopen: could not open log file \"%s\".\n", buf, 0, 0);
50 #else
51                 Debug( LDAP_DEBUG_ANY, "could not open \"%s\"\n", fname, 0, 0 );
52 #endif
53
54                 ldap_unlockf( fileno(*lfp) );
55                 fclose( *lfp );
56                 *lfp = NULL;
57                 return( NULL );
58         }
59
60         return( fp );
61 }
62
63 int
64 lock_fclose( FILE *fp, FILE *lfp )
65 {
66         /* unlock */
67         ldap_unlockf( fileno(lfp) );
68         fclose( lfp );
69
70         return( fclose( fp ) );
71 }