| 1 |
/* operation.c - deal with operation subsystem */ |
| 2 |
/* $OpenLDAP$ */ |
| 3 |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
| 4 |
* |
| 5 |
* Copyright 2001-2024 The OpenLDAP Foundation. |
| 6 |
* Portions Copyright 2001-2003 Pierangelo Masarati. |
| 7 |
* All rights reserved. |
| 8 |
* |
| 9 |
* Redistribution and use in source and binary forms, with or without |
| 10 |
* modification, are permitted only as authorized by the OpenLDAP |
| 11 |
* Public License. |
| 12 |
* |
| 13 |
* A copy of this license is available in file LICENSE in the |
| 14 |
* top-level directory of the distribution or, alternatively, at |
| 15 |
* <http://www.OpenLDAP.org/license.html>. |
| 16 |
*/ |
| 17 |
/* ACKNOWLEDGEMENTS: |
| 18 |
* This work was initially developed by Pierangelo Masarati for inclusion |
| 19 |
* in OpenLDAP Software. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "portable.h" |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <ac/string.h> |
| 26 |
|
| 27 |
#include "slap.h" |
| 28 |
#include "back-monitor.h" |
| 29 |
#include "lber_pvt.h" |
| 30 |
|
| 31 |
struct monitor_ops_t { |
| 32 |
struct berval rdn; |
| 33 |
struct berval nrdn; |
| 34 |
} monitor_op[] = { |
| 35 |
{ BER_BVC( "cn=Bind" ), BER_BVNULL }, |
| 36 |
{ BER_BVC( "cn=Unbind" ), BER_BVNULL }, |
| 37 |
{ BER_BVC( "cn=Search" ), BER_BVNULL }, |
| 38 |
{ BER_BVC( "cn=Compare" ), BER_BVNULL }, |
| 39 |
{ BER_BVC( "cn=Modify" ), BER_BVNULL }, |
| 40 |
{ BER_BVC( "cn=Modrdn" ), BER_BVNULL }, |
| 41 |
{ BER_BVC( "cn=Add" ), BER_BVNULL }, |
| 42 |
{ BER_BVC( "cn=Delete" ), BER_BVNULL }, |
| 43 |
{ BER_BVC( "cn=Abandon" ), BER_BVNULL }, |
| 44 |
{ BER_BVC( "cn=Extended" ), BER_BVNULL }, |
| 45 |
{ BER_BVNULL, BER_BVNULL } |
| 46 |
}; |
| 47 |
|
| 48 |
static int |
| 49 |
monitor_subsys_ops_destroy( |
| 50 |
BackendDB *be, |
| 51 |
monitor_subsys_t *ms ); |
| 52 |
|
| 53 |
static int |
| 54 |
monitor_subsys_ops_update( |
| 55 |
Operation *op, |
| 56 |
SlapReply *rs, |
| 57 |
Entry *e ); |
| 58 |
|
| 59 |
int |
| 60 |
monitor_subsys_ops_init( |
| 61 |
BackendDB *be, |
| 62 |
monitor_subsys_t *ms ) |
| 63 |
{ |
| 64 |
monitor_info_t *mi; |
| 65 |
|
| 66 |
Entry *e_op; |
| 67 |
monitor_entry_t *mp; |
| 68 |
int i; |
| 69 |
struct berval bv_zero = BER_BVC( "0" ); |
| 70 |
|
| 71 |
assert( be != NULL ); |
| 72 |
|
| 73 |
ms->mss_destroy = monitor_subsys_ops_destroy; |
| 74 |
ms->mss_update = monitor_subsys_ops_update; |
| 75 |
|
| 76 |
mi = ( monitor_info_t * )be->be_private; |
| 77 |
|
| 78 |
if ( monitor_cache_get( mi, |
| 79 |
&ms->mss_ndn, &e_op ) ) |
| 80 |
{ |
| 81 |
Debug( LDAP_DEBUG_ANY, |
| 82 |
"monitor_subsys_ops_init: " |
| 83 |
"unable to get entry \"%s\"\n", |
| 84 |
ms->mss_ndn.bv_val ); |
| 85 |
return( -1 ); |
| 86 |
} |
| 87 |
|
| 88 |
attr_merge_one( e_op, mi->mi_ad_monitorOpInitiated, &bv_zero, NULL ); |
| 89 |
attr_merge_one( e_op, mi->mi_ad_monitorOpCompleted, &bv_zero, NULL ); |
| 90 |
|
| 91 |
for ( i = 0; i < SLAP_OP_LAST; i++ ) { |
| 92 |
struct berval rdn; |
| 93 |
Entry *e; |
| 94 |
struct berval bv; |
| 95 |
|
| 96 |
/* |
| 97 |
* Initiated ops |
| 98 |
*/ |
| 99 |
e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &monitor_op[i].rdn, |
| 100 |
mi->mi_oc_monitorOperation, NULL, NULL ); |
| 101 |
|
| 102 |
if ( e == NULL ) { |
| 103 |
Debug( LDAP_DEBUG_ANY, |
| 104 |
"monitor_subsys_ops_init: " |
| 105 |
"unable to create entry \"%s,%s\"\n", |
| 106 |
monitor_op[ i ].rdn.bv_val, |
| 107 |
ms->mss_ndn.bv_val ); |
| 108 |
return( -1 ); |
| 109 |
} |
| 110 |
|
| 111 |
BER_BVSTR( &bv, "0" ); |
| 112 |
attr_merge_one( e, mi->mi_ad_monitorOpInitiated, &bv, NULL ); |
| 113 |
attr_merge_one( e, mi->mi_ad_monitorOpCompleted, &bv, NULL ); |
| 114 |
|
| 115 |
/* steal normalized RDN */ |
| 116 |
dnRdn( &e->e_nname, &rdn ); |
| 117 |
ber_dupbv( &monitor_op[ i ].nrdn, &rdn ); |
| 118 |
|
| 119 |
mp = monitor_entrypriv_create(); |
| 120 |
if ( mp == NULL ) { |
| 121 |
return -1; |
| 122 |
} |
| 123 |
e->e_private = ( void * )mp; |
| 124 |
mp->mp_info = ms; |
| 125 |
mp->mp_flags = ms->mss_flags \ |
| 126 |
| MONITOR_F_SUB | MONITOR_F_PERSISTENT; |
| 127 |
|
| 128 |
if ( monitor_cache_add( mi, e, e_op ) ) { |
| 129 |
Debug( LDAP_DEBUG_ANY, |
| 130 |
"monitor_subsys_ops_init: " |
| 131 |
"unable to add entry \"%s,%s\"\n", |
| 132 |
monitor_op[ i ].rdn.bv_val, |
| 133 |
ms->mss_ndn.bv_val ); |
| 134 |
return( -1 ); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
monitor_cache_release( mi, e_op ); |
| 139 |
|
| 140 |
return( 0 ); |
| 141 |
} |
| 142 |
|
| 143 |
static int |
| 144 |
monitor_subsys_ops_destroy( |
| 145 |
BackendDB *be, |
| 146 |
monitor_subsys_t *ms ) |
| 147 |
{ |
| 148 |
int i; |
| 149 |
|
| 150 |
for ( i = 0; i < SLAP_OP_LAST; i++ ) { |
| 151 |
if ( !BER_BVISNULL( &monitor_op[ i ].nrdn ) ) { |
| 152 |
ch_free( monitor_op[ i ].nrdn.bv_val ); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
static int |
| 160 |
monitor_subsys_ops_update( |
| 161 |
Operation *op, |
| 162 |
SlapReply *rs, |
| 163 |
Entry *e ) |
| 164 |
{ |
| 165 |
monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private; |
| 166 |
|
| 167 |
ldap_pvt_mp_t nInitiated = LDAP_PVT_MP_INIT, |
| 168 |
nCompleted = LDAP_PVT_MP_INIT; |
| 169 |
struct berval rdn; |
| 170 |
int i; |
| 171 |
Attribute *a; |
| 172 |
slap_counters_t *sc; |
| 173 |
static struct berval bv_ops = BER_BVC( "cn=operations" ); |
| 174 |
|
| 175 |
assert( mi != NULL ); |
| 176 |
assert( e != NULL ); |
| 177 |
|
| 178 |
dnRdn( &e->e_nname, &rdn ); |
| 179 |
|
| 180 |
if ( dn_match( &rdn, &bv_ops ) ) { |
| 181 |
ldap_pvt_mp_init( nInitiated ); |
| 182 |
ldap_pvt_mp_init( nCompleted ); |
| 183 |
|
| 184 |
ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex ); |
| 185 |
ldap_pvt_mp_add( nInitiated, slap_counters.sc_ops_initiated ); |
| 186 |
ldap_pvt_mp_add( nCompleted, slap_counters.sc_ops_completed ); |
| 187 |
for ( sc = slap_counters.sc_next; sc; sc = sc->sc_next ) { |
| 188 |
ldap_pvt_thread_mutex_lock( &sc->sc_mutex ); |
| 189 |
ldap_pvt_mp_add( nInitiated, sc->sc_ops_initiated ); |
| 190 |
ldap_pvt_mp_add( nCompleted, sc->sc_ops_completed ); |
| 191 |
ldap_pvt_thread_mutex_unlock( &sc->sc_mutex ); |
| 192 |
} |
| 193 |
ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex ); |
| 194 |
|
| 195 |
} else { |
| 196 |
for ( i = 0; i < SLAP_OP_LAST; i++ ) { |
| 197 |
if ( dn_match( &rdn, &monitor_op[ i ].nrdn ) ) |
| 198 |
{ |
| 199 |
ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex ); |
| 200 |
ldap_pvt_mp_init_set( nInitiated, slap_counters.sc_ops_initiated_[ i ] ); |
| 201 |
ldap_pvt_mp_init_set( nCompleted, slap_counters.sc_ops_completed_[ i ] ); |
| 202 |
for ( sc = slap_counters.sc_next; sc; sc = sc->sc_next ) { |
| 203 |
ldap_pvt_thread_mutex_lock( &sc->sc_mutex ); |
| 204 |
ldap_pvt_mp_add( nInitiated, sc->sc_ops_initiated_[ i ] ); |
| 205 |
ldap_pvt_mp_add( nCompleted, sc->sc_ops_completed_[ i ] ); |
| 206 |
ldap_pvt_thread_mutex_unlock( &sc->sc_mutex ); |
| 207 |
} |
| 208 |
ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex ); |
| 209 |
break; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
if ( i == SLAP_OP_LAST ) { |
| 214 |
/* not found ... */ |
| 215 |
return( 0 ); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
a = attr_find( e->e_attrs, mi->mi_ad_monitorOpInitiated ); |
| 220 |
assert ( a != NULL ); |
| 221 |
|
| 222 |
/* NOTE: no minus sign is allowed in the counters... */ |
| 223 |
UI2BV( &a->a_vals[ 0 ], nInitiated ); |
| 224 |
ldap_pvt_mp_clear( nInitiated ); |
| 225 |
|
| 226 |
a = attr_find( e->e_attrs, mi->mi_ad_monitorOpCompleted ); |
| 227 |
assert ( a != NULL ); |
| 228 |
|
| 229 |
/* NOTE: no minus sign is allowed in the counters... */ |
| 230 |
UI2BV( &a->a_vals[ 0 ], nCompleted ); |
| 231 |
ldap_pvt_mp_clear( nCompleted ); |
| 232 |
|
| 233 |
/* FIXME: touch modifyTimestamp? */ |
| 234 |
|
| 235 |
return SLAP_CB_CONTINUE; |
| 236 |
} |
| 237 |
|