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 */
57 while ( flock( fileno( *lfp ), LOCK_EX ) != 0 )
59 while ( lockf( fileno( *lfp ), F_LOCK, 0 ) != 0 )
65 /* open the log file */
66 if ( (fp = fopen( fname, type )) == NULL ) {
67 Debug( LDAP_DEBUG_ANY,
68 "Error: could not open \"%s\"\n", fname, 0, 0 );
70 flock( fileno( *lfp ), LOCK_UN );
72 lockf( fileno( *lfp ), F_ULOCK, 0 );
92 flock( fileno( lfp ), LOCK_UN );
94 lockf( fileno( lfp ), F_ULOCK, 0 );
98 return( fclose( fp ) );
104 * Apply an advisory lock on a file. Just calls lock_fopen()
113 if (( *rfp = lock_fopen( file, "r+", lfp )) == NULL ) {
114 Debug( LDAP_DEBUG_ANY,
115 "Error: acquire_lock(%ld): Could not acquire lock on \"%s\"\n",
116 (long) getpid(), file, 0);
125 * Relinquish a lock on a file. Calls lock_fclose() and also removes the
135 if ( lock_fclose( rfp, lfp ) == EOF ) {
136 Debug( LDAP_DEBUG_ANY,
137 "Error: relinquish_lock (%ld): Error closing \"%s\"\n",
138 (long) getpid(), file, 0 );