]> git.sur5r.net Git - openldap/blob - slurpd/lock.c
Do not require ac/string.h for lber_pvt.h
[openldap] / slurpd / lock.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright (c) 1996 Regents of the University of Michigan.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * provided that this notice is preserved and that due credit is given
12  * to the University of Michigan at Ann Arbor. The name of the University
13  * may not be used to endorse or promote products derived from this
14  * software without specific prior written permission. This software
15  * is provided ``as is'' without express or implied warranty.
16  */
17
18 /*
19  * lock.c - routines to open and apply an advisory lock to a file
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25
26 #include <ac/param.h>
27 #include <ac/string.h>
28 #include <ac/socket.h>
29 #include <ac/time.h>
30 #include <ac/unistd.h>
31
32 #ifdef HAVE_SYS_FILE_H
33 #include <sys/file.h>
34 #endif
35
36 #include "slurp.h"
37
38
39 FILE *
40 lock_fopen(
41     const char  *fname,
42     const char  *type,
43     FILE        **lfp
44 )
45 {
46         FILE    *fp;
47         char    buf[MAXPATHLEN];
48
49         /* open the lock file */
50         strcpy( buf, fname );
51         strcat( buf, ".lock" );
52         if ( (*lfp = fopen( buf, "w" )) == NULL ) {
53                 Debug( LDAP_DEBUG_ANY,
54                         "Error: could not open \"%s\"\n", buf, 0, 0 );
55                 return( NULL );
56         }
57
58         /* acquire the lock */
59         ldap_lockf( fileno(*lfp) );
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( fileno(*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( fileno(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     const 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     const 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 }