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;
61 ms->mss_update = monitor_subsys_conn_update;
62 ms->mss_create = monitor_subsys_conn_create;
64 mi = ( monitor_info_t * )be->be_private;
66 if ( monitor_cache_get( mi, &ms->mss_ndn, &e_conn ) ) {
67 Debug( LDAP_DEBUG_ANY,
68 "monitor_subsys_conn_init: "
69 "unable to get entry \"%s\"\n",
70 ms->mss_ndn.bv_val, 0, 0 );
74 mp = ( monitor_entry_t * )e_conn->e_private;
75 mp->mp_children = NULL;
76 ep = &mp->mp_children;
81 BER_BVSTR( &bv, "cn=Total" );
82 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
83 mi->mi_oc_monitorCounterObject, mi, NULL, NULL );
86 Debug( LDAP_DEBUG_ANY,
87 "monitor_subsys_conn_init: "
88 "unable to create entry \"cn=Total,%s\"\n",
89 ms->mss_ndn.bv_val, 0, 0 );
93 BER_BVSTR( &bv, "0" );
94 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, &bv );
96 mp = monitor_entrypriv_create();
100 e->e_private = ( void * )mp;
102 mp->mp_flags = ms->mss_flags \
103 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
104 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
106 if ( monitor_cache_add( mi, e ) ) {
107 Debug( LDAP_DEBUG_ANY,
108 "monitor_subsys_conn_init: "
109 "unable to add entry \"cn=Total,%s\"\n",
110 ms->mss_ndn.bv_val, 0, 0 );
120 BER_BVSTR( &bv, "cn=Current" );
121 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
122 mi->mi_oc_monitorCounterObject, mi, NULL, NULL );
125 Debug( LDAP_DEBUG_ANY,
126 "monitor_subsys_conn_init: "
127 "unable to create entry \"cn=Current,%s\"\n",
128 ms->mss_ndn.bv_val, 0, 0 );
132 BER_BVSTR( &bv, "0" );
133 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, &bv );
135 mp = monitor_entrypriv_create();
139 e->e_private = ( void * )mp;
141 mp->mp_flags = ms->mss_flags \
142 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
143 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
145 if ( monitor_cache_add( mi, e ) ) {
146 Debug( LDAP_DEBUG_ANY,
147 "monitor_subsys_conn_init: "
148 "unable to add entry \"cn=Current,%s\"\n",
149 ms->mss_ndn.bv_val, 0, 0 );
156 monitor_cache_release( mi, e_conn );
162 monitor_subsys_conn_update(
167 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
170 static struct berval total_bv = BER_BVC( "cn=total" ),
171 current_bv = BER_BVC( "cn=current" );
174 assert( mi != NULL );
177 dnRdn( &e->e_nname, &rdn );
179 if ( dn_match( &rdn, &total_bv ) ) {
180 n = connections_nextid();
182 } else if ( dn_match( &rdn, ¤t_bv ) ) {
186 for ( n = 0, c = connection_first( &connindex );
188 n++, c = connection_next( c, &connindex ) ) {
191 connection_done( c );
196 char buf[] = "+9223372036854775807L";
199 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
204 snprintf( buf, sizeof( buf ), "%ld", n );
206 if ( len > a->a_vals[ 0 ].bv_len ) {
207 a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
209 a->a_vals[ 0 ].bv_len = len;
210 AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
212 /* FIXME: touch modifyTimestamp? */
215 return SLAP_CB_CONTINUE;
223 monitor_subsys_t *ms )
227 char buf[ BACKMONITOR_BUFSIZE ];
228 char buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
229 char buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
231 struct berval bv, ctmbv, mtmbv, bv2, bv3;
232 struct berval bv_unknown= BER_BVC("unknown");
236 #ifdef HACK_LOCAL_TIME
237 char ctmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
238 char mtmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
242 #endif /* HAVE_GMTIME_R */
245 assert( ep != NULL );
247 #ifndef HAVE_GMTIME_R
248 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
252 tm = gmtime_r( &c->c_starttime, &tm_buf );
254 tm = gmtime( &c->c_starttime );
256 bv2.bv_len = lutil_gentime( buf2, sizeof( buf2 ), tm );
258 #ifdef HACK_LOCAL_TIME
259 # ifdef HAVE_LOCALTIME_R
260 tm = localtime_r( &c->c_starttime, &tm_buf );
262 tm = localtime( &c->c_starttime );
264 ctmbv.bv_len = lutil_localtime( ctmbuf, sizeof( ctmbuf ), tm, -timezone );
265 ctmbv.bv_val = ctmbuf;
266 #else /* !HACK_LOCAL_TIME */
271 tm = gmtime_r( &c->c_activitytime, &tm_buf );
273 tm = gmtime( &c->c_activitytime );
275 bv3.bv_len = lutil_gentime( buf3, sizeof( buf3 ), tm );
277 #ifdef HACK_LOCAL_TIME
278 # ifdef HAVE_LOCALTIME_R
279 tm = localtime_r( &c->c_activitytime, &tm_buf );
281 tm = localtime( &c->c_activitytime );
282 # endif /* HAVE_LOCALTIME_R */
283 mtmbv.bv_len = lutil_localtime( mtmbuf, sizeof( mtmbuf ), tm, -timezone );
284 mtmbv.bv_val = mtmbuf;
285 #else /* !HACK_LOCAL_TIME */
289 #ifndef HAVE_GMTIME_R
290 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
293 bv.bv_len = snprintf( buf, sizeof( buf ),
294 "cn=Connection %ld", c->c_connid );
296 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
297 mi->mi_oc_monitorConnection, mi, &ctmbv, &mtmbv );
300 Debug( LDAP_DEBUG_ANY,
301 "monitor_subsys_conn_create: "
302 "unable to create entry "
303 "\"cn=Connection %ld,%s\"\n",
305 ms->mss_dn.bv_val, 0 );
309 #ifdef MONITOR_LEGACY_CONN
310 /* NOTE: this will disappear, as the exploded data
311 * has been moved to dedicated attributes */
312 bv.bv_len = snprintf( buf, sizeof( buf ),
326 (long) c->c_protocol,
327 c->c_n_ops_received, c->c_n_ops_executing,
328 c->c_n_ops_pending, c->c_n_ops_completed,
330 /* add low-level counters here */
331 c->c_n_get, c->c_n_read, c->c_n_write,
333 c->c_currentber ? "r" : "",
334 c->c_writewaiter ? "w" : "",
335 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
336 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
337 connection_state2str( c->c_conn_state ),
338 c->c_sasl_bind_in_progress ? "S" : "",
340 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
342 c->c_listener_url.bv_val,
343 c->c_peer_domain.bv_val,
344 c->c_peer_name.bv_val,
345 c->c_sock_name.bv_val,
349 attr_merge_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
350 #endif /* MONITOR_LEGACY_CONN */
352 bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", c->c_connid );
353 attr_merge_one( e, mi->mi_ad_monitorConnectionNumber, &bv, NULL );
355 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", (long) c->c_protocol );
356 attr_merge_one( e, mi->mi_ad_monitorConnectionProtocol, &bv, NULL );
358 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_received );
359 attr_merge_one( e, mi->mi_ad_monitorConnectionOpsReceived, &bv, NULL );
361 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_executing );
362 attr_merge_one( e, mi->mi_ad_monitorConnectionOpsExecuting, &bv, NULL );
364 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_pending );
365 attr_merge_one( e, mi->mi_ad_monitorConnectionOpsPending, &bv, NULL );
367 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_completed );
368 attr_merge_one( e, mi->mi_ad_monitorConnectionOpsCompleted, &bv, NULL );
370 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_get );
371 attr_merge_one( e, mi->mi_ad_monitorConnectionGet, &bv, NULL );
373 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_read );
374 attr_merge_one( e, mi->mi_ad_monitorConnectionRead, &bv, NULL );
376 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_write );
377 attr_merge_one( e, mi->mi_ad_monitorConnectionWrite, &bv, NULL );
379 bv.bv_len = snprintf( buf, sizeof( buf ), "%s%s%s%s%s%s",
380 c->c_currentber ? "r" : "",
381 c->c_writewaiter ? "w" : "",
382 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
383 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
384 connection_state2str( c->c_conn_state ),
385 c->c_sasl_bind_in_progress ? "S" : "" );
386 attr_merge_one( e, mi->mi_ad_monitorConnectionMask, &bv, NULL );
388 attr_merge_one( e, mi->mi_ad_monitorConnectionAuthzDN,
389 &c->c_dn, &c->c_ndn );
391 /* NOTE: client connections leave the c_peer_* fields NULL */
392 attr_merge_one( e, mi->mi_ad_monitorConnectionListener,
393 &c->c_listener_url, NULL );
395 attr_merge_one( e, mi->mi_ad_monitorConnectionPeerDomain,
396 BER_BVISNULL( &c->c_peer_domain ) ? &bv_unknown : &c->c_peer_domain,
399 attr_merge_one( e, mi->mi_ad_monitorConnectionPeerAddress,
400 BER_BVISNULL( &c->c_peer_name ) ? &bv_unknown : &c->c_peer_name,
403 attr_merge_one( e, mi->mi_ad_monitorConnectionLocalAddress,
404 &c->c_sock_name, NULL );
406 attr_merge_one( e, mi->mi_ad_monitorConnectionStartTime, &bv2, NULL );
408 attr_merge_one( e, mi->mi_ad_monitorConnectionActivityTime, &bv3, NULL );
410 mp = monitor_entrypriv_create();
414 e->e_private = ( void * )mp;
416 mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
420 return SLAP_CB_CONTINUE;
424 monitor_subsys_conn_create(
431 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
436 int rc = SLAP_CB_CONTINUE;
437 monitor_subsys_t *ms;
439 assert( mi != NULL );
440 assert( e_parent != NULL );
441 assert( ep != NULL );
443 ms = (( monitor_entry_t *)e_parent->e_private)->mp_info;
451 /* create all the children of e_parent */
452 for ( c = connection_first( &connindex );
454 c = connection_next( c, &connindex ) )
456 if ( conn_create( mi, c, &e, ms ) != SLAP_CB_CONTINUE
459 for ( ; e_tmp != NULL; ) {
460 mp = ( monitor_entry_t * )e_tmp->e_private;
464 e_tmp->e_private = NULL;
469 rc = rs->sr_err = LDAP_OTHER;
472 mp = ( monitor_entry_t * )e->e_private;
476 connection_done( c );
480 unsigned long connid;
482 static struct berval nconn_bv = BER_BVC( "cn=connection " );
484 rc = LDAP_NO_SUCH_OBJECT;
486 /* create exactly the required entry;
487 * the normalized DN must start with "cn=connection ",
488 * followed by the connection id, followed by
489 * the RDN separator "," */
490 if ( ndn->bv_len <= nconn_bv.bv_len
491 || strncmp( ndn->bv_val, nconn_bv.bv_val, nconn_bv.bv_len ) != 0 )
496 connid = strtol( &ndn->bv_val[ nconn_bv.bv_len ], &next, 10 );
497 if ( next[ 0 ] != ',' ) {
498 return ( rs->sr_err = LDAP_OTHER );
501 for ( c = connection_first( &connindex );
503 c = connection_next( c, &connindex ) )
505 if ( c->c_connid == connid ) {
506 rc = conn_create( mi, c, ep, ms );
507 if ( rc != SLAP_CB_CONTINUE ) {
510 } else if ( *ep == NULL ) {
511 rc = rs->sr_err = LDAP_OTHER;
518 connection_done( c );