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.
49 #ifndef _BACK_MONITOR_H_
50 #define _BACK_MONITOR_H_
53 #include <ldap_pvt_thread.h>
60 * The cache maps DNs to Entries.
62 * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
64 * This work has beed deveolped for the OpenLDAP Foundation
65 * in the hope that it may be useful to the Open Source community,
66 * but WITHOUT ANY WARRANTY.
68 * Permission is granted to anyone to use this software for any purpose
69 * on any computer system, and to alter it and redistribute it, subject
70 * to the following restrictions:
72 * 1. The author and SysNet s.n.c. are not responsible for the consequences
73 * of use of this software, no matter how awful, even if they arise from
76 * 2. The origin of this software must not be misrepresented, either by
77 * explicit claim or by omission. Since few users ever read sources,
78 * credits should appear in the documentation.
80 * 3. Altered versions must be plainly marked as such, and must not be
81 * misrepresented as being the original software. Since few users
82 * ever read sources, credits should appear in the documentation.
83 * SysNet s.n.c. cannot be responsible for the consequences of the
86 * 4. This notice may not be removed or altered.
92 #include <ac/string.h>
96 #include "back-monitor.h"
98 #define CONN_CN_PREFIX "Connection"
101 monitor_subsys_conn_init(
105 struct monitorinfo *mi;
107 Entry *e, *e_tmp, *e_conn;
108 struct monitorentrypriv *mp;
109 char buf[ BACKMONITOR_BUFSIZE ];
112 assert( be != NULL );
114 mi = ( struct monitorinfo * )be->be_private;
116 if ( monitor_cache_get( mi,
117 &monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn, &e_conn ) ) {
119 LDAP_LOG( OPERATION, CRIT,
120 "monitor_subsys_conn_init: "
121 "unable to get entry '%s'\n",
122 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
124 Debug( LDAP_DEBUG_ANY,
125 "monitor_subsys_conn_init: "
126 "unable to get entry '%s'\n%s%s",
127 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
138 snprintf( buf, sizeof( buf ),
141 "structuralObjectClass: %s\n"
143 "createTimestamp: %s\n"
144 "modifyTimestamp: %s\n",
145 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
146 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
147 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
148 mi->mi_startTime.bv_val,
149 mi->mi_startTime.bv_val );
151 e = str2entry( buf );
154 LDAP_LOG( OPERATION, CRIT,
155 "monitor_subsys_conn_init: "
156 "unable to create entry 'cn=Total,%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=Total,%s'\n%s%s",
162 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
170 attr_merge_one( e, mi->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=Total,%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=Total,%s'\n%s%s",
191 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
202 snprintf( buf, sizeof( buf ),
203 "dn: cn=Current,%s\n"
205 "structuralObjectClass: %s\n"
207 "createTimestamp: %s\n"
208 "modifyTimestamp: %s\n",
209 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
210 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
211 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
212 mi->mi_startTime.bv_val,
213 mi->mi_startTime.bv_val );
215 e = str2entry( buf );
218 LDAP_LOG( OPERATION, CRIT,
219 "monitor_subsys_conn_init: "
220 "unable to create entry 'cn=Current,%s'\n",
221 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
223 Debug( LDAP_DEBUG_ANY,
224 "monitor_subsys_conn_init: "
225 "unable to create entry 'cn=Current,%s'\n%s%s",
226 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
234 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
236 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
237 e->e_private = ( void * )mp;
239 mp->mp_children = NULL;
240 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
241 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
242 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
243 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
245 if ( monitor_cache_add( mi, e ) ) {
247 LDAP_LOG( OPERATION, CRIT,
248 "monitor_subsys_conn_init: "
249 "unable to add entry 'cn=Current,%s'\n",
250 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
252 Debug( LDAP_DEBUG_ANY,
253 "monitor_subsys_conn_init: "
254 "unable to add entry 'cn=Current,%s'\n%s%s",
255 monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
263 mp = ( struct monitorentrypriv * )e_conn->e_private;
264 mp->mp_children = e_tmp;
266 monitor_cache_release( mi, e_conn );
272 monitor_subsys_conn_update(
277 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
283 if ( strncasecmp( e->e_ndn, "cn=total",
284 sizeof("cn=total")-1 ) == 0 ) {
285 n = connections_nextid();
287 } else if ( strncasecmp( e->e_ndn, "cn=current",
288 sizeof("cn=current")-1 ) == 0 ) {
292 for ( n = 0, c = connection_first( &connindex );
294 n++, c = connection_next( c, &connindex ) ) {
302 char buf[] = "+9223372036854775807L";
304 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
309 snprintf( buf, sizeof( buf ), "%ld", n );
310 free( a->a_vals[ 0 ].bv_val );
311 ber_str2bv( buf, 0, 1, a->a_vals );
319 struct monitorinfo *mi,
324 struct monitorentrypriv *mp;
326 char buf[ BACKMONITOR_BUFSIZE ];
327 char buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
328 char buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
335 char ctmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
337 char mtmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
340 assert( ep != NULL );
342 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
343 #ifdef HACK_LOCAL_TIME
344 ctm = localtime( &c->c_starttime );
345 lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
346 mtm = localtime( &c->c_activitytime );
347 lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
348 #else /* !HACK_LOCAL_TIME */
349 ctm = gmtime( &c->c_starttime );
350 lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
351 mtm = gmtime( &c->c_activitytime );
352 lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
353 #endif /* !HACK_LOCAL_TIME */
354 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
356 snprintf( buf, sizeof( buf ),
357 "dn: cn=" CONN_CN_PREFIX " %ld,%s\n"
359 "structuralObjectClass: %s\n"
360 "cn: " CONN_CN_PREFIX " %ld\n"
361 "createTimestamp: %s\n"
362 "modifyTimestamp: %s\n",
363 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
364 mi->mi_oc_monitorConnection->soc_cname.bv_val,
365 mi->mi_oc_monitorConnection->soc_cname.bv_val,
369 e = str2entry( buf );
373 LDAP_LOG( OPERATION, CRIT,
374 "monitor_subsys_conn_create: "
375 "unable to create entry "
376 "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
377 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
379 Debug( LDAP_DEBUG_ANY,
380 "monitor_subsys_conn_create: "
381 "unable to create entry "
382 "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
384 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
389 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
391 ltm = gmtime( &c->c_starttime );
392 lutil_gentime( buf2, sizeof( buf2 ), ltm );
394 ltm = gmtime( &c->c_activitytime );
395 lutil_gentime( buf3, sizeof( buf3 ), ltm );
397 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
406 ": %s : %s : %s : %s",
408 (long) c->c_protocol,
409 c->c_n_ops_received, c->c_n_ops_executing,
410 c->c_n_ops_pending, c->c_n_ops_completed,
412 /* add low-level counters here */
413 c->c_n_get, c->c_n_read, c->c_n_write,
415 c->c_currentber ? "r" : "",
416 c->c_writewaiter ? "w" : "",
417 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
418 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
419 connection_state2str( c->c_conn_state ),
420 c->c_sasl_bind_in_progress ? "S" : "",
422 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
424 c->c_listener_url.bv_val,
425 c->c_peer_domain.bv_val,
426 c->c_peer_name.bv_val,
427 c->c_sock_name.bv_val,
434 bv.bv_len = strlen( buf );
435 attr_merge_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
437 /* connection number */
438 snprintf( buf, sizeof( buf ), "%ld", c->c_connid );
440 bv.bv_len = strlen( buf );
441 attr_merge_one( e, mi->mi_ad_monitorConnectionNumber, &bv, NULL );
444 attr_merge_one( e, mi->mi_ad_monitorConnectionAuthzDN,
445 &c->c_dn, &c->c_ndn );
448 attr_merge_one( e, mi->mi_ad_monitorConnectionLocalAddress,
449 &c->c_sock_name, NULL );
452 attr_merge_one( e, mi->mi_ad_monitorConnectionPeerAddress,
453 &c->c_peer_name, NULL );
455 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
456 e->e_private = ( void * )mp;
457 mp->mp_info = &monitor_subsys[ SLAPD_MONITOR_CONN ];
458 mp->mp_children = NULL;
459 mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
467 monitor_subsys_conn_create(
474 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
477 struct monitorentrypriv *mp;
479 assert( mi != NULL );
480 assert( e_parent != NULL );
481 assert( ep != NULL );
486 Entry *e, *e_tmp = NULL;
488 /* create all the children of e_parent */
489 for ( c = connection_first( &connindex );
491 c = connection_next( c, &connindex )) {
492 if ( conn_create( mi, c, &e ) || e == NULL ) {
494 for ( ; e_tmp != NULL; ) {
495 mp = ( struct monitorentrypriv * )e_tmp->e_private;
499 e_tmp->e_private = NULL;
506 mp = ( struct monitorentrypriv * )e->e_private;
515 LDAPRDN values = NULL;
516 const char *text = NULL;
517 unsigned long connid;
519 /* create exactly the required entry */
521 if ( ldap_bv2rdn( ndn, &values, (char **)&text,
522 LDAP_DN_FORMAT_LDAP ) )
528 assert( values[ 0 ] );
530 connid = atol( values[ 0 ]->la_value.bv_val
531 + sizeof( CONN_CN_PREFIX ) );
533 ldap_rdnfree( values );
535 for ( c = connection_first( &connindex );
537 c = connection_next( c, &connindex )) {
538 if ( c->c_connid == connid ) {
539 if ( conn_create( mi, c, ep ) || *ep == NULL ) {
540 connection_done( c );