]> git.sur5r.net Git - openldap/blob - servers/slurpd/lock.c
LDAPworld P4: SLAPD Crash when Schema Checking
[openldap] / servers / slurpd / lock.c
1 /*
2  * Copyright (c) 1996 Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12
13 /*
14  * lock.c - routines to open and apply an advisory lock to a file
15  */
16
17 #include <stdio.h>
18 #include <sys/time.h>
19 #include <sys/types.h>
20 #include <sys/file.h>
21 #include <sys/param.h>
22 #include <sys/socket.h>
23 #include "portable.h"
24 #ifdef USE_LOCKF
25 #include <unistd.h>
26 #endif
27 #include "../slapd/slap.h"
28
29
30
31 FILE *
32 lock_fopen(
33     char        *fname,
34     char        *type,
35     FILE        **lfp
36 )
37 {
38         FILE    *fp;
39         char    buf[MAXPATHLEN];
40
41         /* open the lock file */
42         strcpy( buf, fname );
43         strcat( buf, ".lock" );
44         if ( (*lfp = fopen( buf, "w" )) == NULL ) {
45                 Debug( LDAP_DEBUG_ANY,
46                         "Error: could not open \"%s\"\n", buf, 0, 0 );
47                 return( NULL );
48         }
49
50         /* acquire the lock */
51 #ifdef USE_LOCKF
52         while ( lockf( fileno( *lfp ), F_LOCK, 0 ) != 0 ) {
53 #else
54         while ( flock( fileno( *lfp ), LOCK_EX ) != 0 ) {
55 #endif
56                 ;       /* NULL */
57         }
58
59         /* open the log file */
60         if ( (fp = fopen( fname, type )) == NULL ) {
61                 Debug( LDAP_DEBUG_ANY,
62                         "Error: could not open \"%s\"\n", fname, 0, 0 );
63 #ifdef USE_LOCKF
64                 lockf( fileno( *lfp ), F_ULOCK, 0 );
65 #else
66                 flock( fileno( *lfp ), LOCK_UN );
67 #endif
68                 return( NULL );
69         }
70
71         return( fp );
72 }
73
74
75
76 int
77 lock_fclose(
78     FILE        *fp,
79     FILE        *lfp
80 )
81 {
82         /* unlock */
83 #ifdef USE_LOCKF
84         lockf( fileno( lfp ), F_ULOCK, 0 );
85 #else
86         flock( fileno( lfp ), LOCK_UN );
87 #endif
88         fclose( lfp );
89
90         return( fclose( fp ) );
91 }
92
93
94
95 /*
96  * Apply an advisory lock on a file.  Just calls lock_fopen()
97  */
98 int
99 acquire_lock(
100     char        *file,
101     FILE        **rfp,
102     FILE        **lfp
103 )
104 {
105     if (( *rfp = lock_fopen( file, "r+", lfp )) == NULL ) {
106         Debug( LDAP_DEBUG_ANY,
107                 "Error: acquire_lock(%d): Could not acquire lock on \"%s\"\n",
108                 getpid(), file, 0);
109         return( -1 );
110     }
111     return( 0 );
112 }
113
114
115
116 /*
117  * Relinquish a lock on a file.  Calls lock_fclose() and also removes the
118  * lock file.
119  */
120 int
121 relinquish_lock(
122     char        *file,
123     FILE        *rfp,
124     FILE        *lfp
125 )
126 {
127     if ( lock_fclose( rfp, lfp ) == EOF ) {
128         Debug( LDAP_DEBUG_ANY,
129                 "Error: relinquish_lock (%d): Error closing \"%s\"\n",
130                 getpid(), file, 0 );
131         return( -1 );
132     }
133     return( 0 );
134 }