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 monitor_subsys_conn_init(
38 Entry *e, **ep, *e_conn;
40 char buf[ BACKMONITOR_BUFSIZE ];
45 mi = ( monitor_info_t * )be->be_private;
47 if ( monitor_cache_get( mi, &ms->mss_ndn, &e_conn ) ) {
48 Debug( LDAP_DEBUG_ANY,
49 "monitor_subsys_conn_init: "
50 "unable to get entry \"%s\"\n",
51 ms->mss_ndn.bv_val, 0, 0 );
55 mp = ( monitor_entry_t * )e_conn->e_private;
56 mp->mp_children = NULL;
57 ep = &mp->mp_children;
62 snprintf( buf, sizeof( buf ),
65 "structuralObjectClass: %s\n"
69 "createTimestamp: %s\n"
70 "modifyTimestamp: %s\n",
72 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
73 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
74 mi->mi_creatorsName.bv_val,
75 mi->mi_creatorsName.bv_val,
76 mi->mi_startTime.bv_val,
77 mi->mi_startTime.bv_val );
81 Debug( LDAP_DEBUG_ANY,
82 "monitor_subsys_conn_init: "
83 "unable to create entry \"cn=Total,%s\"\n",
84 ms->mss_ndn.bv_val, 0, 0 );
88 BER_BVSTR( &bv, "0" );
89 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
91 mp = monitor_entrypriv_create();
95 e->e_private = ( void * )mp;
97 mp->mp_flags = ms->mss_flags \
98 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
99 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
101 if ( monitor_cache_add( mi, e ) ) {
102 Debug( LDAP_DEBUG_ANY,
103 "monitor_subsys_conn_init: "
104 "unable to add entry \"cn=Total,%s\"\n",
105 ms->mss_ndn.bv_val, 0, 0 );
115 snprintf( buf, sizeof( buf ),
116 "dn: cn=Current,%s\n"
118 "structuralObjectClass: %s\n"
121 "modifiersName: %s\n"
122 "createTimestamp: %s\n"
123 "modifyTimestamp: %s\n",
125 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
126 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
127 mi->mi_creatorsName.bv_val,
128 mi->mi_creatorsName.bv_val,
129 mi->mi_startTime.bv_val,
130 mi->mi_startTime.bv_val );
132 e = str2entry( buf );
134 Debug( LDAP_DEBUG_ANY,
135 "monitor_subsys_conn_init: "
136 "unable to create entry \"cn=Current,%s\"\n",
137 ms->mss_ndn.bv_val, 0, 0 );
141 BER_BVSTR( &bv, "0" );
142 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
144 mp = monitor_entrypriv_create();
148 e->e_private = ( void * )mp;
150 mp->mp_flags = ms->mss_flags \
151 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
152 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
154 if ( monitor_cache_add( mi, e ) ) {
155 Debug( LDAP_DEBUG_ANY,
156 "monitor_subsys_conn_init: "
157 "unable to add entry \"cn=Current,%s\"\n",
158 ms->mss_ndn.bv_val, 0, 0 );
165 monitor_cache_release( mi, e_conn );
171 monitor_subsys_conn_update(
176 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
179 static struct berval total_bv = BER_BVC( "cn=total" ),
180 current_bv = BER_BVC( "cn=current" );
186 dnRdn( &e->e_nname, &rdn );
188 if ( dn_match( &rdn, &total_bv ) ) {
189 n = connections_nextid();
191 } else if ( dn_match( &rdn, ¤t_bv ) ) {
195 for ( n = 0, c = connection_first( &connindex );
197 n++, c = connection_next( c, &connindex ) ) {
200 connection_done( c );
205 char buf[] = "+9223372036854775807L";
208 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
213 snprintf( buf, sizeof( buf ), "%ld", n );
215 if ( len > a->a_vals[ 0 ].bv_len ) {
216 a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
218 a->a_vals[ 0 ].bv_len = len;
219 AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
221 /* FIXME: touch modifyTimestamp? */
237 char buf[ BACKMONITOR_BUFSIZE ];
238 char buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
239 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 snprintf( buf, sizeof( buf ),
289 "dn: cn=Connection %ld,%s\n"
291 "structuralObjectClass: %s\n"
292 "cn: Connection %ld\n"
294 "modifiersName: %s\n"
295 "createTimestamp: %s\n"
296 "modifyTimestamp: %s\n",
297 c->c_connid, ms->mss_dn.bv_val,
298 mi->mi_oc_monitorConnection->soc_cname.bv_val,
299 mi->mi_oc_monitorConnection->soc_cname.bv_val,
301 mi->mi_creatorsName.bv_val,
302 mi->mi_creatorsName.bv_val,
306 e = str2entry( buf );
309 Debug( LDAP_DEBUG_ANY,
310 "monitor_subsys_conn_create: "
311 "unable to create entry "
312 "\"cn=Connection %ld,%s\" entry\n",
314 ms->mss_dn.bv_val, 0 );
318 #ifndef HAVE_GMTIME_R
319 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
323 ltm = gmtime_r( &c->c_starttime, &tm_buf );
325 ltm = gmtime( &c->c_starttime );
327 lutil_gentime( buf2, sizeof( buf2 ), ltm );
330 ltm = gmtime_r( &c->c_activitytime, &tm_buf );
332 ltm = gmtime( &c->c_activitytime );
334 lutil_gentime( buf3, sizeof( buf3 ), ltm );
336 #ifndef HAVE_GMTIME_R
337 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
338 #endif /* HAVE_GMTIME_R */
355 (long) c->c_protocol,
356 c->c_n_ops_received, c->c_n_ops_executing,
357 c->c_n_ops_pending, c->c_n_ops_completed,
359 /* add low-level counters here */
360 c->c_n_get, c->c_n_read, c->c_n_write,
362 c->c_currentber ? "r" : "",
363 c->c_writewaiter ? "w" : "",
364 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
365 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
366 connection_state2str( c->c_conn_state ),
367 c->c_sasl_bind_in_progress ? "S" : "",
369 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
371 c->c_listener_url.bv_val,
372 c->c_peer_domain.bv_val,
373 c->c_peer_name.bv_val,
374 c->c_sock_name.bv_val,
381 bv.bv_len = strlen( buf );
382 attr_merge_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
384 /* connection number */
385 snprintf( buf, sizeof( buf ), "%ld", c->c_connid );
387 bv.bv_len = strlen( buf );
388 attr_merge_one( e, mi->mi_ad_monitorConnectionNumber, &bv, NULL );
391 attr_merge_one( e, mi->mi_ad_monitorConnectionAuthzDN,
392 &c->c_dn, &c->c_ndn );
395 attr_merge_one( e, mi->mi_ad_monitorConnectionLocalAddress,
396 &c->c_sock_name, NULL );
399 attr_merge_one( e, mi->mi_ad_monitorConnectionPeerAddress,
400 &c->c_peer_name, NULL );
402 mp = monitor_entrypriv_create();
406 e->e_private = ( void * )mp;
408 mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
416 monitor_subsys_conn_create(
423 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
429 monitor_subsys_t *ms;
431 assert( mi != NULL );
432 assert( e_parent != NULL );
433 assert( ep != NULL );
435 ms = (( monitor_entry_t *)e_parent->e_private)->mp_info;
443 /* create all the children of e_parent */
444 for ( c = connection_first( &connindex );
446 c = connection_next( c, &connindex ))
448 if ( conn_create( mi, c, &e, ms ) || e == NULL ) {
449 for ( ; e_tmp != NULL; ) {
450 mp = ( monitor_entry_t * )e_tmp->e_private;
454 e_tmp->e_private = NULL;
462 mp = ( monitor_entry_t * )e->e_private;
470 unsigned long connid;
472 static struct berval nconn_bv = BER_BVC( "cn=connection " );
475 /* create exactly the required entry;
476 * the normalized DN must start with "cn=connection ",
477 * followed by the connection id, followed by
478 * the RDN separator "," */
479 if ( ndn->bv_len <= nconn_bv.bv_len
480 || strncmp( ndn->bv_val, nconn_bv.bv_val, nconn_bv.bv_len ) != 0 )
485 connid = strtol( &ndn->bv_val[ nconn_bv.bv_len ], &next, 10 );
486 if ( next[ 0 ] != ',' ) {
490 for ( c = connection_first( &connindex );
492 c = connection_next( c, &connindex )) {
493 if ( c->c_connid == connid ) {
494 if ( conn_create( mi, c, ep, ms ) || *ep == NULL ) {