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 ) {
54 LDAP_LOG( OPERATION, ERR,
55 "lock_fopen: could not open lock file \"%s\".\n", buf, 0, 0);
57 Debug( LDAP_DEBUG_ANY, "could not open \"%s\"\n", buf, 0, 0 );
63 /* acquire the lock */
64 ldap_lockf( fileno(*lfp) );
66 /* open the log file */
67 if ( (fp = fopen( fname, type )) == NULL ) {
69 LDAP_LOG( OPERATION, ERR,
70 "lock_fopen: could not open log file \"%s\".\n", buf, 0, 0);
72 Debug( LDAP_DEBUG_ANY, "could not open \"%s\"\n", fname, 0, 0 );
75 ldap_unlockf( fileno(*lfp) );
85 lock_fclose( FILE *fp, FILE *lfp )
88 ldap_unlockf( fileno(lfp) );
91 return( fclose( fp ) );