1 /* conn.c - deal with connection subsystem */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2011 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"
32 monitor_subsys_conn_update(
38 monitor_subsys_conn_create(
46 monitor_subsys_conn_init(
48 monitor_subsys_t *ms )
51 Entry *e, **ep, *e_conn;
53 char buf[ BACKMONITOR_BUFSIZE ];
58 ms->mss_update = monitor_subsys_conn_update;
59 ms->mss_create = monitor_subsys_conn_create;
61 mi = ( monitor_info_t * )be->be_private;
63 if ( monitor_cache_get( mi, &ms->mss_ndn, &e_conn ) ) {
64 Debug( LDAP_DEBUG_ANY,
65 "monitor_subsys_conn_init: "
66 "unable to get entry \"%s\"\n",
67 ms->mss_ndn.bv_val, 0, 0 );
71 mp = ( monitor_entry_t * )e_conn->e_private;
72 mp->mp_children = NULL;
73 ep = &mp->mp_children;
76 * Max file descriptors
78 BER_BVSTR( &bv, "cn=Max File Descriptors" );
79 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
80 mi->mi_oc_monitorCounterObject, mi, NULL, NULL );
83 Debug( LDAP_DEBUG_ANY,
84 "monitor_subsys_conn_init: "
85 "unable to create entry \"%s,%s\"\n",
86 bv.bv_val, ms->mss_ndn.bv_val, 0 );
92 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", dtblsize );
95 BER_BVSTR( &bv, "0" );
97 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
99 mp = monitor_entrypriv_create();
103 e->e_private = ( void * )mp;
105 mp->mp_flags = ms->mss_flags \
106 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
107 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
109 if ( monitor_cache_add( mi, e ) ) {
110 Debug( LDAP_DEBUG_ANY,
111 "monitor_subsys_conn_init: "
112 "unable to add entry \"cn=Total,%s\"\n",
113 ms->mss_ndn.bv_val, 0, 0 );
123 BER_BVSTR( &bv, "cn=Total" );
124 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
125 mi->mi_oc_monitorCounterObject, mi, NULL, NULL );
128 Debug( LDAP_DEBUG_ANY,
129 "monitor_subsys_conn_init: "
130 "unable to create entry \"cn=Total,%s\"\n",
131 ms->mss_ndn.bv_val, 0, 0 );
135 BER_BVSTR( &bv, "-1" );
136 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
138 mp = monitor_entrypriv_create();
142 e->e_private = ( void * )mp;
144 mp->mp_flags = ms->mss_flags \
145 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
146 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
148 if ( monitor_cache_add( mi, e ) ) {
149 Debug( LDAP_DEBUG_ANY,
150 "monitor_subsys_conn_init: "
151 "unable to add entry \"cn=Total,%s\"\n",
152 ms->mss_ndn.bv_val, 0, 0 );
162 BER_BVSTR( &bv, "cn=Current" );
163 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
164 mi->mi_oc_monitorCounterObject, mi, NULL, NULL );
167 Debug( LDAP_DEBUG_ANY,
168 "monitor_subsys_conn_init: "
169 "unable to create entry \"cn=Current,%s\"\n",
170 ms->mss_ndn.bv_val, 0, 0 );
174 BER_BVSTR( &bv, "0" );
175 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
177 mp = monitor_entrypriv_create();
181 e->e_private = ( void * )mp;
183 mp->mp_flags = ms->mss_flags \
184 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
185 mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
187 if ( monitor_cache_add( mi, e ) ) {
188 Debug( LDAP_DEBUG_ANY,
189 "monitor_subsys_conn_init: "
190 "unable to add entry \"cn=Current,%s\"\n",
191 ms->mss_ndn.bv_val, 0, 0 );
198 monitor_cache_release( mi, e_conn );
204 monitor_subsys_conn_update(
209 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
212 static struct berval total_bv = BER_BVC( "cn=total" ),
213 current_bv = BER_BVC( "cn=current" );
216 assert( mi != NULL );
219 dnRdn( &e->e_nname, &rdn );
221 if ( dn_match( &rdn, &total_bv ) ) {
222 n = connections_nextid();
224 } else if ( dn_match( &rdn, ¤t_bv ) ) {
228 for ( n = 0, c = connection_first( &connindex );
230 n++, c = connection_next( c, &connindex ) )
234 connection_done( c );
239 char buf[LDAP_PVT_INTTYPE_CHARS(long)];
242 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
247 snprintf( buf, sizeof( buf ), "%ld", n );
249 if ( len > a->a_vals[ 0 ].bv_len ) {
250 a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
252 a->a_vals[ 0 ].bv_len = len;
253 AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
255 /* FIXME: touch modifyTimestamp? */
258 return SLAP_CB_CONTINUE;
266 monitor_subsys_t *ms )
270 char buf[ BACKMONITOR_BUFSIZE ];
271 char buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
272 char buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
274 struct berval bv, ctmbv, mtmbv;
275 struct berval bv_unknown= BER_BVC("unknown");
280 assert( ep != NULL );
282 ldap_pvt_gmtime( &c->c_starttime, &tm );
284 ctmbv.bv_len = lutil_gentime( buf2, sizeof( buf2 ), &tm );
287 ldap_pvt_gmtime( &c->c_activitytime, &tm );
288 mtmbv.bv_len = lutil_gentime( buf3, sizeof( buf3 ), &tm );
291 bv.bv_len = snprintf( buf, sizeof( buf ),
292 "cn=Connection %ld", c->c_connid );
294 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
295 mi->mi_oc_monitorConnection, mi, &ctmbv, &mtmbv );
298 Debug( LDAP_DEBUG_ANY,
299 "monitor_subsys_conn_create: "
300 "unable to create entry "
301 "\"cn=Connection %ld,%s\"\n",
303 ms->mss_dn.bv_val, 0 );
307 #ifdef MONITOR_LEGACY_CONN
308 /* NOTE: this will disappear, as the exploded data
309 * has been moved to dedicated attributes */
310 bv.bv_len = snprintf( buf, sizeof( buf ),
324 (long) c->c_protocol,
325 c->c_n_ops_received, c->c_n_ops_executing,
326 c->c_n_ops_pending, c->c_n_ops_completed,
328 /* add low-level counters here */
329 c->c_n_get, c->c_n_read, c->c_n_write,
331 c->c_currentber ? "r" : "",
332 c->c_writewaiter ? "w" : "",
333 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
334 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
335 connection_state2str( c->c_conn_state ),
336 c->c_sasl_bind_in_progress ? "S" : "",
338 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
340 c->c_listener_url.bv_val,
341 BER_BVISNULL( &c->c_peer_domain )
342 ? "" : c->c_peer_domain.bv_val,
343 BER_BVISNULL( &c->c_peer_name )
344 ? "" : c->c_peer_name.bv_val,
345 c->c_sock_name.bv_val,
349 attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
350 #endif /* MONITOR_LEGACY_CONN */
352 bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", c->c_connid );
353 attr_merge_one( e, mi->mi_ad_monitorConnectionNumber, &bv, NULL );
355 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", (long) c->c_protocol );
356 attr_merge_normalize_one( e, mi->mi_ad_monitorConnectionProtocol, &bv, NULL );
358 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_received );
359 attr_merge_one( e, mi->mi_ad_monitorConnectionOpsReceived, &bv, NULL );
361 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_executing );
362 attr_merge_one( e, mi->mi_ad_monitorConnectionOpsExecuting, &bv, NULL );
364 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_pending );
365 attr_merge_one( e, mi->mi_ad_monitorConnectionOpsPending, &bv, NULL );
367 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_completed );
368 attr_merge_one( e, mi->mi_ad_monitorConnectionOpsCompleted, &bv, NULL );
370 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_get );
371 attr_merge_one( e, mi->mi_ad_monitorConnectionGet, &bv, NULL );
373 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_read );
374 attr_merge_one( e, mi->mi_ad_monitorConnectionRead, &bv, NULL );
376 bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_write );
377 attr_merge_one( e, mi->mi_ad_monitorConnectionWrite, &bv, NULL );
379 bv.bv_len = snprintf( buf, sizeof( buf ), "%s%s%s%s%s%s",
380 c->c_currentber ? "r" : "",
381 c->c_writewaiter ? "w" : "",
382 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
383 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
384 connection_state2str( c->c_conn_state ),
385 c->c_sasl_bind_in_progress ? "S" : "" );
386 attr_merge_normalize_one( e, mi->mi_ad_monitorConnectionMask, &bv, NULL );
388 attr_merge_one( e, mi->mi_ad_monitorConnectionAuthzDN,
389 &c->c_dn, &c->c_ndn );
391 /* NOTE: client connections leave the c_peer_* fields NULL */
392 assert( !BER_BVISNULL( &c->c_listener_url ) );
393 attr_merge_normalize_one( e, mi->mi_ad_monitorConnectionListener,
394 &c->c_listener_url, NULL );
396 attr_merge_normalize_one( e, mi->mi_ad_monitorConnectionPeerDomain,
397 BER_BVISNULL( &c->c_peer_domain ) ? &bv_unknown : &c->c_peer_domain,
400 attr_merge_normalize_one( e, mi->mi_ad_monitorConnectionPeerAddress,
401 BER_BVISNULL( &c->c_peer_name ) ? &bv_unknown : &c->c_peer_name,
404 assert( !BER_BVISNULL( &c->c_sock_name ) );
405 attr_merge_normalize_one( e, mi->mi_ad_monitorConnectionLocalAddress,
406 &c->c_sock_name, NULL );
408 attr_merge_normalize_one( e, mi->mi_ad_monitorConnectionStartTime, &ctmbv, NULL );
410 attr_merge_normalize_one( e, mi->mi_ad_monitorConnectionActivityTime, &mtmbv, NULL );
412 mp = monitor_entrypriv_create();
416 e->e_private = ( void * )mp;
418 mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
422 return SLAP_CB_CONTINUE;
426 monitor_subsys_conn_create(
433 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
435 int rc = SLAP_CB_CONTINUE;
436 monitor_subsys_t *ms;
438 assert( mi != NULL );
439 assert( e_parent != NULL );
440 assert( ep != NULL );
442 ms = (( monitor_entry_t *)e_parent->e_private)->mp_info;
452 /* create all the children of e_parent */
453 for ( c = connection_first( &connindex );
455 c = connection_next( c, &connindex ) )
459 if ( conn_create( mi, c, &e, ms ) != SLAP_CB_CONTINUE
462 for ( ; e_tmp != NULL; ) {
463 mp = ( monitor_entry_t * )e_tmp->e_private;
467 e_tmp->e_private = NULL;
472 rc = rs->sr_err = LDAP_OTHER;
475 mp = ( monitor_entry_t * )e->e_private;
479 connection_done( c );
485 unsigned long connid;
487 static struct berval nconn_bv = BER_BVC( "cn=connection " );
489 rc = LDAP_NO_SUCH_OBJECT;
491 /* create exactly the required entry;
492 * the normalized DN must start with "cn=connection ",
493 * followed by the connection id, followed by
494 * the RDN separator "," */
495 if ( ndn->bv_len <= nconn_bv.bv_len
496 || strncmp( ndn->bv_val, nconn_bv.bv_val, nconn_bv.bv_len ) != 0 )
501 connid = strtol( &ndn->bv_val[ nconn_bv.bv_len ], &next, 10 );
502 if ( next[ 0 ] != ',' ) {
503 return ( rs->sr_err = LDAP_OTHER );
506 for ( c = connection_first( &connindex );
508 c = connection_next( c, &connindex ) )
510 if ( c->c_connid == connid ) {
511 rc = conn_create( mi, c, ep, ms );
512 if ( rc != SLAP_CB_CONTINUE ) {
515 } else if ( *ep == NULL ) {
516 rc = rs->sr_err = LDAP_OTHER;
523 connection_done( c );