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