]> git.sur5r.net Git - openldap/blob - servers/slurpd/lock.c
6fbd4bbf9cc22cdbe6d36e9437cfd9927ecfa567
[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 "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/string.h>
22 #include <ac/socket.h>
23 #include <ac/time.h>
24 #include <ac/unistd.h>
25 extern int flock ();
26
27 #include <sys/file.h>
28 #include <sys/param.h>
29
30 #include "slurp.h"              /* Was ../slapd/slap.h */
31
32
33 FILE *
34 lock_fopen(
35     char        *fname,
36     char        *type,
37     FILE        **lfp
38 )
39 {
40         FILE    *fp;
41         char    buf[MAXPATHLEN];
42
43         /* open the lock file */
44         strcpy( buf, fname );
45         strcat( buf, ".lock" );
46         if ( (*lfp = fopen( buf, "w" )) == NULL ) {
47                 Debug( LDAP_DEBUG_ANY,
48                         "Error: could not open \"%s\"\n", buf, 0, 0 );
49                 return( NULL );
50         }
51
52         /* acquire the lock */
53 #ifdef HAVE_FLOCK
54         while ( flock( fileno( *lfp ), LOCK_EX ) != 0 ) 
55 #else
56         while ( lockf( fileno( *lfp ), F_LOCK, 0 ) != 0 )
57 #endif
58         {
59                 ;       /* NULL */
60         }
61
62         /* open the log file */
63         if ( (fp = fopen( fname, type )) == NULL ) {
64                 Debug( LDAP_DEBUG_ANY,
65                         "Error: could not open \"%s\"\n", fname, 0, 0 );
66 #ifdef HAVE_FLOCK
67                 flock( fileno( *lfp ), LOCK_UN );
68 #else
69                 lockf( fileno( *lfp ), F_ULOCK, 0 );
70 #endif
71                 fclose( *lfp );
72                 *lfp = NULL;
73                 return( NULL );
74         }
75
76         return( fp );
77 }
78
79
80
81 int
82 lock_fclose(
83     FILE        *fp,
84     FILE        *lfp
85 )
86 {
87         /* unlock */
88 #ifdef HAVE_FLOCK
89         flock( fileno( lfp ), LOCK_UN );
90 #else
91         lockf( fileno( lfp ), F_ULOCK, 0 );
92 #endif
93         fclose( lfp );
94
95         return( fclose( fp ) );
96 }
97
98
99
100 /*
101  * Apply an advisory lock on a file.  Just calls lock_fopen()
102  */
103 int
104 acquire_lock(
105     char        *file,
106     FILE        **rfp,
107     FILE        **lfp
108 )
109 {
110     if (( *rfp = lock_fopen( file, "r+", lfp )) == NULL ) {
111         Debug( LDAP_DEBUG_ANY,
112                 "Error: acquire_lock(%ld): Could not acquire lock on \"%s\"\n",
113                 (long) getpid(), file, 0);
114         return( -1 );
115     }
116     return( 0 );
117 }
118
119
120
121 /*
122  * Relinquish a lock on a file.  Calls lock_fclose() and also removes the
123  * lock file.
124  */
125 int
126 relinquish_lock(
127     char        *file,
128     FILE        *rfp,
129     FILE        *lfp
130 )
131 {
132     if ( lock_fclose( rfp, lfp ) == EOF ) {
133         Debug( LDAP_DEBUG_ANY,
134                 "Error: relinquish_lock (%ld): Error closing \"%s\"\n",
135                 (long) getpid(), file, 0 );
136         return( -1 );
137     }
138     return( 0 );
139 }