]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dbcache.c
Don't crash if attr_find() fails (Howard, you might like to review this)
[openldap] / servers / slapd / back-bdb / dbcache.c
1 /* dbcache.c - manage cache of open databases */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/errno.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15 #include <ac/time.h>
16 #include <sys/stat.h>
17
18 #include "slap.h"
19 #include "back-bdb.h"
20 #include "lutil_hash.h"
21
22 /* Pass-thru hash function. Since the indexer is already giving us hash
23  * values as keys, we don't need BDB to re-hash them.
24  */
25 static u_int32_t
26 bdb_db_hash(
27         DB *db,
28         const void *bytes,
29         u_int32_t length
30 )
31 {
32         u_int32_t ret = 0;
33         unsigned char *dst = (unsigned char *)&ret;
34         const unsigned char *src = (const unsigned char *)bytes;
35
36         if ( length > sizeof(u_int32_t) )
37                 length = sizeof(u_int32_t);
38
39         while ( length ) {
40                 *dst++ = *src++;
41                 length--;
42         }
43         return ret;
44 }
45
46 int
47 bdb_db_cache(
48         Backend *be,
49         DB_TXN *tid,
50         const char *name,
51         DB **dbout )
52 {
53         int i;
54         int rc;
55         int flags;
56         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
57         struct bdb_db_info *db;
58         char *file;
59         DBT lockobj;
60         DB_LOCK lock;
61         u_int32_t locker = 0;
62
63         *dbout = NULL;
64
65         for( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
66                 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
67                         *dbout = bdb->bi_databases[i]->bdi_db;
68                         return 0;
69                 }
70         }
71
72         lockobj.data = "bdb_db_cache";
73         lockobj.size = sizeof("bdb_db_cache");
74
75         if (tid) {
76                 locker = TXN_ID( tid );
77         } else {
78 #ifdef BDB_REUSE_LOCKERS
79 #define op      NULL    /* implicit arg in LOCK_ID */
80 #endif
81                 rc = LOCK_ID( bdb->bi_dbenv, &locker );
82                 if (rc) return rc;
83         }
84         rc = LOCK_GET( bdb->bi_dbenv, locker, 0, &lockobj,
85                 DB_LOCK_WRITE, &lock );
86         if (rc) return rc;
87
88         /* check again! may have been added by another thread */
89         for( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
90                 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
91                         *dbout = bdb->bi_databases[i]->bdi_db;
92                         LOCK_PUT( bdb->bi_dbenv, &lock);
93                         return 0;
94                 }
95         }
96
97         if( i >= BDB_INDICES ) {
98                 LOCK_PUT( bdb->bi_dbenv, &lock);
99                 return -1;
100         }
101
102         db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
103
104         db->bdi_name = ch_strdup( name );
105
106         rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
107         if( rc != 0 ) {
108 #ifdef NEW_LOGGING
109                 LDAP_LOG ( CACHE, ERR, 
110                         "bdb_db_cache: db_create(%s) failed: %s (%d)\n", 
111                         bdb->bi_dbenv_home, db_strerror(rc), rc );
112 #else
113                 Debug( LDAP_DEBUG_ANY,
114                         "bdb_db_cache: db_create(%s) failed: %s (%d)\n",
115                         bdb->bi_dbenv_home, db_strerror(rc), rc );
116 #endif
117                 LOCK_PUT( bdb->bi_dbenv, &lock);
118                 return rc;
119         }
120
121         rc = db->bdi_db->set_pagesize( db->bdi_db, BDB_PAGESIZE );
122         rc = db->bdi_db->set_h_hash( db->bdi_db, bdb_db_hash );
123         rc = db->bdi_db->set_flags( db->bdi_db, DB_DUP | DB_DUPSORT );
124         rc = db->bdi_db->set_dup_compare( db->bdi_db, bdb_bt_compare );
125
126         file = ch_malloc( strlen( name ) + sizeof(BDB_SUFFIX) );
127         sprintf( file, "%s" BDB_SUFFIX, name );
128
129 #ifdef HAVE_EBCDIC
130         __atoe( file );
131 #endif
132         flags = bdb->bi_db_opflags | DB_CREATE | DB_THREAD;
133         if ( !tid ) flags |= DB_AUTO_COMMIT;
134         rc = DB_OPEN( db->bdi_db, tid,
135                 file, NULL /* name */,
136                 DB_HASH, flags,
137                 bdb->bi_dbenv_mode );
138
139         ch_free( file );
140
141         if( rc != 0 ) {
142 #ifdef NEW_LOGGING
143                 LDAP_LOG ( CACHE, ERR, 
144                         "bdb_db_cache: db_open(%s) failed: %s (%d)\n", 
145                         name, db_strerror(rc), rc );
146 #else
147                 Debug( LDAP_DEBUG_ANY,
148                         "bdb_db_cache: db_open(%s) failed: %s (%d)\n",
149                         name, db_strerror(rc), rc );
150 #endif
151                 LOCK_PUT( bdb->bi_dbenv, &lock);
152                 return rc;
153         }
154
155         bdb->bi_databases[i] = db;
156         bdb->bi_ndatabases = i+1;
157
158         *dbout = db->bdi_db;
159
160         LOCK_PUT( bdb->bi_dbenv, &lock );
161         return 0;
162 }