xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/back-monitor/rww.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: rww.c,v 1.3 2021/08/14 16:15:00 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-2021 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.3 2021/08/14 16:15:00 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
monitor_subsys_rww_init(BackendDB * be,monitor_subsys_t * ms)64 monitor_subsys_rww_init(
65 	BackendDB		*be,
66 	monitor_subsys_t	*ms )
67 {
68 	monitor_info_t	*mi;
69 
70 	Entry		**ep, *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 	mp = ( monitor_entry_t * )e_conn->e_private;
90 	mp->mp_children = NULL;
91 	ep = &mp->mp_children;
92 
93 	for ( i = 0; i < MONITOR_RWW_LAST; i++ ) {
94 		struct berval		nrdn, bv;
95 		Entry			*e;
96 
97 		e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &monitor_rww[i].rdn,
98 			mi->mi_oc_monitorCounterObject, NULL, NULL );
99 		if ( e == NULL ) {
100 			Debug( LDAP_DEBUG_ANY,
101 				"monitor_subsys_rww_init: "
102 				"unable to create entry \"cn=Read,%s\"\n",
103 				ms->mss_ndn.bv_val );
104 			return( -1 );
105 		}
106 
107 		/* steal normalized RDN */
108 		dnRdn( &e->e_nname, &nrdn );
109 		ber_dupbv( &monitor_rww[ i ].nrdn, &nrdn );
110 
111 		BER_BVSTR( &bv, "0" );
112 		attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
113 
114 		mp = monitor_entrypriv_create();
115 		if ( mp == NULL ) {
116 			return -1;
117 		}
118 		e->e_private = ( void * )mp;
119 		mp->mp_info = ms;
120 		mp->mp_flags = ms->mss_flags \
121 			| MONITOR_F_SUB | MONITOR_F_PERSISTENT;
122 
123 		if ( monitor_cache_add( mi, e ) ) {
124 			Debug( LDAP_DEBUG_ANY,
125 				"monitor_subsys_rww_init: "
126 				"unable to add entry \"%s,%s\"\n",
127 				monitor_rww[ i ].rdn.bv_val,
128 				ms->mss_ndn.bv_val );
129 			return( -1 );
130 		}
131 
132 		*ep = e;
133 		ep = &mp->mp_next;
134 	}
135 
136 	monitor_cache_release( mi, e_conn );
137 
138 	return( 0 );
139 }
140 
141 static int
monitor_subsys_rww_destroy(BackendDB * be,monitor_subsys_t * ms)142 monitor_subsys_rww_destroy(
143 	BackendDB		*be,
144 	monitor_subsys_t	*ms )
145 {
146 	int		i;
147 
148 	for ( i = 0; i < MONITOR_RWW_LAST; i++ ) {
149 		ber_memfree_x( monitor_rww[ i ].nrdn.bv_val, NULL );
150 	}
151 
152 	return 0;
153 }
154 
155 static int
monitor_subsys_rww_update(Operation * op,SlapReply * rs,Entry * e)156 monitor_subsys_rww_update(
157 	Operation		*op,
158 	SlapReply		*rs,
159 	Entry                   *e )
160 {
161 	monitor_info_t *mi = (monitor_info_t *)op->o_bd->be_private;
162 	Connection	*c;
163 	ber_socket_t	connindex;
164 	long		nconns, nwritewaiters, nreadwaiters;
165 
166 	int		i;
167 	struct berval	nrdn;
168 
169 	Attribute	*a;
170 	char 		buf[LDAP_PVT_INTTYPE_CHARS(long)];
171 	long		num = 0;
172 	ber_len_t	len;
173 
174 	assert( mi != NULL );
175 	assert( e != NULL );
176 
177 	dnRdn( &e->e_nname, &nrdn );
178 
179 	for ( i = 0; !BER_BVISNULL( &monitor_rww[ i ].nrdn ); i++ ) {
180 		if ( dn_match( &nrdn, &monitor_rww[ i ].nrdn ) ) {
181 			break;
182 		}
183 	}
184 
185 	if ( i == MONITOR_RWW_LAST ) {
186 		return SLAP_CB_CONTINUE;
187 	}
188 
189 	nconns = nwritewaiters = nreadwaiters = 0;
190 	for ( c = connection_first( &connindex );
191 			c != NULL;
192 			c = connection_next( c, &connindex ), nconns++ )
193 	{
194 		if ( c->c_writewaiter ) {
195 			nwritewaiters++;
196 		}
197 
198 		/* FIXME: ?!? */
199 		if ( c->c_currentber != NULL ) {
200 			nreadwaiters++;
201 		}
202 	}
203 	connection_done(c);
204 
205 	switch ( i ) {
206 	case MONITOR_RWW_READ:
207 		num = nreadwaiters;
208 		break;
209 
210 	case MONITOR_RWW_WRITE:
211 		num = nwritewaiters;
212 		break;
213 
214 	default:
215 		assert( 0 );
216 	}
217 
218 	snprintf( buf, sizeof( buf ), "%ld", num );
219 
220 	a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
221 	assert( a != NULL );
222 	len = strlen( buf );
223 	if ( len > a->a_vals[ 0 ].bv_len ) {
224 		a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
225 		if ( BER_BVISNULL( &a->a_vals[ 0 ] ) ) {
226 			BER_BVZERO( &a->a_vals[ 0 ] );
227 			return SLAP_CB_CONTINUE;
228 		}
229 	}
230 	AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
231 	a->a_vals[ 0 ].bv_len = len;
232 
233 	/* FIXME: touch modifyTimestamp? */
234 
235 	return SLAP_CB_CONTINUE;
236 }
237 
238