Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / back-bdb / monitor.c
blobaa94780742824b84fea749a949c2f1093fe7a1da
1 /* monitor.c - monitor bdb backend */
2 /* $OpenLDAP: pkg/ldap/servers/slapd/back-bdb/monitor.c,v 1.19.2.9 2008/05/26 18:57:01 ando Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2008 The OpenLDAP Foundation.
6 * All rights reserved.
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.
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>.
17 #include "portable.h"
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-bdb.h"
28 #include "../back-monitor/back-monitor.h"
30 #include "config.h"
32 static ObjectClass *oc_olmBDBDatabase;
34 static AttributeDescription *ad_olmBDBEntryCache,
35 *ad_olmBDBDNCache, *ad_olmBDBIDLCache,
36 *ad_olmDbDirectory;
38 #ifdef BDB_MONITOR_IDX
39 static int
40 bdb_monitor_idx_entry_add(
41 struct bdb_info *bdb,
42 Entry *e );
44 static AttributeDescription *ad_olmBDBNotIndexed;
45 #endif /* BDB_MONITOR_IDX */
48 * NOTE: there's some confusion in monitor OID arc;
49 * by now, let's consider:
51 * Subsystems monitor attributes 1.3.6.1.4.1.4203.666.1.55.0
52 * Databases monitor attributes 1.3.6.1.4.1.4203.666.1.55.0.1
53 * BDB database monitor attributes 1.3.6.1.4.1.4203.666.1.55.0.1.1
55 * Subsystems monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0
56 * Databases monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0.1
57 * BDB database monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0.1.1
60 static struct {
61 char *name;
62 char *oid;
63 } s_oid[] = {
64 { "olmBDBAttributes", "olmDatabaseAttributes:1" },
65 { "olmBDBObjectClasses", "olmDatabaseObjectClasses:1" },
67 { NULL }
70 static struct {
71 char *desc;
72 AttributeDescription **ad;
73 } s_at[] = {
74 { "( olmBDBAttributes:1 "
75 "NAME ( 'olmBDBEntryCache' ) "
76 "DESC 'Number of items in Entry Cache' "
77 "SUP monitorCounter "
78 "NO-USER-MODIFICATION "
79 "USAGE dSAOperation )",
80 &ad_olmBDBEntryCache },
82 { "( olmBDBAttributes:2 "
83 "NAME ( 'olmBDBDNCache' ) "
84 "DESC 'Number of items in DN Cache' "
85 "SUP monitorCounter "
86 "NO-USER-MODIFICATION "
87 "USAGE dSAOperation )",
88 &ad_olmBDBDNCache },
90 { "( olmBDBAttributes:3 "
91 "NAME ( 'olmBDBIDLCache' ) "
92 "DESC 'Number of items in IDL Cache' "
93 "SUP monitorCounter "
94 "NO-USER-MODIFICATION "
95 "USAGE dSAOperation )",
96 &ad_olmBDBIDLCache },
98 { "( olmBDBAttributes:4 "
99 "NAME ( 'olmDbDirectory' ) "
100 "DESC 'Path name of the directory "
101 "where the database environment resides' "
102 "SUP monitoredInfo "
103 "NO-USER-MODIFICATION "
104 "USAGE dSAOperation )",
105 &ad_olmDbDirectory },
107 #ifdef BDB_MONITOR_IDX
108 { "( olmBDBAttributes:5 "
109 "NAME ( 'olmBDBNotIndexed' ) "
110 "DESC 'Missing indexes resulting from candidate selection' "
111 "SUP monitoredInfo "
112 "NO-USER-MODIFICATION "
113 "USAGE dSAOperation )",
114 &ad_olmBDBNotIndexed },
115 #endif /* BDB_MONITOR_IDX */
117 { NULL }
120 static struct {
121 char *desc;
122 ObjectClass **oc;
123 } s_oc[] = {
124 /* augments an existing object, so it must be AUXILIARY
125 * FIXME: derive from some ABSTRACT "monitoredEntity"? */
126 { "( olmBDBObjectClasses:1 "
127 "NAME ( 'olmBDBDatabase' ) "
128 "SUP top AUXILIARY "
129 "MAY ( "
130 "olmBDBEntryCache "
131 "$ olmBDBDNCache "
132 "$ olmBDBIDLCache "
133 "$ olmDbDirectory "
134 #ifdef BDB_MONITOR_IDX
135 "$ olmBDBNotIndexed "
136 #endif /* BDB_MONITOR_IDX */
137 ") )",
138 &oc_olmBDBDatabase },
140 { NULL }
143 static int
144 bdb_monitor_update(
145 Operation *op,
146 SlapReply *rs,
147 Entry *e,
148 void *priv )
150 struct bdb_info *bdb = (struct bdb_info *) priv;
151 Attribute *a;
153 char buf[ BUFSIZ ];
154 struct berval bv;
156 assert( ad_olmBDBEntryCache != NULL );
158 a = attr_find( e->e_attrs, ad_olmBDBEntryCache );
159 assert( a != NULL );
160 bv.bv_val = buf;
161 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_cache.c_cursize );
162 ber_bvreplace( &a->a_vals[ 0 ], &bv );
164 a = attr_find( e->e_attrs, ad_olmBDBDNCache );
165 assert( a != NULL );
166 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_cache.c_eiused );
167 ber_bvreplace( &a->a_vals[ 0 ], &bv );
169 a = attr_find( e->e_attrs, ad_olmBDBIDLCache );
170 assert( a != NULL );
171 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", bdb->bi_idl_cache_size );
172 ber_bvreplace( &a->a_vals[ 0 ], &bv );
174 #ifdef BDB_MONITOR_IDX
175 bdb_monitor_idx_entry_add( bdb, e );
176 #endif /* BDB_MONITOR_IDX */
178 return SLAP_CB_CONTINUE;
181 #if 0 /* uncomment if required */
182 static int
183 bdb_monitor_modify(
184 Operation *op,
185 SlapReply *rs,
186 Entry *e,
187 void *priv )
189 return SLAP_CB_CONTINUE;
191 #endif
193 static int
194 bdb_monitor_free(
195 Entry *e,
196 void **priv )
198 struct berval values[ 2 ];
199 Modification mod = { 0 };
201 const char *text;
202 char textbuf[ SLAP_TEXT_BUFLEN ];
204 int i, rc;
206 /* NOTE: if slap_shutdown != 0, priv might have already been freed */
207 *priv = NULL;
209 /* Remove objectClass */
210 mod.sm_op = LDAP_MOD_DELETE;
211 mod.sm_desc = slap_schema.si_ad_objectClass;
212 mod.sm_values = values;
213 mod.sm_numvals = 1;
214 values[ 0 ] = oc_olmBDBDatabase->soc_cname;
215 BER_BVZERO( &values[ 1 ] );
217 rc = modify_delete_values( e, &mod, 1, &text,
218 textbuf, sizeof( textbuf ) );
219 /* don't care too much about return code... */
221 /* remove attrs */
222 mod.sm_values = NULL;
223 mod.sm_numvals = 0;
224 for ( i = 0; s_at[ i ].desc != NULL; i++ ) {
225 mod.sm_desc = *s_at[ i ].ad;
226 rc = modify_delete_values( e, &mod, 1, &text,
227 textbuf, sizeof( textbuf ) );
228 /* don't care too much about return code... */
231 return SLAP_CB_CONTINUE;
234 #define bdb_monitor_initialize BDB_SYMBOL(monitor_initialize)
237 * call from within bdb_initialize()
239 static int
240 bdb_monitor_initialize( void )
242 int i, code;
243 ConfigArgs c;
244 char *argv[ 3 ];
246 static int bdb_monitor_initialized = 0;
248 if ( backend_info( "monitor" ) == NULL ) {
249 return -1;
252 if ( bdb_monitor_initialized++ ) {
253 return 0;
256 /* register schema here */
258 argv[ 0 ] = "back-bdb/back-hdb monitor";
259 c.argv = argv;
260 c.argc = 3;
261 c.fname = argv[0];
263 for ( i = 0; s_oid[ i ].name; i++ ) {
264 c.lineno = i;
265 argv[ 1 ] = s_oid[ i ].name;
266 argv[ 2 ] = s_oid[ i ].oid;
268 if ( parse_oidm( &c, 0, NULL ) != 0 ) {
269 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_monitor_initialize)
270 ": unable to add "
271 "objectIdentifier \"%s=%s\"\n",
272 s_oid[ i ].name, s_oid[ i ].oid, 0 );
273 return 1;
277 for ( i = 0; s_at[ i ].desc != NULL; i++ ) {
278 code = register_at( s_at[ i ].desc, s_at[ i ].ad, 1 );
279 if ( code != LDAP_SUCCESS ) {
280 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_monitor_initialize)
281 ": register_at failed\n",
282 0, 0, 0 );
284 (*s_at[ i ].ad)->ad_type->sat_flags |= SLAP_AT_HIDE;
287 for ( i = 0; s_oc[ i ].desc != NULL; i++ ) {
288 code = register_oc( s_oc[ i ].desc, s_oc[ i ].oc, 1 );
289 if ( code != LDAP_SUCCESS ) {
290 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_monitor_initialize)
291 ": register_oc failed\n",
292 0, 0, 0 );
294 (*s_oc[ i ].oc)->soc_flags |= SLAP_OC_HIDE;
297 return 0;
301 * call from within bdb_db_init()
304 bdb_monitor_db_init( BackendDB *be )
306 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
308 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
309 return 0;
312 if ( bdb_monitor_initialize() == LDAP_SUCCESS ) {
313 /* monitoring in back-bdb is on by default */
314 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
317 #ifdef BDB_MONITOR_IDX
318 bdb->bi_idx = NULL;
319 ldap_pvt_thread_mutex_init( &bdb->bi_idx_mutex );
320 #endif /* BDB_MONITOR_IDX */
322 return 0;
326 * call from within bdb_db_open()
329 bdb_monitor_db_open( BackendDB *be )
331 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
332 Attribute *a, *next;
333 monitor_callback_t *cb = NULL;
334 int rc = 0;
335 BackendInfo *mi;
336 monitor_extra_t *mbe;
337 struct berval dummy = BER_BVC("");
339 if ( !SLAP_DBMONITORING( be ) ) {
340 return 0;
343 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
344 return 0;
347 mi = backend_info( "monitor" );
348 if ( !mi || !mi->bi_extra ) {
349 SLAP_DBFLAGS( be ) ^= SLAP_DBFLAG_MONITORING;
350 return 0;
352 mbe = mi->bi_extra;
354 /* don't bother if monitor is not configured */
355 if ( !mbe->is_configured() ) {
356 static int warning = 0;
358 if ( warning++ == 0 ) {
359 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_monitor_db_open)
360 ": monitoring disabled; "
361 "configure monitor database to enable\n",
362 0, 0, 0 );
365 return 0;
368 /* alloc as many as required (plus 1 for objectClass) */
369 a = attrs_alloc( 1 + 4 );
370 if ( a == NULL ) {
371 rc = 1;
372 goto cleanup;
375 a->a_desc = slap_schema.si_ad_objectClass;
376 attr_valadd( a, &oc_olmBDBDatabase->soc_cname, NULL, 1 );
377 next = a->a_next;
380 struct berval bv = BER_BVC( "0" );
382 next->a_desc = ad_olmBDBEntryCache;
383 attr_valadd( next, &bv, NULL, 1 );
384 next = next->a_next;
386 next->a_desc = ad_olmBDBDNCache;
387 attr_valadd( next, &bv, NULL, 1 );
388 next = next->a_next;
390 next->a_desc = ad_olmBDBIDLCache;
391 attr_valadd( next, &bv, NULL, 1 );
392 next = next->a_next;
396 struct berval bv, nbv;
397 ber_len_t pathlen = 0, len = 0;
398 char path[ MAXPATHLEN ] = { '\0' };
399 char *fname = bdb->bi_dbenv_home,
400 *ptr;
402 len = strlen( fname );
403 if ( fname[ 0 ] != '/' ) {
404 /* get full path name */
405 getcwd( path, sizeof( path ) );
406 pathlen = strlen( path );
408 if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) {
409 fname += 2;
410 len -= 2;
414 bv.bv_len = pathlen + STRLENOF( "/" ) + len;
415 ptr = bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 );
416 if ( pathlen ) {
417 ptr = lutil_strncopy( ptr, path, pathlen );
418 ptr[ 0 ] = '/';
419 ptr++;
421 ptr = lutil_strncopy( ptr, fname, len );
422 if ( ptr[ -1 ] != '/' ) {
423 ptr[ 0 ] = '/';
424 ptr++;
426 ptr[ 0 ] = '\0';
428 attr_normalize_one( ad_olmDbDirectory, &bv, &nbv, NULL );
430 next->a_desc = ad_olmDbDirectory;
431 next->a_vals = ch_calloc( sizeof( struct berval ), 2 );
432 next->a_vals[ 0 ] = bv;
433 next->a_numvals = 1;
435 if ( BER_BVISNULL( &nbv ) ) {
436 next->a_nvals = next->a_vals;
438 } else {
439 next->a_nvals = ch_calloc( sizeof( struct berval ), 2 );
440 next->a_nvals[ 0 ] = nbv;
443 next = next->a_next;
446 cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
447 cb->mc_update = bdb_monitor_update;
448 #if 0 /* uncomment if required */
449 cb->mc_modify = bdb_monitor_modify;
450 #endif
451 cb->mc_free = bdb_monitor_free;
452 cb->mc_private = (void *)bdb;
454 /* make sure the database is registered; then add monitor attributes */
455 rc = mbe->register_database( be, &bdb->bi_monitor.bdm_ndn );
456 if ( rc == 0 ) {
457 rc = mbe->register_entry_attrs( &bdb->bi_monitor.bdm_ndn, a, cb,
458 &dummy, 0, &dummy );
461 cleanup:;
462 if ( rc != 0 ) {
463 if ( cb != NULL ) {
464 ch_free( cb );
465 cb = NULL;
468 if ( a != NULL ) {
469 attrs_free( a );
470 a = NULL;
474 /* store for cleanup */
475 bdb->bi_monitor.bdm_cb = (void *)cb;
477 /* we don't need to keep track of the attributes, because
478 * bdb_monitor_free() takes care of everything */
479 if ( a != NULL ) {
480 attrs_free( a );
483 return rc;
487 * call from within bdb_db_close()
490 bdb_monitor_db_close( BackendDB *be )
492 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
494 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
495 return 0;
498 if ( !BER_BVISNULL( &bdb->bi_monitor.bdm_ndn ) ) {
499 BackendInfo *mi = backend_info( "monitor" );
500 monitor_extra_t *mbe;
502 if ( mi && &mi->bi_extra ) {
503 mbe = mi->bi_extra;
504 mbe->unregister_entry_callback( &bdb->bi_monitor.bdm_ndn,
505 (monitor_callback_t *)bdb->bi_monitor.bdm_cb,
506 NULL, 0, NULL );
509 memset( &bdb->bi_monitor, 0, sizeof( bdb->bi_monitor ) );
512 return 0;
516 * call from within bdb_db_destroy()
519 bdb_monitor_db_destroy( BackendDB *be )
521 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
522 return 0;
525 #ifdef BDB_MONITOR_IDX
527 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
529 /* TODO: free tree */
530 ldap_pvt_thread_mutex_destroy( &bdb->bi_idx_mutex );
531 avl_free( bdb->bi_idx, ch_free );
533 #endif /* BDB_MONITOR_IDX */
535 return 0;
538 #ifdef BDB_MONITOR_IDX
540 #define BDB_MONITOR_IDX_TYPES (4)
542 typedef struct monitor_idx_t monitor_idx_t;
544 struct monitor_idx_t {
545 AttributeDescription *idx_ad;
546 unsigned long idx_count[BDB_MONITOR_IDX_TYPES];
549 static int
550 bdb_monitor_bitmask2key( slap_mask_t bitmask )
552 int key;
554 for ( key = 0; key < 8*sizeof(slap_mask_t) && !( bitmask & 0x1U ); key++ ) {
555 bitmask >>= 1;
558 return key;
561 static struct berval idxbv[] = {
562 BER_BVC( "present=" ),
563 BER_BVC( "equality=" ),
564 BER_BVC( "approx=" ),
565 BER_BVC( "substr=" ),
566 BER_BVNULL
569 static ber_len_t
570 bdb_monitor_idx2len( monitor_idx_t *idx )
572 int i;
573 ber_len_t len = 0;
575 for ( i = 0; i < BDB_MONITOR_IDX_TYPES; i++ ) {
576 if ( idx->idx_count[ i ] != 0 ) {
577 len += idxbv[i].bv_len;
581 return len;
584 static int
585 monitor_idx_cmp( const void *p1, const void *p2 )
587 const monitor_idx_t *idx1 = (const monitor_idx_t *)p1;
588 const monitor_idx_t *idx2 = (const monitor_idx_t *)p2;
590 return SLAP_PTRCMP( idx1->idx_ad, idx2->idx_ad );
593 static int
594 monitor_idx_dup( void *p1, void *p2 )
596 monitor_idx_t *idx1 = (monitor_idx_t *)p1;
597 monitor_idx_t *idx2 = (monitor_idx_t *)p2;
599 return SLAP_PTRCMP( idx1->idx_ad, idx2->idx_ad ) == 0 ? -1 : 0;
603 bdb_monitor_idx_add(
604 struct bdb_info *bdb,
605 AttributeDescription *desc,
606 slap_mask_t type )
608 monitor_idx_t idx_dummy = { 0 },
609 *idx;
610 int rc = 0, key;
612 idx_dummy.idx_ad = desc;
613 key = bdb_monitor_bitmask2key( type ) - 1;
614 if ( key >= BDB_MONITOR_IDX_TYPES ) {
615 /* invalid index type */
616 return -1;
619 ldap_pvt_thread_mutex_lock( &bdb->bi_idx_mutex );
621 idx = (monitor_idx_t *)avl_find( bdb->bi_idx,
622 (caddr_t)&idx_dummy, monitor_idx_cmp );
623 if ( idx == NULL ) {
624 idx = (monitor_idx_t *)ch_calloc( sizeof( monitor_idx_t ), 1 );
625 idx->idx_ad = desc;
626 idx->idx_count[ key ] = 1;
628 switch ( avl_insert( &bdb->bi_idx, (caddr_t)idx,
629 monitor_idx_cmp, monitor_idx_dup ) )
631 case 0:
632 break;
634 default:
635 ch_free( idx );
636 rc = -1;
639 } else {
640 idx->idx_count[ key ]++;
643 ldap_pvt_thread_mutex_unlock( &bdb->bi_idx_mutex );
645 return rc;
648 static int
649 bdb_monitor_idx_apply( void *v_idx, void *v_valp )
651 monitor_idx_t *idx = (monitor_idx_t *)v_idx;
652 BerVarray *valp = (BerVarray *)v_valp;
654 struct berval bv;
655 char *ptr;
656 char count_buf[ BDB_MONITOR_IDX_TYPES ][ SLAP_TEXT_BUFLEN ];
657 ber_len_t count_len[ BDB_MONITOR_IDX_TYPES ],
658 idx_len;
659 int i, num = 0;
661 idx_len = bdb_monitor_idx2len( idx );
663 bv.bv_len = 0;
664 for ( i = 0; i < BDB_MONITOR_IDX_TYPES; i++ ) {
665 if ( idx->idx_count[ i ] == 0 ) {
666 continue;
669 count_len[ i ] = snprintf( count_buf[ i ],
670 sizeof( count_buf[ i ] ), "%lu", idx->idx_count[ i ] );
671 bv.bv_len += count_len[ i ];
672 num++;
675 bv.bv_len += idx->idx_ad->ad_cname.bv_len
676 + num
677 + idx_len;
678 ptr = bv.bv_val = ch_malloc( bv.bv_len + 1 );
679 ptr = lutil_strcopy( ptr, idx->idx_ad->ad_cname.bv_val );
680 for ( i = 0; i < BDB_MONITOR_IDX_TYPES; i++ ) {
681 if ( idx->idx_count[ i ] == 0 ) {
682 continue;
685 ptr[ 0 ] = '#';
686 ++ptr;
687 ptr = lutil_strcopy( ptr, idxbv[ i ].bv_val );
688 ptr = lutil_strcopy( ptr, count_buf[ i ] );
691 ber_bvarray_add( valp, &bv );
693 return 0;
696 static int
697 bdb_monitor_idx_entry_add(
698 struct bdb_info *bdb,
699 Entry *e )
701 BerVarray vals = NULL;
702 Attribute *a;
704 a = attr_find( e->e_attrs, ad_olmBDBNotIndexed );
706 ldap_pvt_thread_mutex_lock( &bdb->bi_idx_mutex );
708 avl_apply( bdb->bi_idx, bdb_monitor_idx_apply,
709 &vals, -1, AVL_INORDER );
711 ldap_pvt_thread_mutex_unlock( &bdb->bi_idx_mutex );
713 if ( vals != NULL ) {
714 if ( a != NULL ) {
715 assert( a->a_nvals == a->a_vals );
717 ber_bvarray_free( a->a_vals );
719 } else {
720 Attribute **ap;
722 for ( ap = &e->e_attrs; *ap != NULL; ap = &(*ap)->a_next )
724 *ap = attr_alloc( ad_olmBDBNotIndexed );
725 a = *ap;
727 a->a_vals = vals;
728 a->a_nvals = a->a_vals;
731 return 0;
734 #endif /* BDB_MONITOR_IDX */