|
67 |
#include <sys/types.h> |
67 |
#include <sys/types.h> |
68 |
#include <sys/file.h> |
68 |
#include <sys/file.h> |
69 |
#include <sys/stat.h> |
69 |
#include <sys/stat.h> |
70 |
#include <db.h> |
70 |
#include <lmdb.h> |
71 |
#include <stdbool.h> |
71 |
#include <stdbool.h> |
72 |
#include <assert.h> |
72 |
#include <assert.h> |
73 |
|
73 |
|
|
91 |
|
91 |
|
92 |
CacheMasterEntry cache_master_entry; |
92 |
CacheMasterEntry cache_master_entry; |
93 |
|
93 |
|
94 |
DB *dbp; |
94 |
MDB_env *env; |
95 |
#ifdef WITH_DB42 |
95 |
MDB_dbi dbi; |
96 |
DB_ENV *dbenvp; |
|
|
97 |
#endif |
98 |
static FILE *lock_fp=NULL; |
96 |
static FILE *lock_fp=NULL; |
99 |
|
97 |
|
100 |
static struct filter cache_filter; |
98 |
static struct filter cache_filter; |
|
110 |
} |
108 |
} |
111 |
} |
109 |
} |
112 |
|
110 |
|
113 |
#ifdef WITH_DB42 |
111 |
static void cache_error_message(int rv, char *msg) |
114 |
static void cache_panic_call(DB_ENV *dbenvp, int errval) |
|
|
115 |
{ |
112 |
{ |
116 |
exit(1); |
|
|
117 |
} |
118 |
#endif |
119 |
|
120 |
static void cache_error_message(const char *errpfx, char *msg) |
121 |
{ |
122 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
113 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
123 |
"database error: %s", msg); |
114 |
"database error: %s: %s (%d)\n", |
|
|
115 |
msg, mdb_strerror(rv), rv); |
124 |
} |
116 |
} |
125 |
|
117 |
|
126 |
int cache_lock(void) |
118 |
int cache_lock(void) |
|
130 |
|
122 |
|
131 |
assert(!lock_fp); |
123 |
assert(!lock_fp); |
132 |
|
124 |
|
133 |
rv = snprintf(lock_file, PATH_MAX, "%s/cache.db.lock", cache_dir); |
125 |
rv = snprintf(lock_file, PATH_MAX, "%s/cache.lock", cache_dir); |
134 |
if (rv < 0 || rv >= PATH_MAX) |
126 |
if (rv < 0 || rv >= PATH_MAX) |
135 |
abort(); |
127 |
abort(); |
136 |
|
128 |
|
|
156 |
{ |
148 |
{ |
157 |
int rv; |
149 |
int rv; |
158 |
char file[PATH_MAX]; |
150 |
char file[PATH_MAX]; |
|
|
151 |
MDB_txn *txn; |
159 |
|
152 |
|
160 |
snprintf(file, PATH_MAX, "%s/cache.db", cache_dir); |
153 |
snprintf(file, PATH_MAX, "%s/cache", cache_dir); |
161 |
|
154 |
|
162 |
#ifdef WITH_DB42 |
155 |
if ((rv = mdb_env_create(&env)) != 0) { |
163 |
if ((rv = db_env_create(&dbenvp, 0)) != 0) { |
|
|
164 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
156 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
165 |
"creating database environment failed"); |
157 |
"creating environment handle failed"); |
|
|
158 |
cache_error_message(rv, "mdb_env_create"); |
166 |
return rv; |
159 |
return rv; |
167 |
} |
160 |
} |
168 |
dbenvp->set_errcall(dbenvp, cache_error_message); |
161 |
if ((rv = mdb_env_open(env, cache_dir, MDB_NORDAHEAD, 0600)) != 0) { |
169 |
dbenvp->set_paniccall(dbenvp, cache_panic_call); |
|
|
170 |
if ((rv = dbenvp->open(dbenvp, cache_dir, DB_CREATE | DB_INIT_MPOOL | |
171 |
/*DB_INIT_LOCK | */DB_INIT_LOG | DB_INIT_TXN | |
172 |
DB_RECOVER, 0600)) != 0) { |
173 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
162 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
174 |
"opening database environment failed"); |
163 |
"opening database failed"); |
175 |
dbenvp->err(dbenvp, rv, "%s", "environment"); |
164 |
cache_error_message(rv, "mdb_env_open"); |
176 |
return rv; |
165 |
return rv; |
177 |
} |
166 |
} |
178 |
if ((rv = db_create(&dbp, dbenvp, 0)) != 0) { |
167 |
if ((rv = mdb_txn_begin(env, NULL, 0, &txn)) != 0) { |
179 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
168 |
cache_error_message(rv, "mdb_txn_begin"); |
180 |
"creating database handle failed"); |
169 |
mdb_env_close(env); |
181 |
return rv; |
170 |
return rv; |
182 |
} |
171 |
} |
183 |
if ((rv = dbp->open(dbp, NULL, "cache.db", NULL, DB_BTREE, |
172 |
if ((rv = mdb_open(txn, NULL, 0, &dbi)) != 0) { |
184 |
DB_CREATE | DB_CHKSUM | DB_AUTO_COMMIT, |
173 |
cache_error_message(rv, "mdb_open"); |
185 |
0600)) != 0) { |
174 |
mdb_txn_abort(txn); |
186 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
175 |
mdb_env_close(env); |
187 |
"opening database failed"); |
|
|
188 |
dbp->err(dbp, rv, "open"); |
189 |
// FIXME: free dbp |
190 |
return rv; |
176 |
return rv; |
191 |
} |
177 |
} |
192 |
#else |
178 |
if ((rv = mdb_txn_commit(txn)) != 0) { |
193 |
if ((rv = db_create(&dbp, NULL, 0)) != 0) { |
179 |
cache_error_message(rv, "mdb_txn_commit"); |
194 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
180 |
mdb_dbi_close(env, dbi); |
195 |
"creating database handle failed"); |
181 |
mdb_env_close(env); |
196 |
return rv; |
182 |
return rv; |
197 |
} |
183 |
} |
198 |
if ((rv = dbp->open(dbp, file, NULL, DB_BTREE, DB_CREATE, 0600)) != 0) { |
184 |
|
199 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
|
|
200 |
"opening database failed"); |
201 |
dbp->err(dbp, rv, "open"); |
202 |
// FIXME: free dbp |
203 |
return rv; |
204 |
} |
205 |
dbp->set_errcall(dbp, cache_error_message); |
206 |
#endif |
207 |
setup_cache_filter(); |
185 |
setup_cache_filter(); |
208 |
return 0; |
186 |
return 0; |
209 |
} |
187 |
} |
210 |
|
188 |
|
211 |
void cache_sync(void) { |
|
|
212 |
if (!INIT_ONLY && dbp) { |
213 |
dbp->sync(dbp, 0); |
214 |
} |
215 |
} |
216 |
|
217 |
int cache_set_schema_id(const NotifierID value) |
189 |
int cache_set_schema_id(const NotifierID value) |
218 |
{ |
190 |
{ |
219 |
int rv, fd, len; |
191 |
int rv, fd, len; |
|
297 |
|
269 |
|
298 |
int cache_get_master_entry(CacheMasterEntry *master_entry) |
270 |
int cache_get_master_entry(CacheMasterEntry *master_entry) |
299 |
{ |
271 |
{ |
300 |
DBT key, data; |
272 |
MDB_txn *txn; |
|
|
273 |
MDB_cursor *cursor; |
274 |
MDB_val key, data; |
301 |
int rv; |
275 |
int rv; |
302 |
|
276 |
|
303 |
memset(&key, 0, sizeof(DBT)); |
277 |
memset(&key, 0, sizeof(MDB_val)); |
304 |
memset(&data, 0, sizeof(DBT)); |
278 |
memset(&data, 0, sizeof(MDB_val)); |
305 |
|
279 |
|
306 |
key.data=MASTER_KEY; |
280 |
key.mv_data=MASTER_KEY; |
307 |
key.size=MASTER_KEY_SIZE; |
281 |
key.mv_size=MASTER_KEY_SIZE; |
308 |
data.flags = DB_DBT_REALLOC; |
|
|
309 |
|
282 |
|
310 |
if ((rv=dbp->get(dbp, NULL, &key, &data, 0)) == DB_NOTFOUND) |
283 |
if ((rv = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn)) != 0) { |
|
|
284 |
cache_error_message(rv, "mdb_txn_begin"); |
311 |
return rv; |
285 |
return rv; |
|
|
286 |
} |
287 |
if ((rv = mdb_cursor_open(txn, dbi, &cursor)) != 0) { |
288 |
cache_error_message(rv, "mdb_cursor_open"); |
289 |
mdb_txn_abort(txn); |
290 |
return rv; |
291 |
} |
292 |
if ((rv = mdb_cursor_get(cursor, &key, &data, MDB_SET_KEY)) == MDB_NOTFOUND) { |
293 |
mdb_cursor_close(cursor); |
294 |
mdb_txn_abort(txn); |
295 |
return rv; |
296 |
} |
312 |
else if (rv != 0) { |
297 |
else if (rv != 0) { |
313 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
298 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
314 |
"reading master entry from database failed"); |
299 |
"reading master entry from database failed"); |
315 |
dbp->err(dbp, rv, "get"); |
300 |
cache_error_message(rv, "mdb_get"); |
|
|
301 |
mdb_cursor_close(cursor); |
302 |
mdb_txn_abort(txn); |
316 |
return rv; |
303 |
return rv; |
317 |
} |
304 |
} |
|
|
305 |
mdb_cursor_close(cursor); |
306 |
mdb_txn_abort(txn); |
318 |
|
307 |
|
319 |
if (data.size != sizeof(CacheMasterEntry)) { |
308 |
if (data.mv_size != sizeof(CacheMasterEntry)) { |
320 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
309 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
321 |
"master entry has unexpected length"); |
310 |
"master entry has unexpected length"); |
322 |
return 1; |
311 |
return 1; |
323 |
} |
312 |
} |
324 |
|
313 |
|
325 |
memcpy(master_entry, data.data, sizeof(CacheMasterEntry)); |
314 |
memcpy(master_entry, data.mv_data, sizeof(CacheMasterEntry)); |
326 |
free(data.data); |
|
|
327 |
|
315 |
|
328 |
return 0; |
316 |
return 0; |
329 |
} |
317 |
} |
330 |
|
318 |
|
331 |
int cache_update_master_entry(CacheMasterEntry *master_entry, DB_TXN *dbtxnp) |
319 |
/* The dbtxnp argument is only used when WITH_DB42 is defined - useless? */ |
|
|
320 |
int cache_update_master_entry(CacheMasterEntry *master_entry, MDB_txn *dbtxnp) |
332 |
{ |
321 |
{ |
333 |
DBT key, data; |
322 |
MDB_txn *txn = dbtxnp; |
|
|
323 |
MDB_val key, data; |
334 |
int rv; |
324 |
int rv; |
335 |
int flags; |
325 |
int flags; |
336 |
|
326 |
|
337 |
memset(&key, 0, sizeof(DBT)); |
327 |
memset(&key, 0, sizeof(MDB_val)); |
338 |
memset(&data, 0, sizeof(DBT)); |
328 |
memset(&data, 0, sizeof(MDB_val)); |
339 |
|
329 |
|
340 |
key.data=MASTER_KEY; |
330 |
key.mv_data=MASTER_KEY; |
341 |
key.size=MASTER_KEY_SIZE; |
331 |
key.mv_size=MASTER_KEY_SIZE; |
342 |
|
332 |
|
343 |
data.data=(void*)master_entry; |
333 |
data.mv_data=(void*)master_entry; |
344 |
data.size=sizeof(CacheMasterEntry); |
334 |
data.mv_size=sizeof(CacheMasterEntry); |
345 |
|
335 |
|
346 |
#ifdef WITH_DB42 |
336 |
flags = 0; |
347 |
if (dbtxnp == NULL) |
|
|
348 |
flags = DB_AUTO_COMMIT; |
349 |
else |
350 |
#endif |
351 |
flags = 0; |
352 |
|
337 |
|
353 |
if ((rv=dbp->put(dbp, dbtxnp, &key, &data, flags)) != 0) { |
338 |
if (!dbtxnp && (rv = mdb_txn_begin(env, NULL, 0, &txn)) != 0) { |
|
|
339 |
cache_error_message(rv, "mdb_txn_begin"); |
340 |
return rv; |
341 |
} |
342 |
if ((rv = mdb_put(txn, dbi, &key, &data, flags)) != 0) { |
354 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
343 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
355 |
"storing master entry in database failed"); |
344 |
"storing master entry in database failed"); |
356 |
dbp->err(dbp, rv, "put"); |
345 |
cache_error_message(rv, "mdb_put"); |
|
|
346 |
mdb_txn_abort(txn); |
357 |
return rv; |
347 |
return rv; |
358 |
} |
348 |
} |
|
|
349 |
if (!dbtxnp && (rv = mdb_txn_commit(txn)) != 0) { |
350 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
351 |
"storing master entry in database failed"); |
352 |
cache_error_message(rv, "mdb_txn_commit"); |
353 |
return rv; |
354 |
} |
359 |
|
355 |
|
360 |
cache_sync(); |
|
|
361 |
|
362 |
return 0; |
356 |
return 0; |
363 |
} |
357 |
} |
364 |
|
358 |
|
365 |
DB_TXN* cache_new_transaction(NotifierID id, char *dn) |
359 |
MDB_txn *cache_new_transaction(NotifierID id, char *dn) |
366 |
{ |
360 |
{ |
367 |
#ifdef WITH_DB42 |
361 |
#ifdef WITH_DB42 |
368 |
DB_TXN *dbtxnp; |
362 |
int rv; |
|
|
363 |
MDB_txn *txn; |
369 |
CacheMasterEntry master_entry; |
364 |
CacheMasterEntry master_entry; |
370 |
NotifierID *old_id; |
365 |
NotifierID *old_id; |
371 |
|
366 |
|
372 |
dbenvp->txn_begin(dbenvp, NULL, &dbtxnp, 0); |
367 |
if ((rv = mdb_txn_begin(env, NULL, 0, &txn)) != 0) { |
|
|
368 |
cache_error_message(rv, "mdb_txn_begin"); |
369 |
return NULL; |
370 |
} |
373 |
|
371 |
|
374 |
if (id != 0) { |
372 |
if (id != 0) { |
375 |
if (cache_get_master_entry(&master_entry) != 0) { |
373 |
if (cache_get_master_entry(&master_entry) != 0) { |
376 |
dbtxnp->abort(dbtxnp); |
374 |
mdb_txn_abort(txn); |
377 |
return NULL; |
375 |
return NULL; |
378 |
} |
376 |
} |
379 |
|
377 |
|
|
386 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
384 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
387 |
"New ID (%ld) is not greater than old" |
385 |
"New ID (%ld) is not greater than old" |
388 |
" ID (%ld): %s", id, *old_id, dn); |
386 |
" ID (%ld): %s", id, *old_id, dn); |
389 |
dbtxnp->abort(dbtxnp); |
387 |
mdb_txn_abort(txn); |
390 |
return NULL; |
388 |
return NULL; |
391 |
} else |
389 |
} else |
392 |
*old_id = id; |
390 |
*old_id = id; |
393 |
|
391 |
|
394 |
if (cache_update_master_entry(&master_entry, dbtxnp) != 0) { |
392 |
if (cache_update_master_entry(&master_entry, txn) != 0) { |
395 |
dbtxnp->abort(dbtxnp); |
393 |
mdb_txn_abort(txn); |
396 |
return NULL; |
394 |
return NULL; |
397 |
} |
395 |
} |
398 |
} |
396 |
} |
399 |
|
397 |
|
400 |
return dbtxnp; |
398 |
return txn; |
401 |
#else |
399 |
#else |
402 |
return NULL; |
400 |
return NULL; |
403 |
#endif |
401 |
#endif |
|
410 |
cache_delete_entry do nothing (at least if WITH_DB42 is undefined) */ |
408 |
cache_delete_entry do nothing (at least if WITH_DB42 is undefined) */ |
411 |
inline int cache_update_entry(NotifierID id, char *dn, CacheEntry *entry) |
409 |
inline int cache_update_entry(NotifierID id, char *dn, CacheEntry *entry) |
412 |
{ |
410 |
{ |
413 |
DBT key, data; |
411 |
MDB_txn *txn; |
414 |
DB_TXN *dbtxnp; |
412 |
MDB_val key, data; |
415 |
int rv = 0; |
413 |
int rv = 0; |
416 |
|
414 |
|
417 |
memset(&key, 0, sizeof(DBT)); |
415 |
memset(&key, 0, sizeof(MDB_val)); |
418 |
memset(&data, 0, sizeof(DBT)); |
416 |
memset(&data, 0, sizeof(MDB_val)); |
419 |
|
417 |
|
420 |
if ((rv=unparse_entry(&data.data, &data.size, entry)) != 0) { |
418 |
if ((rv=unparse_entry(&data.mv_data, &data.mv_size, entry)) != 0) { |
421 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
419 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
422 |
"unparsing entry failed"); |
420 |
"unparsing entry failed"); |
423 |
return rv; |
421 |
return rv; |
|
426 |
|
424 |
|
427 |
signals_block(); |
425 |
signals_block(); |
428 |
#ifdef WITH_DB42 |
426 |
#ifdef WITH_DB42 |
429 |
dbtxnp = cache_new_transaction(id, dn); |
427 |
txn = cache_new_transaction(id, dn); |
430 |
if (dbtxnp == NULL) { |
428 |
if (txn == NULL) { |
431 |
signals_unblock(); |
429 |
signals_unblock(); |
432 |
free(data.data); |
|
|
433 |
return 1; |
430 |
return 1; |
434 |
} |
431 |
} |
435 |
#else |
432 |
#else |
436 |
dbtxnp = NULL; |
433 |
if ((rv = mdb_txn_begin(env, NULL, 0, &txn)) != 0) { |
|
|
434 |
cache_error_message(rv, "mdb_txn_begin"); |
435 |
signals_unblock(); |
436 |
return rv; |
437 |
} |
437 |
#endif |
438 |
#endif |
438 |
|
439 |
|
439 |
key.data=dn; |
440 |
key.mv_data=dn; |
440 |
key.size=strlen(dn)+1; |
441 |
key.mv_size=strlen(dn)+1; |
441 |
|
442 |
|
442 |
if ((rv=dbp->put(dbp, dbtxnp, &key, &data, 0)) != 0) { |
443 |
if ((rv = mdb_put(txn, dbi, &key, &data, 0)) != 0) { |
443 |
signals_unblock(); |
444 |
signals_unblock(); |
444 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
445 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
445 |
"storing entry in database failed: %s", dn); |
446 |
"storing entry in database failed: %s", dn); |
446 |
dbp->err(dbp, rv, "put"); |
447 |
cache_error_message(rv, "mdb_put"); |
447 |
#ifdef WITH_DB42 |
448 |
mdb_txn_abort(txn); |
448 |
dbtxnp->abort(dbtxnp); |
|
|
449 |
#endif |
450 |
free(data.data); |
451 |
return rv; |
449 |
return rv; |
452 |
} |
450 |
} |
453 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "put %d bytes for %s", data.size, dn); |
451 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "put %zu bytes for %s", data.mv_size, dn); |
454 |
|
452 |
|
455 |
|
453 |
|
456 |
#ifdef WITH_DB42 |
454 |
if ((rv = mdb_txn_commit(txn)) != 0) { |
457 |
dbtxnp->commit(dbtxnp, 0); |
455 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
458 |
#endif |
456 |
"storing master entry in database failed"); |
459 |
cache_sync(); |
457 |
cache_error_message(rv, "mdb_txn_commit"); |
|
|
458 |
return rv; |
459 |
} |
460 |
|
460 |
signals_unblock(); |
461 |
signals_unblock(); |
461 |
|
462 |
|
462 |
free(data.data); |
|
|
463 |
return rv; |
463 |
return rv; |
464 |
} |
464 |
} |
465 |
|
465 |
|
|
482 |
|
482 |
|
483 |
int cache_delete_entry(NotifierID id, char *dn) |
483 |
int cache_delete_entry(NotifierID id, char *dn) |
484 |
{ |
484 |
{ |
485 |
DB_TXN *dbtxnp; |
485 |
MDB_txn *txn; |
486 |
DBT key; |
486 |
MDB_val key; |
487 |
int rv; |
487 |
int rv; |
488 |
|
488 |
|
489 |
memset(&key, 0, sizeof(DBT)); |
489 |
memset(&key, 0, sizeof(MDB_val)); |
490 |
|
490 |
|
491 |
key.data=dn; |
491 |
key.mv_data=dn; |
492 |
key.size=strlen(dn)+1; |
492 |
key.mv_size=strlen(dn)+1; |
493 |
|
493 |
|
494 |
signals_block(); |
494 |
signals_block(); |
495 |
#ifdef WITH_DB42 |
495 |
#ifdef WITH_DB42 |
496 |
dbtxnp = cache_new_transaction(id, dn); |
496 |
txn = cache_new_transaction(id, dn); |
497 |
if (dbtxnp == NULL) { |
497 |
if (txn == NULL) { |
498 |
signals_unblock(); |
498 |
signals_unblock(); |
499 |
return 1; |
499 |
return 1; |
500 |
} |
500 |
} |
501 |
#else |
501 |
#else |
502 |
dbtxnp = NULL; |
502 |
if ((rv = mdb_txn_begin(env, NULL, 0, &txn)) != 0) { |
|
|
503 |
cache_error_message(rv, "mdb_txn_begin"); |
504 |
return rv; |
505 |
} |
503 |
#endif |
506 |
#endif |
504 |
|
507 |
|
505 |
if ((rv=dbp->del(dbp, dbtxnp, &key, 0)) != 0 && rv != DB_NOTFOUND) { |
508 |
if ((rv = mdb_del(txn, dbi, &key, 0)) != 0 && rv != MDB_NOTFOUND) { |
506 |
signals_unblock(); |
509 |
signals_unblock(); |
507 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
510 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
508 |
"removing from database failed: %s", dn); |
511 |
"removing from database failed: %s", dn); |
509 |
dbp->err(dbp, rv, "del"); |
512 |
cache_error_message(rv, "mdb_del"); |
|
|
513 |
mdb_txn_abort(txn); |
514 |
return rv; |
510 |
} |
515 |
} |
511 |
|
516 |
|
512 |
#ifdef WITH_DB42 |
517 |
if ((rv = mdb_txn_commit(txn)) != 0) { |
513 |
dbtxnp->commit(dbtxnp, 0); |
518 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
514 |
#endif |
519 |
"storing master entry in database failed"); |
515 |
cache_sync(); |
520 |
cache_error_message(rv, "mdb_txn_commit"); |
|
|
521 |
return rv; |
522 |
} |
516 |
signals_unblock(); |
523 |
signals_unblock(); |
517 |
|
524 |
|
518 |
return rv; |
525 |
return rv; |
|
551 |
|
558 |
|
552 |
int cache_get_entry(char *dn, CacheEntry *entry) |
559 |
int cache_get_entry(char *dn, CacheEntry *entry) |
553 |
{ |
560 |
{ |
554 |
DBT key, data; |
561 |
MDB_txn *txn; |
|
|
562 |
MDB_val key, data; |
555 |
int rv = 0; |
563 |
int rv = 0; |
556 |
|
564 |
|
557 |
memset(&key, 0, sizeof(DBT)); |
565 |
memset(&key, 0, sizeof(MDB_val)); |
558 |
memset(&data, 0, sizeof(DBT)); |
566 |
memset(&data, 0, sizeof(MDB_val)); |
559 |
memset(entry, 0, sizeof(CacheEntry)); |
567 |
memset(entry, 0, sizeof(CacheEntry)); |
560 |
|
568 |
|
561 |
key.data=dn; |
569 |
key.mv_data=dn; |
562 |
key.size=strlen(dn)+1; |
570 |
key.mv_size=strlen(dn)+1; |
563 |
data.flags = DB_DBT_REALLOC; |
|
|
564 |
|
571 |
|
|
|
572 |
if ((rv = mdb_txn_begin(env, NULL, 0, &txn)) != 0) { |
573 |
cache_error_message(rv, "mdb_txn_begin"); |
574 |
return rv; |
575 |
} |
565 |
signals_block(); |
576 |
signals_block(); |
566 |
rv=dbp->get(dbp, NULL, &key, &data, 0); |
577 |
rv = mdb_get(txn, dbi, &key, &data); |
567 |
signals_unblock(); |
578 |
signals_unblock(); |
|
|
579 |
mdb_txn_abort(txn); |
568 |
|
580 |
|
569 |
if (rv != 0 && rv != DB_NOTFOUND) { |
581 |
if (rv != 0 && rv != MDB_NOTFOUND) { |
570 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
582 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
571 |
"reading %s from database failed", dn); |
583 |
"reading %s from database failed", dn); |
572 |
dbp->err(dbp, rv, "get"); |
584 |
cache_error_message(rv, "mdb_get"); |
573 |
return rv; |
585 |
return rv; |
574 |
} else if (rv == DB_NOTFOUND) { |
586 |
} else if (rv == MDB_NOTFOUND) { |
575 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "no cache entry found for %s", |
587 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "no cache entry found for %s", |
576 |
dn); |
588 |
dn); |
577 |
return rv; |
589 |
return rv; |
578 |
} |
590 |
} |
579 |
|
591 |
|
580 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "got %d bytes for %s", |
592 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "got %zu bytes for %s", |
581 |
data.size, dn); |
593 |
data.mv_size, dn); |
582 |
|
594 |
|
583 |
if ((rv=parse_entry(data.data, data.size, entry)) != 0) { |
595 |
if ((rv=parse_entry(data.mv_data, data.mv_size, entry)) != 0) { |
584 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
596 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
585 |
"parsing entry failed"); |
597 |
"parsing entry failed"); |
586 |
free(data.data); |
|
|
587 |
exit(1); |
598 |
exit(1); |
588 |
} |
599 |
} |
589 |
|
600 |
|
590 |
free(data.data); |
|
|
591 |
return rv; |
601 |
return rv; |
592 |
} |
602 |
} |
593 |
|
603 |
|
|
604 |
} |
614 |
} |
605 |
|
615 |
|
606 |
rv = cache_get_entry(lower_dn, entry); |
616 |
rv = cache_get_entry(lower_dn, entry); |
607 |
if (rv == DB_NOTFOUND && mixedcase ) { |
617 |
if (rv == MDB_NOTFOUND && mixedcase ) { |
608 |
// try again with original dn |
618 |
// try again with original dn |
609 |
rv = cache_get_entry(dn, entry); |
619 |
rv = cache_get_entry(dn, entry); |
610 |
} |
620 |
} |
|
613 |
return rv; |
623 |
return rv; |
614 |
} |
624 |
} |
615 |
|
625 |
|
616 |
int cache_first_entry(DBC **cur, char **dn, CacheEntry *entry) |
626 |
int cache_first_entry(MDB_cursor *cursor, char **dn, CacheEntry *entry) |
617 |
{ |
627 |
{ |
|
|
628 |
MDB_txn *txn; |
618 |
int rv; |
629 |
int rv; |
619 |
|
630 |
|
620 |
if ((rv=dbp->cursor(dbp, NULL, cur, 0)) != 0) { |
631 |
if ((rv = mdb_txn_begin(env, NULL, 0, &txn)) != 0) { |
621 |
dbp->err(dbp, rv, "cursor"); |
632 |
cache_error_message(rv, "mdb_txn_begin"); |
622 |
return rv; |
633 |
return rv; |
623 |
} |
634 |
} |
|
|
635 |
if ((rv = mdb_cursor_open(txn, dbi, &cursor)) != 0) { |
636 |
cache_error_message(rv, "mdb_cursor_open"); |
637 |
mdb_txn_abort(txn); |
638 |
return rv; |
639 |
} |
624 |
|
640 |
|
625 |
return cache_next_entry(cur, dn, entry); |
641 |
return cache_next_entry(cursor, dn, entry); |
626 |
} |
642 |
} |
627 |
|
643 |
|
628 |
int cache_print_entries(char *dn) |
644 |
int cache_print_entries(char *dn) |
629 |
{ |
645 |
{ |
630 |
DBT key, data; |
646 |
MDB_txn *txn; |
631 |
DBC *cur; |
647 |
MDB_val key, data; |
632 |
memset(&key, 0, sizeof(DBT)); |
648 |
MDB_cursor *cursor; |
633 |
memset(&data, 0, sizeof(DBT)); |
649 |
int rv = 0; |
634 |
key.data = strdup(dn); |
|
|
635 |
key.size = strlen(dn)+1; |
636 |
key.flags = DB_DBT_REALLOC; |
637 |
data.flags = DB_DBT_REALLOC; |
638 |
|
650 |
|
639 |
dbp->cursor(dbp, NULL, &cur, 0); |
651 |
memset(&key, 0, sizeof(MDB_val)); |
640 |
cur->c_get(cur, &key, &data, DB_FIRST); |
652 |
memset(&data, 0, sizeof(MDB_val)); |
|
|
653 |
key.mv_data = strdup(dn); |
654 |
key.mv_size = strlen(dn)+1; |
655 |
|
656 |
if ((rv = mdb_txn_begin(env, NULL, 0, &txn)) != 0) { |
657 |
cache_error_message(rv, "mdb_txn_begin"); |
658 |
return rv; |
659 |
} |
660 |
if ((rv = mdb_cursor_open(txn, dbi, &cursor)) != 0) { |
661 |
cache_error_message(rv, "mdb_cursor_open"); |
662 |
mdb_txn_abort(txn); |
663 |
return rv; |
664 |
} |
665 |
rv = mdb_cursor_get(cursor, &key, &data, MDB_FIRST); |
641 |
do { |
666 |
do { |
642 |
printf("%s\n", (char*)key.data); |
667 |
if (rv != 0) { |
643 |
} while (cur->c_get(cur, &key, &data, DB_NEXT) == 0); |
668 |
cache_error_message(rv, "mdb_cursor_get"); |
|
|
669 |
mdb_txn_abort(txn); |
670 |
return rv; |
671 |
} |
672 |
printf("%s\n", (char*)key.mv_data); |
673 |
} while ((rv = mdb_cursor_get(cursor, &key, &data, MDB_NEXT))); |
644 |
|
674 |
|
645 |
cur->c_close(cur); |
675 |
mdb_cursor_close(cursor); |
646 |
free(key.data); |
676 |
mdb_txn_abort(txn); |
647 |
free(data.data); |
|
|
648 |
return 0; |
677 |
return 0; |
649 |
} |
678 |
} |
650 |
|
679 |
|
651 |
int cache_next_entry(DBC **cur, char **dn, CacheEntry *entry) |
680 |
int cache_next_entry(MDB_cursor *cursor, char **dn, CacheEntry *entry) |
652 |
{ |
681 |
{ |
653 |
DBT key, data; |
682 |
MDB_val key, data; |
654 |
int rv; |
683 |
int rv; |
655 |
|
684 |
|
656 |
memset(&key, 0, sizeof(DBT)); |
685 |
memset(&key, 0, sizeof(MDB_val)); |
657 |
key.flags = DB_DBT_REALLOC; |
686 |
memset(&data, 0, sizeof(MDB_val)); |
658 |
memset(&data, 0, sizeof(DBT)); |
|
|
659 |
data.flags = DB_DBT_REALLOC; |
660 |
|
687 |
|
661 |
if ((rv=(*cur)->c_get(*cur, &key, &data, DB_NEXT)) == DB_NOTFOUND) { |
688 |
if ((rv=mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == MDB_NOTFOUND) { |
662 |
return rv; |
689 |
return rv; |
663 |
} else if (rv != 0) { |
690 |
} else if (rv != 0) { |
664 |
dbp->err(dbp, rv, "c_get"); |
691 |
cache_error_message(rv, "mdb_cursor_get"); |
665 |
return rv; |
692 |
return rv; |
666 |
} |
693 |
} |
667 |
|
694 |
|
668 |
/* skip master entry */ |
695 |
/* skip master entry */ |
669 |
if (strcmp(key.data, MASTER_KEY) == 0) { |
696 |
if (strcmp(key.mv_data, MASTER_KEY) == 0) { |
670 |
free(key.data); |
697 |
return cache_next_entry(cursor, dn, entry); |
671 |
free(data.data); |
|
|
672 |
return cache_next_entry(cur, dn, entry); |
673 |
} |
698 |
} |
674 |
|
699 |
|
675 |
if (*dn) |
700 |
if (*dn) |
676 |
free(*dn); |
701 |
free(*dn); |
677 |
*dn = strdup(key.data); |
702 |
*dn = strdup(key.mv_data); |
678 |
|
703 |
|
679 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "got %d bytes", data.size); |
704 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "got %zu bytes", data.mv_size); |
680 |
|
705 |
|
681 |
if ((rv=parse_entry(data.data, data.size, entry)) != 0) { |
706 |
if ((rv=parse_entry(data.mv_data, data.mv_size, entry)) != 0) { |
682 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
707 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
683 |
"parsing entry failed: %s", *dn); |
708 |
"parsing entry failed: %s", *dn); |
684 |
printf("%d\n", data.size); |
709 |
printf("%zu\n", data.mv_size); |
685 |
free(key.data); |
|
|
686 |
free(data.data); |
687 |
return rv; |
710 |
return rv; |
688 |
} |
711 |
} |
689 |
|
712 |
|
690 |
free(key.data); |
|
|
691 |
free(data.data); |
692 |
|
693 |
return 0; |
713 |
return 0; |
694 |
} |
714 |
} |
695 |
|
715 |
|
696 |
int cache_free_cursor(DBC *cur) |
716 |
void cache_free_cursor(MDB_cursor *cursor) |
697 |
{ |
717 |
{ |
698 |
return cur->c_close(cur); |
718 |
mdb_cursor_close(cursor); |
|
|
719 |
mdb_txn_commit(mdb_cursor_txn(cursor)); |
699 |
} |
720 |
} |
700 |
|
721 |
|
701 |
int cache_close(void) |
722 |
void cache_close(void) |
702 |
{ |
723 |
{ |
703 |
int rv; |
724 |
mdb_close(env, dbi); |
|
|
725 |
mdb_env_close(env); |
704 |
|
726 |
|
705 |
if (dbp && (rv = dbp->close(dbp, 0)) != 0) { |
|
|
706 |
dbp->err(dbp, rv, "close"); |
707 |
} |
708 |
dbp = NULL; |
709 |
#ifdef WITH_DB42 |
710 |
if ((rv = dbenvp->close(dbenvp, 0)) != 0) { |
711 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, |
712 |
"closing database environment failed"); |
713 |
} |
714 |
#endif |
715 |
if (lock_fp != NULL) { |
727 |
if (lock_fp != NULL) { |
716 |
fclose(lock_fp); |
728 |
fclose(lock_fp); |
717 |
lock_fp = NULL; |
729 |
lock_fp = NULL; |
718 |
} |
730 |
} |
719 |
return rv; |
|
|
720 |
} |
731 |
} |