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