3 * Copyright (c) 1996 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
15 * lock.c - routines to open and apply an advisory lock to a file
22 #include <ac/string.h>
23 #include <ac/socket.h>
25 #include <ac/unistd.h>
27 #ifdef HAVE_SYS_FILE_H
30 #ifdef HAVE_SYS_PARAM_H
31 #include <sys/param.h>
47 /* open the lock file */
49 strcat( buf, ".lock" );
50 if ( (*lfp = fopen( buf, "w" )) == NULL ) {
51 Debug( LDAP_DEBUG_ANY,
52 "Error: could not open \"%s\"\n", buf, 0, 0 );
56 /* acquire the lock */
57 ldap_lockf( fileno(*lfp) );
59 /* open the log file */
60 if ( (fp = fopen( fname, type )) == NULL ) {
61 Debug( LDAP_DEBUG_ANY,
62 "Error: could not open \"%s\"\n", fname, 0, 0 );
63 ldap_unlockf( fileno(*lfp) );
81 ldap_unlockf( fileno(lfp) );
84 return( fclose( fp ) );
90 * Apply an advisory lock on a file. Just calls lock_fopen()
99 if (( *rfp = lock_fopen( file, "r+", lfp )) == NULL ) {
100 Debug( LDAP_DEBUG_ANY,
101 "Error: acquire_lock(%ld): Could not acquire lock on \"%s\"\n",
102 (long) getpid(), file, 0);
111 * Relinquish a lock on a file. Calls lock_fclose() and also removes the
121 if ( lock_fclose( rfp, lfp ) == EOF ) {
122 Debug( LDAP_DEBUG_ANY,
123 "Error: relinquish_lock (%ld): Error closing \"%s\"\n",
124 (long) getpid(), file, 0 );