2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1996 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
26 * This work was originally developed by the University of Michigan
27 * (as part of U-MICH LDAP).
31 * lock.c - routines to open and apply an advisory lock to a file
39 #include <ac/string.h>
40 #include <ac/socket.h>
42 #include <ac/unistd.h>
44 #ifdef HAVE_SYS_FILE_H
61 /* open the lock file */
62 snprintf( buf, sizeof buf, "%s.lock", fname );
64 if ( (*lfp = fopen( buf, "w" )) == NULL ) {
65 Debug( LDAP_DEBUG_ANY,
66 "Error: could not open \"%s\"\n", buf, 0, 0 );
70 /* acquire the lock */
71 ldap_lockf( fileno(*lfp) );
73 /* open the log file */
74 if ( (fp = fopen( fname, type )) == NULL ) {
75 Debug( LDAP_DEBUG_ANY,
76 "Error: could not open \"%s\"\n", fname, 0, 0 );
77 ldap_unlockf( fileno(*lfp) );
94 int rc = fclose( fp );
97 ldap_unlockf( fileno(lfp) );
106 * Apply an advisory lock on a file. Just calls lock_fopen()
115 if (( *rfp = lock_fopen( file, "r+", lfp )) == NULL ) {
116 Debug( LDAP_DEBUG_ANY,
117 "Error: acquire_lock(%ld): Could not acquire lock on \"%s\"\n",
118 (long) getpid(), file, 0);
127 * Relinquish a lock on a file. Calls lock_fclose() and also removes the
137 if ( lock_fclose( rfp, lfp ) == EOF ) {
138 Debug( LDAP_DEBUG_ANY,
139 "Error: relinquish_lock (%ld): Error closing \"%s\"\n",
140 (long) getpid(), file, 0 );