]> git.sur5r.net Git - openldap/blob - servers/slurpd/lock.c
Minor change to signal handler. Changed _shutdown flag to
[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 #include <sys/file.h>
27 #include <sys/param.h>
28
29 #include "slurp.h"
30
31
32 FILE *
33 lock_fopen(
34     char        *fname,
35     char        *type,
36     FILE        **lfp
37 )
38 {
39         FILE    *fp;
40         char    buf[MAXPATHLEN];
41
42         /* open the lock file */
43         strcpy( buf, fname );
44         strcat( buf, ".lock" );
45         if ( (*lfp = fopen( buf, "w" )) == NULL ) {
46                 Debug( LDAP_DEBUG_ANY,
47                         "Error: could not open \"%s\"\n", buf, 0, 0 );
48                 return( NULL );
49         }
50
51         /* acquire the lock */
52 #ifdef HAVE_FLOCK
53         while ( flock( fileno( *lfp ), LOCK_EX ) != 0 ) 
54 #else
55         while ( lockf( fileno( *lfp ), F_LOCK, 0 ) != 0 )
56 #endif
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 #ifdef HAVE_FLOCK
66                 flock( fileno( *lfp ), LOCK_UN );
67 #else
68                 lockf( fileno( *lfp ), F_ULOCK, 0 );
69 #endif
70                 fclose( *lfp );
71                 *lfp = NULL;
72                 return( NULL );
73         }
74
75         return( fp );
76 }
77
78
79
80 int
81 lock_fclose(
82     FILE        *fp,
83     FILE        *lfp
84 )
85 {
86         /* unlock */
87 #ifdef HAVE_FLOCK
88         flock( fileno( lfp ), LOCK_UN );
89 #else
90         lockf( fileno( lfp ), F_ULOCK, 0 );
91 #endif
92         fclose( lfp );
93
94         return( fclose( fp ) );
95 }
96
97
98
99 /*
100  * Apply an advisory lock on a file.  Just calls lock_fopen()
101  */
102 int
103 acquire_lock(
104     char        *file,
105     FILE        **rfp,
106     FILE        **lfp
107 )
108 {
109     if (( *rfp = lock_fopen( file, "r+", lfp )) == NULL ) {
110         Debug( LDAP_DEBUG_ANY,
111                 "Error: acquire_lock(%ld): Could not acquire lock on \"%s\"\n",
112                 (long) getpid(), file, 0);
113         return( -1 );
114     }
115     return( 0 );
116 }
117
118
119
120 /*
121  * Relinquish a lock on a file.  Calls lock_fclose() and also removes the
122  * lock file.
123  */
124 int
125 relinquish_lock(
126     char        *file,
127     FILE        *rfp,
128     FILE        *lfp
129 )
130 {
131     if ( lock_fclose( rfp, lfp ) == EOF ) {
132         Debug( LDAP_DEBUG_ANY,
133                 "Error: relinquish_lock (%ld): Error closing \"%s\"\n",
134                 (long) getpid(), file, 0 );
135         return( -1 );
136     }
137     return( 0 );
138 }