2 * Copyright (c) 1996 Regents of the University of Michigan.
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.
14 * lock.c - routines to open and apply an advisory lock to a file
21 #include <ac/string.h>
22 #include <ac/socket.h>
24 #include <ac/unistd.h>
26 #ifdef HAVE_SYS_FILE_H
29 #ifdef HAVE_SYS_PARAM_H
30 #include <sys/param.h>
46 /* open the lock file */
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 );
55 /* acquire the lock */
56 while ( ldap_lockf( *lfp ) != 0 )
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 );
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 );