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 ) )
188 connection_done( c );
193 char buf[] = "+9223372036854775807L";
196 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
201 snprintf( buf, sizeof( buf ), "%ld", n );
203 if ( len > a->a_vals[ 0 ].bv_len ) {
204 a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
206 a->a_vals[ 0 ].bv_len = len;
207 AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
209 /* FIXME: touch modifyTimestamp? */
212 return SLAP_CB_CONTINUE;
220 monitor_subsys_t *ms )
224 char buf[ BACKMONITOR_BUFSIZE ];
225 char buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
226 char buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
228 struct berval bv, ctmbv, mtmbv, bv2, bv3;
229 struct berval bv_unknown= BER_BVC("unknown");
233 #ifdef HACK_LOCAL_TIME
234 char ctmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
235 char mtmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
239 #endif /* HAVE_GMTIME_R */
242 assert( ep != NULL );
244 #ifndef HAVE_GMTIME_R
245 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
249 tm = gmtime_r( &c->c_starttime, &tm_buf );
251 tm = gmtime( &c->c_starttime );
253 bv2.bv_len = lutil_gentime( buf2, sizeof( buf2 ), tm );
255 #ifdef HACK_LOCAL_TIME
256 # ifdef HAVE_LOCALTIME_R
257 tm = localtime_r( &c->c_starttime, &tm_buf );
259 tm = localtime( &c->c_starttime );
261 ctmbv.bv_len = lutil_localtime( ctmbuf, sizeof( ctmbuf ), tm, -timezone );
262 ctmbv.bv_val = ctmbuf;
263 #else /* !HACK_LOCAL_TIME */
268 tm = gmtime_r( &c->c_activitytime, &tm_buf );
270 tm = gmtime( &c->c_activitytime );
272 bv3.bv_len = lutil_gentime( buf3, sizeof( buf3 ), tm );
274 #ifdef HACK_LOCAL_TIME
275 # ifdef HAVE_LOCALTIME_R
276 tm = localtime_r( &c->c_activitytime, &tm_buf );
278 tm = localtime( &c->c_activitytime );
279 # endif /* HAVE_LOCALTIME_R */
280 mtmbv.bv_len = lutil_localtime( mtmbuf, sizeof( mtmbuf ), tm, -timezone );
281 mtmbv.bv_val = mtmbuf;
282 #else /* !HACK_LOCAL_TIME */
286 #ifndef HAVE_GMTIME_R
287 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
290 bv.bv_len = snprintf( buf, sizeof( buf ),
291 "cn=Connection %ld", c->c_connid );
293 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
294 mi->mi_oc_monitorConnection, mi, &ctmbv, &mtmbv );
297 Debug( LDAP_DEBUG_ANY,
298 "monitor_subsys_conn_create: "
299 "unable to create entry "
300 "\"cn=Connection %ld,%s\"\n",
302 ms->mss_dn.bv_val, 0 );
306 #ifdef MONITOR_LEGACY_CONN
307 /* NOTE: this will disappear, as the exploded data
308 * has been moved to dedicated attributes */
309 bv.bv_len = snprintf( buf, sizeof( buf ),
323 (long) c->c_protocol,
324 c->c_n_ops_received, c->c_n_ops_executing,
325 c->c_n_ops_pending, c->c_n_ops_completed,
327 /* add low-level counters here */
328 c->c_n_get, c->c_n_read, c->c_n_write,
330 c->c_currentber ? "r" : "",
331 c->c_writewaiter ? "w" : "",
332 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
333 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
334 connection_state2str( c->c_conn_state ),
335 c->c_sasl_bind_in_progress ? "S" : "",
337 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
339 c->c_listener_url.bv_val,
340 c->c_peer_domain.bv_val,
341 c->c_peer_name.bv_val,
342 c->c_sock_name.bv_val,
346 attr_merge_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
347 #endif /* MONITOR_LEGACY_CONN */
349 bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", c->c_connid );
350 attr_merge_one( e, mi->mi_ad_monitorConnectionNumber, &bv, NULL );
352 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", (long) c->c_protocol );
353 attr_merge_one( e, mi->mi_ad_monitorConnectionProtocol, &bv, NULL );
355 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_received );
356 attr_merge_one( e, mi->mi_ad_monitorConnectionOpsReceived, &bv, NULL );
358 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_executing );
359 attr_merge_one( e, mi->mi_ad_monitorConnectionOpsExecuting, &bv, NULL );
361 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_pending );
362 attr_merge_one( e, mi->mi_ad_monitorConnectionOpsPending, &bv, NULL );
364 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_completed );
365 attr_merge_one( e, mi->mi_ad_monitorConnectionOpsCompleted, &bv, NULL );
367 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_get );
368 attr_merge_one( e, mi->mi_ad_monitorConnectionGet, &bv, NULL );
370 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_read );
371 attr_merge_one( e, mi->mi_ad_monitorConnectionRead, &bv, NULL );
373 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_write );
374 attr_merge_one( e, mi->mi_ad_monitorConnectionWrite, &bv, NULL );
376 bv.bv_len = snprintf( buf, sizeof( buf ), "%s%s%s%s%s%s",
377 c->c_currentber ? "r" : "",
378 c->c_writewaiter ? "w" : "",
379 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
380 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
381 connection_state2str( c->c_conn_state ),
382 c->c_sasl_bind_in_progress ? "S" : "" );
383 attr_merge_one( e, mi->mi_ad_monitorConnectionMask, &bv, NULL );
385 attr_merge_one( e, mi->mi_ad_monitorConnectionAuthzDN,
386 &c->c_dn, &c->c_ndn );
388 /* NOTE: client connections leave the c_peer_* fields NULL */
389 attr_merge_one( e, mi->mi_ad_monitorConnectionListener,
390 &c->c_listener_url, NULL );
392 attr_merge_one( e, mi->mi_ad_monitorConnectionPeerDomain,
393 BER_BVISNULL( &c->c_peer_domain ) ? &bv_unknown : &c->c_peer_domain,
396 attr_merge_one( e, mi->mi_ad_monitorConnectionPeerAddress,
397 BER_BVISNULL( &c->c_peer_name ) ? &bv_unknown : &c->c_peer_name,
400 attr_merge_one( e, mi->mi_ad_monitorConnectionLocalAddress,
401 &c->c_sock_name, NULL );
403 attr_merge_one( e, mi->mi_ad_monitorConnectionStartTime, &bv2, NULL );
405 attr_merge_one( e, mi->mi_ad_monitorConnectionActivityTime, &bv3, NULL );
407 mp = monitor_entrypriv_create();
411 e->e_private = ( void * )mp;
413 mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
417 return SLAP_CB_CONTINUE;
421 monitor_subsys_conn_create(
428 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
430 int rc = SLAP_CB_CONTINUE;
431 monitor_subsys_t *ms;
433 assert( mi != NULL );
434 assert( e_parent != NULL );
435 assert( ep != NULL );
437 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 ) )
454 if ( conn_create( mi, c, &e, ms ) != SLAP_CB_CONTINUE
457 for ( ; e_tmp != NULL; ) {
458 mp = ( monitor_entry_t * )e_tmp->e_private;
462 e_tmp->e_private = NULL;
467 rc = rs->sr_err = LDAP_OTHER;
470 mp = ( monitor_entry_t * )e->e_private;
474 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 );