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 "createTimestamp: %s\n"
89 "modifyTimestamp: %s\n",
90 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
91 mi->oc_monitorCounterObject->soc_cname.bv_val,
92 mi->oc_monitorCounterObject->soc_cname.bv_val,
93 mi->mi_startTime.bv_val,
94 mi->mi_startTime.bv_val );
99 LDAP_LOG( OPERATION, CRIT,
100 "monitor_subsys_conn_init: "
101 "unable to create entry 'cn=Total,%s'\n",
102 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
104 Debug( LDAP_DEBUG_ANY,
105 "monitor_subsys_conn_init: "
106 "unable to create entry 'cn=Total,%s'\n%s%s",
107 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
115 attr_merge_one( e, mi->ad_monitorCounter, &bv, NULL );
117 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
118 e->e_private = ( void * )mp;
120 mp->mp_children = NULL;
121 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
122 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
123 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
124 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
126 if ( monitor_cache_add( mi, e ) ) {
128 LDAP_LOG( OPERATION, CRIT,
129 "monitor_subsys_conn_init: "
130 "unable to add entry 'cn=Total,%s'\n",
131 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
133 Debug( LDAP_DEBUG_ANY,
134 "monitor_subsys_conn_init: "
135 "unable to add entry 'cn=Total,%s'\n%s%s",
136 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
147 snprintf( buf, sizeof( buf ),
148 "dn: cn=Current,%s\n"
150 "structuralObjectClass: %s\n"
152 "createTimestamp: %s\n"
153 "modifyTimestamp: %s\n",
154 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
155 mi->oc_monitorCounterObject->soc_cname.bv_val,
156 mi->oc_monitorCounterObject->soc_cname.bv_val,
157 mi->mi_startTime.bv_val,
158 mi->mi_startTime.bv_val );
160 e = str2entry( buf );
163 LDAP_LOG( OPERATION, CRIT,
164 "monitor_subsys_conn_init: "
165 "unable to create entry 'cn=Current,%s'\n",
166 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
168 Debug( LDAP_DEBUG_ANY,
169 "monitor_subsys_conn_init: "
170 "unable to create entry 'cn=Current,%s'\n%s%s",
171 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
179 attr_merge_one( e, mi->ad_monitorCounter, &bv, NULL );
181 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
182 e->e_private = ( void * )mp;
184 mp->mp_children = NULL;
185 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
186 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
187 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
188 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
190 if ( monitor_cache_add( mi, e ) ) {
192 LDAP_LOG( OPERATION, CRIT,
193 "monitor_subsys_conn_init: "
194 "unable to add entry 'cn=Current,%s'\n",
195 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
197 Debug( LDAP_DEBUG_ANY,
198 "monitor_subsys_conn_init: "
199 "unable to add entry 'cn=Current,%s'\n%s%s",
200 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
208 mp = ( struct monitorentrypriv * )e_conn->e_private;
209 mp->mp_children = e_tmp;
211 monitor_cache_release( mi, e_conn );
217 monitor_subsys_conn_update(
222 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
228 if ( strncasecmp( e->e_ndn, "cn=total",
229 sizeof("cn=total")-1 ) == 0 ) {
230 n = connections_nextid();
232 } else if ( strncasecmp( e->e_ndn, "cn=current",
233 sizeof("cn=current")-1 ) == 0 ) {
237 for ( n = 0, c = connection_first( &connindex );
239 n++, c = connection_next( c, &connindex ) ) {
247 char buf[] = "+9223372036854775807L";
249 a = attr_find( e->e_attrs, mi->ad_monitorCounter );
254 snprintf( buf, sizeof( buf ), "%ld", n );
255 free( a->a_vals[ 0 ].bv_val );
256 ber_str2bv( buf, 0, 1, a->a_vals );
264 struct monitorinfo *mi,
269 struct monitorentrypriv *mp;
271 char buf[ BACKMONITOR_BUFSIZE ];
272 char buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
273 char buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
280 char ctmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
282 char mtmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
285 assert( ep != NULL );
287 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
288 #ifdef HACK_LOCAL_TIME
289 ctm = localtime( &c->c_starttime );
290 lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
291 mtm = localtime( &c->c_activitytime );
292 lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
293 #else /* !HACK_LOCAL_TIME */
294 ctm = gmtime( &c->c_starttime );
295 lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
296 mtm = gmtime( &c->c_activitytime );
297 lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
298 #endif /* !HACK_LOCAL_TIME */
299 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
301 snprintf( buf, sizeof( buf ),
302 "dn: cn=" CONN_CN_PREFIX " %ld,%s\n"
304 "structuralObjectClass: %s\n"
305 "cn: " CONN_CN_PREFIX " %ld\n"
306 "createTimestamp: %s\n"
307 "modifyTimestamp: %s\n",
308 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
309 mi->oc_monitorConnection->soc_cname.bv_val,
310 mi->oc_monitorConnection->soc_cname.bv_val,
314 e = str2entry( buf );
318 LDAP_LOG( OPERATION, CRIT,
319 "monitor_subsys_conn_create: "
320 "unable to create entry "
321 "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
322 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
324 Debug( LDAP_DEBUG_ANY,
325 "monitor_subsys_conn_create: "
326 "unable to create entry "
327 "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
329 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
334 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
336 ltm = gmtime( &c->c_starttime );
337 lutil_gentime( buf2, sizeof( buf2 ), ltm );
339 ltm = gmtime( &c->c_activitytime );
340 lutil_gentime( buf3, sizeof( buf3 ), ltm );
342 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
351 ": %s : %s : %s : %s",
353 (long) c->c_protocol,
354 c->c_n_ops_received, c->c_n_ops_executing,
355 c->c_n_ops_pending, c->c_n_ops_completed,
357 /* add low-level counters here */
358 c->c_n_get, c->c_n_read, c->c_n_write,
360 c->c_currentber ? "r" : "",
361 c->c_writewaiter ? "w" : "",
362 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
363 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
364 connection_state2str( c->c_conn_state ),
365 c->c_sasl_bind_in_progress ? "S" : "",
367 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
369 c->c_listener_url.bv_val,
370 c->c_peer_domain.bv_val,
371 c->c_peer_name.bv_val,
372 c->c_sock_name.bv_val,
379 bv.bv_len = strlen( buf );
380 attr_merge_one( e, mi->ad_monitoredInfo, &bv, NULL );
382 /* connection number */
383 snprintf( buf, sizeof( buf ), "%ld", c->c_connid );
385 bv.bv_len = strlen( buf );
386 attr_merge_one( e, mi->ad_monitorConnectionNumber, &bv, NULL );
389 attr_merge_one( e, mi->ad_monitorConnectionAuthzDN,
390 &c->c_dn, &c->c_ndn );
393 attr_merge_one( e, mi->ad_monitorConnectionLocalAddress,
394 &c->c_sock_name, NULL );
397 attr_merge_one( e, mi->ad_monitorConnectionPeerAddress,
398 &c->c_peer_name, NULL );
400 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
401 e->e_private = ( void * )mp;
402 mp->mp_info = &monitor_subsys[ SLAPD_MONITOR_CONN ];
403 mp->mp_children = NULL;
404 mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
412 monitor_subsys_conn_create(
419 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
422 struct monitorentrypriv *mp;
424 assert( mi != NULL );
425 assert( e_parent != NULL );
426 assert( ep != NULL );
431 Entry *e, *e_tmp = NULL;
433 /* create all the children of e_parent */
434 for ( c = connection_first( &connindex );
436 c = connection_next( c, &connindex )) {
437 if ( conn_create( mi, c, &e ) || e == NULL ) {
439 for ( ; e_tmp != NULL; ) {
440 mp = ( struct monitorentrypriv * )e_tmp->e_private;
444 e_tmp->e_private = NULL;
451 mp = ( struct monitorentrypriv * )e->e_private;
460 LDAPRDN values = NULL;
461 const char *text = NULL;
462 unsigned long connid;
464 /* create exactly the required entry */
466 if ( ldap_bv2rdn( ndn, &values, (char **)&text,
467 LDAP_DN_FORMAT_LDAP ) )
473 assert( values[ 0 ] );
475 connid = atol( values[ 0 ]->la_value.bv_val
476 + sizeof( CONN_CN_PREFIX ) );
478 ldap_rdnfree( values );
480 for ( c = connection_first( &connindex );
482 c = connection_next( c, &connindex )) {
483 if ( c->c_connid == connid ) {
484 if ( conn_create( mi, c, ep ) || *ep == NULL ) {
485 connection_done( c );