]> git.sur5r.net Git - openldap/blob - servers/slurpd/lock.c
ITS#3534, 3546, 3571 revert abandon behavior
[openldap] / servers / slurpd / lock.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1996 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25 /* ACKNOWLEDGEMENTS:
26  * This work was originally developed by the University of Michigan
27  * (as part of U-MICH LDAP).
28  */
29
30 /*
31  * lock.c - routines to open and apply an advisory lock to a file
32  */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37
38 #include <ac/param.h>
39 #include <ac/string.h>
40 #include <ac/socket.h>
41 #include <ac/time.h>
42 #include <ac/unistd.h>
43
44 #ifdef HAVE_SYS_FILE_H
45 #include <sys/file.h>
46 #endif
47
48 #include "slurp.h"
49
50
51 FILE *
52 lock_fopen(
53     const char  *fname,
54     const char  *type,
55     FILE        **lfp
56 )
57 {
58         FILE    *fp;
59         char    buf[MAXPATHLEN];
60
61         /* open the lock file */
62         snprintf( buf, sizeof buf, "%s.lock", fname );
63
64         if ( (*lfp = fopen( buf, "w" )) == NULL ) {
65 #ifdef NEW_LOGGING
66                 LDAP_LOG ( SLURPD, ERR, "lock_fopen: "
67                         "Error: could not open \"%s\"\n", buf, 0, 0 );
68 #else
69                 Debug( LDAP_DEBUG_ANY,
70                         "Error: could not open \"%s\"\n", buf, 0, 0 );
71 #endif
72                 return( NULL );
73         }
74
75         /* acquire the lock */
76         ldap_lockf( fileno(*lfp) );
77
78         /* open the log file */
79         if ( (fp = fopen( fname, type )) == NULL ) {
80 #ifdef NEW_LOGGING
81                 LDAP_LOG ( SLURPD, ERR, "lock_fopen: "
82                         "Error: could not open \"%s\"\n", fname, 0, 0 );
83 #else
84                 Debug( LDAP_DEBUG_ANY,
85                         "Error: could not open \"%s\"\n", fname, 0, 0 );
86 #endif
87                 ldap_unlockf( fileno(*lfp) );
88                 fclose( *lfp );
89                 *lfp = NULL;
90                 return( NULL );
91         }
92
93         return( fp );
94 }
95
96
97
98 int
99 lock_fclose(
100     FILE        *fp,
101     FILE        *lfp
102 )
103 {
104         int rc = fclose( fp );
105
106         /* unlock */
107         ldap_unlockf( fileno(lfp) );
108         fclose( lfp );
109
110         return( rc );
111 }
112
113
114
115 /*
116  * Apply an advisory lock on a file.  Just calls lock_fopen()
117  */
118 int
119 acquire_lock(
120     const char  *file,
121     FILE        **rfp,
122     FILE        **lfp
123 )
124 {
125     if (( *rfp = lock_fopen( file, "r+", lfp )) == NULL ) {
126 #ifdef NEW_LOGGING
127         LDAP_LOG ( SLURPD, ERR, "acquire_lock: "
128                 "Error: acquire_lock(%ld): Could not acquire lock on \"%s\"\n",
129                 (long) getpid(), file, 0 );
130 #else
131         Debug( LDAP_DEBUG_ANY,
132                 "Error: acquire_lock(%ld): Could not acquire lock on \"%s\"\n",
133                 (long) getpid(), file, 0);
134 #endif
135         return( -1 );
136     }
137     return( 0 );
138 }
139
140
141
142 /*
143  * Relinquish a lock on a file.  Calls lock_fclose() and also removes the
144  * lock file.
145  */
146 int
147 relinquish_lock(
148     const char  *file,
149     FILE        *rfp,
150     FILE        *lfp
151 )
152 {
153     if ( lock_fclose( rfp, lfp ) == EOF ) {
154 #ifdef NEW_LOGGING
155         LDAP_LOG ( SLURPD, ERR, "relinguish_lock: "
156                 "Error: relinquish_lock (%ld): Error closing \"%s\"\n",
157                 (long) getpid(), file, 0 );
158 #else
159         Debug( LDAP_DEBUG_ANY,
160                 "Error: relinquish_lock (%ld): Error closing \"%s\"\n",
161                 (long) getpid(), file, 0 );
162 #endif
163         return( -1 );
164     }
165     return( 0 );
166 }