1 /* conn.c - deal with connection subsystem */
3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
9 * This work has beed deveolped for the OpenLDAP Foundation
10 * in the hope that it may be useful to the Open Source community,
11 * but WITHOUT ANY WARRANTY.
13 * Permission is granted to anyone to use this software for any purpose
14 * on any computer system, and to alter it and redistribute it, subject
15 * to the following restrictions:
17 * 1. The author and SysNet s.n.c. are not responsible for the consequences
18 * of use of this software, no matter how awful, even if they arise from
21 * 2. The origin of this software must not be misrepresented, either by
22 * explicit claim or by omission. Since few users ever read sources,
23 * credits should appear in the documentation.
25 * 3. Altered versions must be plainly marked as such, and must not be
26 * misrepresented as being the original software. Since few users
27 * ever read sources, credits should appear in the documentation.
28 * SysNet s.n.c. cannot be responsible for the consequences of the
31 * 4. This notice may not be removed or altered.
40 #include "back-monitor.h"
42 #define CONN_CN_PREFIX "Connection"
45 monitor_subsys_conn_init(
49 struct monitorinfo *mi;
51 Entry *e, *e_tmp, *e_conn;
52 struct monitorentrypriv *mp;
53 char buf[ BACKMONITOR_BUFSIZE ];
58 mi = ( struct monitorinfo * )be->be_private;
60 if ( monitor_cache_get( mi,
61 &monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn, &e_conn ) ) {
63 LDAP_LOG( OPERATION, CRIT,
64 "monitor_subsys_conn_init: "
65 "unable to get entry '%s'\n",
66 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
68 Debug( LDAP_DEBUG_ANY,
69 "monitor_subsys_conn_init: "
70 "unable to get entry '%s'\n%s%s",
71 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
82 snprintf( buf, sizeof( buf ),
85 "structuralObjectClass: %s\n"
87 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
88 mi->oc_monitorCounterObject->soc_cname.bv_val,
89 mi->oc_monitorCounterObject->soc_cname.bv_val );
94 LDAP_LOG( OPERATION, CRIT,
95 "monitor_subsys_conn_init: "
96 "unable to create entry 'cn=Total,%s'\n",
97 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
99 Debug( LDAP_DEBUG_ANY,
100 "monitor_subsys_conn_init: "
101 "unable to create entry 'cn=Total,%s'\n%s%s",
102 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
110 attr_merge_one( e, mi->ad_monitorCounter, &bv, NULL );
112 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
113 e->e_private = ( void * )mp;
115 mp->mp_children = NULL;
116 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
117 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
118 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
119 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
121 if ( monitor_cache_add( mi, e ) ) {
123 LDAP_LOG( OPERATION, CRIT,
124 "monitor_subsys_conn_init: "
125 "unable to add entry 'cn=Total,%s'\n",
126 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
128 Debug( LDAP_DEBUG_ANY,
129 "monitor_subsys_conn_init: "
130 "unable to add entry 'cn=Total,%s'\n%s%s",
131 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
142 snprintf( buf, sizeof( buf ),
143 "dn: cn=Current,%s\n"
145 "structuralObjectClass: %s\n"
147 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
148 mi->oc_monitorCounterObject->soc_cname.bv_val,
149 mi->oc_monitorCounterObject->soc_cname.bv_val );
151 e = str2entry( buf );
154 LDAP_LOG( OPERATION, CRIT,
155 "monitor_subsys_conn_init: "
156 "unable to create entry 'cn=Current,%s'\n",
157 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
159 Debug( LDAP_DEBUG_ANY,
160 "monitor_subsys_conn_init: "
161 "unable to create entry 'cn=Current,%s'\n%s%s",
162 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
170 attr_merge_one( e, mi->ad_monitorCounter, &bv, NULL );
172 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
173 e->e_private = ( void * )mp;
175 mp->mp_children = NULL;
176 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
177 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
178 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
179 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
181 if ( monitor_cache_add( mi, e ) ) {
183 LDAP_LOG( OPERATION, CRIT,
184 "monitor_subsys_conn_init: "
185 "unable to add entry 'cn=Current,%s'\n",
186 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
188 Debug( LDAP_DEBUG_ANY,
189 "monitor_subsys_conn_init: "
190 "unable to add entry 'cn=Current,%s'\n%s%s",
191 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
199 mp = ( struct monitorentrypriv * )e_conn->e_private;
200 mp->mp_children = e_tmp;
202 monitor_cache_release( mi, e_conn );
208 monitor_subsys_conn_update(
213 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
219 if ( strncasecmp( e->e_ndn, "cn=total",
220 sizeof("cn=total")-1 ) == 0 ) {
221 n = connections_nextid();
223 } else if ( strncasecmp( e->e_ndn, "cn=current",
224 sizeof("cn=current")-1 ) == 0 ) {
228 for ( n = 0, c = connection_first( &connindex );
230 n++, c = connection_next( c, &connindex ) ) {
238 char buf[] = "+9223372036854775807L";
240 a = attr_find( e->e_attrs, mi->ad_monitorCounter );
245 snprintf( buf, sizeof( buf ), "%ld", n );
246 free( a->a_vals[ 0 ].bv_val );
247 ber_str2bv( buf, 0, 1, a->a_vals );
255 struct monitorinfo *mi,
260 struct monitorentrypriv *mp;
262 char buf[ BACKMONITOR_BUFSIZE ];
263 char buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
264 char buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
271 assert( ep != NULL );
273 snprintf( buf, sizeof( buf ),
274 "dn: cn=" CONN_CN_PREFIX " %ld,%s\n"
276 "structuralObjectClass: %s\n"
277 "cn: " CONN_CN_PREFIX " %ld\n",
278 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
279 mi->oc_monitorConnection->soc_cname.bv_val,
280 mi->oc_monitorConnection->soc_cname.bv_val,
282 e = str2entry( buf );
286 LDAP_LOG( OPERATION, CRIT,
287 "monitor_subsys_conn_create: "
288 "unable to create entry "
289 "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
290 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
292 Debug( LDAP_DEBUG_ANY,
293 "monitor_subsys_conn_create: "
294 "unable to create entry "
295 "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
297 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
302 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
304 ltm = gmtime( &c->c_starttime );
305 lutil_gentime( buf2, sizeof( buf2 ), ltm );
307 ltm = gmtime( &c->c_activitytime );
308 lutil_gentime( buf3, sizeof( buf3 ), ltm );
310 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
319 ": %s : %s : %s : %s",
321 (long) c->c_protocol,
322 c->c_n_ops_received, c->c_n_ops_executing,
323 c->c_n_ops_pending, c->c_n_ops_completed,
325 /* add low-level counters here */
326 c->c_n_get, c->c_n_read, c->c_n_write,
328 c->c_currentber ? "r" : "",
329 c->c_writewaiter ? "w" : "",
330 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
331 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
332 connection_state2str( c->c_conn_state ),
333 c->c_sasl_bind_in_progress ? "S" : "",
335 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
337 c->c_listener_url.bv_val,
338 c->c_peer_domain.bv_val,
339 c->c_peer_name.bv_val,
340 c->c_sock_name.bv_val,
347 bv.bv_len = strlen( buf );
348 attr_merge_one( e, mi->ad_monitoredInfo, &bv, NULL );
350 /* connection number */
351 snprintf( buf, sizeof( buf ), "%ld", c->c_connid );
353 bv.bv_len = strlen( buf );
354 attr_merge_one( e, mi->ad_monitorConnectionNumber, &bv, NULL );
357 attr_merge_one( e, mi->ad_monitorConnectionAuthzDN,
358 &c->c_dn, &c->c_ndn );
361 attr_merge_one( e, mi->ad_monitorConnectionLocalAddress,
362 &c->c_sock_name, NULL );
365 attr_merge_one( e, mi->ad_monitorConnectionPeerAddress,
366 &c->c_peer_name, NULL );
368 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
369 e->e_private = ( void * )mp;
370 mp->mp_info = &monitor_subsys[ SLAPD_MONITOR_CONN ];
371 mp->mp_children = NULL;
372 mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
380 monitor_subsys_conn_create(
387 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
390 struct monitorentrypriv *mp;
392 assert( mi != NULL );
393 assert( e_parent != NULL );
394 assert( ep != NULL );
399 Entry *e, *e_tmp = NULL;
401 /* create all the children of e_parent */
402 for ( c = connection_first( &connindex );
404 c = connection_next( c, &connindex )) {
405 if ( conn_create( mi, c, &e ) || e == NULL ) {
407 for ( ; e_tmp != NULL; ) {
408 mp = ( struct monitorentrypriv * )e_tmp->e_private;
412 e_tmp->e_private = NULL;
419 mp = ( struct monitorentrypriv * )e->e_private;
428 LDAPRDN values = NULL;
429 const char *text = NULL;
430 unsigned long connid;
432 /* create exactly the required entry */
434 if ( ldap_bv2rdn( ndn, &values, (char **)&text,
435 LDAP_DN_FORMAT_LDAP ) )
441 assert( values[ 0 ] );
443 connid = atol( values[ 0 ]->la_value.bv_val
444 + sizeof( CONN_CN_PREFIX ) );
446 ldap_rdnfree( values );
448 for ( c = connection_first( &connindex );
450 c = connection_next( c, &connindex )) {
451 if ( c->c_connid == connid ) {
452 if ( conn_create( mi, c, ep ) || *ep == NULL ) {
453 connection_done( c );