]> git.sur5r.net Git - openldap/blob - servers/slurpd/lock.c
7b459abc31b517d8dc8b356b0f5ab1a27f0b3a8f
[openldap] / servers / slurpd / lock.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2007 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                 Debug( LDAP_DEBUG_ANY,
66                         "Error: could not open \"%s\"\n", buf, 0, 0 );
67                 return( NULL );
68         }
69
70         /* acquire the lock */
71         ldap_lockf( fileno(*lfp) );
72
73         /* open the log file */
74         if ( (fp = fopen( fname, type )) == NULL ) {
75                 Debug( LDAP_DEBUG_ANY,
76                         "Error: could not open \"%s\"\n", fname, 0, 0 );
77                 ldap_unlockf( fileno(*lfp) );
78                 fclose( *lfp );
79                 *lfp = NULL;
80                 return( NULL );
81         }
82
83         return( fp );
84 }
85
86
87
88 int
89 lock_fclose(
90     FILE        *fp,
91     FILE        *lfp
92 )
93 {
94         int rc = fclose( fp );
95
96         /* unlock */
97         ldap_unlockf( fileno(lfp) );
98         fclose( lfp );
99
100         return( rc );
101 }
102
103
104
105 /*
106  * Apply an advisory lock on a file.  Just calls lock_fopen()
107  */
108 int
109 acquire_lock(
110     const char  *file,
111     FILE        **rfp,
112     FILE        **lfp
113 )
114 {
115     if (( *rfp = lock_fopen( file, "r+", lfp )) == NULL ) {
116         Debug( LDAP_DEBUG_ANY,
117                 "Error: acquire_lock(%ld): Could not acquire lock on \"%s\"\n",
118                 (long) getpid(), file, 0);
119         return( -1 );
120     }
121     return( 0 );
122 }
123
124
125
126 /*
127  * Relinquish a lock on a file.  Calls lock_fclose() and also removes the
128  * lock file.
129  */
130 int
131 relinquish_lock(
132     const char  *file,
133     FILE        *rfp,
134     FILE        *lfp
135 )
136 {
137     if ( lock_fclose( rfp, lfp ) == EOF ) {
138         Debug( LDAP_DEBUG_ANY,
139                 "Error: relinquish_lock (%ld): Error closing \"%s\"\n",
140                 (long) getpid(), file, 0 );
141         return( -1 );
142     }
143     return( 0 );
144 }