| 1 |
/* monitor.c - monitor mdb backend */ |
| 2 |
/* $OpenLDAP$ */ |
| 3 |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
| 4 |
* |
| 5 |
* Copyright 2000-2024 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 |
|
| 17 |
#include "portable.h" |
| 18 |
|
| 19 |
#include <stdio.h> |
| 20 |
#include <ac/string.h> |
| 21 |
#include <ac/unistd.h> |
| 22 |
#include <ac/stdlib.h> |
| 23 |
#include <ac/errno.h> |
| 24 |
#include <sys/stat.h> |
| 25 |
#include "lutil.h" |
| 26 |
#include "back-mdb.h" |
| 27 |
|
| 28 |
#include "../back-monitor/back-monitor.h" |
| 29 |
|
| 30 |
#include "slap-config.h" |
| 31 |
|
| 32 |
static ObjectClass *oc_olmMDBDatabase; |
| 33 |
|
| 34 |
static AttributeDescription *ad_olmDbDirectory; |
| 35 |
|
| 36 |
#ifdef MDB_MONITOR_IDX |
| 37 |
static int |
| 38 |
mdb_monitor_idx_entry_add( |
| 39 |
struct mdb_info *mdb, |
| 40 |
Entry *e ); |
| 41 |
|
| 42 |
static AttributeDescription *ad_olmDbNotIndexed; |
| 43 |
#endif /* MDB_MONITOR_IDX */ |
| 44 |
|
| 45 |
static AttributeDescription *ad_olmMDBPagesMax, |
| 46 |
*ad_olmMDBPagesUsed, *ad_olmMDBPagesFree; |
| 47 |
|
| 48 |
static AttributeDescription *ad_olmMDBReadersMax, |
| 49 |
*ad_olmMDBReadersUsed; |
| 50 |
|
| 51 |
static AttributeDescription *ad_olmMDBEntries; |
| 52 |
|
| 53 |
/* |
| 54 |
* NOTE: there's some confusion in monitor OID arc; |
| 55 |
* by now, let's consider: |
| 56 |
* |
| 57 |
* Subsystems monitor attributes 1.3.6.1.4.1.4203.666.1.55.0 |
| 58 |
* Databases monitor attributes 1.3.6.1.4.1.4203.666.1.55.0.1 |
| 59 |
* MDB database monitor attributes 1.3.6.1.4.1.4203.666.1.55.0.1.3 |
| 60 |
* |
| 61 |
* Subsystems monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0 |
| 62 |
* Databases monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0.1 |
| 63 |
* MDB database monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0.1.3 |
| 64 |
*/ |
| 65 |
|
| 66 |
static struct { |
| 67 |
char *name; |
| 68 |
char *oid; |
| 69 |
} s_oid[] = { |
| 70 |
{ "olmMDBAttributes", "olmDatabaseAttributes:1" }, |
| 71 |
{ "olmMDBObjectClasses", "olmDatabaseObjectClasses:1" }, |
| 72 |
|
| 73 |
{ NULL } |
| 74 |
}; |
| 75 |
|
| 76 |
static struct { |
| 77 |
char *desc; |
| 78 |
AttributeDescription **ad; |
| 79 |
} s_at[] = { |
| 80 |
{ "( olmDatabaseAttributes:1 " |
| 81 |
"NAME ( 'olmDbDirectory' ) " |
| 82 |
"DESC 'Path name of the directory " |
| 83 |
"where the database environment resides' " |
| 84 |
"SUP monitoredInfo " |
| 85 |
"NO-USER-MODIFICATION " |
| 86 |
"USAGE dSAOperation )", |
| 87 |
&ad_olmDbDirectory }, |
| 88 |
|
| 89 |
#ifdef MDB_MONITOR_IDX |
| 90 |
{ "( olmDatabaseAttributes:2 " |
| 91 |
"NAME ( 'olmDbNotIndexed' ) " |
| 92 |
"DESC 'Missing indexes resulting from candidate selection' " |
| 93 |
"SUP monitoredInfo " |
| 94 |
"NO-USER-MODIFICATION " |
| 95 |
"USAGE dSAOperation )", |
| 96 |
&ad_olmDbNotIndexed }, |
| 97 |
#endif /* MDB_MONITOR_IDX */ |
| 98 |
|
| 99 |
{ "( olmMDBAttributes:1 " |
| 100 |
"NAME ( 'olmMDBPagesMax' ) " |
| 101 |
"DESC 'Maximum number of pages' " |
| 102 |
"SUP monitorCounter " |
| 103 |
"NO-USER-MODIFICATION " |
| 104 |
"USAGE dSAOperation )", |
| 105 |
&ad_olmMDBPagesMax }, |
| 106 |
|
| 107 |
{ "( olmMDBAttributes:2 " |
| 108 |
"NAME ( 'olmMDBPagesUsed' ) " |
| 109 |
"DESC 'Number of pages in use' " |
| 110 |
"SUP monitorCounter " |
| 111 |
"NO-USER-MODIFICATION " |
| 112 |
"USAGE dSAOperation )", |
| 113 |
&ad_olmMDBPagesUsed }, |
| 114 |
|
| 115 |
{ "( olmMDBAttributes:3 " |
| 116 |
"NAME ( 'olmMDBPagesFree' ) " |
| 117 |
"DESC 'Number of free pages' " |
| 118 |
"SUP monitorCounter " |
| 119 |
"NO-USER-MODIFICATION " |
| 120 |
"USAGE dSAOperation )", |
| 121 |
&ad_olmMDBPagesFree }, |
| 122 |
|
| 123 |
{ "( olmMDBAttributes:4 " |
| 124 |
"NAME ( 'olmMDBReadersMax' ) " |
| 125 |
"DESC 'Maximum number of readers' " |
| 126 |
"SUP monitorCounter " |
| 127 |
"NO-USER-MODIFICATION " |
| 128 |
"USAGE dSAOperation )", |
| 129 |
&ad_olmMDBReadersMax }, |
| 130 |
|
| 131 |
{ "( olmMDBAttributes:5 " |
| 132 |
"NAME ( 'olmMDBReadersUsed' ) " |
| 133 |
"DESC 'Number of readers in use' " |
| 134 |
"SUP monitorCounter " |
| 135 |
"NO-USER-MODIFICATION " |
| 136 |
"USAGE dSAOperation )", |
| 137 |
&ad_olmMDBReadersUsed }, |
| 138 |
|
| 139 |
{ "( olmMDBAttributes:6 " |
| 140 |
"NAME ( 'olmMDBEntries' ) " |
| 141 |
"DESC 'Number of entries in DB' " |
| 142 |
"SUP monitorCounter " |
| 143 |
"NO-USER-MODIFICATION " |
| 144 |
"USAGE dSAOperation )", |
| 145 |
&ad_olmMDBEntries }, |
| 146 |
{ NULL } |
| 147 |
}; |
| 148 |
|
| 149 |
static struct { |
| 150 |
char *desc; |
| 151 |
ObjectClass **oc; |
| 152 |
} s_oc[] = { |
| 153 |
/* augments an existing object, so it must be AUXILIARY |
| 154 |
* FIXME: derive from some ABSTRACT "monitoredEntity"? */ |
| 155 |
{ "( olmMDBObjectClasses:2 " |
| 156 |
"NAME ( 'olmMDBDatabase' ) " |
| 157 |
"SUP top AUXILIARY " |
| 158 |
"MAY ( " |
| 159 |
"olmDbDirectory " |
| 160 |
#ifdef MDB_MONITOR_IDX |
| 161 |
"$ olmDbNotIndexed " |
| 162 |
#endif /* MDB_MONITOR_IDX */ |
| 163 |
"$ olmMDBPagesMax $ olmMDBPagesUsed $ olmMDBPagesFree " |
| 164 |
"$ olmMDBReadersMax $ olmMDBReadersUsed $ olmMDBEntries " |
| 165 |
") )", |
| 166 |
&oc_olmMDBDatabase }, |
| 167 |
|
| 168 |
{ NULL } |
| 169 |
}; |
| 170 |
|
| 171 |
static int |
| 172 |
mdb_monitor_update( |
| 173 |
Operation *op, |
| 174 |
SlapReply *rs, |
| 175 |
Entry *e, |
| 176 |
void *priv ) |
| 177 |
{ |
| 178 |
struct mdb_info *mdb = (struct mdb_info *) priv; |
| 179 |
Attribute *a; |
| 180 |
char buf[ BUFSIZ ]; |
| 181 |
struct berval bv; |
| 182 |
MDB_stat mst; |
| 183 |
MDB_envinfo mei; |
| 184 |
MDB_txn *txn; |
| 185 |
int rc; |
| 186 |
|
| 187 |
#ifdef MDB_MONITOR_IDX |
| 188 |
|
| 189 |
mdb_monitor_idx_entry_add( mdb, e ); |
| 190 |
#endif /* MDB_MONITOR_IDX */ |
| 191 |
|
| 192 |
mdb_env_stat( mdb->mi_dbenv, &mst ); |
| 193 |
mdb_env_info( mdb->mi_dbenv, &mei ); |
| 194 |
|
| 195 |
a = attr_find( e->e_attrs, ad_olmMDBPagesMax ); |
| 196 |
assert( a != NULL ); |
| 197 |
bv.bv_val = buf; |
| 198 |
bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", mei.me_mapsize / mst.ms_psize ); |
| 199 |
ber_bvreplace( &a->a_vals[ 0 ], &bv ); |
| 200 |
|
| 201 |
a = attr_find( e->e_attrs, ad_olmMDBPagesUsed ); |
| 202 |
assert( a != NULL ); |
| 203 |
bv.bv_val = buf; |
| 204 |
bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", mei.me_last_pgno+1 ); |
| 205 |
ber_bvreplace( &a->a_vals[ 0 ], &bv ); |
| 206 |
|
| 207 |
a = attr_find( e->e_attrs, ad_olmMDBReadersMax ); |
| 208 |
assert( a != NULL ); |
| 209 |
bv.bv_val = buf; |
| 210 |
bv.bv_len = snprintf( buf, sizeof( buf ), "%u", mei.me_maxreaders ); |
| 211 |
ber_bvreplace( &a->a_vals[ 0 ], &bv ); |
| 212 |
|
| 213 |
a = attr_find( e->e_attrs, ad_olmMDBReadersUsed ); |
| 214 |
assert( a != NULL ); |
| 215 |
bv.bv_val = buf; |
| 216 |
bv.bv_len = snprintf( buf, sizeof( buf ), "%u", mei.me_numreaders ); |
| 217 |
ber_bvreplace( &a->a_vals[ 0 ], &bv ); |
| 218 |
|
| 219 |
rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &txn ); |
| 220 |
if ( !rc ) { |
| 221 |
MDB_cursor *cursor; |
| 222 |
MDB_val key, data; |
| 223 |
size_t pages = 0, *iptr; |
| 224 |
|
| 225 |
rc = mdb_cursor_open( txn, 0, &cursor ); |
| 226 |
if ( !rc ) { |
| 227 |
while (( rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT )) == 0 ) { |
| 228 |
iptr = data.mv_data; |
| 229 |
pages += *iptr; |
| 230 |
} |
| 231 |
mdb_cursor_close( cursor ); |
| 232 |
} |
| 233 |
|
| 234 |
mdb_stat( txn, mdb->mi_id2entry, &mst ); |
| 235 |
a = attr_find( e->e_attrs, ad_olmMDBEntries ); |
| 236 |
assert( a != NULL ); |
| 237 |
bv.bv_val = buf; |
| 238 |
bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", mst.ms_entries ); |
| 239 |
ber_bvreplace( &a->a_vals[ 0 ], &bv ); |
| 240 |
|
| 241 |
mdb_txn_abort( txn ); |
| 242 |
|
| 243 |
a = attr_find( e->e_attrs, ad_olmMDBPagesFree ); |
| 244 |
assert( a != NULL ); |
| 245 |
bv.bv_val = buf; |
| 246 |
bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", pages ); |
| 247 |
ber_bvreplace( &a->a_vals[ 0 ], &bv ); |
| 248 |
} |
| 249 |
return SLAP_CB_CONTINUE; |
| 250 |
} |
| 251 |
|
| 252 |
#if 0 /* uncomment if required */ |
| 253 |
static int |
| 254 |
mdb_monitor_modify( |
| 255 |
Operation *op, |
| 256 |
SlapReply *rs, |
| 257 |
Entry *e, |
| 258 |
void *priv ) |
| 259 |
{ |
| 260 |
return SLAP_CB_CONTINUE; |
| 261 |
} |
| 262 |
#endif |
| 263 |
|
| 264 |
static int |
| 265 |
mdb_monitor_free( |
| 266 |
Entry *e, |
| 267 |
void **priv ) |
| 268 |
{ |
| 269 |
struct berval values[ 2 ]; |
| 270 |
Modification mod = { 0 }; |
| 271 |
|
| 272 |
const char *text; |
| 273 |
char textbuf[ SLAP_TEXT_BUFLEN ]; |
| 274 |
|
| 275 |
int i, rc; |
| 276 |
|
| 277 |
/* NOTE: if slap_shutdown != 0, priv might have already been freed */ |
| 278 |
*priv = NULL; |
| 279 |
|
| 280 |
/* Remove objectClass */ |
| 281 |
mod.sm_op = LDAP_MOD_DELETE; |
| 282 |
mod.sm_desc = slap_schema.si_ad_objectClass; |
| 283 |
mod.sm_values = values; |
| 284 |
mod.sm_numvals = 1; |
| 285 |
values[ 0 ] = oc_olmMDBDatabase->soc_cname; |
| 286 |
BER_BVZERO( &values[ 1 ] ); |
| 287 |
|
| 288 |
rc = modify_delete_values( e, &mod, 1, &text, |
| 289 |
textbuf, sizeof( textbuf ) ); |
| 290 |
/* don't care too much about return code... */ |
| 291 |
|
| 292 |
/* remove attrs */ |
| 293 |
mod.sm_values = NULL; |
| 294 |
mod.sm_numvals = 0; |
| 295 |
for ( i = 0; s_at[ i ].desc != NULL; i++ ) { |
| 296 |
mod.sm_desc = *s_at[ i ].ad; |
| 297 |
rc = modify_delete_values( e, &mod, 1, &text, |
| 298 |
textbuf, sizeof( textbuf ) ); |
| 299 |
/* don't care too much about return code... */ |
| 300 |
} |
| 301 |
|
| 302 |
return SLAP_CB_CONTINUE; |
| 303 |
} |
| 304 |
|
| 305 |
/* |
| 306 |
* call from within mdb_initialize() |
| 307 |
*/ |
| 308 |
static int |
| 309 |
mdb_monitor_initialize( void ) |
| 310 |
{ |
| 311 |
int i, code; |
| 312 |
ConfigArgs c; |
| 313 |
char *argv[ 3 ]; |
| 314 |
|
| 315 |
static int mdb_monitor_initialized = 0; |
| 316 |
|
| 317 |
/* set to 0 when successfully initialized; otherwise, remember failure */ |
| 318 |
static int mdb_monitor_initialized_failure = 1; |
| 319 |
|
| 320 |
if ( mdb_monitor_initialized++ ) { |
| 321 |
return mdb_monitor_initialized_failure; |
| 322 |
} |
| 323 |
|
| 324 |
if ( backend_info( "monitor" ) == NULL ) { |
| 325 |
return -1; |
| 326 |
} |
| 327 |
|
| 328 |
/* register schema here */ |
| 329 |
|
| 330 |
argv[ 0 ] = "back-mdb monitor"; |
| 331 |
c.argv = argv; |
| 332 |
c.argc = 3; |
| 333 |
c.fname = argv[0]; |
| 334 |
|
| 335 |
for ( i = 0; s_oid[ i ].name; i++ ) { |
| 336 |
c.lineno = i; |
| 337 |
argv[ 1 ] = s_oid[ i ].name; |
| 338 |
argv[ 2 ] = s_oid[ i ].oid; |
| 339 |
|
| 340 |
if ( parse_oidm( &c, 0, NULL ) != 0 ) { |
| 341 |
Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(mdb_monitor_initialize) |
| 342 |
": unable to add " |
| 343 |
"objectIdentifier \"%s=%s\"\n", |
| 344 |
s_oid[ i ].name, s_oid[ i ].oid ); |
| 345 |
return 2; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
for ( i = 0; s_at[ i ].desc != NULL; i++ ) { |
| 350 |
code = register_at( s_at[ i ].desc, s_at[ i ].ad, 1 ); |
| 351 |
if ( code != LDAP_SUCCESS ) { |
| 352 |
Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(mdb_monitor_initialize) |
| 353 |
": register_at failed for attributeType (%s)\n", |
| 354 |
s_at[ i ].desc ); |
| 355 |
return 3; |
| 356 |
|
| 357 |
} else { |
| 358 |
(*s_at[ i ].ad)->ad_type->sat_flags |= SLAP_AT_HIDE; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
for ( i = 0; s_oc[ i ].desc != NULL; i++ ) { |
| 363 |
code = register_oc( s_oc[ i ].desc, s_oc[ i ].oc, 1 ); |
| 364 |
if ( code != LDAP_SUCCESS ) { |
| 365 |
Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(mdb_monitor_initialize) |
| 366 |
": register_oc failed for objectClass (%s)\n", |
| 367 |
s_oc[ i ].desc ); |
| 368 |
return 4; |
| 369 |
|
| 370 |
} else { |
| 371 |
(*s_oc[ i ].oc)->soc_flags |= SLAP_OC_HIDE; |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
return ( mdb_monitor_initialized_failure = LDAP_SUCCESS ); |
| 376 |
} |
| 377 |
|
| 378 |
/* |
| 379 |
* call from within mdb_db_init() |
| 380 |
*/ |
| 381 |
int |
| 382 |
mdb_monitor_db_init( BackendDB *be ) |
| 383 |
{ |
| 384 |
#ifdef MDB_MONITOR_IDX |
| 385 |
struct mdb_info *mdb = (struct mdb_info *) be->be_private; |
| 386 |
#endif /* MDB_MONITOR_IDX */ |
| 387 |
|
| 388 |
if ( mdb_monitor_initialize() == LDAP_SUCCESS ) { |
| 389 |
/* monitoring in back-mdb is on by default */ |
| 390 |
SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING; |
| 391 |
} |
| 392 |
|
| 393 |
#ifdef MDB_MONITOR_IDX |
| 394 |
mdb->mi_idx = NULL; |
| 395 |
ldap_pvt_thread_mutex_init( &mdb->mi_idx_mutex ); |
| 396 |
#endif /* MDB_MONITOR_IDX */ |
| 397 |
|
| 398 |
return 0; |
| 399 |
} |
| 400 |
|
| 401 |
/* |
| 402 |
* call from within mdb_db_open() |
| 403 |
*/ |
| 404 |
int |
| 405 |
mdb_monitor_db_open( BackendDB *be ) |
| 406 |
{ |
| 407 |
struct mdb_info *mdb = (struct mdb_info *) be->be_private; |
| 408 |
Attribute *a, *next; |
| 409 |
monitor_callback_t *cb = NULL; |
| 410 |
int rc = 0; |
| 411 |
BackendInfo *mi; |
| 412 |
monitor_extra_t *mbe; |
| 413 |
|
| 414 |
if ( !SLAP_DBMONITORING( be ) ) { |
| 415 |
return 0; |
| 416 |
} |
| 417 |
|
| 418 |
mi = backend_info( "monitor" ); |
| 419 |
if ( !mi || !mi->bi_extra ) { |
| 420 |
SLAP_DBFLAGS( be ) ^= SLAP_DBFLAG_MONITORING; |
| 421 |
return 0; |
| 422 |
} |
| 423 |
mbe = mi->bi_extra; |
| 424 |
|
| 425 |
/* don't bother if monitor is not configured */ |
| 426 |
if ( !mbe->is_configured() ) { |
| 427 |
static int warning = 0; |
| 428 |
|
| 429 |
if ( warning++ == 0 ) { |
| 430 |
Debug( LDAP_DEBUG_CONFIG, LDAP_XSTRING(mdb_monitor_db_open) |
| 431 |
": monitoring disabled; " |
| 432 |
"configure monitor database to enable\n" ); |
| 433 |
} |
| 434 |
|
| 435 |
return 0; |
| 436 |
} |
| 437 |
|
| 438 |
/* alloc as many as required (plus 1 for objectClass) */ |
| 439 |
a = attrs_alloc( 1 + 7 ); |
| 440 |
if ( a == NULL ) { |
| 441 |
rc = 1; |
| 442 |
goto cleanup; |
| 443 |
} |
| 444 |
|
| 445 |
a->a_desc = slap_schema.si_ad_objectClass; |
| 446 |
attr_valadd( a, &oc_olmMDBDatabase->soc_cname, NULL, 1 ); |
| 447 |
next = a->a_next; |
| 448 |
|
| 449 |
{ |
| 450 |
struct berval bv = BER_BVC( "0" ); |
| 451 |
|
| 452 |
next->a_desc = ad_olmMDBPagesMax; |
| 453 |
attr_valadd( next, &bv, NULL, 1 ); |
| 454 |
next = next->a_next; |
| 455 |
|
| 456 |
next->a_desc = ad_olmMDBPagesUsed; |
| 457 |
attr_valadd( next, &bv, NULL, 1 ); |
| 458 |
next = next->a_next; |
| 459 |
|
| 460 |
next->a_desc = ad_olmMDBPagesFree; |
| 461 |
attr_valadd( next, &bv, NULL, 1 ); |
| 462 |
next = next->a_next; |
| 463 |
|
| 464 |
next->a_desc = ad_olmMDBReadersMax; |
| 465 |
attr_valadd( next, &bv, NULL, 1 ); |
| 466 |
next = next->a_next; |
| 467 |
|
| 468 |
next->a_desc = ad_olmMDBReadersUsed; |
| 469 |
attr_valadd( next, &bv, NULL, 1 ); |
| 470 |
next = next->a_next; |
| 471 |
|
| 472 |
next->a_desc = ad_olmMDBEntries; |
| 473 |
attr_valadd( next, &bv, NULL, 1 ); |
| 474 |
next = next->a_next; |
| 475 |
} |
| 476 |
|
| 477 |
{ |
| 478 |
struct berval bv, nbv; |
| 479 |
ber_len_t pathlen = 0, len = 0; |
| 480 |
char path[ MAXPATHLEN ] = { '\0' }; |
| 481 |
char *fname = mdb->mi_dbenv_home, |
| 482 |
*ptr; |
| 483 |
|
| 484 |
len = strlen( fname ); |
| 485 |
if ( fname[ 0 ] != '/' ) { |
| 486 |
/* get full path name */ |
| 487 |
getcwd( path, sizeof( path ) ); |
| 488 |
pathlen = strlen( path ); |
| 489 |
|
| 490 |
if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) { |
| 491 |
fname += 2; |
| 492 |
len -= 2; |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
bv.bv_len = pathlen + STRLENOF( "/" ) + len; |
| 497 |
ptr = bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 ); |
| 498 |
if ( pathlen ) { |
| 499 |
ptr = lutil_strncopy( ptr, path, pathlen ); |
| 500 |
ptr[ 0 ] = '/'; |
| 501 |
ptr++; |
| 502 |
} |
| 503 |
ptr = lutil_strncopy( ptr, fname, len ); |
| 504 |
if ( ptr[ -1 ] != '/' ) { |
| 505 |
ptr[ 0 ] = '/'; |
| 506 |
ptr++; |
| 507 |
} |
| 508 |
ptr[ 0 ] = '\0'; |
| 509 |
|
| 510 |
attr_normalize_one( ad_olmDbDirectory, &bv, &nbv, NULL ); |
| 511 |
|
| 512 |
next->a_desc = ad_olmDbDirectory; |
| 513 |
next->a_vals = ch_calloc( sizeof( struct berval ), 2 ); |
| 514 |
next->a_vals[ 0 ] = bv; |
| 515 |
next->a_numvals = 1; |
| 516 |
|
| 517 |
if ( BER_BVISNULL( &nbv ) ) { |
| 518 |
next->a_nvals = next->a_vals; |
| 519 |
|
| 520 |
} else { |
| 521 |
next->a_nvals = ch_calloc( sizeof( struct berval ), 2 ); |
| 522 |
next->a_nvals[ 0 ] = nbv; |
| 523 |
} |
| 524 |
|
| 525 |
next = next->a_next; |
| 526 |
} |
| 527 |
|
| 528 |
cb = ch_calloc( sizeof( monitor_callback_t ), 1 ); |
| 529 |
cb->mc_update = mdb_monitor_update; |
| 530 |
#if 0 /* uncomment if required */ |
| 531 |
cb->mc_modify = mdb_monitor_modify; |
| 532 |
#endif |
| 533 |
cb->mc_free = mdb_monitor_free; |
| 534 |
cb->mc_private = (void *)mdb; |
| 535 |
|
| 536 |
/* make sure the database is registered; then add monitor attributes */ |
| 537 |
rc = mbe->register_database( be, &mdb->mi_monitor.mdm_ndn ); |
| 538 |
if ( rc == 0 ) { |
| 539 |
rc = mbe->register_entry_attrs( &mdb->mi_monitor.mdm_ndn, a, cb, |
| 540 |
NULL, -1, NULL ); |
| 541 |
} |
| 542 |
|
| 543 |
cleanup:; |
| 544 |
if ( rc != 0 ) { |
| 545 |
if ( cb != NULL ) { |
| 546 |
ch_free( cb ); |
| 547 |
cb = NULL; |
| 548 |
} |
| 549 |
|
| 550 |
if ( a != NULL ) { |
| 551 |
attrs_free( a ); |
| 552 |
a = NULL; |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
/* store for cleanup */ |
| 557 |
mdb->mi_monitor.mdm_cb = (void *)cb; |
| 558 |
|
| 559 |
/* we don't need to keep track of the attributes, because |
| 560 |
* mdb_monitor_free() takes care of everything */ |
| 561 |
if ( a != NULL ) { |
| 562 |
attrs_free( a ); |
| 563 |
} |
| 564 |
|
| 565 |
return rc; |
| 566 |
} |
| 567 |
|
| 568 |
/* |
| 569 |
* call from within mdb_db_close() |
| 570 |
*/ |
| 571 |
int |
| 572 |
mdb_monitor_db_close( BackendDB *be ) |
| 573 |
{ |
| 574 |
struct mdb_info *mdb = (struct mdb_info *) be->be_private; |
| 575 |
|
| 576 |
if ( !BER_BVISNULL( &mdb->mi_monitor.mdm_ndn ) ) { |
| 577 |
BackendInfo *mi = backend_info( "monitor" ); |
| 578 |
monitor_extra_t *mbe; |
| 579 |
|
| 580 |
if ( mi && mi->bi_extra ) { |
| 581 |
struct berval dummy = BER_BVNULL; |
| 582 |
mbe = mi->bi_extra; |
| 583 |
mbe->unregister_entry_callback( &mdb->mi_monitor.mdm_ndn, |
| 584 |
(monitor_callback_t *)mdb->mi_monitor.mdm_cb, |
| 585 |
&dummy, 0, &dummy ); |
| 586 |
} |
| 587 |
|
| 588 |
memset( &mdb->mi_monitor, 0, sizeof( mdb->mi_monitor ) ); |
| 589 |
} |
| 590 |
|
| 591 |
return 0; |
| 592 |
} |
| 593 |
|
| 594 |
/* |
| 595 |
* call from within mdb_db_destroy() |
| 596 |
*/ |
| 597 |
int |
| 598 |
mdb_monitor_db_destroy( BackendDB *be ) |
| 599 |
{ |
| 600 |
#ifdef MDB_MONITOR_IDX |
| 601 |
struct mdb_info *mdb = (struct mdb_info *) be->be_private; |
| 602 |
|
| 603 |
/* TODO: free tree */ |
| 604 |
ldap_pvt_thread_mutex_destroy( &mdb->mi_idx_mutex ); |
| 605 |
ldap_avl_free( mdb->mi_idx, ch_free ); |
| 606 |
#endif /* MDB_MONITOR_IDX */ |
| 607 |
|
| 608 |
return 0; |
| 609 |
} |
| 610 |
|
| 611 |
#ifdef MDB_MONITOR_IDX |
| 612 |
|
| 613 |
#define MDB_MONITOR_IDX_TYPES (4) |
| 614 |
|
| 615 |
typedef struct monitor_idx_t monitor_idx_t; |
| 616 |
|
| 617 |
struct monitor_idx_t { |
| 618 |
AttributeDescription *idx_ad; |
| 619 |
unsigned long idx_count[MDB_MONITOR_IDX_TYPES]; |
| 620 |
}; |
| 621 |
|
| 622 |
static int |
| 623 |
mdb_monitor_bitmask2key( slap_mask_t bitmask ) |
| 624 |
{ |
| 625 |
int key; |
| 626 |
|
| 627 |
for ( key = 0; key < 8 * (int)sizeof(slap_mask_t) && !( bitmask & 0x1U ); |
| 628 |
key++ ) |
| 629 |
bitmask >>= 1; |
| 630 |
|
| 631 |
return key; |
| 632 |
} |
| 633 |
|
| 634 |
static struct berval idxbv[] = { |
| 635 |
BER_BVC( "present=" ), |
| 636 |
BER_BVC( "equality=" ), |
| 637 |
BER_BVC( "approx=" ), |
| 638 |
BER_BVC( "substr=" ), |
| 639 |
BER_BVNULL |
| 640 |
}; |
| 641 |
|
| 642 |
static ber_len_t |
| 643 |
mdb_monitor_idx2len( monitor_idx_t *idx ) |
| 644 |
{ |
| 645 |
int i; |
| 646 |
ber_len_t len = 0; |
| 647 |
|
| 648 |
for ( i = 0; i < MDB_MONITOR_IDX_TYPES; i++ ) { |
| 649 |
if ( idx->idx_count[ i ] != 0 ) { |
| 650 |
len += idxbv[i].bv_len; |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
return len; |
| 655 |
} |
| 656 |
|
| 657 |
static int |
| 658 |
monitor_idx_cmp( const void *p1, const void *p2 ) |
| 659 |
{ |
| 660 |
const monitor_idx_t *idx1 = (const monitor_idx_t *)p1; |
| 661 |
const monitor_idx_t *idx2 = (const monitor_idx_t *)p2; |
| 662 |
|
| 663 |
return SLAP_PTRCMP( idx1->idx_ad, idx2->idx_ad ); |
| 664 |
} |
| 665 |
|
| 666 |
static int |
| 667 |
monitor_idx_dup( void *p1, void *p2 ) |
| 668 |
{ |
| 669 |
monitor_idx_t *idx1 = (monitor_idx_t *)p1; |
| 670 |
monitor_idx_t *idx2 = (monitor_idx_t *)p2; |
| 671 |
|
| 672 |
return SLAP_PTRCMP( idx1->idx_ad, idx2->idx_ad ) == 0 ? -1 : 0; |
| 673 |
} |
| 674 |
|
| 675 |
int |
| 676 |
mdb_monitor_idx_add( |
| 677 |
struct mdb_info *mdb, |
| 678 |
AttributeDescription *desc, |
| 679 |
slap_mask_t type ) |
| 680 |
{ |
| 681 |
monitor_idx_t idx_dummy = { 0 }, |
| 682 |
*idx; |
| 683 |
int rc = 0, key; |
| 684 |
|
| 685 |
idx_dummy.idx_ad = desc; |
| 686 |
key = mdb_monitor_bitmask2key( type ) - 1; |
| 687 |
if ( key >= MDB_MONITOR_IDX_TYPES ) { |
| 688 |
/* invalid index type */ |
| 689 |
return -1; |
| 690 |
} |
| 691 |
|
| 692 |
ldap_pvt_thread_mutex_lock( &mdb->mi_idx_mutex ); |
| 693 |
|
| 694 |
idx = (monitor_idx_t *)ldap_avl_find( mdb->mi_idx, |
| 695 |
(caddr_t)&idx_dummy, monitor_idx_cmp ); |
| 696 |
if ( idx == NULL ) { |
| 697 |
idx = (monitor_idx_t *)ch_calloc( sizeof( monitor_idx_t ), 1 ); |
| 698 |
idx->idx_ad = desc; |
| 699 |
idx->idx_count[ key ] = 1; |
| 700 |
|
| 701 |
switch ( ldap_avl_insert( &mdb->mi_idx, (caddr_t)idx, |
| 702 |
monitor_idx_cmp, monitor_idx_dup ) ) |
| 703 |
{ |
| 704 |
case 0: |
| 705 |
break; |
| 706 |
|
| 707 |
default: |
| 708 |
ch_free( idx ); |
| 709 |
rc = -1; |
| 710 |
} |
| 711 |
|
| 712 |
} else { |
| 713 |
idx->idx_count[ key ]++; |
| 714 |
} |
| 715 |
|
| 716 |
ldap_pvt_thread_mutex_unlock( &mdb->mi_idx_mutex ); |
| 717 |
|
| 718 |
return rc; |
| 719 |
} |
| 720 |
|
| 721 |
static int |
| 722 |
mdb_monitor_idx_apply( void *v_idx, void *v_valp ) |
| 723 |
{ |
| 724 |
monitor_idx_t *idx = (monitor_idx_t *)v_idx; |
| 725 |
BerVarray *valp = (BerVarray *)v_valp; |
| 726 |
|
| 727 |
struct berval bv; |
| 728 |
char *ptr; |
| 729 |
char count_buf[ MDB_MONITOR_IDX_TYPES ][ SLAP_TEXT_BUFLEN ]; |
| 730 |
ber_len_t count_len[ MDB_MONITOR_IDX_TYPES ], |
| 731 |
idx_len; |
| 732 |
int i, num = 0; |
| 733 |
|
| 734 |
idx_len = mdb_monitor_idx2len( idx ); |
| 735 |
|
| 736 |
bv.bv_len = 0; |
| 737 |
for ( i = 0; i < MDB_MONITOR_IDX_TYPES; i++ ) { |
| 738 |
if ( idx->idx_count[ i ] == 0 ) { |
| 739 |
continue; |
| 740 |
} |
| 741 |
|
| 742 |
count_len[ i ] = snprintf( count_buf[ i ], |
| 743 |
sizeof( count_buf[ i ] ), "%lu", idx->idx_count[ i ] ); |
| 744 |
bv.bv_len += count_len[ i ]; |
| 745 |
num++; |
| 746 |
} |
| 747 |
|
| 748 |
bv.bv_len += idx->idx_ad->ad_cname.bv_len |
| 749 |
+ num |
| 750 |
+ idx_len; |
| 751 |
ptr = bv.bv_val = ch_malloc( bv.bv_len + 1 ); |
| 752 |
ptr = lutil_strcopy( ptr, idx->idx_ad->ad_cname.bv_val ); |
| 753 |
for ( i = 0; i < MDB_MONITOR_IDX_TYPES; i++ ) { |
| 754 |
if ( idx->idx_count[ i ] == 0 ) { |
| 755 |
continue; |
| 756 |
} |
| 757 |
|
| 758 |
ptr[ 0 ] = '#'; |
| 759 |
++ptr; |
| 760 |
ptr = lutil_strcopy( ptr, idxbv[ i ].bv_val ); |
| 761 |
ptr = lutil_strcopy( ptr, count_buf[ i ] ); |
| 762 |
} |
| 763 |
|
| 764 |
ber_bvarray_add( valp, &bv ); |
| 765 |
|
| 766 |
return 0; |
| 767 |
} |
| 768 |
|
| 769 |
static int |
| 770 |
mdb_monitor_idx_entry_add( |
| 771 |
struct mdb_info *mdb, |
| 772 |
Entry *e ) |
| 773 |
{ |
| 774 |
BerVarray vals = NULL; |
| 775 |
Attribute *a; |
| 776 |
|
| 777 |
a = attr_find( e->e_attrs, ad_olmDbNotIndexed ); |
| 778 |
|
| 779 |
ldap_pvt_thread_mutex_lock( &mdb->mi_idx_mutex ); |
| 780 |
|
| 781 |
ldap_avl_apply( mdb->mi_idx, mdb_monitor_idx_apply, |
| 782 |
&vals, -1, AVL_INORDER ); |
| 783 |
|
| 784 |
ldap_pvt_thread_mutex_unlock( &mdb->mi_idx_mutex ); |
| 785 |
|
| 786 |
if ( vals != NULL ) { |
| 787 |
if ( a != NULL ) { |
| 788 |
assert( a->a_nvals == a->a_vals ); |
| 789 |
|
| 790 |
ber_bvarray_free( a->a_vals ); |
| 791 |
|
| 792 |
} else { |
| 793 |
Attribute **ap; |
| 794 |
|
| 795 |
for ( ap = &e->e_attrs; *ap != NULL; ap = &(*ap)->a_next ) |
| 796 |
; |
| 797 |
*ap = attr_alloc( ad_olmDbNotIndexed ); |
| 798 |
a = *ap; |
| 799 |
} |
| 800 |
a->a_vals = vals; |
| 801 |
a->a_nvals = a->a_vals; |
| 802 |
} |
| 803 |
|
| 804 |
return 0; |
| 805 |
} |
| 806 |
|
| 807 |
#endif /* MDB_MONITOR_IDX */ |