1 /* conn.c - deal with connection subsystem */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2005 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_init(
38 monitor_subsys_t *ms )
41 Entry *e, **ep, *e_conn;
43 char buf[ BACKMONITOR_BUFSIZE ];
48 mi = ( monitor_info_t * )be->be_private;
50 if ( monitor_cache_get( mi, &ms->mss_ndn, &e_conn ) ) {
51 Debug( LDAP_DEBUG_ANY,
52 "monitor_subsys_conn_init: "
53 "unable to get entry \"%s\"\n",
54 ms->mss_ndn.bv_val, 0, 0 );
58 mp = ( monitor_entry_t * )e_conn->e_private;
59 mp->mp_children = NULL;
60 ep = &mp->mp_children;
65 snprintf( buf, sizeof( buf ),
68 "structuralObjectClass: %s\n"
72 "createTimestamp: %s\n"
73 "modifyTimestamp: %s\n",
75 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
76 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
77 mi->mi_creatorsName.bv_val,
78 mi->mi_creatorsName.bv_val,
79 mi->mi_startTime.bv_val,
80 mi->mi_startTime.bv_val );
84 Debug( LDAP_DEBUG_ANY,
85 "monitor_subsys_conn_init: "
86 "unable to create entry \"cn=Total,%s\"\n",
87 ms->mss_ndn.bv_val, 0, 0 );
91 BER_BVSTR( &bv, "0" );
92 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, &bv );
94 mp = monitor_entrypriv_create();
98 e->e_private = ( void * )mp;
100 mp->mp_flags = ms->mss_flags \
101 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
102 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
104 if ( monitor_cache_add( mi, e ) ) {
105 Debug( LDAP_DEBUG_ANY,
106 "monitor_subsys_conn_init: "
107 "unable to add entry \"cn=Total,%s\"\n",
108 ms->mss_ndn.bv_val, 0, 0 );
118 snprintf( buf, sizeof( buf ),
119 "dn: cn=Current,%s\n"
121 "structuralObjectClass: %s\n"
124 "modifiersName: %s\n"
125 "createTimestamp: %s\n"
126 "modifyTimestamp: %s\n",
128 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
129 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
130 mi->mi_creatorsName.bv_val,
131 mi->mi_creatorsName.bv_val,
132 mi->mi_startTime.bv_val,
133 mi->mi_startTime.bv_val );
135 e = str2entry( buf );
137 Debug( LDAP_DEBUG_ANY,
138 "monitor_subsys_conn_init: "
139 "unable to create entry \"cn=Current,%s\"\n",
140 ms->mss_ndn.bv_val, 0, 0 );
144 BER_BVSTR( &bv, "0" );
145 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, &bv );
147 mp = monitor_entrypriv_create();
151 e->e_private = ( void * )mp;
153 mp->mp_flags = ms->mss_flags \
154 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
155 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
157 if ( monitor_cache_add( mi, e ) ) {
158 Debug( LDAP_DEBUG_ANY,
159 "monitor_subsys_conn_init: "
160 "unable to add entry \"cn=Current,%s\"\n",
161 ms->mss_ndn.bv_val, 0, 0 );
168 monitor_cache_release( mi, e_conn );
174 monitor_subsys_conn_update(
179 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
182 static struct berval total_bv = BER_BVC( "cn=total" ),
183 current_bv = BER_BVC( "cn=current" );
186 assert( mi != NULL );
189 dnRdn( &e->e_nname, &rdn );
191 if ( dn_match( &rdn, &total_bv ) ) {
192 n = connections_nextid();
194 } else if ( dn_match( &rdn, ¤t_bv ) ) {
198 for ( n = 0, c = connection_first( &connindex );
200 n++, c = connection_next( c, &connindex ) ) {
203 connection_done( c );
208 char buf[] = "+9223372036854775807L";
211 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
216 snprintf( buf, sizeof( buf ), "%ld", n );
218 if ( len > a->a_vals[ 0 ].bv_len ) {
219 a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
221 a->a_vals[ 0 ].bv_len = len;
222 AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
224 /* FIXME: touch modifyTimestamp? */
227 return SLAP_CB_CONTINUE;
235 monitor_subsys_t *ms )
239 char buf[ BACKMONITOR_BUFSIZE ];
240 char buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
241 char buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
246 char ctmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
248 char mtmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
251 #endif /* HAVE_GMTIME_R */
254 assert( ep != NULL );
256 #ifndef HAVE_GMTIME_R
257 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
259 #ifdef HACK_LOCAL_TIME
260 # ifdef HAVE_LOCALTIME_R
261 ctm = localtime_r( &c->c_starttime, &tm_buf );
262 lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
263 mtm = localtime_r( &c->c_activitytime, &tm_buf );
264 lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
266 ctm = localtime( &c->c_starttime );
267 lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
268 mtm = localtime( &c->c_activitytime );
269 lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
270 # endif /* HAVE_LOCALTIME_R */
271 #else /* !HACK_LOCAL_TIME */
272 # ifdef HAVE_GMTIME_R
273 ctm = gmtime_r( &c->c_starttime, &tm_buf );
274 lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
275 mtm = gmtime_r( &c->c_activitytime, &tm_buf );
276 lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
278 ctm = gmtime( &c->c_starttime );
279 lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
280 mtm = gmtime( &c->c_activitytime );
281 lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
282 # endif /* HAVE_GMTIME_R */
283 #endif /* !HACK_LOCAL_TIME */
284 #ifndef HAVE_GMTIME_R
285 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
288 #ifndef HAVE_GMTIME_R
289 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
293 ltm = gmtime_r( &c->c_starttime, &tm_buf );
295 ltm = gmtime( &c->c_starttime );
297 lutil_gentime( buf2, sizeof( buf2 ), ltm );
300 ltm = gmtime_r( &c->c_activitytime, &tm_buf );
302 ltm = gmtime( &c->c_activitytime );
304 lutil_gentime( buf3, sizeof( buf3 ), ltm );
306 #ifndef HAVE_GMTIME_R
307 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
308 #endif /* HAVE_GMTIME_R */
310 snprintf( buf, sizeof( buf ),
311 "dn: cn=Connection %ld,%s\n"
313 "structuralObjectClass: %s\n"
314 "cn: Connection %ld\n"
316 #ifdef MONITOR_LEGACY_CONN
317 /* NOTE: this will disappear, as the exploded data
318 * has been moved to dedicated attributes */
332 #endif /* MONITOR_LEGACY_CONN */
359 "modifiersName: %s\n"
360 "createTimestamp: %s\n"
361 "modifyTimestamp: %s\n",
362 c->c_connid, ms->mss_dn.bv_val,
363 mi->mi_oc_monitorConnection->soc_cname.bv_val,
364 mi->mi_oc_monitorConnection->soc_cname.bv_val,
367 #ifdef MONITOR_LEGACY_CONN
368 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
370 (long) c->c_protocol,
371 c->c_n_ops_received, c->c_n_ops_executing,
372 c->c_n_ops_pending, c->c_n_ops_completed,
374 /* add low-level counters here */
375 c->c_n_get, c->c_n_read, c->c_n_write,
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" : "",
384 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
386 c->c_listener_url.bv_val,
387 c->c_peer_domain.bv_val,
388 c->c_peer_name.bv_val,
389 c->c_sock_name.bv_val,
393 #endif /* MONITOR_LEGACY_CONN */
395 mi->mi_ad_monitorConnectionNumber->ad_cname.bv_val,
397 mi->mi_ad_monitorConnectionProtocol->ad_cname.bv_val,
400 mi->mi_ad_monitorConnectionOpsReceived->ad_cname.bv_val,
402 mi->mi_ad_monitorConnectionOpsExecuting->ad_cname.bv_val,
403 c->c_n_ops_executing,
404 mi->mi_ad_monitorConnectionOpsPending->ad_cname.bv_val,
406 mi->mi_ad_monitorConnectionOpsCompleted->ad_cname.bv_val,
407 c->c_n_ops_completed,
409 mi->mi_ad_monitorConnectionGet->ad_cname.bv_val,
411 mi->mi_ad_monitorConnectionRead->ad_cname.bv_val,
413 mi->mi_ad_monitorConnectionWrite->ad_cname.bv_val,
416 mi->mi_ad_monitorConnectionMask->ad_cname.bv_val,
417 c->c_currentber ? "r" : "",
418 c->c_writewaiter ? "w" : "",
419 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
420 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
421 connection_state2str( c->c_conn_state ),
422 c->c_sasl_bind_in_progress ? "S" : "",
424 mi->mi_ad_monitorConnectionAuthzDN->ad_cname.bv_val,
425 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
427 /* NOTE: client connections leave the c_peer_* fields NULL */
428 mi->mi_ad_monitorConnectionListener->ad_cname.bv_val,
429 c->c_listener_url.bv_val,
430 mi->mi_ad_monitorConnectionPeerDomain->ad_cname.bv_val,
431 BER_BVISNULL( &c->c_peer_domain ) ? "unknown" : c->c_peer_domain.bv_val,
432 mi->mi_ad_monitorConnectionLocalAddress->ad_cname.bv_val,
433 BER_BVISNULL( &c->c_peer_name ) ? "unknown" : c->c_peer_name.bv_val,
434 mi->mi_ad_monitorConnectionPeerAddress->ad_cname.bv_val,
435 c->c_sock_name.bv_val,
437 mi->mi_ad_monitorConnectionStartTime->ad_cname.bv_val,
439 mi->mi_ad_monitorConnectionActivityTime->ad_cname.bv_val,
442 mi->mi_creatorsName.bv_val,
443 mi->mi_creatorsName.bv_val,
447 e = str2entry( buf );
450 Debug( LDAP_DEBUG_ANY,
451 "monitor_subsys_conn_create: "
452 "unable to create entry "
453 "\"cn=Connection %ld,%s\" entry\n",
455 ms->mss_dn.bv_val, 0 );
459 mp = monitor_entrypriv_create();
463 e->e_private = ( void * )mp;
465 mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
469 return SLAP_CB_CONTINUE;
473 monitor_subsys_conn_create(
480 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
485 int rc = SLAP_CB_CONTINUE;
486 monitor_subsys_t *ms;
488 assert( mi != NULL );
489 assert( e_parent != NULL );
490 assert( ep != NULL );
492 ms = (( monitor_entry_t *)e_parent->e_private)->mp_info;
500 /* create all the children of e_parent */
501 for ( c = connection_first( &connindex );
503 c = connection_next( c, &connindex ) )
505 if ( conn_create( mi, c, &e, ms ) != SLAP_CB_CONTINUE
508 for ( ; e_tmp != NULL; ) {
509 mp = ( monitor_entry_t * )e_tmp->e_private;
513 e_tmp->e_private = NULL;
518 rc = rs->sr_err = LDAP_OTHER;
521 mp = ( monitor_entry_t * )e->e_private;
525 connection_done( c );
529 unsigned long connid;
531 static struct berval nconn_bv = BER_BVC( "cn=connection " );
534 /* create exactly the required entry;
535 * the normalized DN must start with "cn=connection ",
536 * followed by the connection id, followed by
537 * the RDN separator "," */
538 if ( ndn->bv_len <= nconn_bv.bv_len
539 || strncmp( ndn->bv_val, nconn_bv.bv_val, nconn_bv.bv_len ) != 0 )
544 connid = strtol( &ndn->bv_val[ nconn_bv.bv_len ], &next, 10 );
545 if ( next[ 0 ] != ',' ) {
546 return ( rs->sr_err = LDAP_OTHER );
549 for ( c = connection_first( &connindex );
551 c = connection_next( c, &connindex ) )
553 if ( c->c_connid == connid ) {
554 rc = conn_create( mi, c, ep, ms );
555 if ( rc != SLAP_CB_CONTINUE ) {
558 } else if ( *ep == NULL ) {
559 rc = rs->sr_err = LDAP_OTHER;
566 connection_done( c );