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>
27 #include <sys/param.h>
29 #include "../slapd/slap.h"
43 /* open the lock file */
45 strcat( buf, ".lock" );
46 if ( (*lfp = fopen( buf, "w" )) == NULL ) {
47 Debug( LDAP_DEBUG_ANY,
48 "Error: could not open \"%s\"\n", buf, 0, 0 );
52 /* acquire the lock */
54 while ( flock( fileno( *lfp ), LOCK_EX ) != 0 )
56 while ( lockf( fileno( *lfp ), F_LOCK, 0 ) != 0 )
62 /* open the log file */
63 if ( (fp = fopen( fname, type )) == NULL ) {
64 Debug( LDAP_DEBUG_ANY,
65 "Error: could not open \"%s\"\n", fname, 0, 0 );
67 flock( fileno( *lfp ), LOCK_UN );
69 lockf( fileno( *lfp ), F_ULOCK, 0 );
89 flock( fileno( lfp ), LOCK_UN );
91 lockf( fileno( lfp ), F_ULOCK, 0 );
95 return( fclose( fp ) );
101 * Apply an advisory lock on a file. Just calls lock_fopen()
110 if (( *rfp = lock_fopen( file, "r+", lfp )) == NULL ) {
111 Debug( LDAP_DEBUG_ANY,
112 "Error: acquire_lock(%d): Could not acquire lock on \"%s\"\n",
122 * Relinquish a lock on a file. Calls lock_fclose() and also removes the
132 if ( lock_fclose( rfp, lfp ) == EOF ) {
133 Debug( LDAP_DEBUG_ANY,
134 "Error: relinquish_lock (%d): Error closing \"%s\"\n",