1 /* conn.c - deal with connection subsystem */
3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
9 * This work has beed deveolped for the OpenLDAP Foundation
10 * in the hope that it may be useful to the Open Source community,
11 * but WITHOUT ANY WARRANTY.
13 * Permission is granted to anyone to use this software for any purpose
14 * on any computer system, and to alter it and redistribute it, subject
15 * to the following restrictions:
17 * 1. The author and SysNet s.n.c. are not responsible for the consequences
18 * of use of this software, no matter how awful, even if they arise from
21 * 2. The origin of this software must not be misrepresented, either by
22 * explicit claim or by omission. Since few users ever read sources,
23 * credits should appear in the documentation.
25 * 3. Altered versions must be plainly marked as such, and must not be
26 * misrepresented as being the original software. Since few users
27 * ever read sources, credits should appear in the documentation.
28 * SysNet s.n.c. cannot be responsible for the consequences of the
31 * 4. This notice may not be removed or altered.
37 #include <ac/string.h>
41 #include "back-monitor.h"
43 #define CONN_CN_PREFIX "Connection"
46 monitor_subsys_conn_init(
50 struct monitorinfo *mi;
52 Entry *e, *e_tmp, *e_conn;
53 struct monitorentrypriv *mp;
54 char buf[ BACKMONITOR_BUFSIZE ];
59 mi = ( struct monitorinfo * )be->be_private;
61 if ( monitor_cache_get( mi,
62 &monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn, &e_conn ) ) {
64 LDAP_LOG( OPERATION, CRIT,
65 "monitor_subsys_conn_init: "
66 "unable to get entry '%s'\n",
67 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
69 Debug( LDAP_DEBUG_ANY,
70 "monitor_subsys_conn_init: "
71 "unable to get entry '%s'\n%s%s",
72 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
83 snprintf( buf, sizeof( buf ),
86 "structuralObjectClass: %s\n"
88 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
89 mi->oc_monitorCounterObject->soc_cname.bv_val,
90 mi->oc_monitorCounterObject->soc_cname.bv_val );
95 LDAP_LOG( OPERATION, CRIT,
96 "monitor_subsys_conn_init: "
97 "unable to create entry 'cn=Total,%s'\n",
98 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
100 Debug( LDAP_DEBUG_ANY,
101 "monitor_subsys_conn_init: "
102 "unable to create entry 'cn=Total,%s'\n%s%s",
103 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
111 attr_merge_one( e, mi->ad_monitorCounter, &bv, NULL );
113 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
114 e->e_private = ( void * )mp;
116 mp->mp_children = NULL;
117 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
118 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
119 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
120 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
122 if ( monitor_cache_add( mi, e ) ) {
124 LDAP_LOG( OPERATION, CRIT,
125 "monitor_subsys_conn_init: "
126 "unable to add entry 'cn=Total,%s'\n",
127 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
129 Debug( LDAP_DEBUG_ANY,
130 "monitor_subsys_conn_init: "
131 "unable to add entry 'cn=Total,%s'\n%s%s",
132 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
143 snprintf( buf, sizeof( buf ),
144 "dn: cn=Current,%s\n"
146 "structuralObjectClass: %s\n"
148 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
149 mi->oc_monitorCounterObject->soc_cname.bv_val,
150 mi->oc_monitorCounterObject->soc_cname.bv_val );
152 e = str2entry( buf );
155 LDAP_LOG( OPERATION, CRIT,
156 "monitor_subsys_conn_init: "
157 "unable to create entry 'cn=Current,%s'\n",
158 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
160 Debug( LDAP_DEBUG_ANY,
161 "monitor_subsys_conn_init: "
162 "unable to create entry 'cn=Current,%s'\n%s%s",
163 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
171 attr_merge_one( e, mi->ad_monitorCounter, &bv, NULL );
173 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
174 e->e_private = ( void * )mp;
176 mp->mp_children = NULL;
177 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
178 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
179 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
180 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
182 if ( monitor_cache_add( mi, e ) ) {
184 LDAP_LOG( OPERATION, CRIT,
185 "monitor_subsys_conn_init: "
186 "unable to add entry 'cn=Current,%s'\n",
187 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
189 Debug( LDAP_DEBUG_ANY,
190 "monitor_subsys_conn_init: "
191 "unable to add entry 'cn=Current,%s'\n%s%s",
192 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
200 mp = ( struct monitorentrypriv * )e_conn->e_private;
201 mp->mp_children = e_tmp;
203 monitor_cache_release( mi, e_conn );
209 monitor_subsys_conn_update(
214 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
220 if ( strncasecmp( e->e_ndn, "cn=total",
221 sizeof("cn=total")-1 ) == 0 ) {
222 n = connections_nextid();
224 } else if ( strncasecmp( e->e_ndn, "cn=current",
225 sizeof("cn=current")-1 ) == 0 ) {
229 for ( n = 0, c = connection_first( &connindex );
231 n++, c = connection_next( c, &connindex ) ) {
239 char buf[] = "+9223372036854775807L";
241 a = attr_find( e->e_attrs, mi->ad_monitorCounter );
246 snprintf( buf, sizeof( buf ), "%ld", n );
247 free( a->a_vals[ 0 ].bv_val );
248 ber_str2bv( buf, 0, 1, a->a_vals );
256 struct monitorinfo *mi,
261 struct monitorentrypriv *mp;
263 char buf[ BACKMONITOR_BUFSIZE ];
264 char buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
265 char buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
272 assert( ep != NULL );
274 snprintf( buf, sizeof( buf ),
275 "dn: cn=" CONN_CN_PREFIX " %ld,%s\n"
277 "structuralObjectClass: %s\n"
278 "cn: " CONN_CN_PREFIX " %ld\n",
279 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
280 mi->oc_monitorConnection->soc_cname.bv_val,
281 mi->oc_monitorConnection->soc_cname.bv_val,
283 e = str2entry( buf );
287 LDAP_LOG( OPERATION, CRIT,
288 "monitor_subsys_conn_create: "
289 "unable to create entry "
290 "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
291 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
293 Debug( LDAP_DEBUG_ANY,
294 "monitor_subsys_conn_create: "
295 "unable to create entry "
296 "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
298 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
303 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
305 ltm = gmtime( &c->c_starttime );
306 lutil_gentime( buf2, sizeof( buf2 ), ltm );
308 ltm = gmtime( &c->c_activitytime );
309 lutil_gentime( buf3, sizeof( buf3 ), ltm );
311 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
320 ": %s : %s : %s : %s",
322 (long) c->c_protocol,
323 c->c_n_ops_received, c->c_n_ops_executing,
324 c->c_n_ops_pending, c->c_n_ops_completed,
326 /* add low-level counters here */
327 c->c_n_get, c->c_n_read, c->c_n_write,
329 c->c_currentber ? "r" : "",
330 c->c_writewaiter ? "w" : "",
331 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
332 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
333 connection_state2str( c->c_conn_state ),
334 c->c_sasl_bind_in_progress ? "S" : "",
336 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
338 c->c_listener_url.bv_val,
339 c->c_peer_domain.bv_val,
340 c->c_peer_name.bv_val,
341 c->c_sock_name.bv_val,
348 bv.bv_len = strlen( buf );
349 attr_merge_one( e, mi->ad_monitoredInfo, &bv, NULL );
351 /* connection number */
352 snprintf( buf, sizeof( buf ), "%ld", c->c_connid );
354 bv.bv_len = strlen( buf );
355 attr_merge_one( e, mi->ad_monitorConnectionNumber, &bv, NULL );
358 attr_merge_one( e, mi->ad_monitorConnectionAuthzDN,
359 &c->c_dn, &c->c_ndn );
362 attr_merge_one( e, mi->ad_monitorConnectionLocalAddress,
363 &c->c_sock_name, NULL );
366 attr_merge_one( e, mi->ad_monitorConnectionPeerAddress,
367 &c->c_peer_name, NULL );
369 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
370 e->e_private = ( void * )mp;
371 mp->mp_info = &monitor_subsys[ SLAPD_MONITOR_CONN ];
372 mp->mp_children = NULL;
373 mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
381 monitor_subsys_conn_create(
388 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
391 struct monitorentrypriv *mp;
393 assert( mi != NULL );
394 assert( e_parent != NULL );
395 assert( ep != NULL );
400 Entry *e, *e_tmp = NULL;
402 /* create all the children of e_parent */
403 for ( c = connection_first( &connindex );
405 c = connection_next( c, &connindex )) {
406 if ( conn_create( mi, c, &e ) || e == NULL ) {
408 for ( ; e_tmp != NULL; ) {
409 mp = ( struct monitorentrypriv * )e_tmp->e_private;
413 e_tmp->e_private = NULL;
420 mp = ( struct monitorentrypriv * )e->e_private;
429 LDAPRDN values = NULL;
430 const char *text = NULL;
431 unsigned long connid;
433 /* create exactly the required entry */
435 if ( ldap_bv2rdn( ndn, &values, (char **)&text,
436 LDAP_DN_FORMAT_LDAP ) )
442 assert( values[ 0 ] );
444 connid = atol( values[ 0 ]->la_value.bv_val
445 + sizeof( CONN_CN_PREFIX ) );
447 ldap_rdnfree( values );
449 for ( c = connection_first( &connindex );
451 c = connection_next( c, &connindex )) {
452 if ( c->c_connid == connid ) {
453 if ( conn_create( mi, c, ep ) || *ep == NULL ) {
454 connection_done( c );