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 ) ) {
52 LDAP_LOG( OPERATION, CRIT,
53 "monitor_subsys_conn_init: "
54 "unable to get entry '%s'\n",
55 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
57 Debug( LDAP_DEBUG_ANY,
58 "monitor_subsys_conn_init: "
59 "unable to get entry '%s'\n%s%s",
60 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
71 snprintf( buf, sizeof( buf ),
74 "structuralObjectClass: %s\n"
76 "createTimestamp: %s\n"
77 "modifyTimestamp: %s\n",
78 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
79 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
80 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
81 mi->mi_startTime.bv_val,
82 mi->mi_startTime.bv_val );
87 LDAP_LOG( OPERATION, CRIT,
88 "monitor_subsys_conn_init: "
89 "unable to create entry 'cn=Total,%s'\n",
90 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
92 Debug( LDAP_DEBUG_ANY,
93 "monitor_subsys_conn_init: "
94 "unable to create entry 'cn=Total,%s'\n%s%s",
95 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
103 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
105 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
106 e->e_private = ( void * )mp;
108 mp->mp_children = NULL;
109 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
110 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
111 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
112 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
114 if ( monitor_cache_add( mi, e ) ) {
116 LDAP_LOG( OPERATION, CRIT,
117 "monitor_subsys_conn_init: "
118 "unable to add entry 'cn=Total,%s'\n",
119 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
121 Debug( LDAP_DEBUG_ANY,
122 "monitor_subsys_conn_init: "
123 "unable to add entry 'cn=Total,%s'\n%s%s",
124 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
135 snprintf( buf, sizeof( buf ),
136 "dn: cn=Current,%s\n"
138 "structuralObjectClass: %s\n"
140 "createTimestamp: %s\n"
141 "modifyTimestamp: %s\n",
142 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
143 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
144 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
145 mi->mi_startTime.bv_val,
146 mi->mi_startTime.bv_val );
148 e = str2entry( buf );
151 LDAP_LOG( OPERATION, CRIT,
152 "monitor_subsys_conn_init: "
153 "unable to create entry 'cn=Current,%s'\n",
154 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
156 Debug( LDAP_DEBUG_ANY,
157 "monitor_subsys_conn_init: "
158 "unable to create entry 'cn=Current,%s'\n%s%s",
159 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
167 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
169 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
170 e->e_private = ( void * )mp;
172 mp->mp_children = NULL;
173 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
174 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
175 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
176 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
178 if ( monitor_cache_add( mi, e ) ) {
180 LDAP_LOG( OPERATION, CRIT,
181 "monitor_subsys_conn_init: "
182 "unable to add entry 'cn=Current,%s'\n",
183 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
185 Debug( LDAP_DEBUG_ANY,
186 "monitor_subsys_conn_init: "
187 "unable to add entry 'cn=Current,%s'\n%s%s",
188 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
196 mp = ( struct monitorentrypriv * )e_conn->e_private;
197 mp->mp_children = e_tmp;
199 monitor_cache_release( mi, e_conn );
205 monitor_subsys_conn_update(
210 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
216 if ( strncasecmp( e->e_ndn, "cn=total",
217 sizeof("cn=total")-1 ) == 0 ) {
218 n = connections_nextid();
220 } else if ( strncasecmp( e->e_ndn, "cn=current",
221 sizeof("cn=current")-1 ) == 0 ) {
225 for ( n = 0, c = connection_first( &connindex );
227 n++, c = connection_next( c, &connindex ) ) {
235 char buf[] = "+9223372036854775807L";
237 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
242 snprintf( buf, sizeof( buf ), "%ld", n );
243 free( a->a_vals[ 0 ].bv_val );
244 ber_str2bv( buf, 0, 1, a->a_vals );
252 struct monitorinfo *mi,
257 struct monitorentrypriv *mp;
259 char buf[ BACKMONITOR_BUFSIZE ];
260 char buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
261 char buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
268 char ctmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
270 char mtmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
273 #endif /* HAVE_GMTIME_R */
276 assert( ep != NULL );
278 #ifndef HAVE_GMTIME_R
279 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
281 #ifdef HACK_LOCAL_TIME
282 # ifdef HAVE_LOCALTIME_R
283 ctm = localtime_r( &c->c_starttime, &tm_buf );
284 lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
285 mtm = localtime_r( &c->c_activitytime, &tm_buf );
286 lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
288 ctm = localtime( &c->c_starttime );
289 lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
290 mtm = localtime( &c->c_activitytime );
291 lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
292 # endif /* HAVE_LOCALTIME_R */
293 #else /* !HACK_LOCAL_TIME */
294 # ifdef HAVE_GMTIME_R
295 ctm = gmtime_r( &c->c_starttime, &tm_buf );
296 lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
297 mtm = gmtime_r( &c->c_activitytime, &tm_buf );
298 lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
300 ctm = gmtime( &c->c_starttime );
301 lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
302 mtm = gmtime( &c->c_activitytime );
303 lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
304 # endif /* HAVE_GMTIME_R */
305 #endif /* !HACK_LOCAL_TIME */
306 #ifndef HAVE_GMTIME_R
307 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
310 snprintf( buf, sizeof( buf ),
311 "dn: cn=" CONN_CN_PREFIX " %ld,%s\n"
313 "structuralObjectClass: %s\n"
314 "cn: " CONN_CN_PREFIX " %ld\n"
315 "createTimestamp: %s\n"
316 "modifyTimestamp: %s\n",
317 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
318 mi->mi_oc_monitorConnection->soc_cname.bv_val,
319 mi->mi_oc_monitorConnection->soc_cname.bv_val,
323 e = str2entry( buf );
327 LDAP_LOG( OPERATION, CRIT,
328 "monitor_subsys_conn_create: "
329 "unable to create entry "
330 "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
331 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
333 Debug( LDAP_DEBUG_ANY,
334 "monitor_subsys_conn_create: "
335 "unable to create entry "
336 "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
338 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
343 #ifndef HAVE_GMTIME_R
344 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
348 ltm = gmtime_r( &c->c_starttime, &tm_buf );
350 ltm = gmtime( &c->c_starttime );
352 lutil_gentime( buf2, sizeof( buf2 ), ltm );
355 ltm = gmtime_r( &c->c_activitytime, &tm_buf );
357 ltm = gmtime( &c->c_activitytime );
359 lutil_gentime( buf3, sizeof( buf3 ), ltm );
361 #ifndef HAVE_GMTIME_R
362 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
363 #endif /* HAVE_GMTIME_R */
372 ": %s : %s : %s : %s",
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,
400 bv.bv_len = strlen( buf );
401 attr_merge_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
403 /* connection number */
404 snprintf( buf, sizeof( buf ), "%ld", c->c_connid );
406 bv.bv_len = strlen( buf );
407 attr_merge_one( e, mi->mi_ad_monitorConnectionNumber, &bv, NULL );
410 attr_merge_one( e, mi->mi_ad_monitorConnectionAuthzDN,
411 &c->c_dn, &c->c_ndn );
414 attr_merge_one( e, mi->mi_ad_monitorConnectionLocalAddress,
415 &c->c_sock_name, NULL );
418 attr_merge_one( e, mi->mi_ad_monitorConnectionPeerAddress,
419 &c->c_peer_name, NULL );
421 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
422 e->e_private = ( void * )mp;
423 mp->mp_info = &monitor_subsys[ SLAPD_MONITOR_CONN ];
424 mp->mp_children = NULL;
425 mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
433 monitor_subsys_conn_create(
440 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
443 struct monitorentrypriv *mp;
445 assert( mi != NULL );
446 assert( e_parent != NULL );
447 assert( ep != NULL );
452 Entry *e, *e_tmp = NULL;
454 /* create all the children of e_parent */
455 for ( c = connection_first( &connindex );
457 c = connection_next( c, &connindex )) {
458 if ( conn_create( mi, c, &e ) || e == NULL ) {
460 for ( ; e_tmp != NULL; ) {
461 mp = ( struct monitorentrypriv * )e_tmp->e_private;
465 e_tmp->e_private = NULL;
472 mp = ( struct monitorentrypriv * )e->e_private;
481 LDAPRDN values = NULL;
482 const char *text = NULL;
483 unsigned long connid;
485 /* create exactly the required entry */
487 if ( ldap_bv2rdn( ndn, &values, (char **)&text,
488 LDAP_DN_FORMAT_LDAP ) )
494 assert( values[ 0 ] );
496 connid = atol( values[ 0 ]->la_value.bv_val
497 + sizeof( CONN_CN_PREFIX ) );
499 ldap_rdnfree( values );
501 for ( c = connection_first( &connindex );
503 c = connection_next( c, &connindex )) {
504 if ( c->c_connid == connid ) {
505 if ( conn_create( mi, c, ep ) || *ep == NULL ) {
506 connection_done( c );