Home | History | Annotate | Line # | Download | only in back-monitor
      1 /*	$NetBSD: rww.c,v 1.4 2025/09/05 21:16:29 christos Exp $	*/
      2 
      3 /* readw.c - deal with read waiters subsystem */
      4 /* $OpenLDAP$ */
      5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      6  *
      7  * Copyright 2001-2024 The OpenLDAP Foundation.
      8  * Portions Copyright 2001-2003 Pierangelo Masarati.
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted only as authorized by the OpenLDAP
     13  * Public License.
     14  *
     15  * A copy of this license is available in file LICENSE in the
     16  * top-level directory of the distribution or, alternatively, at
     17  * <http://www.OpenLDAP.org/license.html>.
     18  */
     19 /* ACKNOWLEDGEMENTS:
     20  * This work was initially developed by Pierangelo Masarati for inclusion
     21  * in OpenLDAP Software.
     22  */
     23 
     24 #include <sys/cdefs.h>
     25 __RCSID("$NetBSD: rww.c,v 1.4 2025/09/05 21:16:29 christos Exp $");
     26 
     27 #include "portable.h"
     28 
     29 #include <stdio.h>
     30 #include <ac/string.h>
     31 
     32 #include "slap.h"
     33 #include "lutil.h"
     34 #include "back-monitor.h"
     35 
     36 static int
     37 monitor_subsys_rww_destroy(
     38 	BackendDB		*be,
     39 	monitor_subsys_t	*ms );
     40 
     41 static int
     42 monitor_subsys_rww_update(
     43 	Operation		*op,
     44 	SlapReply		*rs,
     45 	Entry                   *e );
     46 
     47 enum {
     48 	MONITOR_RWW_READ = 0,
     49 	MONITOR_RWW_WRITE,
     50 
     51 	MONITOR_RWW_LAST
     52 };
     53 
     54 static struct monitor_rww_t {
     55 	struct berval	rdn;
     56 	struct berval	nrdn;
     57 } monitor_rww[] = {
     58 	{ BER_BVC("cn=Read"),		BER_BVNULL },
     59 	{ BER_BVC("cn=Write"),		BER_BVNULL },
     60 	{ BER_BVNULL,			BER_BVNULL }
     61 };
     62 
     63 int
     64 monitor_subsys_rww_init(
     65 	BackendDB		*be,
     66 	monitor_subsys_t	*ms )
     67 {
     68 	monitor_info_t	*mi;
     69 
     70 	Entry		*e_conn;
     71 	monitor_entry_t	*mp;
     72 	int			i;
     73 
     74 	assert( be != NULL );
     75 
     76 	ms->mss_destroy = monitor_subsys_rww_destroy;
     77 	ms->mss_update = monitor_subsys_rww_update;
     78 
     79 	mi = ( monitor_info_t * )be->be_private;
     80 
     81 	if ( monitor_cache_get( mi, &ms->mss_ndn, &e_conn ) ) {
     82 		Debug( LDAP_DEBUG_ANY,
     83 			"monitor_subsys_rww_init: "
     84 			"unable to get entry \"%s\"\n",
     85 			ms->mss_ndn.bv_val );
     86 		return( -1 );
     87 	}
     88 
     89 	for ( i = 0; i < MONITOR_RWW_LAST; i++ ) {
     90 		struct berval		nrdn, bv;
     91 		Entry			*e;
     92 
     93 		e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &monitor_rww[i].rdn,
     94 			mi->mi_oc_monitorCounterObject, NULL, NULL );
     95 		if ( e == NULL ) {
     96 			Debug( LDAP_DEBUG_ANY,
     97 				"monitor_subsys_rww_init: "
     98 				"unable to create entry \"cn=Read,%s\"\n",
     99 				ms->mss_ndn.bv_val );
    100 			return( -1 );
    101 		}
    102 
    103 		/* steal normalized RDN */
    104 		dnRdn( &e->e_nname, &nrdn );
    105 		ber_dupbv( &monitor_rww[ i ].nrdn, &nrdn );
    106 
    107 		BER_BVSTR( &bv, "0" );
    108 		attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
    109 
    110 		mp = monitor_entrypriv_create();
    111 		if ( mp == NULL ) {
    112 			return -1;
    113 		}
    114 		e->e_private = ( void * )mp;
    115 		mp->mp_info = ms;
    116 		mp->mp_flags = ms->mss_flags \
    117 			| MONITOR_F_SUB | MONITOR_F_PERSISTENT;
    118 
    119 		if ( monitor_cache_add( mi, e, e_conn ) ) {
    120 			Debug( LDAP_DEBUG_ANY,
    121 				"monitor_subsys_rww_init: "
    122 				"unable to add entry \"%s,%s\"\n",
    123 				monitor_rww[ i ].rdn.bv_val,
    124 				ms->mss_ndn.bv_val );
    125 			return( -1 );
    126 		}
    127 	}
    128 
    129 	monitor_cache_release( mi, e_conn );
    130 
    131 	return( 0 );
    132 }
    133 
    134 static int
    135 monitor_subsys_rww_destroy(
    136 	BackendDB		*be,
    137 	monitor_subsys_t	*ms )
    138 {
    139 	int		i;
    140 
    141 	for ( i = 0; i < MONITOR_RWW_LAST; i++ ) {
    142 		ber_memfree_x( monitor_rww[ i ].nrdn.bv_val, NULL );
    143 	}
    144 
    145 	return 0;
    146 }
    147 
    148 static int
    149 monitor_subsys_rww_update(
    150 	Operation		*op,
    151 	SlapReply		*rs,
    152 	Entry                   *e )
    153 {
    154 	monitor_info_t *mi = (monitor_info_t *)op->o_bd->be_private;
    155 	Connection	*c;
    156 	ber_socket_t	connindex;
    157 	long		nconns, nwritewaiters, nreadwaiters;
    158 
    159 	int		i;
    160 	struct berval	nrdn;
    161 
    162 	Attribute	*a;
    163 	char 		buf[LDAP_PVT_INTTYPE_CHARS(long)];
    164 	long		num = 0;
    165 	ber_len_t	len;
    166 
    167 	assert( mi != NULL );
    168 	assert( e != NULL );
    169 
    170 	dnRdn( &e->e_nname, &nrdn );
    171 
    172 	for ( i = 0; !BER_BVISNULL( &monitor_rww[ i ].nrdn ); i++ ) {
    173 		if ( dn_match( &nrdn, &monitor_rww[ i ].nrdn ) ) {
    174 			break;
    175 		}
    176 	}
    177 
    178 	if ( i == MONITOR_RWW_LAST ) {
    179 		return SLAP_CB_CONTINUE;
    180 	}
    181 
    182 	nconns = nwritewaiters = nreadwaiters = 0;
    183 	for ( c = connection_first( &connindex );
    184 			c != NULL;
    185 			c = connection_next( c, &connindex ), nconns++ )
    186 	{
    187 		if ( c->c_writewaiter ) {
    188 			nwritewaiters++;
    189 		}
    190 
    191 		/* FIXME: ?!? */
    192 		if ( c->c_currentber != NULL ) {
    193 			nreadwaiters++;
    194 		}
    195 	}
    196 	connection_done(c);
    197 
    198 	switch ( i ) {
    199 	case MONITOR_RWW_READ:
    200 		num = nreadwaiters;
    201 		break;
    202 
    203 	case MONITOR_RWW_WRITE:
    204 		num = nwritewaiters;
    205 		break;
    206 
    207 	default:
    208 		assert( 0 );
    209 	}
    210 
    211 	snprintf( buf, sizeof( buf ), "%ld", num );
    212 
    213 	a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
    214 	assert( a != NULL );
    215 	len = strlen( buf );
    216 	if ( len > a->a_vals[ 0 ].bv_len ) {
    217 		a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
    218 		if ( BER_BVISNULL( &a->a_vals[ 0 ] ) ) {
    219 			BER_BVZERO( &a->a_vals[ 0 ] );
    220 			return SLAP_CB_CONTINUE;
    221 		}
    222 	}
    223 	AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
    224 	a->a_vals[ 0 ].bv_len = len;
    225 
    226 	/* FIXME: touch modifyTimestamp? */
    227 
    228 	return SLAP_CB_CONTINUE;
    229 }
    230 
    231