2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 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 ) {
66 LDAP_LOG ( SLURPD, ERR, "lock_fopen: "
67 "Error: could not open \"%s\"\n", buf, 0, 0 );
69 Debug( LDAP_DEBUG_ANY,
70 "Error: could not open \"%s\"\n", buf, 0, 0 );
75 /* acquire the lock */
76 ldap_lockf( fileno(*lfp) );
78 /* open the log file */
79 if ( (fp = fopen( fname, type )) == NULL ) {
81 LDAP_LOG ( SLURPD, ERR, "lock_fopen: "
82 "Error: could not open \"%s\"\n", fname, 0, 0 );
84 Debug( LDAP_DEBUG_ANY,
85 "Error: could not open \"%s\"\n", fname, 0, 0 );
87 ldap_unlockf( fileno(*lfp) );
105 ldap_unlockf( fileno(lfp) );
108 return( fclose( fp ) );
114 * Apply an advisory lock on a file. Just calls lock_fopen()
123 if (( *rfp = lock_fopen( file, "r+", lfp )) == NULL ) {
125 LDAP_LOG ( SLURPD, ERR, "acquire_lock: "
126 "Error: acquire_lock(%ld): Could not acquire lock on \"%s\"\n",
127 (long) getpid(), file, 0 );
129 Debug( LDAP_DEBUG_ANY,
130 "Error: acquire_lock(%ld): Could not acquire lock on \"%s\"\n",
131 (long) getpid(), file, 0);
141 * Relinquish a lock on a file. Calls lock_fclose() and also removes the
151 if ( lock_fclose( rfp, lfp ) == EOF ) {
153 LDAP_LOG ( SLURPD, ERR, "relinguish_lock: "
154 "Error: relinquish_lock (%ld): Error closing \"%s\"\n",
155 (long) getpid(), file, 0 );
157 Debug( LDAP_DEBUG_ANY,
158 "Error: relinquish_lock (%ld): Error closing \"%s\"\n",
159 (long) getpid(), file, 0 );