]> git.sur5r.net Git - openldap/blob - servers/slurpd/lock.c
Fix slurpd constification where dependent upon slap.h
[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     const char  *fname,
39     const 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         ldap_lockf( fileno(*lfp) );
57
58         /* open the log file */
59         if ( (fp = fopen( fname, type )) == NULL ) {
60                 Debug( LDAP_DEBUG_ANY,
61                         "Error: could not open \"%s\"\n", fname, 0, 0 );
62                 ldap_unlockf( fileno(*lfp) );
63                 fclose( *lfp );
64                 *lfp = NULL;
65                 return( NULL );
66         }
67
68         return( fp );
69 }
70
71
72
73 int
74 lock_fclose(
75     FILE        *fp,
76     FILE        *lfp
77 )
78 {
79         /* unlock */
80         ldap_unlockf( fileno(lfp) );
81         fclose( lfp );
82
83         return( fclose( fp ) );
84 }
85
86
87
88 /*
89  * Apply an advisory lock on a file.  Just calls lock_fopen()
90  */
91 int
92 acquire_lock(
93     const char  *file,
94     FILE        **rfp,
95     FILE        **lfp
96 )
97 {
98     if (( *rfp = lock_fopen( file, "r+", lfp )) == NULL ) {
99         Debug( LDAP_DEBUG_ANY,
100                 "Error: acquire_lock(%ld): Could not acquire lock on \"%s\"\n",
101                 (long) getpid(), file, 0);
102         return( -1 );
103     }
104     return( 0 );
105 }
106
107
108
109 /*
110  * Relinquish a lock on a file.  Calls lock_fclose() and also removes the
111  * lock file.
112  */
113 int
114 relinquish_lock(
115     const char  *file,
116     FILE        *rfp,
117     FILE        *lfp
118 )
119 {
120     if ( lock_fclose( rfp, lfp ) == EOF ) {
121         Debug( LDAP_DEBUG_ANY,
122                 "Error: relinquish_lock (%ld): Error closing \"%s\"\n",
123                 (long) getpid(), file, 0 );
124         return( -1 );
125     }
126     return( 0 );
127 }