1 /* conn.c - deal with connection subsystem */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2004 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"
31 #define CONN_CN_PREFIX "Connection"
34 monitor_subsys_conn_init(
38 struct monitorinfo *mi;
40 Entry *e, *e_tmp, *e_conn;
41 struct monitorentrypriv *mp;
42 char buf[ BACKMONITOR_BUFSIZE ];
47 mi = ( struct monitorinfo * )be->be_private;
49 if ( monitor_cache_get( mi,
50 &monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn, &e_conn ) ) {
51 Debug( LDAP_DEBUG_ANY,
52 "monitor_subsys_conn_init: "
53 "unable to get entry '%s'\n%s%s",
54 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
64 snprintf( buf, sizeof( buf ),
67 "structuralObjectClass: %s\n"
71 "createTimestamp: %s\n"
72 "modifyTimestamp: %s\n",
73 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
74 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
75 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
76 mi->mi_creatorsName.bv_val,
77 mi->mi_creatorsName.bv_val,
78 mi->mi_startTime.bv_val,
79 mi->mi_startTime.bv_val );
83 Debug( LDAP_DEBUG_ANY,
84 "monitor_subsys_conn_init: "
85 "unable to create entry 'cn=Total,%s'\n%s%s",
86 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
93 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
95 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
96 e->e_private = ( void * )mp;
98 mp->mp_children = NULL;
99 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
100 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].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%s%s",
108 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
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",
127 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
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%s%s",
140 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
147 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
149 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
150 e->e_private = ( void * )mp;
152 mp->mp_children = NULL;
153 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
154 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].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%s%s",
162 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
169 mp = ( struct monitorentrypriv * )e_conn->e_private;
170 mp->mp_children = e_tmp;
172 monitor_cache_release( mi, e_conn );
178 monitor_subsys_conn_update(
183 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
189 if ( strncasecmp( e->e_ndn, "cn=total",
190 sizeof("cn=total")-1 ) == 0 ) {
191 n = connections_nextid();
193 } else if ( strncasecmp( e->e_ndn, "cn=current",
194 sizeof("cn=current")-1 ) == 0 ) {
198 for ( n = 0, c = connection_first( &connindex );
200 n++, c = connection_next( c, &connindex ) ) {
208 char buf[] = "+9223372036854775807L";
210 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
215 snprintf( buf, sizeof( buf ), "%ld", n );
216 free( a->a_vals[ 0 ].bv_val );
217 ber_str2bv( buf, 0, 1, a->a_vals );
225 struct monitorinfo *mi,
230 struct monitorentrypriv *mp;
232 char buf[ BACKMONITOR_BUFSIZE ];
233 char buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
234 char buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
241 char ctmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
243 char mtmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
246 #endif /* HAVE_GMTIME_R */
249 assert( ep != NULL );
251 #ifndef HAVE_GMTIME_R
252 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
254 #ifdef HACK_LOCAL_TIME
255 # ifdef HAVE_LOCALTIME_R
256 ctm = localtime_r( &c->c_starttime, &tm_buf );
257 lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
258 mtm = localtime_r( &c->c_activitytime, &tm_buf );
259 lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
261 ctm = localtime( &c->c_starttime );
262 lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
263 mtm = localtime( &c->c_activitytime );
264 lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
265 # endif /* HAVE_LOCALTIME_R */
266 #else /* !HACK_LOCAL_TIME */
267 # ifdef HAVE_GMTIME_R
268 ctm = gmtime_r( &c->c_starttime, &tm_buf );
269 lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
270 mtm = gmtime_r( &c->c_activitytime, &tm_buf );
271 lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
273 ctm = gmtime( &c->c_starttime );
274 lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
275 mtm = gmtime( &c->c_activitytime );
276 lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
277 # endif /* HAVE_GMTIME_R */
278 #endif /* !HACK_LOCAL_TIME */
279 #ifndef HAVE_GMTIME_R
280 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
283 snprintf( buf, sizeof( buf ),
284 "dn: cn=" CONN_CN_PREFIX " %ld,%s\n"
286 "structuralObjectClass: %s\n"
287 "cn: " CONN_CN_PREFIX " %ld\n"
289 "modifiersName: %s\n"
290 "createTimestamp: %s\n"
291 "modifyTimestamp: %s\n",
292 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
293 mi->mi_oc_monitorConnection->soc_cname.bv_val,
294 mi->mi_oc_monitorConnection->soc_cname.bv_val,
296 mi->mi_creatorsName.bv_val,
297 mi->mi_creatorsName.bv_val,
301 e = str2entry( buf );
304 Debug( LDAP_DEBUG_ANY,
305 "monitor_subsys_conn_create: "
306 "unable to create entry "
307 "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
309 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
313 #ifndef HAVE_GMTIME_R
314 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
318 ltm = gmtime_r( &c->c_starttime, &tm_buf );
320 ltm = gmtime( &c->c_starttime );
322 lutil_gentime( buf2, sizeof( buf2 ), ltm );
325 ltm = gmtime_r( &c->c_activitytime, &tm_buf );
327 ltm = gmtime( &c->c_activitytime );
329 lutil_gentime( buf3, sizeof( buf3 ), ltm );
331 #ifndef HAVE_GMTIME_R
332 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
333 #endif /* HAVE_GMTIME_R */
342 ": %s : %s : %s : %s",
344 (long) c->c_protocol,
345 c->c_n_ops_received, c->c_n_ops_executing,
346 c->c_n_ops_pending, c->c_n_ops_completed,
348 /* add low-level counters here */
349 c->c_n_get, c->c_n_read, c->c_n_write,
351 c->c_currentber ? "r" : "",
352 c->c_writewaiter ? "w" : "",
353 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
354 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
355 connection_state2str( c->c_conn_state ),
356 c->c_sasl_bind_in_progress ? "S" : "",
358 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
360 c->c_listener_url.bv_val,
361 c->c_peer_domain.bv_val,
362 c->c_peer_name.bv_val,
363 c->c_sock_name.bv_val,
370 bv.bv_len = strlen( buf );
371 attr_merge_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
373 /* connection number */
374 snprintf( buf, sizeof( buf ), "%ld", c->c_connid );
376 bv.bv_len = strlen( buf );
377 attr_merge_one( e, mi->mi_ad_monitorConnectionNumber, &bv, NULL );
380 attr_merge_one( e, mi->mi_ad_monitorConnectionAuthzDN,
381 &c->c_dn, &c->c_ndn );
384 attr_merge_one( e, mi->mi_ad_monitorConnectionLocalAddress,
385 &c->c_sock_name, NULL );
388 attr_merge_one( e, mi->mi_ad_monitorConnectionPeerAddress,
389 &c->c_peer_name, NULL );
391 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
392 e->e_private = ( void * )mp;
393 mp->mp_info = &monitor_subsys[ SLAPD_MONITOR_CONN ];
394 mp->mp_children = NULL;
395 mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
403 monitor_subsys_conn_create(
410 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
413 struct monitorentrypriv *mp;
415 assert( mi != NULL );
416 assert( e_parent != NULL );
417 assert( ep != NULL );
422 Entry *e, *e_tmp = NULL;
424 /* create all the children of e_parent */
425 for ( c = connection_first( &connindex );
427 c = connection_next( c, &connindex )) {
428 if ( conn_create( mi, c, &e ) || e == NULL ) {
430 for ( ; e_tmp != NULL; ) {
431 mp = ( struct monitorentrypriv * )e_tmp->e_private;
435 e_tmp->e_private = NULL;
442 mp = ( struct monitorentrypriv * )e->e_private;
451 LDAPRDN values = NULL;
452 const char *text = NULL;
453 unsigned long connid;
455 /* create exactly the required entry */
457 if ( ldap_bv2rdn( ndn, &values, (char **)&text,
458 LDAP_DN_FORMAT_LDAP ) )
464 assert( values[ 0 ] );
466 connid = atol( values[ 0 ]->la_value.bv_val
467 + sizeof( CONN_CN_PREFIX ) );
469 ldap_rdnfree( values );
471 for ( c = connection_first( &connindex );
473 c = connection_next( c, &connindex )) {
474 if ( c->c_connid == connid ) {
475 if ( conn_create( mi, c, ep ) || *ep == NULL ) {
476 connection_done( c );