1 /* conn.c - deal with connection subsystem */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2006 The OpenLDAP Foundation.
6 * Portions Copyright 2001-2003 Pierangelo Masarati.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by Pierangelo Masarati for inclusion
19 * in OpenLDAP Software.
25 #include <ac/string.h>
29 #include "back-monitor.h"
32 monitor_subsys_conn_update(
38 monitor_subsys_conn_create(
46 monitor_subsys_conn_init(
48 monitor_subsys_t *ms )
51 Entry *e, **ep, *e_conn;
57 ms->mss_update = monitor_subsys_conn_update;
58 ms->mss_create = monitor_subsys_conn_create;
60 mi = ( monitor_info_t * )be->be_private;
62 if ( monitor_cache_get( mi, &ms->mss_ndn, &e_conn ) ) {
63 Debug( LDAP_DEBUG_ANY,
64 "monitor_subsys_conn_init: "
65 "unable to get entry \"%s\"\n",
66 ms->mss_ndn.bv_val, 0, 0 );
70 mp = ( monitor_entry_t * )e_conn->e_private;
71 mp->mp_children = NULL;
72 ep = &mp->mp_children;
77 BER_BVSTR( &bv, "cn=Total" );
78 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
79 mi->mi_oc_monitorCounterObject, mi, NULL, NULL );
82 Debug( LDAP_DEBUG_ANY,
83 "monitor_subsys_conn_init: "
84 "unable to create entry \"cn=Total,%s\"\n",
85 ms->mss_ndn.bv_val, 0, 0 );
89 BER_BVSTR( &bv, "0" );
90 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, &bv );
92 mp = monitor_entrypriv_create();
96 e->e_private = ( void * )mp;
98 mp->mp_flags = ms->mss_flags \
99 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
100 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
102 if ( monitor_cache_add( mi, e ) ) {
103 Debug( LDAP_DEBUG_ANY,
104 "monitor_subsys_conn_init: "
105 "unable to add entry \"cn=Total,%s\"\n",
106 ms->mss_ndn.bv_val, 0, 0 );
116 BER_BVSTR( &bv, "cn=Current" );
117 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
118 mi->mi_oc_monitorCounterObject, mi, NULL, NULL );
121 Debug( LDAP_DEBUG_ANY,
122 "monitor_subsys_conn_init: "
123 "unable to create entry \"cn=Current,%s\"\n",
124 ms->mss_ndn.bv_val, 0, 0 );
128 BER_BVSTR( &bv, "0" );
129 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, &bv );
131 mp = monitor_entrypriv_create();
135 e->e_private = ( void * )mp;
137 mp->mp_flags = ms->mss_flags \
138 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
139 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
141 if ( monitor_cache_add( mi, e ) ) {
142 Debug( LDAP_DEBUG_ANY,
143 "monitor_subsys_conn_init: "
144 "unable to add entry \"cn=Current,%s\"\n",
145 ms->mss_ndn.bv_val, 0, 0 );
152 monitor_cache_release( mi, e_conn );
158 monitor_subsys_conn_update(
163 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
166 static struct berval total_bv = BER_BVC( "cn=total" ),
167 current_bv = BER_BVC( "cn=current" );
170 assert( mi != NULL );
173 dnRdn( &e->e_nname, &rdn );
175 if ( dn_match( &rdn, &total_bv ) ) {
176 n = connections_nextid();
178 } else if ( dn_match( &rdn, ¤t_bv ) ) {
182 for ( n = 0, c = connection_first( &connindex );
184 n++, c = connection_next( c, &connindex ) ) {
187 connection_done( c );
192 char buf[] = "+9223372036854775807L";
195 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
200 snprintf( buf, sizeof( buf ), "%ld", n );
202 if ( len > a->a_vals[ 0 ].bv_len ) {
203 a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
205 a->a_vals[ 0 ].bv_len = len;
206 AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
208 /* FIXME: touch modifyTimestamp? */
211 return SLAP_CB_CONTINUE;
219 monitor_subsys_t *ms )
223 char buf[ BACKMONITOR_BUFSIZE ];
224 char buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
225 char buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
227 struct berval bv, ctmbv, mtmbv, bv2, bv3;
228 struct berval bv_unknown= BER_BVC("unknown");
232 #ifdef HACK_LOCAL_TIME
233 char ctmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
234 char mtmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
238 #endif /* HAVE_GMTIME_R */
241 assert( ep != NULL );
243 #ifndef HAVE_GMTIME_R
244 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
248 tm = gmtime_r( &c->c_starttime, &tm_buf );
250 tm = gmtime( &c->c_starttime );
252 bv2.bv_len = lutil_gentime( buf2, sizeof( buf2 ), tm );
254 #ifdef HACK_LOCAL_TIME
255 # ifdef HAVE_LOCALTIME_R
256 tm = localtime_r( &c->c_starttime, &tm_buf );
258 tm = localtime( &c->c_starttime );
260 ctmbv.bv_len = lutil_localtime( ctmbuf, sizeof( ctmbuf ), tm, -timezone );
261 ctmbv.bv_val = ctmbuf;
262 #else /* !HACK_LOCAL_TIME */
267 tm = gmtime_r( &c->c_activitytime, &tm_buf );
269 tm = gmtime( &c->c_activitytime );
271 bv3.bv_len = lutil_gentime( buf3, sizeof( buf3 ), tm );
273 #ifdef HACK_LOCAL_TIME
274 # ifdef HAVE_LOCALTIME_R
275 tm = localtime_r( &c->c_activitytime, &tm_buf );
277 tm = localtime( &c->c_activitytime );
278 # endif /* HAVE_LOCALTIME_R */
279 mtmbv.bv_len = lutil_localtime( mtmbuf, sizeof( mtmbuf ), tm, -timezone );
280 mtmbv.bv_val = mtmbuf;
281 #else /* !HACK_LOCAL_TIME */
285 #ifndef HAVE_GMTIME_R
286 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
289 bv.bv_len = snprintf( buf, sizeof( buf ),
290 "cn=Connection %ld", c->c_connid );
292 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
293 mi->mi_oc_monitorConnection, mi, &ctmbv, &mtmbv );
296 Debug( LDAP_DEBUG_ANY,
297 "monitor_subsys_conn_create: "
298 "unable to create entry "
299 "\"cn=Connection %ld,%s\"\n",
301 ms->mss_dn.bv_val, 0 );
305 #ifdef MONITOR_LEGACY_CONN
306 /* NOTE: this will disappear, as the exploded data
307 * has been moved to dedicated attributes */
308 bv.bv_len = snprintf( buf, sizeof( buf ),
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,
345 attr_merge_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
346 #endif /* MONITOR_LEGACY_CONN */
348 bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", c->c_connid );
349 attr_merge_one( e, mi->mi_ad_monitorConnectionNumber, &bv, NULL );
351 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", (long) c->c_protocol );
352 attr_merge_one( e, mi->mi_ad_monitorConnectionProtocol, &bv, NULL );
354 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_received );
355 attr_merge_one( e, mi->mi_ad_monitorConnectionOpsReceived, &bv, NULL );
357 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_executing );
358 attr_merge_one( e, mi->mi_ad_monitorConnectionOpsExecuting, &bv, NULL );
360 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_pending );
361 attr_merge_one( e, mi->mi_ad_monitorConnectionOpsPending, &bv, NULL );
363 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_completed );
364 attr_merge_one( e, mi->mi_ad_monitorConnectionOpsCompleted, &bv, NULL );
366 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_get );
367 attr_merge_one( e, mi->mi_ad_monitorConnectionGet, &bv, NULL );
369 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_read );
370 attr_merge_one( e, mi->mi_ad_monitorConnectionRead, &bv, NULL );
372 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_write );
373 attr_merge_one( e, mi->mi_ad_monitorConnectionWrite, &bv, NULL );
375 bv.bv_len = snprintf( buf, sizeof( buf ), "%s%s%s%s%s%s",
376 c->c_currentber ? "r" : "",
377 c->c_writewaiter ? "w" : "",
378 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
379 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
380 connection_state2str( c->c_conn_state ),
381 c->c_sasl_bind_in_progress ? "S" : "" );
382 attr_merge_one( e, mi->mi_ad_monitorConnectionMask, &bv, NULL );
384 attr_merge_one( e, mi->mi_ad_monitorConnectionAuthzDN,
385 &c->c_dn, &c->c_ndn );
387 /* NOTE: client connections leave the c_peer_* fields NULL */
388 attr_merge_one( e, mi->mi_ad_monitorConnectionListener,
389 &c->c_listener_url, NULL );
391 attr_merge_one( e, mi->mi_ad_monitorConnectionPeerDomain,
392 BER_BVISNULL( &c->c_peer_domain ) ? &bv_unknown : &c->c_peer_domain,
395 attr_merge_one( e, mi->mi_ad_monitorConnectionPeerAddress,
396 BER_BVISNULL( &c->c_peer_name ) ? &bv_unknown : &c->c_peer_name,
399 attr_merge_one( e, mi->mi_ad_monitorConnectionLocalAddress,
400 &c->c_sock_name, NULL );
402 attr_merge_one( e, mi->mi_ad_monitorConnectionStartTime, &bv2, NULL );
404 attr_merge_one( e, mi->mi_ad_monitorConnectionActivityTime, &bv3, NULL );
406 mp = monitor_entrypriv_create();
410 e->e_private = ( void * )mp;
412 mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
416 return SLAP_CB_CONTINUE;
420 monitor_subsys_conn_create(
427 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
432 int rc = SLAP_CB_CONTINUE;
433 monitor_subsys_t *ms;
435 assert( mi != NULL );
436 assert( e_parent != NULL );
437 assert( ep != NULL );
439 ms = (( monitor_entry_t *)e_parent->e_private)->mp_info;
447 /* create all the children of e_parent */
448 for ( c = connection_first( &connindex );
450 c = connection_next( c, &connindex ) )
452 if ( conn_create( mi, c, &e, ms ) != SLAP_CB_CONTINUE
455 for ( ; e_tmp != NULL; ) {
456 mp = ( monitor_entry_t * )e_tmp->e_private;
460 e_tmp->e_private = NULL;
465 rc = rs->sr_err = LDAP_OTHER;
468 mp = ( monitor_entry_t * )e->e_private;
472 connection_done( c );
476 unsigned long connid;
478 static struct berval nconn_bv = BER_BVC( "cn=connection " );
480 rc = LDAP_NO_SUCH_OBJECT;
482 /* create exactly the required entry;
483 * the normalized DN must start with "cn=connection ",
484 * followed by the connection id, followed by
485 * the RDN separator "," */
486 if ( ndn->bv_len <= nconn_bv.bv_len
487 || strncmp( ndn->bv_val, nconn_bv.bv_val, nconn_bv.bv_len ) != 0 )
492 connid = strtol( &ndn->bv_val[ nconn_bv.bv_len ], &next, 10 );
493 if ( next[ 0 ] != ',' ) {
494 return ( rs->sr_err = LDAP_OTHER );
497 for ( c = connection_first( &connindex );
499 c = connection_next( c, &connindex ) )
501 if ( c->c_connid == connid ) {
502 rc = conn_create( mi, c, ep, ms );
503 if ( rc != SLAP_CB_CONTINUE ) {
506 } else if ( *ep == NULL ) {
507 rc = rs->sr_err = LDAP_OTHER;
514 connection_done( c );