]> git.sur5r.net Git - openldap/blob - servers/slurpd/lock.c
Sync with HEAD
[openldap] / servers / slurpd / lock.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2003 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         /* unlock */
105         ldap_unlockf( fileno(lfp) );
106         fclose( lfp );
107
108         return( fclose( fp ) );
109 }
110
111
112
113 /*
114  * Apply an advisory lock on a file.  Just calls lock_fopen()
115  */
116 int
117 acquire_lock(
118     const char  *file,
119     FILE        **rfp,
120     FILE        **lfp
121 )
122 {
123     if (( *rfp = lock_fopen( file, "r+", lfp )) == NULL ) {
124 #ifdef NEW_LOGGING
125         LDAP_LOG ( SLURPD, ERR, "acquire_lock: "
126                 "Error: acquire_lock(%ld): Could not acquire lock on \"%s\"\n",
127                 (long) getpid(), file, 0 );
128 #else
129         Debug( LDAP_DEBUG_ANY,
130                 "Error: acquire_lock(%ld): Could not acquire lock on \"%s\"\n",
131                 (long) getpid(), file, 0);
132 #endif
133         return( -1 );
134     }
135     return( 0 );
136 }
137
138
139
140 /*
141  * Relinquish a lock on a file.  Calls lock_fclose() and also removes the
142  * lock file.
143  */
144 int
145 relinquish_lock(
146     const char  *file,
147     FILE        *rfp,
148     FILE        *lfp
149 )
150 {
151     if ( lock_fclose( rfp, lfp ) == EOF ) {
152 #ifdef NEW_LOGGING
153         LDAP_LOG ( SLURPD, ERR, "relinguish_lock: "
154                 "Error: relinquish_lock (%ld): Error closing \"%s\"\n",
155                 (long) getpid(), file, 0 );
156 #else
157         Debug( LDAP_DEBUG_ANY,
158                 "Error: relinquish_lock (%ld): Error closing \"%s\"\n",
159                 (long) getpid(), file, 0 );
160 #endif
161         return( -1 );
162     }
163     return( 0 );
164 }