]> git.sur5r.net Git - openldap/blob - servers/slapd/lock.c
ITS#3032: retry when GSSAPI creds are not available
[openldap] / servers / slapd / lock.c
1 /* lock.c - routines to open and apply an advisory lock to a file */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
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.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/string.h>
32 #include <ac/socket.h>
33 #include <ac/time.h>
34 #include <ac/unistd.h>
35
36 #ifdef HAVE_SYS_FILE_H
37 #include <sys/file.h>
38 #endif
39
40 #include "slap.h"
41 #include <lutil.h>
42
43 FILE *
44 lock_fopen( const char *fname, const char *type, FILE **lfp )
45 {
46         FILE    *fp;
47         char    buf[MAXPATHLEN];
48
49         /* open the lock file */
50         snprintf( buf, sizeof buf, "%s.lock", fname );
51
52         if ( (*lfp = fopen( buf, "w" )) == NULL ) {
53 #ifdef NEW_LOGGING
54                 LDAP_LOG( OPERATION, ERR, 
55                         "lock_fopen: could not open lock file \"%s\".\n", buf, 0, 0);
56 #else
57                 Debug( LDAP_DEBUG_ANY, "could not open \"%s\"\n", buf, 0, 0 );
58 #endif
59
60                 return( NULL );
61         }
62
63         /* acquire the lock */
64         ldap_lockf( fileno(*lfp) );
65
66         /* open the log file */
67         if ( (fp = fopen( fname, type )) == NULL ) {
68 #ifdef NEW_LOGGING
69                 LDAP_LOG( OPERATION, ERR, 
70                         "lock_fopen: could not open log file \"%s\".\n", buf, 0, 0);
71 #else
72                 Debug( LDAP_DEBUG_ANY, "could not open \"%s\"\n", fname, 0, 0 );
73 #endif
74
75                 ldap_unlockf( fileno(*lfp) );
76                 fclose( *lfp );
77                 *lfp = NULL;
78                 return( NULL );
79         }
80
81         return( fp );
82 }
83
84 int
85 lock_fclose( FILE *fp, FILE *lfp )
86 {
87         /* unlock */
88         ldap_unlockf( fileno(lfp) );
89         fclose( lfp );
90
91         return( fclose( fp ) );
92 }