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;
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 ),
84 SLAPD_MONITOR_OBJECTCLASSES
86 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val );
91 LDAP_LOG( OPERATION, CRIT,
92 "monitor_subsys_conn_init: "
93 "unable to create entry 'cn=Total,%s'\n",
94 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
96 Debug( LDAP_DEBUG_ANY,
97 "monitor_subsys_conn_init: "
98 "unable to create entry 'cn=Total,%s'\n%s%s",
99 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
107 attr_merge_one( e, mi->monitor_ad_description, &bv, NULL );
109 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
110 e->e_private = ( void * )mp;
112 mp->mp_children = NULL;
113 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
114 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
115 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
116 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
118 if ( monitor_cache_add( mi, e ) ) {
120 LDAP_LOG( OPERATION, CRIT,
121 "monitor_subsys_conn_init: "
122 "unable to add entry 'cn=Total,%s'\n",
123 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
125 Debug( LDAP_DEBUG_ANY,
126 "monitor_subsys_conn_init: "
127 "unable to add entry 'cn=Total,%s'\n%s%s",
128 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
139 snprintf( buf, sizeof( buf ),
140 "dn: cn=Current,%s\n"
141 SLAPD_MONITOR_OBJECTCLASSES
143 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val );
145 e = str2entry( buf );
148 LDAP_LOG( OPERATION, CRIT,
149 "monitor_subsys_conn_init: "
150 "unable to create entry 'cn=Current,%s'\n",
151 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
153 Debug( LDAP_DEBUG_ANY,
154 "monitor_subsys_conn_init: "
155 "unable to create entry 'cn=Current,%s'\n%s%s",
156 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
164 attr_merge_one( e, mi->monitor_ad_description, &bv, NULL );
166 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
167 e->e_private = ( void * )mp;
169 mp->mp_children = NULL;
170 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
171 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
172 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
173 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
175 if ( monitor_cache_add( mi, e ) ) {
177 LDAP_LOG( OPERATION, CRIT,
178 "monitor_subsys_conn_init: "
179 "unable to add entry 'cn=Current,%s'\n",
180 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
182 Debug( LDAP_DEBUG_ANY,
183 "monitor_subsys_conn_init: "
184 "unable to add entry 'cn=Current,%s'\n%s%s",
185 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
193 mp = ( struct monitorentrypriv * )e_conn->e_private;
194 mp->mp_children = e_tmp;
196 monitor_cache_release( mi, e_conn );
202 monitor_subsys_conn_update(
207 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
213 if ( strncasecmp( e->e_ndn, "cn=total",
214 sizeof("cn=total")-1 ) == 0 ) {
215 n = connections_nextid();
217 } else if ( strncasecmp( e->e_ndn, "cn=current",
218 sizeof("cn=current")-1 ) == 0 ) {
222 for ( n = 0, c = connection_first( &connindex );
224 n++, c = connection_next( c, &connindex ) ) {
234 a = attr_find( e->e_attrs, mi->monitor_ad_description );
239 snprintf( buf, sizeof( buf ), "%ld", n );
240 free( a->a_vals[ 0 ].bv_val );
241 ber_str2bv( buf, 0, 1, a->a_vals );
249 struct monitorinfo *mi,
254 struct monitorentrypriv *mp;
257 char buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
258 char buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
265 assert( ep != NULL );
267 snprintf( buf, sizeof( buf ),
268 "dn: cn=" CONN_CN_PREFIX " %ld,%s\n"
269 SLAPD_MONITOR_OBJECTCLASSES
270 "cn: " CONN_CN_PREFIX " %ld\n",
271 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
273 e = str2entry( buf );
277 LDAP_LOG( OPERATION, CRIT,
278 "monitor_subsys_conn_create: "
279 "unable to create entry "
280 "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
281 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
283 Debug( LDAP_DEBUG_ANY,
284 "monitor_subsys_conn_create: "
285 "unable to create entry "
286 "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
288 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
293 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
295 ltm = gmtime( &c->c_starttime );
296 lutil_gentime( buf2, sizeof( buf2 ), ltm );
298 ltm = gmtime( &c->c_activitytime );
299 lutil_gentime( buf3, sizeof( buf3 ), ltm );
301 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
309 ": %s : %s : %s : %s",
311 (long) c->c_protocol,
312 c->c_n_ops_received, c->c_n_ops_executing,
313 c->c_n_ops_pending, c->c_n_ops_completed,
315 /* add low-level counters here */
316 c->c_n_get, c->c_n_read, c->c_n_write,
318 c->c_currentber ? "r" : "",
319 c->c_writewaiter ? "w" : "",
320 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
321 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
322 connection_state2str( c->c_conn_state ),
323 c->c_sasl_bind_in_progress ? "S" : "",
325 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
327 c->c_listener_url.bv_val,
328 c->c_peer_domain.bv_val,
329 c->c_peer_name.bv_val,
330 c->c_sock_name.bv_val,
337 bv.bv_len = strlen( buf );
338 attr_merge_one( e, mi->monitor_ad_description, &bv, NULL );
340 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
341 e->e_private = ( void * )mp;
342 mp->mp_info = &monitor_subsys[ SLAPD_MONITOR_CONN ];
343 mp->mp_children = NULL;
344 mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
352 monitor_subsys_conn_create(
359 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
362 struct monitorentrypriv *mp;
364 assert( mi != NULL );
365 assert( e_parent != NULL );
366 assert( ep != NULL );
371 Entry *e, *e_tmp = NULL;
373 /* create all the children of e_parent */
374 for ( c = connection_first( &connindex );
376 c = connection_next( c, &connindex )) {
377 if ( conn_create( mi, c, &e ) || e == NULL ) {
379 for ( ; e_tmp != NULL; ) {
380 mp = ( struct monitorentrypriv * )e_tmp->e_private;
384 e_tmp->e_private = NULL;
391 mp = ( struct monitorentrypriv * )e->e_private;
400 LDAPRDN values = NULL;
401 const char *text = NULL;
402 unsigned long connid;
404 /* create exactly the required entry */
406 if ( ldap_bv2rdn( ndn, &values, (char **)&text,
407 LDAP_DN_FORMAT_LDAP ) )
413 assert( values[ 0 ] );
415 connid = atol( values[ 0 ]->la_value.bv_val
416 + sizeof( CONN_CN_PREFIX ) );
418 ldap_rdnfree( values );
420 for ( c = connection_first( &connindex );
422 c = connection_next( c, &connindex )) {
423 if ( c->c_connid == connid ) {
424 if ( conn_create( mi, c, ep ) || *ep == NULL ) {