]> git.sur5r.net Git - openldap/blob - servers/slurpd/lock.c
replace integer argument NULL with 0
[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 #ifdef HAVE_FLOCK
57         while ( flock( fileno( *lfp ), LOCK_EX ) != 0 ) 
58 #else
59         while ( lockf( fileno( *lfp ), F_LOCK, 0 ) != 0 )
60 #endif
61         {
62                 ;       /* NULL */
63         }
64
65         /* open the log file */
66         if ( (fp = fopen( fname, type )) == NULL ) {
67                 Debug( LDAP_DEBUG_ANY,
68                         "Error: could not open \"%s\"\n", fname, 0, 0 );
69 #ifdef HAVE_FLOCK
70                 flock( fileno( *lfp ), LOCK_UN );
71 #else
72                 lockf( fileno( *lfp ), F_ULOCK, 0 );
73 #endif
74                 fclose( *lfp );
75                 *lfp = NULL;
76                 return( NULL );
77         }
78
79         return( fp );
80 }
81
82
83
84 int
85 lock_fclose(
86     FILE        *fp,
87     FILE        *lfp
88 )
89 {
90         /* unlock */
91 #ifdef HAVE_FLOCK
92         flock( fileno( lfp ), LOCK_UN );
93 #else
94         lockf( fileno( lfp ), F_ULOCK, 0 );
95 #endif
96         fclose( lfp );
97
98         return( fclose( fp ) );
99 }
100
101
102
103 /*
104  * Apply an advisory lock on a file.  Just calls lock_fopen()
105  */
106 int
107 acquire_lock(
108     char        *file,
109     FILE        **rfp,
110     FILE        **lfp
111 )
112 {
113     if (( *rfp = lock_fopen( file, "r+", lfp )) == NULL ) {
114         Debug( LDAP_DEBUG_ANY,
115                 "Error: acquire_lock(%ld): Could not acquire lock on \"%s\"\n",
116                 (long) getpid(), file, 0);
117         return( -1 );
118     }
119     return( 0 );
120 }
121
122
123
124 /*
125  * Relinquish a lock on a file.  Calls lock_fclose() and also removes the
126  * lock file.
127  */
128 int
129 relinquish_lock(
130     char        *file,
131     FILE        *rfp,
132     FILE        *lfp
133 )
134 {
135     if ( lock_fclose( rfp, lfp ) == EOF ) {
136         Debug( LDAP_DEBUG_ANY,
137                 "Error: relinquish_lock (%ld): Error closing \"%s\"\n",
138                 (long) getpid(), file, 0 );
139         return( -1 );
140     }
141     return( 0 );
142 }