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 ) {
53 Debug( LDAP_DEBUG_ANY,
54 "Error: could not open \"%s\"\n", buf, 0, 0 );
58 /* acquire the lock */
59 ldap_lockf( fileno(*lfp) );
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) );
83 ldap_unlockf( fileno(lfp) );
86 return( fclose( fp ) );
92 * Apply an advisory lock on a file. Just calls lock_fopen()
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);
113 * Relinquish a lock on a file. Calls lock_fclose() and also removes the
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 );