]> git.sur5r.net Git - openldap/blob - servers/slurpd/lock.c
fix up stats/debug messages
[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/param.h>
23 #include <ac/string.h>
24 #include <ac/socket.h>
25 #include <ac/time.h>
26 #include <ac/unistd.h>
27
28 #ifdef HAVE_SYS_FILE_H
29 #include <sys/file.h>
30 #endif
31
32 #include "slurp.h"
33
34
35 FILE *
36 lock_fopen(
37     const char  *fname,
38     const char  *type,
39     FILE        **lfp
40 )
41 {
42         FILE    *fp;
43         char    buf[MAXPATHLEN];
44
45         /* open the lock file */
46         strcpy( buf, fname );
47         strcat( buf, ".lock" );
48         if ( (*lfp = fopen( buf, "w" )) == NULL ) {
49                 Debug( LDAP_DEBUG_ANY,
50                         "Error: could not open \"%s\"\n", buf, 0, 0 );
51                 return( NULL );
52         }
53
54         /* acquire the lock */
55         ldap_lockf( fileno(*lfp) );
56
57         /* open the log file */
58         if ( (fp = fopen( fname, type )) == NULL ) {
59                 Debug( LDAP_DEBUG_ANY,
60                         "Error: could not open \"%s\"\n", fname, 0, 0 );
61                 ldap_unlockf( fileno(*lfp) );
62                 fclose( *lfp );
63                 *lfp = NULL;
64                 return( NULL );
65         }
66
67         return( fp );
68 }
69
70
71
72 int
73 lock_fclose(
74     FILE        *fp,
75     FILE        *lfp
76 )
77 {
78         /* unlock */
79         ldap_unlockf( fileno(lfp) );
80         fclose( lfp );
81
82         return( fclose( fp ) );
83 }
84
85
86
87 /*
88  * Apply an advisory lock on a file.  Just calls lock_fopen()
89  */
90 int
91 acquire_lock(
92     const char  *file,
93     FILE        **rfp,
94     FILE        **lfp
95 )
96 {
97     if (( *rfp = lock_fopen( file, "r+", lfp )) == NULL ) {
98         Debug( LDAP_DEBUG_ANY,
99                 "Error: acquire_lock(%ld): Could not acquire lock on \"%s\"\n",
100                 (long) getpid(), file, 0);
101         return( -1 );
102     }
103     return( 0 );
104 }
105
106
107
108 /*
109  * Relinquish a lock on a file.  Calls lock_fclose() and also removes the
110  * lock file.
111  */
112 int
113 relinquish_lock(
114     const char  *file,
115     FILE        *rfp,
116     FILE        *lfp
117 )
118 {
119     if ( lock_fclose( rfp, lfp ) == EOF ) {
120         Debug( LDAP_DEBUG_ANY,
121                 "Error: relinquish_lock (%ld): Error closing \"%s\"\n",
122                 (long) getpid(), file, 0 );
123         return( -1 );
124     }
125     return( 0 );
126 }