1 /* conn.c - deal with connection subsystem */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by Pierangelo Masarati for inclusion
18 * in OpenLDAP Software.
20 /* This is an altered version */
22 * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
24 * This work has beed deveolped for the OpenLDAP Foundation
25 * in the hope that it may be useful to the Open Source community,
26 * but WITHOUT ANY WARRANTY.
28 * Permission is granted to anyone to use this software for any purpose
29 * on any computer system, and to alter it and redistribute it, subject
30 * to the following restrictions:
32 * 1. The author and SysNet s.n.c. are not responsible for the consequences
33 * of use of this software, no matter how awful, even if they arise from
36 * 2. The origin of this software must not be misrepresented, either by
37 * explicit claim or by omission. Since few users ever read sources,
38 * credits should appear in the documentation.
40 * 3. Altered versions must be plainly marked as such, and must not be
41 * misrepresented as being the original software. Since few users
42 * ever read sources, credits should appear in the documentation.
43 * SysNet s.n.c. cannot be responsible for the consequences of the
46 * 4. This notice may not be removed or altered.
52 #include <ac/string.h>
56 #include "back-monitor.h"
58 #define CONN_CN_PREFIX "Connection"
61 monitor_subsys_conn_init(
65 struct monitorinfo *mi;
67 Entry *e, *e_tmp, *e_conn;
68 struct monitorentrypriv *mp;
69 char buf[ BACKMONITOR_BUFSIZE ];
74 mi = ( struct monitorinfo * )be->be_private;
76 if ( monitor_cache_get( mi,
77 &monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn, &e_conn ) ) {
79 LDAP_LOG( OPERATION, CRIT,
80 "monitor_subsys_conn_init: "
81 "unable to get entry '%s'\n",
82 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
84 Debug( LDAP_DEBUG_ANY,
85 "monitor_subsys_conn_init: "
86 "unable to get entry '%s'\n%s%s",
87 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
98 snprintf( buf, sizeof( buf ),
101 "structuralObjectClass: %s\n"
103 "createTimestamp: %s\n"
104 "modifyTimestamp: %s\n",
105 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
106 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
107 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
108 mi->mi_startTime.bv_val,
109 mi->mi_startTime.bv_val );
111 e = str2entry( buf );
114 LDAP_LOG( OPERATION, CRIT,
115 "monitor_subsys_conn_init: "
116 "unable to create entry 'cn=Total,%s'\n",
117 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
119 Debug( LDAP_DEBUG_ANY,
120 "monitor_subsys_conn_init: "
121 "unable to create entry 'cn=Total,%s'\n%s%s",
122 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
130 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
132 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
133 e->e_private = ( void * )mp;
135 mp->mp_children = NULL;
136 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
137 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
138 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
139 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
141 if ( monitor_cache_add( mi, e ) ) {
143 LDAP_LOG( OPERATION, CRIT,
144 "monitor_subsys_conn_init: "
145 "unable to add entry 'cn=Total,%s'\n",
146 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
148 Debug( LDAP_DEBUG_ANY,
149 "monitor_subsys_conn_init: "
150 "unable to add entry 'cn=Total,%s'\n%s%s",
151 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
162 snprintf( buf, sizeof( buf ),
163 "dn: cn=Current,%s\n"
165 "structuralObjectClass: %s\n"
167 "createTimestamp: %s\n"
168 "modifyTimestamp: %s\n",
169 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
170 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
171 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
172 mi->mi_startTime.bv_val,
173 mi->mi_startTime.bv_val );
175 e = str2entry( buf );
178 LDAP_LOG( OPERATION, CRIT,
179 "monitor_subsys_conn_init: "
180 "unable to create entry 'cn=Current,%s'\n",
181 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
183 Debug( LDAP_DEBUG_ANY,
184 "monitor_subsys_conn_init: "
185 "unable to create entry 'cn=Current,%s'\n%s%s",
186 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
194 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
196 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
197 e->e_private = ( void * )mp;
199 mp->mp_children = NULL;
200 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
201 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
202 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
203 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
205 if ( monitor_cache_add( mi, e ) ) {
207 LDAP_LOG( OPERATION, CRIT,
208 "monitor_subsys_conn_init: "
209 "unable to add entry 'cn=Current,%s'\n",
210 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
212 Debug( LDAP_DEBUG_ANY,
213 "monitor_subsys_conn_init: "
214 "unable to add entry 'cn=Current,%s'\n%s%s",
215 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
223 mp = ( struct monitorentrypriv * )e_conn->e_private;
224 mp->mp_children = e_tmp;
226 monitor_cache_release( mi, e_conn );
232 monitor_subsys_conn_update(
237 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
243 if ( strncasecmp( e->e_ndn, "cn=total",
244 sizeof("cn=total")-1 ) == 0 ) {
245 n = connections_nextid();
247 } else if ( strncasecmp( e->e_ndn, "cn=current",
248 sizeof("cn=current")-1 ) == 0 ) {
252 for ( n = 0, c = connection_first( &connindex );
254 n++, c = connection_next( c, &connindex ) ) {
262 char buf[] = "+9223372036854775807L";
264 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
269 snprintf( buf, sizeof( buf ), "%ld", n );
270 free( a->a_vals[ 0 ].bv_val );
271 ber_str2bv( buf, 0, 1, a->a_vals );
279 struct monitorinfo *mi,
284 struct monitorentrypriv *mp;
286 char buf[ BACKMONITOR_BUFSIZE ];
287 char buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
288 char buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
295 char ctmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
297 char mtmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
300 assert( ep != NULL );
302 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
303 #ifdef HACK_LOCAL_TIME
304 ctm = localtime( &c->c_starttime );
305 lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
306 mtm = localtime( &c->c_activitytime );
307 lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
308 #else /* !HACK_LOCAL_TIME */
309 ctm = gmtime( &c->c_starttime );
310 lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
311 mtm = gmtime( &c->c_activitytime );
312 lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
313 #endif /* !HACK_LOCAL_TIME */
314 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
316 snprintf( buf, sizeof( buf ),
317 "dn: cn=" CONN_CN_PREFIX " %ld,%s\n"
319 "structuralObjectClass: %s\n"
320 "cn: " CONN_CN_PREFIX " %ld\n"
321 "createTimestamp: %s\n"
322 "modifyTimestamp: %s\n",
323 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
324 mi->mi_oc_monitorConnection->soc_cname.bv_val,
325 mi->mi_oc_monitorConnection->soc_cname.bv_val,
329 e = str2entry( buf );
333 LDAP_LOG( OPERATION, CRIT,
334 "monitor_subsys_conn_create: "
335 "unable to create entry "
336 "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
337 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
339 Debug( LDAP_DEBUG_ANY,
340 "monitor_subsys_conn_create: "
341 "unable to create entry "
342 "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
344 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
349 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
351 ltm = gmtime( &c->c_starttime );
352 lutil_gentime( buf2, sizeof( buf2 ), ltm );
354 ltm = gmtime( &c->c_activitytime );
355 lutil_gentime( buf3, sizeof( buf3 ), ltm );
357 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
366 ": %s : %s : %s : %s",
368 (long) c->c_protocol,
369 c->c_n_ops_received, c->c_n_ops_executing,
370 c->c_n_ops_pending, c->c_n_ops_completed,
372 /* add low-level counters here */
373 c->c_n_get, c->c_n_read, c->c_n_write,
375 c->c_currentber ? "r" : "",
376 c->c_writewaiter ? "w" : "",
377 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
378 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
379 connection_state2str( c->c_conn_state ),
380 c->c_sasl_bind_in_progress ? "S" : "",
382 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
384 c->c_listener_url.bv_val,
385 c->c_peer_domain.bv_val,
386 c->c_peer_name.bv_val,
387 c->c_sock_name.bv_val,
394 bv.bv_len = strlen( buf );
395 attr_merge_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
397 /* connection number */
398 snprintf( buf, sizeof( buf ), "%ld", c->c_connid );
400 bv.bv_len = strlen( buf );
401 attr_merge_one( e, mi->mi_ad_monitorConnectionNumber, &bv, NULL );
404 attr_merge_one( e, mi->mi_ad_monitorConnectionAuthzDN,
405 &c->c_dn, &c->c_ndn );
408 attr_merge_one( e, mi->mi_ad_monitorConnectionLocalAddress,
409 &c->c_sock_name, NULL );
412 attr_merge_one( e, mi->mi_ad_monitorConnectionPeerAddress,
413 &c->c_peer_name, NULL );
415 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
416 e->e_private = ( void * )mp;
417 mp->mp_info = &monitor_subsys[ SLAPD_MONITOR_CONN ];
418 mp->mp_children = NULL;
419 mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
427 monitor_subsys_conn_create(
434 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
437 struct monitorentrypriv *mp;
439 assert( mi != NULL );
440 assert( e_parent != NULL );
441 assert( ep != NULL );
446 Entry *e, *e_tmp = NULL;
448 /* create all the children of e_parent */
449 for ( c = connection_first( &connindex );
451 c = connection_next( c, &connindex )) {
452 if ( conn_create( mi, c, &e ) || e == NULL ) {
454 for ( ; e_tmp != NULL; ) {
455 mp = ( struct monitorentrypriv * )e_tmp->e_private;
459 e_tmp->e_private = NULL;
466 mp = ( struct monitorentrypriv * )e->e_private;
475 LDAPRDN values = NULL;
476 const char *text = NULL;
477 unsigned long connid;
479 /* create exactly the required entry */
481 if ( ldap_bv2rdn( ndn, &values, (char **)&text,
482 LDAP_DN_FORMAT_LDAP ) )
488 assert( values[ 0 ] );
490 connid = atol( values[ 0 ]->la_value.bv_val
491 + sizeof( CONN_CN_PREFIX ) );
493 ldap_rdnfree( values );
495 for ( c = connection_first( &connindex );
497 c = connection_next( c, &connindex )) {
498 if ( c->c_connid == connid ) {
499 if ( conn_create( mi, c, ep ) || *ep == NULL ) {
500 connection_done( c );