]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-monitor/log.c
Cleanup and SIGTRAP
[openldap] / servers / slapd / back-monitor / log.c
index f1e6835392e3307a1d32405c9cb106f86dd80faa..9e99fb1eede4dc21d54bc4c2369c0b92d30a6874 100644 (file)
@@ -1,37 +1,22 @@
 /* log.c - deal with log subsystem */
-/*
- * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 2001-2004 The OpenLDAP Foundation.
+ * Portions Copyright 2001-2003 Pierangelo Masarati.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
  */
-/*
- * Copyright 2001 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
- * 
- * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
- * 
- * This work has beed deveolped for the OpenLDAP Foundation 
- * in the hope that it may be useful to the Open Source community, 
- * but WITHOUT ANY WARRANTY.
- * 
- * Permission is granted to anyone to use this software for any purpose
- * on any computer system, and to alter it and redistribute it, subject
- * to the following restrictions:
- * 
- * 1. The author and SysNet s.n.c. are not responsible for the consequences
- *    of use of this software, no matter how awful, even if they arise from
- *    flaws in it.
- * 
- * 2. The origin of this software must not be misrepresented, either by
- *    explicit claim or by omission.  Since few users ever read sources,
- *    credits should appear in the documentation.
- * 
- * 3. Altered versions must be plainly marked as such, and must not be
- *    misrepresented as being the original software.  Since few users
- *    ever read sources, credits should appear in the documentation.
- *    SysNet s.n.c. cannot be responsible for the consequences of the
- *    alterations.
- * 
- * 4. This notice may not be removed or altered.
+/* ACKNOWLEDGEMENTS:
+ * This work was initially developed by Pierangelo Masarati for inclusion
+ * in OpenLDAP Software.
  */
 
 #include "portable.h"
@@ -41,6 +26,7 @@
 #include <ac/string.h>
 
 #include "slap.h"
+#include <lber_pvt.h>
 #include "lutil.h"
 #include "ldif.h"
 #include "back-monitor.h"
@@ -52,27 +38,28 @@ ldap_pvt_thread_mutex_t             monitor_log_mutex;
 
 static struct {
        int i;
-       const char *s;
+       struct berval s;
+       struct berval n;
 } int_2_level[] = {
-       { LDAP_DEBUG_TRACE,     "Trace" },
-       { LDAP_DEBUG_PACKETS,   "Packets" },
-       { LDAP_DEBUG_ARGS,      "Args" },
-       { LDAP_DEBUG_CONNS,     "Conns" },
-       { LDAP_DEBUG_BER,       "BER" },
-       { LDAP_DEBUG_FILTER,    "Filter" },
-       { LDAP_DEBUG_CONFIG,    "Config" },     /* useless */
-       { LDAP_DEBUG_ACL,       "ACL" },
-       { LDAP_DEBUG_STATS,     "Stats" },
-       { LDAP_DEBUG_STATS2,    "Stats2" },
-       { LDAP_DEBUG_SHELL,     "Shell" },
-       { LDAP_DEBUG_PARSE,     "Parse" },
-       { LDAP_DEBUG_CACHE,     "Cache" },
-       { LDAP_DEBUG_INDEX,     "Index" },
-       { 0,                    NULL }
+       { LDAP_DEBUG_TRACE,     BER_BVC("Trace"),       BER_BVNULL },
+       { LDAP_DEBUG_PACKETS,   BER_BVC("Packets"),     BER_BVNULL },
+       { LDAP_DEBUG_ARGS,      BER_BVC("Args"),        BER_BVNULL },
+       { LDAP_DEBUG_CONNS,     BER_BVC("Conns"),       BER_BVNULL },
+       { LDAP_DEBUG_BER,       BER_BVC("BER"), BER_BVNULL },
+       { LDAP_DEBUG_FILTER,    BER_BVC("Filter"),      BER_BVNULL },
+       { LDAP_DEBUG_CONFIG,    BER_BVC("Config"),      BER_BVNULL },   /* useless */
+       { LDAP_DEBUG_ACL,       BER_BVC("ACL"), BER_BVNULL },
+       { LDAP_DEBUG_STATS,     BER_BVC("Stats"),       BER_BVNULL },
+       { LDAP_DEBUG_STATS2,    BER_BVC("Stats2"),      BER_BVNULL },
+       { LDAP_DEBUG_SHELL,     BER_BVC("Shell"),       BER_BVNULL },
+       { LDAP_DEBUG_PARSE,     BER_BVC("Parse"),       BER_BVNULL },
+       { LDAP_DEBUG_CACHE,     BER_BVC("Cache"),       BER_BVNULL },
+       { LDAP_DEBUG_INDEX,     BER_BVC("Index"),       BER_BVNULL },
+       { 0,                    BER_BVNULL,     BER_BVNULL }
 };
 
