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