| 1 |
/* readw.c - deal with read waiters 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 "lutil.h" |
| 29 |
#include "back-monitor.h" |
| 30 |
|
| 31 |
static int |
| 32 |
monitor_subsys_rww_destroy( |
| 33 |
BackendDB *be, |
| 34 |
monitor_subsys_t *ms ); |
| 35 |
|
| 36 |
static int |
| 37 |
monitor_subsys_rww_update( |
| 38 |
Operation *op, |
| 39 |
SlapReply *rs, |
| 40 |
Entry *e ); |
| 41 |
|
| 42 |
enum { |
| 43 |
MONITOR_RWW_READ = 0, |
| 44 |
MONITOR_RWW_WRITE, |
| 45 |
|
| 46 |
MONITOR_RWW_LAST |
| 47 |
}; |
| 48 |
|
| 49 |
static struct monitor_rww_t { |
| 50 |
struct berval rdn; |
| 51 |
struct berval nrdn; |
| 52 |
} monitor_rww[] = { |
| 53 |
{ BER_BVC("cn=Read"), BER_BVNULL }, |
| 54 |
{ BER_BVC("cn=Write"), BER_BVNULL }, |
| 55 |
{ BER_BVNULL, BER_BVNULL } |
| 56 |
}; |
| 57 |
|
| 58 |
int |
| 59 |
monitor_subsys_rww_init( |
| 60 |
BackendDB *be, |
| 61 |
monitor_subsys_t *ms ) |
| 62 |
{ |
| 63 |
monitor_info_t *mi; |
| 64 |
|
| 65 |
Entry *e_conn; |
| 66 |
monitor_entry_t *mp; |
| 67 |
int i; |
| 68 |
|
| 69 |
assert( be != NULL ); |
| 70 |
|
| 71 |
ms->mss_destroy = monitor_subsys_rww_destroy; |
| 72 |
ms->mss_update = monitor_subsys_rww_update; |
| 73 |
|
| 74 |
mi = ( monitor_info_t * )be->be_private; |
| 75 |
|
| 76 |
if ( monitor_cache_get( mi, &ms->mss_ndn, &e_conn ) ) { |
| 77 |
Debug( LDAP_DEBUG_ANY, |
| 78 |
"monitor_subsys_rww_init: " |
| 79 |
"unable to get entry \"%s\"\n", |
| 80 |
ms->mss_ndn.bv_val ); |
| 81 |
return( -1 ); |
| 82 |
} |
| 83 |
|
| 84 |
for ( i = 0; i < MONITOR_RWW_LAST; i++ ) { |
| 85 |
struct berval nrdn, bv; |
| 86 |
Entry *e; |
| 87 |
|
| 88 |
e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &monitor_rww[i].rdn, |
| 89 |
mi->mi_oc_monitorCounterObject, NULL, NULL ); |
| 90 |
if ( e == NULL ) { |
| 91 |
Debug( LDAP_DEBUG_ANY, |
| 92 |
"monitor_subsys_rww_init: " |
| 93 |
"unable to create entry \"cn=Read,%s\"\n", |
| 94 |
ms->mss_ndn.bv_val ); |
| 95 |
return( -1 ); |
| 96 |
} |
| 97 |
|
| 98 |
/* steal normalized RDN */ |
| 99 |
dnRdn( &e->e_nname, &nrdn ); |
| 100 |
ber_dupbv( &monitor_rww[ i ].nrdn, &nrdn ); |
| 101 |
|
| 102 |
BER_BVSTR( &bv, "0" ); |
| 103 |
attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL ); |
| 104 |
|
| 105 |
mp = monitor_entrypriv_create(); |
| 106 |
if ( mp == NULL ) { |
| 107 |
return -1; |
| 108 |
} |
| 109 |
e->e_private = ( void * )mp; |
| 110 |
mp->mp_info = ms; |
| 111 |
mp->mp_flags = ms->mss_flags \ |
| 112 |
| MONITOR_F_SUB | MONITOR_F_PERSISTENT; |
| 113 |
|
| 114 |
if ( monitor_cache_add( mi, e, e_conn ) ) { |
| 115 |
Debug( LDAP_DEBUG_ANY, |
| 116 |
"monitor_subsys_rww_init: " |
| 117 |
"unable to add entry \"%s,%s\"\n", |
| 118 |
monitor_rww[ i ].rdn.bv_val, |
| 119 |
ms->mss_ndn.bv_val ); |
| 120 |
return( -1 ); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
monitor_cache_release( mi, e_conn ); |
| 125 |
|
| 126 |
return( 0 ); |
| 127 |
} |
| 128 |
|
| 129 |
static int |
| 130 |
monitor_subsys_rww_destroy( |
| 131 |
BackendDB *be, |
| 132 |
monitor_subsys_t *ms ) |
| 133 |
{ |
| 134 |
int i; |
| 135 |
|
| 136 |
for ( i = 0; i < MONITOR_RWW_LAST; i++ ) { |
| 137 |
ber_memfree_x( monitor_rww[ i ].nrdn.bv_val, NULL ); |
| 138 |
} |
| 139 |
|
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
static int |
| 144 |
monitor_subsys_rww_update( |
| 145 |
Operation *op, |
| 146 |
SlapReply *rs, |
| 147 |
Entry *e ) |
| 148 |
{ |
| 149 |
monitor_info_t *mi = (monitor_info_t *)op->o_bd->be_private; |
| 150 |
Connection *c; |
| 151 |
ber_socket_t connindex; |
| 152 |
long nconns, nwritewaiters, nreadwaiters; |
| 153 |
|
| 154 |
int i; |
| 155 |
struct berval nrdn; |
| 156 |
|
| 157 |
Attribute *a; |
| 158 |
char buf[LDAP_PVT_INTTYPE_CHARS(long)]; |
| 159 |
long num = 0; |
| 160 |
ber_len_t len; |
| 161 |
|
| 162 |
assert( mi != NULL ); |
| 163 |
assert( e != NULL ); |
| 164 |
|
| 165 |
dnRdn( &e->e_nname, &nrdn ); |
| 166 |
|
| 167 |
for ( i = 0; !BER_BVISNULL( &monitor_rww[ i ].nrdn ); i++ ) { |
| 168 |
if ( dn_match( &nrdn, &monitor_rww[ i ].nrdn ) ) { |
| 169 |
break; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
if ( i == MONITOR_RWW_LAST ) { |
| 174 |
return SLAP_CB_CONTINUE; |
| 175 |
} |
| 176 |
|
| 177 |
nconns = nwritewaiters = nreadwaiters = 0; |
| 178 |
for ( c = connection_first( &connindex ); |
| 179 |
c != NULL; |
| 180 |
c = connection_next( c, &connindex ), nconns++ ) |
| 181 |
{ |
| 182 |
if ( c->c_writewaiter ) { |
| 183 |
nwritewaiters++; |
| 184 |
} |
| 185 |
|
| 186 |
/* FIXME: ?!? */ |
| 187 |
if ( c->c_currentber != NULL ) { |
| 188 |
nreadwaiters++; |
| 189 |
} |
| 190 |
} |
| 191 |
connection_done(c); |
| 192 |
|
| 193 |
switch ( i ) { |
| 194 |
case MONITOR_RWW_READ: |
| 195 |
num = nreadwaiters; |
| 196 |
break; |
| 197 |
|
| 198 |
case MONITOR_RWW_WRITE: |
| 199 |
num = nwritewaiters; |
| 200 |
break; |
| 201 |
|
| 202 |
default: |
| 203 |
assert( 0 ); |
| 204 |
} |
| 205 |
|
| 206 |
snprintf( buf, sizeof( buf ), "%ld", num ); |
| 207 |
|
| 208 |
a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter ); |
| 209 |
assert( a != NULL ); |
| 210 |
len = strlen( buf ); |
| 211 |
if ( len > a->a_vals[ 0 ].bv_len ) { |
| 212 |
a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 ); |
| 213 |
if ( BER_BVISNULL( &a->a_vals[ 0 ] ) ) { |
| 214 |
BER_BVZERO( &a->a_vals[ 0 ] ); |
| 215 |
return SLAP_CB_CONTINUE; |
| 216 |
} |
| 217 |
} |
| 218 |
AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 ); |
| 219 |
a->a_vals[ 0 ].bv_len = len; |
| 220 |
|
| 221 |
/* FIXME: touch modifyTimestamp? */ |
| 222 |
|
| 223 |
return SLAP_CB_CONTINUE; |
| 224 |
} |
| 225 |
|