3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1996 Regents of the University of Michigan.
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.
19 * lock.c - routines to open and apply an advisory lock to a file
27 #include <ac/string.h>
28 #include <ac/socket.h>
30 #include <ac/unistd.h>
32 #ifdef HAVE_SYS_FILE_H
49 /* open the lock file */
51 strcat( buf, ".lock" );
52 if ( (*lfp = fopen( buf, "w" )) == NULL ) {
54 LDAP_LOG (( "lock", LDAP_LEVEL_ERR, "lock_fopen: "
55 "Error: could not open \"%s\"\n", buf ));
57 Debug( LDAP_DEBUG_ANY,
58 "Error: could not open \"%s\"\n", buf, 0, 0 );
63 /* acquire the lock */
64 ldap_lockf( fileno(*lfp) );
66 /* open the log file */
67 if ( (fp = fopen( fname, type )) == NULL ) {
69 LDAP_LOG (( "lock", LDAP_LEVEL_ERR, "lock_fopen: "
70 "Error: could not open \"%s\"\n", fname ));
72 Debug( LDAP_DEBUG_ANY,
73 "Error: could not open \"%s\"\n", fname, 0, 0 );
75 ldap_unlockf( fileno(*lfp) );
93 ldap_unlockf( fileno(lfp) );
96 return( fclose( fp ) );
102 * Apply an advisory lock on a file. Just calls lock_fopen()
111 if (( *rfp = lock_fopen( file, "r+", lfp )) == NULL ) {
113 LDAP_LOG (( "lock", LDAP_LEVEL_ERR, "acquire_lock: "
114 "Error: acquire_lock(%ld): Could not acquire lock on \"%s\"\n",
115 (long) getpid(), file ));
117 Debug( LDAP_DEBUG_ANY,
118 "Error: acquire_lock(%ld): Could not acquire lock on \"%s\"\n",
119 (long) getpid(), file, 0);
129 * Relinquish a lock on a file. Calls lock_fclose() and also removes the
139 if ( lock_fclose( rfp, lfp ) == EOF ) {
141 LDAP_LOG (( "lock", LDAP_LEVEL_ERR, "relinguish_lock: "
142 "Error: relinquish_lock (%ld): Error closing \"%s\"\n",
143 (long) getpid(), file ));
145 Debug( LDAP_DEBUG_ANY,
146 "Error: relinquish_lock (%ld): Error closing \"%s\"\n",
147 (long) getpid(), file, 0 );