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(
181 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
184 static struct berval total_bv = BER_BVC( "cn=total" ),
185 current_bv = BER_BVC( "cn=current" );
191 dnRdn( &e->e_nname, &rdn );
193 if ( dn_match( &rdn, &total_bv ) ) {
194 n = connections_nextid();
196 } else if ( dn_match( &rdn, ¤t_bv ) ) {
200 for ( n = 0, c = connection_first( &connindex );
202 n++, c = connection_next( c, &connindex ) ) {
205 connection_done( c );
210 char buf[] = "+9223372036854775807L";
213 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
218 snprintf( buf, sizeof( buf ), "%ld", n );
220 if ( len > a->a_vals[ 0 ].bv_len ) {
221 a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
223 a->a_vals[ 0 ].bv_len = len;
224 AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
226 /* FIXME: touch modifyTimestamp? */
229 return SLAP_CB_CONTINUE;
242 char buf[ BACKMONITOR_BUFSIZE ];
243 char buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
244 char buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
249 char ctmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
251 char mtmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
254 #endif /* HAVE_GMTIME_R */
257 assert( ep != NULL );
259 #ifndef HAVE_GMTIME_R
260 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
262 #ifdef HACK_LOCAL_TIME
263 # ifdef HAVE_LOCALTIME_R
264 ctm = localtime_r( &c->c_starttime, &tm_buf );
265 lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
266 mtm = localtime_r( &c->c_activitytime, &tm_buf );
267 lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
269 ctm = localtime( &c->c_starttime );
270 lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
271 mtm = localtime( &c->c_activitytime );
272 lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
273 # endif /* HAVE_LOCALTIME_R */
274 #else /* !HACK_LOCAL_TIME */
275 # ifdef HAVE_GMTIME_R
276 ctm = gmtime_r( &c->c_starttime, &tm_buf );
277 lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
278 mtm = gmtime_r( &c->c_activitytime, &tm_buf );
279 lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
281 ctm = gmtime( &c->c_starttime );
282 lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
283 mtm = gmtime( &c->c_activitytime );
284 lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
285 # endif /* HAVE_GMTIME_R */
286 #endif /* !HACK_LOCAL_TIME */
287 #ifndef HAVE_GMTIME_R
288 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
291 #ifndef HAVE_GMTIME_R
292 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
296 ltm = gmtime_r( &c->c_starttime, &tm_buf );
298 ltm = gmtime( &c->c_starttime );
300 lutil_gentime( buf2, sizeof( buf2 ), ltm );
303 ltm = gmtime_r( &c->c_activitytime, &tm_buf );
305 ltm = gmtime( &c->c_activitytime );
307 lutil_gentime( buf3, sizeof( buf3 ), ltm );
309 #ifndef HAVE_GMTIME_R
310 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
311 #endif /* HAVE_GMTIME_R */
313 snprintf( buf, sizeof( buf ),
314 "dn: cn=Connection %ld,%s\n"
316 "structuralObjectClass: %s\n"
317 "cn: Connection %ld\n"
319 #ifdef MONITOR_LEGACY_CONN
320 /* NOTE: this will disappear, as the exploded data
321 * has been moved to dedicated attributes */
335 #endif /* MONITOR_LEGACY_CONN */
362 "modifiersName: %s\n"
363 "createTimestamp: %s\n"
364 "modifyTimestamp: %s\n",
365 c->c_connid, ms->mss_dn.bv_val,
366 mi->mi_oc_monitorConnection->soc_cname.bv_val,
367 mi->mi_oc_monitorConnection->soc_cname.bv_val,
370 #ifdef MONITOR_LEGACY_CONN
371 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
373 (long) c->c_protocol,
374 c->c_n_ops_received, c->c_n_ops_executing,
375 c->c_n_ops_pending, c->c_n_ops_completed,
377 /* add low-level counters here */
378 c->c_n_get, c->c_n_read, c->c_n_write,
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" : "",
387 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
389 c->c_listener_url.bv_val,
390 c->c_peer_domain.bv_val,
391 c->c_peer_name.bv_val,
392 c->c_sock_name.bv_val,
396 #endif /* MONITOR_LEGACY_CONN */
398 mi->mi_ad_monitorConnectionNumber->ad_cname.bv_val,
400 mi->mi_ad_monitorConnectionProtocol->ad_cname.bv_val,
403 mi->mi_ad_monitorConnectionOpsReceived->ad_cname.bv_val,
405 mi->mi_ad_monitorConnectionOpsExecuting->ad_cname.bv_val,
406 c->c_n_ops_executing,
407 mi->mi_ad_monitorConnectionOpsPending->ad_cname.bv_val,
409 mi->mi_ad_monitorConnectionOpsCompleted->ad_cname.bv_val,
410 c->c_n_ops_completed,
412 mi->mi_ad_monitorConnectionGet->ad_cname.bv_val,
414 mi->mi_ad_monitorConnectionRead->ad_cname.bv_val,
416 mi->mi_ad_monitorConnectionWrite->ad_cname.bv_val,
419 mi->mi_ad_monitorConnectionMask->ad_cname.bv_val,
420 c->c_currentber ? "r" : "",
421 c->c_writewaiter ? "w" : "",
422 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
423 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
424 connection_state2str( c->c_conn_state ),
425 c->c_sasl_bind_in_progress ? "S" : "",
427 mi->mi_ad_monitorConnectionAuthzDN->ad_cname.bv_val,
428 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
430 mi->mi_ad_monitorConnectionListener->ad_cname.bv_val,
431 c->c_listener_url.bv_val,
432 mi->mi_ad_monitorConnectionPeerDomain->ad_cname.bv_val,
433 c->c_peer_domain.bv_val,
434 mi->mi_ad_monitorConnectionLocalAddress->ad_cname.bv_val,
435 c->c_peer_name.bv_val,
436 mi->mi_ad_monitorConnectionPeerAddress->ad_cname.bv_val,
437 c->c_sock_name.bv_val,
439 mi->mi_ad_monitorConnectionStartTime->ad_cname.bv_val,
441 mi->mi_ad_monitorConnectionActivityTime->ad_cname.bv_val,
444 mi->mi_creatorsName.bv_val,
445 mi->mi_creatorsName.bv_val,
449 e = str2entry( buf );
452 Debug( LDAP_DEBUG_ANY,
453 "monitor_subsys_conn_create: "
454 "unable to create entry "
455 "\"cn=Connection %ld,%s\" entry\n",
457 ms->mss_dn.bv_val, 0 );
461 mp = monitor_entrypriv_create();
465 e->e_private = ( void * )mp;
467 mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
471 return SLAP_CB_CONTINUE;
475 monitor_subsys_conn_create(
483 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
488 int rc = SLAP_CB_CONTINUE;
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;
519 rc = rs->sr_err = LDAP_OTHER;
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 ] != ',' ) {
547 return ( rs->sr_err = LDAP_OTHER );
550 for ( c = connection_first( &connindex );
552 c = connection_next( c, &connindex ) )
554 if ( c->c_connid == connid ) {
555 rc = conn_create( mi, c, ep, ms );
556 if ( rc != SLAP_CB_CONTINUE ) {
559 } else if ( *ep == NULL ) {
560 rc = rs->sr_err = LDAP_OTHER;
567 connection_done( c );