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(
42 Entry *e, **ep, *e_conn;
44 char buf[ BACKMONITOR_BUFSIZE ];
49 mi = ( monitor_info_t * )be->be_private;
51 if ( monitor_cache_get( mi, &ms->mss_ndn, &e_conn ) ) {
52 Debug( LDAP_DEBUG_ANY,
53 "monitor_subsys_conn_init: "
54 "unable to get entry \"%s\"\n",
55 ms->mss_ndn.bv_val, 0, 0 );
59 mp = ( monitor_entry_t * )e_conn->e_private;
60 mp->mp_children = NULL;
61 ep = &mp->mp_children;
66 snprintf( buf, sizeof( buf ),
69 "structuralObjectClass: %s\n"
73 "createTimestamp: %s\n"
74 "modifyTimestamp: %s\n",
76 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
77 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
78 mi->mi_creatorsName.bv_val,
79 mi->mi_creatorsName.bv_val,
80 mi->mi_startTime.bv_val,
81 mi->mi_startTime.bv_val );
85 Debug( LDAP_DEBUG_ANY,
86 "monitor_subsys_conn_init: "
87 "unable to create entry \"cn=Total,%s\"\n",
88 ms->mss_ndn.bv_val, 0, 0 );
92 BER_BVSTR( &bv, "0" );
93 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, &bv );
95 mp = monitor_entrypriv_create();
99 e->e_private = ( void * )mp;
101 mp->mp_flags = ms->mss_flags \
102 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
103 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
105 if ( monitor_cache_add( mi, e ) ) {
106 Debug( LDAP_DEBUG_ANY,
107 "monitor_subsys_conn_init: "
108 "unable to add entry \"cn=Total,%s\"\n",
109 ms->mss_ndn.bv_val, 0, 0 );
119 snprintf( buf, sizeof( buf ),
120 "dn: cn=Current,%s\n"
122 "structuralObjectClass: %s\n"
125 "modifiersName: %s\n"
126 "createTimestamp: %s\n"
127 "modifyTimestamp: %s\n",
129 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
130 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
131 mi->mi_creatorsName.bv_val,
132 mi->mi_creatorsName.bv_val,
133 mi->mi_startTime.bv_val,
134 mi->mi_startTime.bv_val );
136 e = str2entry( buf );
138 Debug( LDAP_DEBUG_ANY,
139 "monitor_subsys_conn_init: "
140 "unable to create entry \"cn=Current,%s\"\n",
141 ms->mss_ndn.bv_val, 0, 0 );
145 BER_BVSTR( &bv, "0" );
146 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, &bv );
148 mp = monitor_entrypriv_create();
152 e->e_private = ( void * )mp;
154 mp->mp_flags = ms->mss_flags \
155 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
156 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
158 if ( monitor_cache_add( mi, e ) ) {
159 Debug( LDAP_DEBUG_ANY,
160 "monitor_subsys_conn_init: "
161 "unable to add entry \"cn=Current,%s\"\n",
162 ms->mss_ndn.bv_val, 0, 0 );
169 monitor_cache_release( mi, e_conn );
175 monitor_subsys_conn_update(
180 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
183 static struct berval total_bv = BER_BVC( "cn=total" ),
184 current_bv = BER_BVC( "cn=current" );
190 dnRdn( &e->e_nname, &rdn );
192 if ( dn_match( &rdn, &total_bv ) ) {
193 n = connections_nextid();
195 } else if ( dn_match( &rdn, ¤t_bv ) ) {
199 for ( n = 0, c = connection_first( &connindex );
201 n++, c = connection_next( c, &connindex ) ) {
204 connection_done( c );
209 char buf[] = "+9223372036854775807L";
212 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
217 snprintf( buf, sizeof( buf ), "%ld", n );
219 if ( len > a->a_vals[ 0 ].bv_len ) {
220 a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
222 a->a_vals[ 0 ].bv_len = len;
223 AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
225 /* FIXME: touch modifyTimestamp? */
241 char buf[ BACKMONITOR_BUFSIZE ];
242 char buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
243 char buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
250 char ctmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
252 char mtmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
255 #endif /* HAVE_GMTIME_R */
258 assert( ep != NULL );
260 #ifndef HAVE_GMTIME_R
261 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
263 #ifdef HACK_LOCAL_TIME
264 # ifdef HAVE_LOCALTIME_R
265 ctm = localtime_r( &c->c_starttime, &tm_buf );
266 lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
267 mtm = localtime_r( &c->c_activitytime, &tm_buf );
268 lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
270 ctm = localtime( &c->c_starttime );
271 lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
272 mtm = localtime( &c->c_activitytime );
273 lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
274 # endif /* HAVE_LOCALTIME_R */
275 #else /* !HACK_LOCAL_TIME */
276 # ifdef HAVE_GMTIME_R
277 ctm = gmtime_r( &c->c_starttime, &tm_buf );
278 lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
279 mtm = gmtime_r( &c->c_activitytime, &tm_buf );
280 lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
282 ctm = gmtime( &c->c_starttime );
283 lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
284 mtm = gmtime( &c->c_activitytime );
285 lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
286 # endif /* HAVE_GMTIME_R */
287 #endif /* !HACK_LOCAL_TIME */
288 #ifndef HAVE_GMTIME_R
289 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
292 #ifndef HAVE_GMTIME_R
293 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
297 ltm = gmtime_r( &c->c_starttime, &tm_buf );
299 ltm = gmtime( &c->c_starttime );
301 lutil_gentime( buf2, sizeof( buf2 ), ltm );
304 ltm = gmtime_r( &c->c_activitytime, &tm_buf );
306 ltm = gmtime( &c->c_activitytime );
308 lutil_gentime( buf3, sizeof( buf3 ), ltm );
310 #ifndef HAVE_GMTIME_R
311 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
312 #endif /* HAVE_GMTIME_R */
314 snprintf( buf, sizeof( buf ),
315 "dn: cn=Connection %ld,%s\n"
317 "structuralObjectClass: %s\n"
318 "cn: Connection %ld\n"
320 #ifdef MONITOR_LEGACY_CONN
321 /* NOTE: this will disappear, as the exploded data
322 * has been moved to dedicated attributes */
336 #endif /* MONITOR_LEGACY_CONN */
363 "modifiersName: %s\n"
364 "createTimestamp: %s\n"
365 "modifyTimestamp: %s\n",
366 c->c_connid, ms->mss_dn.bv_val,
367 mi->mi_oc_monitorConnection->soc_cname.bv_val,
368 mi->mi_oc_monitorConnection->soc_cname.bv_val,
371 #ifdef MONITOR_LEGACY_CONN
372 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
374 (long) c->c_protocol,
375 c->c_n_ops_received, c->c_n_ops_executing,
376 c->c_n_ops_pending, c->c_n_ops_completed,
378 /* add low-level counters here */
379 c->c_n_get, c->c_n_read, c->c_n_write,
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" : "",
388 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
390 c->c_listener_url.bv_val,
391 c->c_peer_domain.bv_val,
392 c->c_peer_name.bv_val,
393 c->c_sock_name.bv_val,
397 #endif /* MONITOR_LEGACY_CONN */
399 mi->mi_ad_monitorConnectionNumber->ad_cname.bv_val,
401 mi->mi_ad_monitorConnectionProtocol->ad_cname.bv_val,
404 mi->mi_ad_monitorConnectionOpsReceived->ad_cname.bv_val,
406 mi->mi_ad_monitorConnectionOpsExecuting->ad_cname.bv_val,
407 c->c_n_ops_executing,
408 mi->mi_ad_monitorConnectionOpsPending->ad_cname.bv_val,
410 mi->mi_ad_monitorConnectionOpsCompleted->ad_cname.bv_val,
411 c->c_n_ops_completed,
413 mi->mi_ad_monitorConnectionGet->ad_cname.bv_val,
415 mi->mi_ad_monitorConnectionRead->ad_cname.bv_val,
417 mi->mi_ad_monitorConnectionWrite->ad_cname.bv_val,
420 mi->mi_ad_monitorConnectionMask->ad_cname.bv_val,
421 c->c_currentber ? "r" : "",
422 c->c_writewaiter ? "w" : "",
423 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
424 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
425 connection_state2str( c->c_conn_state ),
426 c->c_sasl_bind_in_progress ? "S" : "",
428 mi->mi_ad_monitorConnectionAuthzDN->ad_cname.bv_val,
429 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
431 mi->mi_ad_monitorConnectionListener->ad_cname.bv_val,
432 c->c_listener_url.bv_val,
433 mi->mi_ad_monitorConnectionPeerDomain->ad_cname.bv_val,
434 c->c_peer_domain.bv_val,
435 mi->mi_ad_monitorConnectionLocalAddress->ad_cname.bv_val,
436 c->c_peer_name.bv_val,
437 mi->mi_ad_monitorConnectionPeerAddress->ad_cname.bv_val,
438 c->c_sock_name.bv_val,
440 mi->mi_ad_monitorConnectionStartTime->ad_cname.bv_val,
442 mi->mi_ad_monitorConnectionActivityTime->ad_cname.bv_val,
445 mi->mi_creatorsName.bv_val,
446 mi->mi_creatorsName.bv_val,
450 e = str2entry( buf );
453 Debug( LDAP_DEBUG_ANY,
454 "monitor_subsys_conn_create: "
455 "unable to create entry "
456 "\"cn=Connection %ld,%s\" entry\n",
458 ms->mss_dn.bv_val, 0 );
462 mp = monitor_entrypriv_create();
466 e->e_private = ( void * )mp;
468 mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
476 monitor_subsys_conn_create(
483 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
489 monitor_subsys_t *ms;
491 assert( mi != NULL );
492 assert( e_parent != NULL );
493 assert( ep != NULL );
495 ms = (( monitor_entry_t *)e_parent->e_private)->mp_info;
503 /* create all the children of e_parent */
504 for ( c = connection_first( &connindex );
506 c = connection_next( c, &connindex ))
508 if ( conn_create( mi, c, &e, ms ) || e == NULL ) {
509 for ( ; e_tmp != NULL; ) {
510 mp = ( monitor_entry_t * )e_tmp->e_private;
514 e_tmp->e_private = NULL;
522 mp = ( monitor_entry_t * )e->e_private;
530 unsigned long connid;
532 static struct berval nconn_bv = BER_BVC( "cn=connection " );
535 /* create exactly the required entry;
536 * the normalized DN must start with "cn=connection ",
537 * followed by the connection id, followed by
538 * the RDN separator "," */
539 if ( ndn->bv_len <= nconn_bv.bv_len
540 || strncmp( ndn->bv_val, nconn_bv.bv_val, nconn_bv.bv_len ) != 0 )
545 connid = strtol( &ndn->bv_val[ nconn_bv.bv_len ], &next, 10 );
546 if ( next[ 0 ] != ',' ) {
550 for ( c = connection_first( &connindex );
552 c = connection_next( c, &connindex )) {
553 if ( c->c_connid == connid ) {
554 if ( conn_create( mi, c, ep, ms ) || *ep == NULL ) {