1 /*	$NetBSD: sent.c,v 1.3 2021/08/14 16:15:00 christos Exp $	*/
2 
3 /* sent.c - deal with data sent 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: sent.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 "back-monitor.h"
34 
35 static int
36 monitor_subsys_sent_destroy(
37 	BackendDB		*be,
38 	monitor_subsys_t	*ms );
39 
40 static int
41 monitor_subsys_sent_update(
42 	Operation		*op,
43 	SlapReply		*rs,
44 	Entry                   *e );
45 
46 enum {
47 	MONITOR_SENT_BYTES = 0,
48 	MONITOR_SENT_PDU,
49 	MONITOR_SENT_ENTRIES,
50 	MONITOR_SENT_REFERRALS,
51 
52 	MONITOR_SENT_LAST
53 };
54 
55 struct monitor_sent_t {
56 	struct berval	rdn;
57 	struct berval	nrdn;
58 } monitor_sent[] = {
59 	{ BER_BVC("cn=Bytes"),		BER_BVNULL },
60 	{ BER_BVC("cn=PDU"),		BER_BVNULL },
61 	{ BER_BVC("cn=Entries"),	BER_BVNULL },
62 	{ BER_BVC("cn=Referrals"),	BER_BVNULL },
63 	{ BER_BVNULL,			BER_BVNULL }
64 };
65 
66 int
monitor_subsys_sent_init(BackendDB * be,monitor_subsys_t * ms)67 monitor_subsys_sent_init(
68 	BackendDB		*be,
69 	monitor_subsys_t	*ms )
70 {
71 	monitor_info_t	*mi;
72 
73 	Entry		**ep, *e_sent;
74 	monitor_entry_t	*mp;
75 	int			i;
76 
77 	assert( be != NULL );
78 
79 	ms->mss_destroy = monitor_subsys_sent_destroy;
80 	ms->mss_update = monitor_subsys_sent_update;
81 
82 	mi = ( monitor_info_t * )be->be_private;
83 
84 	if ( monitor_cache_get( mi, &ms->mss_ndn, &e_sent ) ) {
85 		Debug( LDAP_DEBUG_ANY,
86 			"monitor_subsys_sent_init: "
87 			"unable to get entry \"%s\"\n",
88 			ms->mss_ndn.bv_val );
89 		return( -1 );
90 	}
91 
92 	mp = ( monitor_entry_t * )e_sent->e_private;
93 	mp->mp_children = NULL;
94 	ep = &mp->mp_children;
95 
96 	for ( i = 0; i < MONITOR_SENT_LAST; i++ ) {
97 		struct berval		nrdn, bv;
98 		Entry			*e;
99 
100 		e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn,
101 			&monitor_sent[i].rdn, mi->mi_oc_monitorCounterObject,
102 			NULL, NULL );
103 
104 		if ( e == NULL ) {
105 			Debug( LDAP_DEBUG_ANY,
106 				"monitor_subsys_sent_init: "
107 				"unable to create entry \"%s,%s\"\n",
108 				monitor_sent[ i ].rdn.bv_val,
109 				ms->mss_ndn.bv_val );
110 			return( -1 );
111 		}
112 
113 		/* steal normalized RDN */
114 		dnRdn( &e->e_nname, &nrdn );
115 		ber_dupbv( &monitor_sent[ i ].nrdn, &nrdn );
116 
117 		BER_BVSTR( &bv, "0" );
118 		attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
119 
120 		mp = monitor_entrypriv_create();
121 		if ( mp == NULL ) {
122 			return -1;
123 		}
124 		e->e_private = ( void * )mp;
125 		mp->mp_info = ms;
126 		mp->mp_flags = ms->mss_flags \
127 			| MONITOR_F_SUB | MONITOR_F_PERSISTENT;
128 
129 		if ( monitor_cache_add( mi, e ) ) {
130 			Debug( LDAP_DEBUG_ANY,
131 				"monitor_subsys_sent_init: "
132 				"unable to add entry \"%s,%s\"\n",
133 				monitor_sent[ i ].rdn.bv_val,
134 				ms->mss_ndn.bv_val );
135 			return( -1 );
136 		}
137 
138 		*ep = e;
139 		ep = &mp->mp_next;
140 	}
141 
142 	monitor_cache_release( mi, e_sent );
143 
144 	return( 0 );
145 }
146 
147 static int
monitor_subsys_sent_destroy(BackendDB * be,monitor_subsys_t * ms)148 monitor_subsys_sent_destroy(
149 	BackendDB		*be,
150 	monitor_subsys_t	*ms )
151 {
152 	int		i;
153 
154 	for ( i = 0; i < MONITOR_SENT_LAST; i++ ) {
155 		if ( !BER_BVISNULL( &monitor_sent[ i ].nrdn ) ) {
156 			ch_free( monitor_sent[ i ].nrdn.bv_val );
157 		}
158 	}
159 
160 	return 0;
161 }
162 
163 static int
monitor_subsys_sent_update(Operation * op,SlapReply * rs,Entry * e)164 monitor_subsys_sent_update(
165 	Operation		*op,
166 	SlapReply		*rs,
167 	Entry                   *e )
168 {
169 	monitor_info_t	*mi = ( monitor_info_t *)op->o_bd->be_private;
170 
171 	struct berval		nrdn;
172 	ldap_pvt_mp_t		n;
173 	Attribute		*a;
174 	slap_counters_t *sc;
175 	int			i;
176 
177 	assert( mi != NULL );
178 	assert( e != NULL );
179 
180 	dnRdn( &e->e_nname, &nrdn );
181 
182 	for ( i = 0; i < MONITOR_SENT_LAST; i++ ) {
183 		if ( dn_match( &nrdn, &monitor_sent[ i ].nrdn ) ) {
184 			break;
185 		}
186 	}
187 
188 	if ( i == MONITOR_SENT_LAST ) {
189 		return SLAP_CB_CONTINUE;
190 	}
191 
192 	ldap_pvt_thread_mutex_lock(&slap_counters.sc_mutex);
193 	switch ( i ) {
194 	case MONITOR_SENT_ENTRIES:
195 		ldap_pvt_mp_init_set( n, slap_counters.sc_entries );
196 		for ( sc = slap_counters.sc_next; sc; sc = sc->sc_next ) {
197 			ldap_pvt_thread_mutex_lock( &sc->sc_mutex );
198 			ldap_pvt_mp_add( n, sc->sc_entries );
199 			ldap_pvt_thread_mutex_unlock( &sc->sc_mutex );
200 		}
201 		break;
202 
203 	case MONITOR_SENT_REFERRALS:
204 		ldap_pvt_mp_init_set( n, slap_counters.sc_refs );
205 		for ( sc = slap_counters.sc_next; sc; sc = sc->sc_next ) {
206 			ldap_pvt_thread_mutex_lock( &sc->sc_mutex );
207 			ldap_pvt_mp_add( n, sc->sc_refs );
208 			ldap_pvt_thread_mutex_unlock( &sc->sc_mutex );
209 		}
210 		break;
211 
212 	case MONITOR_SENT_PDU:
213 		ldap_pvt_mp_init_set( n, slap_counters.sc_pdu );
214 		for ( sc = slap_counters.sc_next; sc; sc = sc->sc_next ) {
215 			ldap_pvt_thread_mutex_lock( &sc->sc_mutex );
216 			ldap_pvt_mp_add( n, sc->sc_pdu );
217 			ldap_pvt_thread_mutex_unlock( &sc->sc_mutex );
218 		}
219 		break;
220 
221 	case MONITOR_SENT_BYTES:
222 		ldap_pvt_mp_init_set( n, slap_counters.sc_bytes );
223 		for ( sc = slap_counters.sc_next; sc; sc = sc->sc_next ) {
224 			ldap_pvt_thread_mutex_lock( &sc->sc_mutex );
225 			ldap_pvt_mp_add( n, sc->sc_bytes );
226 			ldap_pvt_thread_mutex_unlock( &sc->sc_mutex );
227 		}
228 		break;
229 
230 	default:
231 		assert(0);
232 	}
233 	ldap_pvt_thread_mutex_unlock(&slap_counters.sc_mutex);
234 
235 	a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
236 	assert( a != NULL );
237 
238 	/* NOTE: no minus sign is allowed in the counters... */
239 	UI2BV( &a->a_vals[ 0 ], n );
240 	ldap_pvt_mp_clear( n );
241 
242 	/* FIXME: touch modifyTimestamp? */
243 
244 	return SLAP_CB_CONTINUE;
245 }
246 
247