-static int loglevel2int( const char *str );
-static const char * int2loglevel( int n );
+static int loglevel2int( struct berval *l );
+static int int2loglevel( int n );
 
 static int add_values( Entry *e, Modification *mod, int *newlevel );
 static int delete_values( Entry *e, Modification *mod, int *newlevel );
@@ -89,40 +76,59 @@ monitor_subsys_log_init(
        struct monitorinfo      *mi;
        Entry                   *e;
        int                     i;
-       struct monitorentrypriv *mp;
-       struct berval           val, *bv[2] = { &val, NULL };
+       struct berval           desc[] = {
+               BER_BVC("This entry allows to set the log level runtime."),
+               BER_BVC("Set the attribute 'managedInfo' to the desired log levels."),
+               BER_BVNULL
+       };
 
        ldap_pvt_thread_mutex_init( &monitor_log_mutex );
 
        mi = ( struct monitorinfo * )be->be_private;
 
-       if ( monitor_cache_get( mi, monitor_subsys[SLAPD_MONITOR_LOG].mss_ndn, 
+       if ( monitor_cache_get( mi, &monitor_subsys[SLAPD_MONITOR_LOG].mss_ndn, 
                                &e ) ) {
 #ifdef NEW_LOGGING
-               LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
+               LDAP_LOG( OPERATION, CRIT,
                        "monitor_subsys_log_init: "
                        "unable to get entry '%s'\n",
-                       monitor_subsys[SLAPD_MONITOR_LOG].mss_ndn ));
+                       monitor_subsys[SLAPD_MONITOR_LOG].mss_ndn.bv_val, 0, 0 );
 #else
                Debug( LDAP_DEBUG_ANY,
                        "monitor_subsys_log_init: "
                        "unable to get entry '%s'\n%s%s",
-                       monitor_subsys[SLAPD_MONITOR_LOG].mss_ndn, 
+                       monitor_subsys[SLAPD_MONITOR_LOG].mss_ndn.bv_val
                        "", "" );
 #endif
                return( -1 );
        }
 
-       /* initialize the debug level */
+       /* initialize the debug level(s) */
        for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
-               if ( int_2_level[ i ].i & ldap_syslog ) {
-                       val.bv_val = ( char * )int_2_level[ i ].s;
-                       val.bv_len = strlen( val.bv_val );
 
-                       attr_merge( e, monitor_ad_desc, bv );
+               if ( mi->mi_ad_managedInfo->ad_type->sat_equality->smr_normalize ) {
+                       int     rc;
+
+                       rc = (*mi->mi_ad_managedInfo->ad_type->sat_equality->smr_normalize)(
+                                       SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
+                                       mi->mi_ad_managedInfo->ad_type->sat_syntax,
+                                       mi->mi_ad_managedInfo->ad_type->sat_equality,
+                                       &int_2_level[ i ].s,
+                                       &int_2_level[ i ].n, NULL );
+                       if ( rc ) {
+                               return( -1 );
+                       }
+               }
+
+               if ( int_2_level[ i ].i & ldap_syslog ) {
+                       attr_merge_one( e, mi->mi_ad_managedInfo,
+                                       &int_2_level[ i ].s,
+                                       &int_2_level[ i ].n );
                }
        }
 
+       attr_merge( e, mi->mi_ad_description, desc, NULL );
+
        monitor_cache_release( mi, e );
 
        return( 0 );
@@ -130,14 +136,15 @@ monitor_subsys_log_init(
 
 int 
 monitor_subsys_log_modify( 
-       struct monitorinfo      *mi,
-       Entry                   *e,
-       Modifications           *modlist
+       Operation               *op,
+       Entry                   *e
 )
 {
+       struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
        int             rc = LDAP_OTHER;
        int             newlevel = ldap_syslog;
        Attribute       *save_attrs;
+       Modifications   *modlist = op->oq_modify.rs_modlist;
        Modifications   *ml;
 
        ldap_pvt_thread_mutex_lock( &monitor_log_mutex );
@@ -149,40 +156,21 @@ monitor_subsys_log_modify(
                Modification    *mod = &ml->sml_mod;
 
                /*
-                * Operational attributes
+                * accept all operational attributes
                 */
-#if 0
-               if ( mod->sm_desc == slap_schema.si_ad_modifyTimestamp 
-                       || mod->sm_desc == slap_schema.si_ad_modifiersName ) {
-                       ( void ) attr_delete( &e->e_attrs, mod->sm_desc );
-                       rc = attr_merge( e, mod->sm_desc, mod->sm_bvalues );
-                       if ( rc != 0 ) {
-                               rc = LDAP_OTHER;
-                               break;
-                       }
-                       continue;
-
-               /*
-                * unhandled operational attributes
-                */
-               } else if ( is_at_operational( mod->sm_desc->ad_type ) ) {
-                       continue;
-
-#else
                if ( is_at_operational( mod->sm_desc->ad_type ) ) {
                        ( void ) attr_delete( &e->e_attrs, mod->sm_desc );
-                       rc = attr_merge( e, mod->sm_desc, mod->sm_bvalues );
+                       rc = attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues );
                        if ( rc != 0 ) {
                                rc = LDAP_OTHER;
                                break;
                        }
                        continue;
 
-#endif
                /*
                 * only the monitor description attribute can be modified
                 */
-               } else if ( mod->sm_desc != monitor_ad_desc ) {
+               } else if ( mod->sm_desc != mi->mi_ad_managedInfo) {
                        rc = LDAP_UNWILLING_TO_PERFORM;
                        break;
                }
@@ -201,7 +189,7 @@ monitor_subsys_log_modify(
                        break;
 
                default:
-                       rc = LDAP_OPERATIONS_ERROR;
+                       rc = LDAP_OTHER;
                        break;
                }
 
@@ -212,30 +200,29 @@ monitor_subsys_log_modify(
 
        /* set the new debug level */
        if ( rc == LDAP_SUCCESS ) {
-               const char *text;
-               static char textbuf[1024];
+               const char      *text;
+               static char     textbuf[ BACKMONITOR_BUFSIZE ];
 
-#if 0  /* need op */
                /* check for abandon */
-               ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
                if ( op->o_abandon ) {
-                       ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
                        rc = SLAPD_ABANDON;
 
                        goto cleanup;
                }
-#endif
 
                /* check that the entry still obeys the schema */
-               rc = entry_schema_check( e, save_attrs, &text, textbuf
-                               sizeof( textbuf ) );
+               rc = entry_schema_check( be_monitor, e, save_attrs
+                               &text, textbuf, sizeof( textbuf ) );
                if ( rc != LDAP_SUCCESS ) {
                        goto cleanup;
                }
 
+               /*
+                * Do we need to protect this with a mutex?
+                */
                ldap_syslog = newlevel;
 
-#if 0
+#if 0  /* debug rather than log */
                slap_debug = newlevel;
                lutil_set_debug_level( "slapd", slap_debug );
                ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
@@ -259,12 +246,16 @@ cleanup:;
 }
 
 static int
-loglevel2int( const char *str )
+loglevel2int( struct berval *l )
 {
        int             i;
        
        for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
-               if ( strcasecmp( str, int_2_level[ i ].s ) == 0 ) {
+               if ( l->bv_len != int_2_level[ i ].s.bv_len ) {
+                       continue;
+               }
+
+               if ( strcasecmp( l->bv_val, int_2_level[ i ].s.bv_val ) == 0 ) {
                        return int_2_level[ i ].i;
                }
        }
@@ -272,18 +263,18 @@ loglevel2int( const char *str )
        return 0;
 }
 
-static const char *
+static int
 int2loglevel( int n )
 {
        int             i;
        
        for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
                if ( int_2_level[ i ].i == n ) {
-                       return int_2_level[ i ].s;
+                       return i;
                }
        }
 
-       return NULL;
+       return -1;
 }
 
 static int
@@ -291,20 +282,28 @@ check_constraints( Modification *mod, int *newlevel )
 {
        int             i;
 
-       for ( i = 0; mod->sm_bvalues && mod->sm_bvalues[i] != NULL; i++ ) {
-               int len, l;
-               const char *s;
+       for ( i = 0; mod->sm_values && mod->sm_values[i].bv_val != NULL; i++ ) {
+               int             l;
                
-               l = loglevel2int( mod->sm_bvalues[i]->bv_val );
+               l = loglevel2int( &mod->sm_values[i] );
                if ( !l ) {
                        return LDAP_CONSTRAINT_VIOLATION;
                }
 
-               s = int2loglevel( l );
-               len = strlen( s );
-               assert( len == mod->sm_bvalues[i]->bv_len );
+               if ( ( l = int2loglevel( l ) ) == -1 ) {
+                       return LDAP_OTHER;
+               }
+
+               assert( int_2_level[ l ].s.bv_len
+                               == mod->sm_values[i].bv_len );
                
-               AC_MEMCPY( mod->sm_bvalues[i]->bv_val, s, len );
+               AC_MEMCPY( mod->sm_values[i].bv_val,
+                               int_2_level[ l ].s.bv_val,
+                               int_2_level[ l ].s.bv_len );
+
+               AC_MEMCPY( mod->sm_nvalues[i].bv_val,
+                               int_2_level[ l ].n.bv_val,
+                               int_2_level[ l ].n.bv_len );
 
                *newlevel |= l;
        }
@@ -327,48 +326,45 @@ add_values( Entry *e, Modification *mod, int *newlevel )
        a = attr_find( e->e_attrs, mod->sm_desc );
 
        if ( a != NULL ) {
-               
                /* "description" SHOULD have appropriate rules ... */
                if ( mr == NULL || !mr->smr_match ) {
                        return LDAP_INAPPROPRIATE_MATCHING;
                }
 
-               for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
+               for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
                        int rc;
                        int j;
                        const char *text = NULL;
-                       struct berval *asserted;
+                       struct berval asserted;
 
-                       rc = value_normalize( mod->sm_desc,
-                                       SLAP_MR_EQUALITY,
-                                       mod->sm_bvalues[i],
-                                       &asserted,
-                                       &text );
+                       rc = asserted_value_validate_normalize(
+                               mod->sm_desc, mr, SLAP_MR_EQUALITY,
+                               &mod->sm_values[i], &asserted, &text, NULL );
 
                        if ( rc != LDAP_SUCCESS ) {
                                return rc;
                        }
 
-                       for ( j = 0; a->a_vals[j] != NULL; j++ ) {
+                       for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
                                int match;
                                int rc = value_match( &match, mod->sm_desc, mr,
-                                               SLAP_MR_VALUE_SYNTAX_MATCH,
-                                               a->a_vals[j], asserted, &text );
+                                       0, &a->a_vals[j], &asserted, &text );
 
                                if ( rc == LDAP_SUCCESS && match == 0 ) {
-                                       ber_bvfree( asserted );
+                                       free( asserted.bv_val );
                                        return LDAP_TYPE_OR_VALUE_EXISTS;
                                }
                        }
 
-                       ber_bvfree( asserted );
+                       free( asserted.bv_val );
                }
        }
 
        /* no - add them */
-       if ( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
-               /* this should return result return of attr_merge */
-               return LDAP_OTHER;
+       rc = attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues );
+       if ( rc != LDAP_SUCCESS ) {
+               /* this should return result of attr_mergeit */
+               return rc;
        }
 
        return LDAP_SUCCESS;
@@ -379,8 +375,7 @@ delete_values( Entry *e, Modification *mod, int *newlevel )
 {
        int             i, j, k, found, rc, nl = 0;
        Attribute       *a;
-       char *desc = mod->sm_desc->ad_cname.bv_val;
-       MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
+       MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
 
        rc = check_constraints( mod, &nl );
        if ( rc != LDAP_SUCCESS ) {
@@ -390,7 +385,7 @@ delete_values( Entry *e, Modification *mod, int *newlevel )
        *newlevel &= ~nl;
 
        /* delete the entire attribute */
-       if ( mod->sm_bvalues == NULL ) {
+       if ( mod->sm_values == NULL ) {
                int rc = attr_delete( &e->e_attrs, mod->sm_desc );
 
                if ( rc ) {
@@ -414,26 +409,24 @@ delete_values( Entry *e, Modification *mod, int *newlevel )
        }
 
        /* find each value to delete */
-       for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
+       for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
                int rc;
                const char *text = NULL;
 
-               struct berval *asserted;
+               struct berval asserted;
 
-               rc = value_normalize( mod->sm_desc,
-                               SLAP_MR_EQUALITY,
-                               mod->sm_bvalues[i],
-                               &asserted,
-                               &text );
+               rc = asserted_value_validate_normalize(
+                               mod->sm_desc, mr, SLAP_MR_EQUALITY,
+                               &mod->sm_values[i], &asserted, &text, NULL );
 
                if( rc != LDAP_SUCCESS ) return rc;
 
                found = 0;
-               for ( j = 0; a->a_vals[j] != NULL; j++ ) {
+               for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
                        int match;
                        int rc = value_match( &match, mod->sm_desc, mr,
-                                       SLAP_MR_VALUE_SYNTAX_MATCH,
-                                       a->a_vals[j], asserted, &text );
+                               0,
+                               &a->a_vals[j], &asserted, &text );
 
                        if( rc == LDAP_SUCCESS && match != 0 ) {
                                continue;
@@ -443,16 +436,16 @@ delete_values( Entry *e, Modification *mod, int *newlevel )
                        found = 1;
 
                        /* delete it */
-                       ber_bvfree( a->a_vals[j] );
-                       for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
+                       free( a->a_vals[j].bv_val );
+                       for ( k = j + 1; a->a_vals[k].bv_val != NULL; k++ ) {
                                a->a_vals[k - 1] = a->a_vals[k];
                        }
-                       a->a_vals[k - 1] = NULL;
+                       a->a_vals[k - 1].bv_val = NULL;
 
                        break;
                }
 
-               ber_bvfree( asserted );
+               free( asserted.bv_val );
 
                /* looked through them all w/o finding it */
                if ( ! found ) {
@@ -461,7 +454,7 @@ delete_values( Entry *e, Modification *mod, int *newlevel )
        }
 
        /* if no values remain, delete the entire attribute */
-       if ( a->a_vals[0] == NULL ) {
+       if ( a->a_vals[0].bv_val == NULL ) {
                /* should already be zero */
                *newlevel = 0;
                
@@ -476,7 +469,7 @@ delete_values( Entry *e, Modification *mod, int *newlevel )
 static int
 replace_values( Entry *e, Modification *mod, int *newlevel )
 {
-       int i, rc;
+       int rc;
 
        *newlevel = 0;
        rc = check_constraints( mod, newlevel );
@@ -490,9 +483,11 @@ replace_values( Entry *e, Modification *mod, int *newlevel )
                return rc;
        }
 
-       if ( mod->sm_bvalues != NULL &&
-               attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
-               return LDAP_OTHER;
+       if ( mod->sm_values != NULL ) {
+               rc = attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues );
+               if ( rc != LDAP_SUCCESS ) {
+                       return rc;
+               }
        }
 
        return LDAP_SUCCESS;