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