]> git.sur5r.net Git - openldap/blob - servers/slurpd/lock.c
17bc995efaa650bebcfd43f62f4820a9726c579a
[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
26 #include <sys/file.h>
27 #include <sys/param.h>
28
29 #include "../slapd/slap.h"
30
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                 return( NULL );
72         }
73
74         return( fp );
75 }
76
77
78
79 int
80 lock_fclose(
81     FILE        *fp,
82     FILE        *lfp
83 )
84 {
85         /* unlock */
86 #ifdef HAVE_FLOCK
87         flock( fileno( lfp ), LOCK_UN );
88 #else
89         lockf( fileno( lfp ), F_ULOCK, 0 );
90 #endif
91         fclose( lfp );
92
93         return( fclose( fp ) );
94 }
95
96
97
98 /*
99  * Apply an advisory lock on a file.  Just calls lock_fopen()
100  */
101 int
102 acquire_lock(
103     char        *file,
104     FILE        **rfp,
105     FILE        **lfp
106 )
107 {
108     if (( *rfp = lock_fopen( file, "r+", lfp )) == NULL ) {
109         Debug( LDAP_DEBUG_ANY,
110                 "Error: acquire_lock(%d): Could not acquire lock on \"%s\"\n",
111                 getpid(), file, 0);
112         return( -1 );
113     }
114     return( 0 );
115 }
116
117
118
119 /*
120  * Relinquish a lock on a file.  Calls lock_fclose() and also removes the
121  * lock file.
122  */
123 int
124 relinquish_lock(
125     char        *file,
126     FILE        *rfp,
127     FILE        *lfp
128 )
129 {
130     if ( lock_fclose( rfp, lfp ) == EOF ) {
131         Debug( LDAP_DEBUG_ANY,
132                 "Error: relinquish_lock (%d): Error closing \"%s\"\n",
133                 getpid(), file, 0 );
134         return( -1 );
135     }
136     return( 0 );
137 }