]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-bdb/monitor.c
Unify use of BDB lockers
[openldap] / servers / slapd / back-bdb / monitor.c
index c88e71c0aeb59b84414848a94d6f485f2cd06211..9114fac70b8ade71f5b883173f4b6eb4ddca6251 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2000-2006 The OpenLDAP Foundation.
+ * Copyright 2000-2007 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 #include <sys/stat.h>
 #include "lutil.h"
 #include "back-bdb.h"
+
 #include "../back-monitor/back-monitor.h"
 
+#include "config.h"
+
 static ObjectClass             *oc_olmBDBDatabase;
 
 static AttributeDescription    *ad_olmBDBEntryCache,
-       *ad_olmBDBEntryInfo, *ad_olmBDBIDLCache;
+       *ad_olmBDBEntryInfo, *ad_olmBDBIDLCache,
+       *ad_olmDbDirectory;
+
+/*
+ * NOTE: there's some confusion in monitor OID arc;
+ * by now, let's consider:
+ * 
+ * Subsystems monitor attributes       1.3.6.1.4.1.4203.666.1.55.0
+ * Databases monitor attributes                1.3.6.1.4.1.4203.666.1.55.0.1
+ * BDB database monitor attributes     1.3.6.1.4.1.4203.666.1.55.0.1.1
+ *
+ * Subsystems monitor objectclasses    1.3.6.1.4.1.4203.666.3.16.0
+ * Databases monitor objectclasses     1.3.6.1.4.1.4203.666.3.16.0.1
+ * BDB database monitor objectclasses  1.3.6.1.4.1.4203.666.3.16.0.1.1
+ */
+
+static struct {
+       char                    *name;
+       char                    *oid;
+}              s_oid[] = {
+       { "olmBDBAttributes",                   "olmDatabaseAttributes:1" },
+       { "olmBDBObjectClasses",                "olmDatabaseObjectClasses:1" },
+
+       { NULL }
+};
+
+static struct {
+       char                    *desc;
+       AttributeDescription    **ad;
+}              s_at[] = {
+       { "( olmBDBAttributes:1 "
+               "NAME ( 'olmBDBEntryCache' ) "
+               "DESC 'Number of items in Entry Cache' "
+               "SUP monitorCounter "
+               "NO-USER-MODIFICATION "
+               "USAGE dSAOperation )",
+               &ad_olmBDBEntryCache },
+
+       { "( olmBDBAttributes:2 "
+               "NAME ( 'olmBDBEntryInfo' ) "
+               "DESC 'Number of items in EntryInfo Cache' "
+               "SUP monitorCounter "
+               "NO-USER-MODIFICATION "
+               "USAGE dSAOperation )",
+               &ad_olmBDBEntryInfo },
+
+       { "( olmBDBAttributes:3 "
+               "NAME ( 'olmBDBIDLCache' ) "
+               "DESC 'Number of items in IDL Cache' "
+               "SUP monitorCounter "
+               "NO-USER-MODIFICATION "
+               "USAGE dSAOperation )",
+               &ad_olmBDBIDLCache },
+
+       { "( olmBDBAttributes:4 "
+               "NAME ( 'olmDbDirectory' ) "
+               "DESC 'Path name of the directory "
+                       "where the database environment resides' "
+               "SUP monitoredInfo "
+               "NO-USER-MODIFICATION "
+               "USAGE dSAOperation )",
+               &ad_olmDbDirectory },
+
+       { NULL }
+};
+
+static struct {
+       char            *desc;
+       ObjectClass     **oc;
+}              s_oc[] = {
+       /* augments an existing object, so it must be AUXILIARY
+        * FIXME: derive from some ABSTRACT "monitoredEntity"? */
+       { "( olmBDBObjectClasses:1 "
+               "NAME ( 'olmBDBDatabase' ) "
+               "SUP top AUXILIARY "
+               "MAY ( "
+                       "olmBDBEntryCache "
+                       "$ olmBDBEntryInfo "
+                       "$ olmBDBIDLCache "
+                       "$ olmDbDirectory "
+                       ") )",
+               &oc_olmBDBDatabase },
+
+       { NULL }
+};
 
 static int
 bdb_monitor_update(
@@ -41,7 +128,6 @@ bdb_monitor_update(
        struct bdb_info         *bdb = (struct bdb_info *) priv;
        Attribute               *a;
 
-       unsigned long           u;
        char                    buf[ BUFSIZ ];
        struct berval           bv;
 
@@ -66,6 +152,7 @@ bdb_monitor_update(
        return SLAP_CB_CONTINUE;
 }
 
+#if 0  /* uncomment if required */
 static int
 bdb_monitor_modify(
        Operation       *op,
@@ -73,186 +160,108 @@ bdb_monitor_modify(
        Entry           *e,
        void            *priv )
 {
-       struct bdb_info         *bdb = (struct bdb_info *) priv;
-       
        return SLAP_CB_CONTINUE;
 }
+#endif
 
 static int
 bdb_monitor_free(
        Entry           *e,
-       void            *priv )
+       void            **priv )
 {
-       struct bdb_info         *bdb = (struct bdb_info *) priv;
+       struct berval   values[ 2 ];
+       Modification    mod = { 0 };
+
+       const char      *text;
+       char            textbuf[ SLAP_TEXT_BUFLEN ];
+
+       int             i, rc;
+
+       /* NOTE: if slap_shutdown != 0, priv might have already been freed */
+       *priv = NULL;
+
+       /* Remove objectClass */
+       mod.sm_op = LDAP_MOD_DELETE;
+       mod.sm_desc = slap_schema.si_ad_objectClass;
+       mod.sm_values = values;
+       values[ 0 ] = oc_olmBDBDatabase->soc_cname;
+       BER_BVZERO( &values[ 1 ] );
+
+       rc = modify_delete_values( e, &mod, 1, &text,
+               textbuf, sizeof( textbuf ) );
+       /* don't care too much about return code... */
+
+       /* remove attrs */
+       for ( i = 0; s_at[ i ].desc != NULL; i++ ) {
+               mod.sm_desc = *s_at[ i ].ad;
+               mod.sm_values = NULL;
+               rc = modify_delete_values( e, &mod, 1, &text,
+                       textbuf, sizeof( textbuf ) );
+               /* don't care too much about return code... */
+       }
        
        return SLAP_CB_CONTINUE;
 }
 
-/*
- * NOTE: there's some confusion in monitor OID arc;
- * by now, let's consider:
- * 
- * Subsystems monitor attributes       1.3.6.1.4.1.4203.666.1.55.0
- * Databases monitor attributes                1.3.6.1.4.1.4203.666.1.55.0.1
- * BDB database monitor attributes     1.3.6.1.4.1.4203.666.1.55.0.1.1
- *
- * Subsystems monitor objectclasses    1.3.6.1.4.1.4203.666.3.16.0
- * Databases monitor objectclasses     1.3.6.1.4.1.4203.666.3.16.0.1
- * BDB database monitor objectclasses  1.3.6.1.4.1.4203.666.3.16.0.1.1
- */
-#define        BDB_MONITOR_SCHEMA_AD           "1.3.6.1.4.1.4203.666.1.55.0.1.1"
-#define        BDB_MONITOR_SCHEMA_OC           "1.3.6.1.4.1.4203.666.3.16.0.1.1"
-
-static struct {
-       char                    *name;
-       char                    *desc;
-       AttributeDescription    **ad;
-}              s_at[] = {
-       { "olmBDBEntryCache", "( " BDB_MONITOR_SCHEMA_AD ".1 "
-               "NAME ( 'olmBDBEntryCache' ) "
-               "DESC 'Number of items in Entry Cache' "
-               "SUP monitorCounter "
-               "NO-USER-MODIFICATION "
-               "USAGE directoryOperation )",
-               &ad_olmBDBEntryCache },
-
-       { "olmBDBEntryInfo", "( " BDB_MONITOR_SCHEMA_AD ".2 "
-               "NAME ( 'olmBDBEntryInfo' ) "
-               "DESC 'Number of items in EntryInfo Cache' "
-               "SUP monitorCounter "
-               "NO-USER-MODIFICATION "
-               "USAGE directoryOperation )",
-               &ad_olmBDBEntryInfo },
-
-       { "olmBDBIDLCache", "( " BDB_MONITOR_SCHEMA_AD ".3 "
-               "NAME ( 'olmBDBIDLCache' ) "
-               "DESC 'Number of items in IDL Cache' "
-               "SUP monitorCounter "
-               "NO-USER-MODIFICATION "
-               "USAGE directoryOperation )",
-               &ad_olmBDBIDLCache },
-
-       { NULL }
-};
-
-static struct {
-       char            *name;
-       char            *desc;
-       ObjectClass     **oc;
-}              s_oc[] = {
-       { "olmBDBDatabase", "( " BDB_MONITOR_SCHEMA_OC ".1 "
-               "NAME ( 'olmBDBDatabase' ) "
-               "SUP monitoredObject STRUCTURAL "
-               "MAY ( "
-                       "olmBDBEntryCache $ olmBDBEntryInfo $ olmBDBIDLCache "
-                       ") )",
-               &oc_olmBDBDatabase },
-
-       { NULL }
-};
+#define        bdb_monitor_initialize  BDB_SYMBOL(monitor_initialize)
 
 /*
  * call from within bdb_initialize()
  */
-int
+static int
 bdb_monitor_initialize( void )
 {
        int             i, code;
-       const char      *err;
+       ConfigArgs c;
+       char    *argv[ 3 ];
 
        static int      bdb_monitor_initialized = 0;
 
-       /* register schema here; if compiled as dynamic object,
-        * must be loaded __after__ back_monitor.la */
+       if ( backend_info( "monitor" ) == NULL ) {
+               return -1;
+       }
 
        if ( bdb_monitor_initialized++ ) {
                return 0;
        }
 
-       for ( i = 0; s_at[ i ].name != NULL; i++ ) {
-               LDAPAttributeType       *at;
+       /* register schema here */
 
-               at = ldap_str2attributetype( s_at[ i ].desc,
-                       &code, &err, LDAP_SCHEMA_ALLOW_ALL );
-               if ( !at ) {
-                       Debug( LDAP_DEBUG_ANY,
-                               "bdb_monitor_initialize: "
-                               "AttributeType load failed: %s %s\n",
-                               ldap_scherr2str( code ), err, 0 );
-                       return LDAP_INVALID_SYNTAX;
-               }
+       argv[ 0 ] = "back-bdb/back-hdb monitor";
+       c.argv = argv;
+       c.argc = 3;
+       c.fname = argv[0];
 
-               code = at_add( at, 0, NULL, &err );
-               if ( code != LDAP_SUCCESS ) {
-                       Debug( LDAP_DEBUG_ANY,
-                               "bdb_monitor_initialize: "
-                               "AttributeType load failed: %s %s\n",
-                               scherr2str( code ), err, 0 );
-                       code = LDAP_INVALID_SYNTAX;
-                       goto done_at;
-               }
+       for ( i = 0; s_oid[ i ].name; i++ ) {
+               c.lineno = i;
+               argv[ 1 ] = s_oid[ i ].name;
+               argv[ 2 ] = s_oid[ i ].oid;
 
-               code = slap_str2ad( s_at[ i ].name,
-                               s_at[ i ].ad, &err );
-               if ( code != LDAP_SUCCESS ) {
+               if ( parse_oidm( &c, 0, NULL ) != 0 ) {
                        Debug( LDAP_DEBUG_ANY,
-                               "bdb_monitor_initialize: "
-                               "unable to find AttributeDescription "
-                               "\"%s\": %d (%s)\n",
-                               s_at[ i ].name, code, err );
-                       code = LDAP_UNDEFINED_TYPE;
-                       goto done_at;
-               }
-
-done_at:;
-               if ( code ) {
-                       ldap_attributetype_free( at );
-                       return code;
+                               "bdb_monitor_initialize: unable to add "
+                               "objectIdentifier \"%s=%s\"\n",
+                               s_oid[ i ].name, s_oid[ i ].oid, 0 );
+                       return 1;
                }
-
-               ldap_memfree( at );
        }
 
-       for ( i = 0; s_oc[ i ].name != NULL; i++ ) {
-               LDAPObjectClass *oc;
-
-               oc = ldap_str2objectclass( s_oc[ i ].desc,
-                               &code, &err, LDAP_SCHEMA_ALLOW_ALL );
-               if ( !oc ) {
-                       Debug( LDAP_DEBUG_ANY,
-                               "bdb_monitor_initialize: "
-                               "ObjectClass load failed: %s %s\n",
-                               ldap_scherr2str( code ), err, 0 );
-                       return LDAP_INVALID_SYNTAX;
-               }
-
-               code = oc_add( oc, 0, NULL, &err );
+       for ( i = 0; s_at[ i ].desc != NULL; i++ ) {
+               code = register_at( s_at[ i ].desc, s_at[ i ].ad, 1 );
                if ( code != LDAP_SUCCESS ) {
                        Debug( LDAP_DEBUG_ANY,
-                               "bdb_monitor_initialize: "
-                               "ObjectClass load failed: %s %s\n",
-                               scherr2str( code ), err, 0 );
-                       code = LDAP_INVALID_SYNTAX;
-                       goto done_oc;
-               }
-
-               *s_oc[ i ].oc = oc_find( s_oc[ i ].name );
-               if ( *s_oc[ i ].oc == NULL ) {
-                       code = LDAP_UNDEFINED_TYPE;
-                       Debug( LDAP_DEBUG_ANY,
-                               "bdb_monitor_initialize: "
-                               "unable to find objectClass \"%s\"\n",
-                               s_oc[ i ].name, 0, 0 );
-                       goto done_oc;
+                               "bdb_monitor_initialize: register_at failed\n",
+                               0, 0, 0 );
                }
+       }
 
-done_oc:;
+       for ( i = 0; s_oc[ i ].desc != NULL; i++ ) {
+               code = register_oc( s_oc[ i ].desc, s_oc[ i ].oc, 1 );
                if ( code != LDAP_SUCCESS ) {
-                       ldap_objectclass_free( oc );
-                       return code;
+                       Debug( LDAP_DEBUG_ANY,
+                               "bdb_monitor_initialize: register_oc failed\n",
+                               0, 0, 0 );
                }
-
-               ldap_memfree( oc );
        }
 
        return 0;
@@ -262,8 +271,17 @@ done_oc:;
  * call from within bdb_db_init()
  */
 int
-bdb_monitor_init( BackendDB *be )
+bdb_monitor_db_init( BackendDB *be )
 {
+       struct bdb_info         *bdb = (struct bdb_info *) be->be_private;
+
+       if ( bdb_monitor_initialize() == LDAP_SUCCESS ) {
+               /* monitoring in back-bdb is on by default */
+               SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
+       }
+
+       bdb->bi_monitor.bdm_scope = -1;
+
        return 0;
 }
 
@@ -271,18 +289,30 @@ bdb_monitor_init( BackendDB *be )
  * call from within bdb_db_open()
  */
 int
-bdb_monitor_open( BackendDB *be )
+bdb_monitor_db_open( BackendDB *be )
 {
        struct bdb_info         *bdb = (struct bdb_info *) be->be_private;
        Attribute               *a, *next;
-       monitor_callback_t      *cb;
-       struct berval           base = BER_BVC( "cn=databases,cn=monitor" );
-       struct berval           suffix, filter;
+       monitor_callback_t      *cb = NULL;
+       struct berval           suffix, *filter, *base;
        char                    *ptr;
        int                     rc = 0;
+       BackendInfo             *mi;
+       monitor_extra_t         *mbe;
+
+       if ( !SLAP_DBMONITORING( be ) ) {
+               return 0;
+       }
+
+       mi = backend_info( "monitor" );
+       if ( !mi || !mi->bi_extra ) {
+               SLAP_DBFLAGS( be ) ^= SLAP_DBFLAG_MONITORING;
+               return 0;
+       }
+       mbe = mi->bi_extra;
 
        /* don't bother if monitor is not configured */
-       if ( !monitor_back_is_configured() ) {
+       if ( !mbe->is_configured() ) {
                static int warning = 0;
 
                if ( warning++ == 0 ) {
@@ -295,9 +325,13 @@ bdb_monitor_open( BackendDB *be )
                return 0;
        }
 
-       /* monitor_back_register_entry_attrs() with a NULL ndn,
-        * base="cn=Databases,cn=Monitor", scope=LDAP_SCOPE_ONE 
-        * and filter="(namingContexts:distinguishedNameMatch:=<suffix>)" */
+       if ( bdb->bi_monitor.bdm_scope == -1 ) {
+               bdb->bi_monitor.bdm_scope = LDAP_SCOPE_ONELEVEL;
+       }
+       base = &bdb->bi_monitor.bdm_nbase;
+       BER_BVSTR( base, "cn=databases,cn=monitor" );
+       filter = &bdb->bi_monitor.bdm_filter;
+       BER_BVZERO( filter );
 
        suffix.bv_len = ldap_bv2escaped_filter_value_len( &be->be_nsuffix[ 0 ] );
        if ( suffix.bv_len == be->be_nsuffix[ 0 ].bv_len ) {
@@ -306,22 +340,34 @@ bdb_monitor_open( BackendDB *be )
        } else {
                ldap_bv2escaped_filter_value( &be->be_nsuffix[ 0 ], &suffix );
        }
-       
-       filter.bv_len = STRLENOF( "(namingContexts:distinguishedNameMatch:=)" ) + suffix.bv_len;
-       ptr = filter.bv_val = ch_malloc( filter.bv_len + 1 );
-       ptr = lutil_strcopy( ptr, "(namingContexts:distinguishedNameMatch:=" );
-       ptr = lutil_strncopy( ptr, suffix.bv_val, suffix.bv_len );
-       ptr[ 0 ] = ')';
-       ptr++;
+
+       if ( BER_BVISEMPTY( &suffix ) ) {
+               /* frontend also has empty suffix, sigh! */
+               filter->bv_len = STRLENOF( "(&(namingContexts:distinguishedNameMatch:=" )
+                       + suffix.bv_len + STRLENOF( ")(!(cn=frontend)))" );
+               ptr = filter->bv_val = ch_malloc( filter->bv_len + 1 );
+               ptr = lutil_strcopy( ptr, "(&(namingContexts:distinguishedNameMatch:=" );
+               ptr = lutil_strncopy( ptr, suffix.bv_val, suffix.bv_len );
+               ptr = lutil_strcopy( ptr, ")(!(cn=frontend)))" );
+
+       } else {
+               /* just look for the naming context */
+               filter->bv_len = STRLENOF( "(namingContexts:distinguishedNameMatch:=" )
+                       + suffix.bv_len + STRLENOF( ")" );
+               ptr = filter->bv_val = ch_malloc( filter->bv_len + 1 );
+               ptr = lutil_strcopy( ptr, "(namingContexts:distinguishedNameMatch:=" );
+               ptr = lutil_strncopy( ptr, suffix.bv_val, suffix.bv_len );
+               ptr = lutil_strcopy( ptr, ")" );
+       }
        ptr[ 0 ] = '\0';
-       assert( filter.bv_len == ptr - filter.bv_val );
+       assert( filter->bv_len == ptr - filter->bv_val );
        
        if ( suffix.bv_val != be->be_nsuffix[ 0 ].bv_val ) {
                ch_free( suffix.bv_val );
        }
 
        /* alloc as many as required (plus 1 for objectClass) */
-       a = attrs_alloc( 1 + 3 );
+       a = attrs_alloc( 1 + 4 );
        if ( a == NULL ) {
                rc = 1;
                goto cleanup;
@@ -348,32 +394,97 @@ bdb_monitor_open( BackendDB *be )
                next->a_desc = ad_olmBDBIDLCache;
                value_add_one( &next->a_vals, &bv );
                next->a_nvals = next->a_vals;
+               next = next->a_next;
+       }
+
+       {
+               struct berval   bv, nbv;
+               ber_len_t       pathlen = 0, len = 0;
+               char            path[ PATH_MAX ] = { '\0' };
+               char            *fname = bdb->bi_dbenv_home,
+                               *ptr;
+
+               len = strlen( fname );
+               if ( fname[ 0 ] != '/' ) {
+                       /* get full path name */
+                       getcwd( path, sizeof( path ) );
+                       pathlen = strlen( path );
+
+                       if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) {
+                               fname += 2;
+                               len -= 2;
+                       }
+               }
+
+               bv.bv_len = pathlen + STRLENOF( "/" ) + len;
+               ptr = bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 );
+               if ( pathlen ) {
+                       ptr = lutil_strncopy( ptr, path, pathlen );
+                       ptr[ 0 ] = '/';
+                       ptr++;
+               }
+               ptr = lutil_strncopy( ptr, fname, len );
+               if ( ptr[ -1 ] != '/' ) {
+                       ptr[ 0 ] = '/';
+                       ptr++;
+               }
+               ptr[ 0 ] = '\0';
+               
+               attr_normalize_one( ad_olmDbDirectory, &bv, &nbv, NULL );
+
+               next->a_desc = ad_olmDbDirectory;
+               next->a_vals = ch_calloc( sizeof( struct berval ), 2 );
+               next->a_vals[ 0 ] = bv;
+
+               if ( BER_BVISNULL( &nbv ) ) {
+                       next->a_nvals = next->a_vals;
+
+               } else {
+                       next->a_nvals = ch_calloc( sizeof( struct berval ), 2 );
+                       next->a_nvals[ 0 ] = nbv;
+               }
+
+               next = next->a_next;
        }
 
        cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
        cb->mc_update = bdb_monitor_update;
+#if 0  /* uncomment if required */
        cb->mc_modify = bdb_monitor_modify;
+#endif
        cb->mc_free = bdb_monitor_free;
        cb->mc_private = (void *)bdb;
 
-       rc = monitor_back_register_entry_attrs( NULL,
-               a, cb, &base, LDAP_SCOPE_ONELEVEL, &filter );
+       rc = mbe->register_entry_attrs( NULL, a, cb,
+               base, bdb->bi_monitor.bdm_scope, filter );
 
 cleanup:;
        if ( rc != 0 ) {
                if ( cb != NULL ) {
                        ch_free( cb );
+                       cb = NULL;
+               }
+
+               if ( a != NULL ) {
+                       attrs_free( a );
+                       a = NULL;
                }
-       }
 
-       if ( !BER_BVISNULL( &filter ) ) {
-               ch_free( filter.bv_val );
+               if ( !BER_BVISNULL( filter ) ) {
+                       ch_free( filter->bv_val );
+                       BER_BVZERO( filter );
+               }
        }
 
+       /* store for cleanup */
+       bdb->bi_monitor.bdm_cb = (void *)cb;
+
+       /* we don't need to keep track of the attributes, because
+        * bdb_monitor_free() takes care of everything */
        if ( a != NULL ) {
                attrs_free( a );
        }
-       
+
        return rc;
 }
 
@@ -381,8 +492,30 @@ cleanup:;
  * call from within bdb_db_close()
  */
 int
-bdb_monitor_close( BackendDB *be )
+bdb_monitor_db_close( BackendDB *be )
 {
+       struct bdb_info         *bdb = (struct bdb_info *) be->be_private;
+
+       if ( !BER_BVISNULL( &bdb->bi_monitor.bdm_filter ) ) {
+               BackendInfo             *mi = backend_info( "monitor" );
+               monitor_extra_t         *mbe;
+
+               if ( mi && &mi->bi_extra ) {
+                       mbe = mi->bi_extra;
+                       mbe->unregister_entry_callback( NULL,
+                               (monitor_callback_t *)bdb->bi_monitor.bdm_cb,
+                               &bdb->bi_monitor.bdm_nbase,
+                               bdb->bi_monitor.bdm_scope,
+                               &bdb->bi_monitor.bdm_filter );
+               }
+
+               if ( !BER_BVISNULL( &bdb->bi_monitor.bdm_filter ) ) {
+                       ch_free( bdb->bi_monitor.bdm_filter.bv_val );
+               }
+
+               memset( &bdb->bi_monitor, 0, sizeof( bdb->bi_monitor ) );
+       }
+
        return 0;
 }
 
@@ -390,8 +523,7 @@ bdb_monitor_close( BackendDB *be )
  * call from within bdb_db_destroy()
  */
 int
-bdb_monitor_destroy( BackendDB *be )
+bdb_monitor_db_destroy( BackendDB *be )
 {
        return 0;
 }
-