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 #define MONITOR_LEGACY_CONN
36 monitor_subsys_conn_update(
42 monitor_subsys_conn_create(
50 monitor_subsys_conn_init(
52 monitor_subsys_t *ms )
55 Entry *e, **ep, *e_conn;
57 char buf[ BACKMONITOR_BUFSIZE ];
62 ms->mss_update = monitor_subsys_conn_update;
63 ms->mss_create = monitor_subsys_conn_create;
65 mi = ( monitor_info_t * )be->be_private;
67 if ( monitor_cache_get( mi, &ms->mss_ndn, &e_conn ) ) {
68 Debug( LDAP_DEBUG_ANY,
69 "monitor_subsys_conn_init: "
70 "unable to get entry \"%s\"\n",
71 ms->mss_ndn.bv_val, 0, 0 );
75 mp = ( monitor_entry_t * )e_conn->e_private;
76 mp->mp_children = NULL;
77 ep = &mp->mp_children;
82 BER_BVSTR( &bv, "cn=Total" );
83 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
84 mi->mi_oc_monitorCounterObject, mi, NULL, NULL );
87 Debug( LDAP_DEBUG_ANY,
88 "monitor_subsys_conn_init: "
89 "unable to create entry \"cn=Total,%s\"\n",
90 ms->mss_ndn.bv_val, 0, 0 );
94 BER_BVSTR( &bv, "0" );
95 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, &bv );
97 mp = monitor_entrypriv_create();
101 e->e_private = ( void * )mp;
103 mp->mp_flags = ms->mss_flags \
104 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
105 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
107 if ( monitor_cache_add( mi, e ) ) {
108 Debug( LDAP_DEBUG_ANY,
109 "monitor_subsys_conn_init: "
110 "unable to add entry \"cn=Total,%s\"\n",
111 ms->mss_ndn.bv_val, 0, 0 );
121 BER_BVSTR( &bv, "cn=Current" );
122 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
123 mi->mi_oc_monitorCounterObject, mi, NULL, NULL );
126 Debug( LDAP_DEBUG_ANY,
127 "monitor_subsys_conn_init: "
128 "unable to create entry \"cn=Current,%s\"\n",
129 ms->mss_ndn.bv_val, 0, 0 );
133 BER_BVSTR( &bv, "0" );
134 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, &bv );
136 mp = monitor_entrypriv_create();
140 e->e_private = ( void * )mp;
142 mp->mp_flags = ms->mss_flags \
143 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
144 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
146 if ( monitor_cache_add( mi, e ) ) {
147 Debug( LDAP_DEBUG_ANY,
148 "monitor_subsys_conn_init: "
149 "unable to add entry \"cn=Current,%s\"\n",
150 ms->mss_ndn.bv_val, 0, 0 );
157 monitor_cache_release( mi, e_conn );
163 monitor_subsys_conn_update(
168 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
171 static struct berval total_bv = BER_BVC( "cn=total" ),
172 current_bv = BER_BVC( "cn=current" );
175 assert( mi != NULL );
178 dnRdn( &e->e_nname, &rdn );
180 if ( dn_match( &rdn, &total_bv ) ) {
181 n = connections_nextid();
183 } else if ( dn_match( &rdn, ¤t_bv ) ) {
187 for ( n = 0, c = connection_first( &connindex );
189 n++, c = connection_next( c, &connindex ) ) {
192 connection_done( c );
197 char buf[] = "+9223372036854775807L";
200 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
205 snprintf( buf, sizeof( buf ), "%ld", n );
207 if ( len > a->a_vals[ 0 ].bv_len ) {
208 a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
210 a->a_vals[ 0 ].bv_len = len;
211 AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
213 /* FIXME: touch modifyTimestamp? */
216 return SLAP_CB_CONTINUE;
224 monitor_subsys_t *ms )
228 char buf[ BACKMONITOR_BUFSIZE ];
229 char buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
230 char buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
232 struct berval bv, ctmbv, mtmbv, bv2, bv3;
233 struct berval bv_unknown= BER_BVC("unknown");
237 #ifdef HACK_LOCAL_TIME
238 char ctmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
239 char mtmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
243 #endif /* HAVE_GMTIME_R */
246 assert( ep != NULL );
248 #ifndef HAVE_GMTIME_R
249 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
253 tm = gmtime_r( &c->c_starttime, &tm_buf );
255 tm = gmtime( &c->c_starttime );
257 bv2.bv_len = lutil_gentime( buf2, sizeof( buf2 ), tm );
259 #ifdef HACK_LOCAL_TIME
260 # ifdef HAVE_LOCALTIME_R
261 tm = localtime_r( &c->c_starttime, &tm_buf );
263 tm = localtime( &c->c_starttime );
265 ctmbv.bv_len = lutil_localtime( ctmbuf, sizeof( ctmbuf ), tm, -timezone );
266 ctmbv.bv_val = ctmbuf;
267 #else /* !HACK_LOCAL_TIME */
272 tm = gmtime_r( &c->c_activitytime, &tm_buf );
274 tm = gmtime( &c->c_activitytime );
276 bv3.bv_len = lutil_gentime( buf3, sizeof( buf3 ), tm );
278 #ifdef HACK_LOCAL_TIME
279 # ifdef HAVE_LOCALTIME_R
280 tm = localtime_r( &c->c_activitytime, &tm_buf );
282 tm = localtime( &c->c_activitytime );
283 # endif /* HAVE_LOCALTIME_R */
284 mtmbv.bv_len = lutil_localtime( mtmbuf, sizeof( mtmbuf ), tm, -timezone );
285 mtmbv.bv_val = mtmbuf;
286 #else /* !HACK_LOCAL_TIME */
290 #ifndef HAVE_GMTIME_R
291 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
294 bv.bv_len = snprintf( buf, sizeof( buf ),
295 "cn=Connection %ld", c->c_connid );
297 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
298 mi->mi_oc_monitorConnection, mi, &ctmbv, &mtmbv );
301 Debug( LDAP_DEBUG_ANY,
302 "monitor_subsys_conn_create: "
303 "unable to create entry "
304 "\"cn=Connection %ld,%s\"\n",
306 ms->mss_dn.bv_val, 0 );
310 #ifdef MONITOR_LEGACY_CONN
311 /* NOTE: this will disappear, as the exploded data
312 * has been moved to dedicated attributes */
313 bv.bv_len = snprintf( buf, sizeof( buf ),
327 (long) c->c_protocol,
328 c->c_n_ops_received, c->c_n_ops_executing,
329 c->c_n_ops_pending, c->c_n_ops_completed,
331 /* add low-level counters here */
332 c->c_n_get, c->c_n_read, c->c_n_write,
334 c->c_currentber ? "r" : "",
335 c->c_writewaiter ? "w" : "",
336 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
337 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
338 connection_state2str( c->c_conn_state ),
339 c->c_sasl_bind_in_progress ? "S" : "",
341 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
343 c->c_listener_url.bv_val,
344 c->c_peer_domain.bv_val,
345 c->c_peer_name.bv_val,
346 c->c_sock_name.bv_val,
350 attr_merge_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
351 #endif /* MONITOR_LEGACY_CONN */
353 bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", c->c_connid );
354 attr_merge_one( e, mi->mi_ad_monitorConnectionNumber, &bv, NULL );
356 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_protocol );
357 attr_merge_one( e, mi->mi_ad_monitorConnectionProtocol, &bv, NULL );
359 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_received );
360 attr_merge_one( e, mi->mi_ad_monitorConnectionOpsReceived, &bv, NULL );
362 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_executing );
363 attr_merge_one( e, mi->mi_ad_monitorConnectionOpsExecuting, &bv, NULL );
365 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_pending );
366 attr_merge_one( e, mi->mi_ad_monitorConnectionOpsPending, &bv, NULL );
368 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_completed );
369 attr_merge_one( e, mi->mi_ad_monitorConnectionOpsCompleted, &bv, NULL );
371 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_get );
372 attr_merge_one( e, mi->mi_ad_monitorConnectionGet, &bv, NULL );
374 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_read );
375 attr_merge_one( e, mi->mi_ad_monitorConnectionRead, &bv, NULL );
377 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_write );
378 attr_merge_one( e, mi->mi_ad_monitorConnectionWrite, &bv, NULL );
380 bv.bv_len = snprintf( buf, sizeof( buf ), "%s%s%s%s%s%s",
381 c->c_currentber ? "r" : "",
382 c->c_writewaiter ? "w" : "",
383 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
384 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
385 connection_state2str( c->c_conn_state ),
386 c->c_sasl_bind_in_progress ? "S" : "" );
387 attr_merge_one( e, mi->mi_ad_monitorConnectionMask, &bv, NULL );
389 attr_merge_one( e, mi->mi_ad_monitorConnectionAuthzDN,
390 &c->c_dn, &c->c_ndn );
392 /* NOTE: client connections leave the c_peer_* fields NULL */
393 attr_merge_one( e, mi->mi_ad_monitorConnectionListener,
394 &c->c_listener_url, NULL );
396 attr_merge_one( e, mi->mi_ad_monitorConnectionPeerDomain,
397 BER_BVISNULL( &c->c_peer_domain ) ? &bv_unknown : &c->c_peer_domain,
400 attr_merge_one( e, mi->mi_ad_monitorConnectionPeerAddress,
401 BER_BVISNULL( &c->c_peer_name ) ? &bv_unknown : &c->c_peer_name,
404 attr_merge_one( e, mi->mi_ad_monitorConnectionLocalAddress,
405 &c->c_sock_name, NULL );
407 attr_merge_one( e, mi->mi_ad_monitorConnectionStartTime, &bv2, NULL );
409 attr_merge_one( e, mi->mi_ad_monitorConnectionActivityTime, &bv3, NULL );
411 mp = monitor_entrypriv_create();
415 e->e_private = ( void * )mp;
417 mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
421 return SLAP_CB_CONTINUE;
425 monitor_subsys_conn_create(
432 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
437 int rc = SLAP_CB_CONTINUE;
438 monitor_subsys_t *ms;
440 assert( mi != NULL );
441 assert( e_parent != NULL );
442 assert( ep != NULL );
444 ms = (( monitor_entry_t *)e_parent->e_private)->mp_info;
452 /* create all the children of e_parent */
453 for ( c = connection_first( &connindex );
455 c = connection_next( c, &connindex ) )
457 if ( conn_create( mi, c, &e, ms ) != SLAP_CB_CONTINUE
460 for ( ; e_tmp != NULL; ) {
461 mp = ( monitor_entry_t * )e_tmp->e_private;
465 e_tmp->e_private = NULL;
470 rc = rs->sr_err = LDAP_OTHER;
473 mp = ( monitor_entry_t * )e->e_private;
477 connection_done( c );
481 unsigned long connid;
483 static struct berval nconn_bv = BER_BVC( "cn=connection " );
485 rc = LDAP_NO_SUCH_OBJECT;
487 /* create exactly the required entry;
488 * the normalized DN must start with "cn=connection ",
489 * followed by the connection id, followed by
490 * the RDN separator "," */
491 if ( ndn->bv_len <= nconn_bv.bv_len
492 || strncmp( ndn->bv_val, nconn_bv.bv_val, nconn_bv.bv_len ) != 0 )
497 connid = strtol( &ndn->bv_val[ nconn_bv.bv_len ], &next, 10 );
498 if ( next[ 0 ] != ',' ) {
499 return ( rs->sr_err = LDAP_OTHER );
502 for ( c = connection_first( &connindex );
504 c = connection_next( c, &connindex ) )
506 if ( c->c_connid == connid ) {
507 rc = conn_create( mi, c, ep, ms );
508 if ( rc != SLAP_CB_CONTINUE ) {
511 } else if ( *ep == NULL ) {
512 rc = rs->sr_err = LDAP_OTHER;
519 connection_done( c );