Back to index

openldap  2.4.31
operation.c
Go to the documentation of this file.
00001 /* operation.c - deal with operation subsystem */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 2001-2012 The OpenLDAP Foundation.
00006  * Portions Copyright 2001-2003 Pierangelo Masarati.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted only as authorized by the OpenLDAP
00011  * Public License.
00012  *
00013  * A copy of this license is available in file LICENSE in the
00014  * top-level directory of the distribution or, alternatively, at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 /* ACKNOWLEDGEMENTS:
00018  * This work was initially developed by Pierangelo Masarati for inclusion
00019  * in OpenLDAP Software.
00020  */
00021 
00022 #include "portable.h"
00023 
00024 #include <stdio.h>
00025 #include <ac/string.h>
00026 
00027 #include "slap.h"
00028 #include "back-monitor.h"
00029 #include "lber_pvt.h"
00030 
00031 struct monitor_ops_t {
00032        struct berval rdn;
00033        struct berval nrdn;
00034 } monitor_op[] = {
00035        { BER_BVC( "cn=Bind" ),            BER_BVNULL },
00036        { BER_BVC( "cn=Unbind" ),   BER_BVNULL },
00037        { BER_BVC( "cn=Search" ),   BER_BVNULL },
00038        { BER_BVC( "cn=Compare" ),  BER_BVNULL },
00039        { BER_BVC( "cn=Modify" ),   BER_BVNULL },
00040        { BER_BVC( "cn=Modrdn" ),   BER_BVNULL },
00041        { BER_BVC( "cn=Add" ),             BER_BVNULL },
00042        { BER_BVC( "cn=Delete" ),   BER_BVNULL },
00043        { BER_BVC( "cn=Abandon" ),  BER_BVNULL },
00044        { BER_BVC( "cn=Extended" ), BER_BVNULL },
00045        { BER_BVNULL,               BER_BVNULL }
00046 };
00047 
00048 static int
00049 monitor_subsys_ops_destroy(
00050        BackendDB            *be,
00051        monitor_subsys_t     *ms );
00052 
00053 static int
00054 monitor_subsys_ops_update(
00055        Operation            *op,
00056        SlapReply            *rs,
00057        Entry                   *e );
00058 
00059 int
00060 monitor_subsys_ops_init(
00061        BackendDB            *be,
00062        monitor_subsys_t     *ms )
00063 {
00064        monitor_info_t       *mi;
00065        
00066        Entry         *e_op, **ep;
00067        monitor_entry_t      *mp;
00068        int           i;
00069        struct berval bv_zero = BER_BVC( "0" );
00070 
00071        assert( be != NULL );
00072 
00073        ms->mss_destroy = monitor_subsys_ops_destroy;
00074        ms->mss_update = monitor_subsys_ops_update;
00075 
00076        mi = ( monitor_info_t * )be->be_private;
00077 
00078        if ( monitor_cache_get( mi,
00079                      &ms->mss_ndn, &e_op ) )
00080        {
00081               Debug( LDAP_DEBUG_ANY,
00082                      "monitor_subsys_ops_init: "
00083                      "unable to get entry \"%s\"\n",
00084                      ms->mss_ndn.bv_val, 
00085                      0, 0 );
00086               return( -1 );
00087        }
00088 
00089        attr_merge_one( e_op, mi->mi_ad_monitorOpInitiated, &bv_zero, NULL );
00090        attr_merge_one( e_op, mi->mi_ad_monitorOpCompleted, &bv_zero, NULL );
00091 
00092        mp = ( monitor_entry_t * )e_op->e_private;
00093        mp->mp_children = NULL;
00094        ep = &mp->mp_children;
00095 
00096        for ( i = 0; i < SLAP_OP_LAST; i++ ) {
00097               struct berval rdn;
00098               Entry         *e;
00099               struct berval bv;
00100 
00101               /*
00102                * Initiated ops
00103                */
00104               e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &monitor_op[i].rdn,
00105                      mi->mi_oc_monitorOperation, mi, NULL, NULL );
00106 
00107               if ( e == NULL ) {
00108                      Debug( LDAP_DEBUG_ANY,
00109                             "monitor_subsys_ops_init: "
00110                             "unable to create entry \"%s,%s\"\n",
00111                             monitor_op[ i ].rdn.bv_val,
00112                             ms->mss_ndn.bv_val, 0 );
00113                      return( -1 );
00114               }
00115 
00116               BER_BVSTR( &bv, "0" );
00117               attr_merge_one( e, mi->mi_ad_monitorOpInitiated, &bv, NULL );
00118               attr_merge_one( e, mi->mi_ad_monitorOpCompleted, &bv, NULL );
00119 
00120               /* steal normalized RDN */
00121               dnRdn( &e->e_nname, &rdn );
00122               ber_dupbv( &monitor_op[ i ].nrdn, &rdn );
00123        
00124               mp = monitor_entrypriv_create();
00125               if ( mp == NULL ) {
00126                      return -1;
00127               }
00128               e->e_private = ( void * )mp;
00129               mp->mp_info = ms;
00130               mp->mp_flags = ms->mss_flags \
00131                      | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
00132 
00133               if ( monitor_cache_add( mi, e ) ) {
00134                      Debug( LDAP_DEBUG_ANY,
00135                             "monitor_subsys_ops_init: "
00136                             "unable to add entry \"%s,%s\"\n",
00137                             monitor_op[ i ].rdn.bv_val,
00138                             ms->mss_ndn.bv_val, 0 );
00139                      return( -1 );
00140               }
00141 
00142               *ep = e;
00143               ep = &mp->mp_next;
00144        }
00145 
00146        monitor_cache_release( mi, e_op );
00147 
00148        return( 0 );
00149 }
00150 
00151 static int
00152 monitor_subsys_ops_destroy(
00153        BackendDB            *be,
00154        monitor_subsys_t     *ms )
00155 {
00156        int           i;
00157 
00158        for ( i = 0; i < SLAP_OP_LAST; i++ ) {
00159               if ( !BER_BVISNULL( &monitor_op[ i ].nrdn ) ) {
00160                      ch_free( monitor_op[ i ].nrdn.bv_val );
00161               }
00162        }
00163 
00164        return 0;
00165 }
00166 
00167 static int
00168 monitor_subsys_ops_update(
00169        Operation            *op,
00170        SlapReply            *rs,
00171        Entry                   *e )
00172 {
00173        monitor_info_t              *mi = ( monitor_info_t * )op->o_bd->be_private;
00174 
00175        ldap_pvt_mp_t        nInitiated = LDAP_PVT_MP_INIT,
00176                             nCompleted = LDAP_PVT_MP_INIT;
00177        struct berval        rdn;
00178        int                  i;
00179        Attribute            *a;
00180        slap_counters_t *sc;
00181        static struct berval bv_ops = BER_BVC( "cn=operations" );
00182 
00183        assert( mi != NULL );
00184        assert( e != NULL );
00185 
00186        dnRdn( &e->e_nname, &rdn );
00187 
00188        if ( dn_match( &rdn, &bv_ops ) ) {
00189               ldap_pvt_mp_init( nInitiated );
00190               ldap_pvt_mp_init( nCompleted );
00191 
00192               ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
00193               for ( i = 0; i < SLAP_OP_LAST; i++ ) {
00194                      ldap_pvt_mp_add( nInitiated, slap_counters.sc_ops_initiated_[ i ] );
00195                      ldap_pvt_mp_add( nCompleted, slap_counters.sc_ops_completed_[ i ] );
00196               }
00197               for ( sc = slap_counters.sc_next; sc; sc = sc->sc_next ) {
00198                      ldap_pvt_thread_mutex_lock( &sc->sc_mutex );
00199                      for ( i = 0; i < SLAP_OP_LAST; i++ ) {
00200                             ldap_pvt_mp_add( nInitiated, sc->sc_ops_initiated_[ i ] );
00201                             ldap_pvt_mp_add( nCompleted, sc->sc_ops_completed_[ i ] );
00202                      }
00203                      ldap_pvt_thread_mutex_unlock( &sc->sc_mutex );
00204               }
00205               ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
00206               
00207        } else {
00208               for ( i = 0; i < SLAP_OP_LAST; i++ ) {
00209                      if ( dn_match( &rdn, &monitor_op[ i ].nrdn ) )
00210                      {
00211                             ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
00212                             ldap_pvt_mp_init_set( nInitiated, slap_counters.sc_ops_initiated_[ i ] );
00213                             ldap_pvt_mp_init_set( nCompleted, slap_counters.sc_ops_completed_[ i ] );
00214                             for ( sc = slap_counters.sc_next; sc; sc = sc->sc_next ) {
00215                                    ldap_pvt_thread_mutex_lock( &sc->sc_mutex );
00216                                    ldap_pvt_mp_add( nInitiated, sc->sc_ops_initiated_[ i ] );
00217                                    ldap_pvt_mp_add( nCompleted, sc->sc_ops_completed_[ i ] );
00218                                    ldap_pvt_thread_mutex_unlock( &sc->sc_mutex );
00219                             }
00220                             ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
00221                             break;
00222                      }
00223               }
00224 
00225               if ( i == SLAP_OP_LAST ) {
00226                      /* not found ... */
00227                      return( 0 );
00228               }
00229        }
00230 
00231        a = attr_find( e->e_attrs, mi->mi_ad_monitorOpInitiated );
00232        assert ( a != NULL );
00233 
00234        /* NOTE: no minus sign is allowed in the counters... */
00235        UI2BV( &a->a_vals[ 0 ], nInitiated );
00236        ldap_pvt_mp_clear( nInitiated );
00237        
00238        a = attr_find( e->e_attrs, mi->mi_ad_monitorOpCompleted );
00239        assert ( a != NULL );
00240 
00241        /* NOTE: no minus sign is allowed in the counters... */
00242        UI2BV( &a->a_vals[ 0 ], nCompleted );
00243        ldap_pvt_mp_clear( nCompleted );
00244 
00245        /* FIXME: touch modifyTimestamp? */
00246 
00247        return SLAP_CB_CONTINUE;
00248 }
00249