1 /* lock.c - routines to open and apply an advisory lock to a file */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
31 #include <ac/string.h>
32 #include <ac/socket.h>
34 #include <ac/unistd.h>
36 #ifdef HAVE_SYS_FILE_H
44 lock_fopen( const char *fname, const char *type, FILE **lfp )
49 /* open the lock file */
50 snprintf( buf, sizeof buf, "%s.lock", fname );
52 if ( (*lfp = fopen( buf, "w" )) == NULL ) {
53 Debug( LDAP_DEBUG_ANY, "could not open \"%s\"\n", buf, 0, 0 );
58 /* acquire the lock */
59 ldap_lockf( fileno(*lfp) );
61 /* open the log file */
62 if ( (fp = fopen( fname, type )) == NULL ) {
63 Debug( LDAP_DEBUG_ANY, "could not open \"%s\"\n", fname, 0, 0 );
65 ldap_unlockf( fileno(*lfp) );
75 lock_fclose( FILE *fp, FILE *lfp )
78 ldap_unlockf( fileno(lfp) );
81 return( fclose( fp ) );