]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/conn.c
cleanup
[openldap] / servers / slapd / back-monitor / conn.c
1 /* conn.c - deal with connection subsystem */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2001-2006 The OpenLDAP Foundation.
6  * Portions Copyright 2001-2003 Pierangelo Masarati.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
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>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Pierangelo Masarati for inclusion
19  * in OpenLDAP Software.
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25 #include <ac/string.h>
26
27 #include "slap.h"
28 #include "lutil.h"
29 #include "back-monitor.h"
30
31 #ifndef LDAP_DEVEL
32 #define MONITOR_LEGACY_CONN
33 #endif
34
35 static int
36 monitor_subsys_conn_update(
37         Operation               *op,
38         SlapReply               *rs,
39         Entry                   *e );
40
41 static int 
42 monitor_subsys_conn_create( 
43         Operation               *op,
44         SlapReply               *rs,
45         struct berval           *ndn,
46         Entry                   *e_parent,
47         Entry                   **ep );
48
49 int
50 monitor_subsys_conn_init(
51         BackendDB               *be,
52         monitor_subsys_t        *ms )
53 {
54         monitor_info_t  *mi;
55         Entry           *e, **ep, *e_conn;
56         monitor_entry_t *mp;
57         char            buf[ BACKMONITOR_BUFSIZE ];
58         struct berval   bv;
59
60         assert( be != NULL );
61
62         ms->mss_update = monitor_subsys_conn_update;
63         ms->mss_create = monitor_subsys_conn_create;
64
65         mi = ( monitor_info_t * )be->be_private;
66
67         if ( monitor_cache_get( mi, &ms->mss_ndn, &e_conn ) ) {
68                 Debug( LDAP_DEBUG_ANY,
69                         "monitor_subsys_conn_init: "
70                         "unable to get entry \"%s\"\n",
71                         ms->mss_ndn.bv_val, 0, 0 );
72                 return( -1 );
73         }
74
75         mp = ( monitor_entry_t * )e_conn->e_private;
76         mp->mp_children = NULL;
77         ep = &mp->mp_children;
78
79         /*
80          * Total conns
81          */
82         BER_BVSTR( &bv, "cn=Total" );
83         e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
84                 mi->mi_oc_monitorCounterObject, mi, NULL, NULL );
85         
86         if ( e == NULL ) {
87                 Debug( LDAP_DEBUG_ANY,
88                         "monitor_subsys_conn_init: "
89                         "unable to create entry \"cn=Total,%s\"\n",
90                         ms->mss_ndn.bv_val, 0, 0 );
91                 return( -1 );
92         }
93         
94         BER_BVSTR( &bv, "0" );
95         attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, &bv );
96         
97         mp = monitor_entrypriv_create();
98         if ( mp == NULL ) {
99                 return -1;
100         }
101         e->e_private = ( void * )mp;
102         mp->mp_info = ms;
103         mp->mp_flags = ms->mss_flags \
104                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
105         mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
106
107         if ( monitor_cache_add( mi, e ) ) {
108                 Debug( LDAP_DEBUG_ANY,
109                         "monitor_subsys_conn_init: "
110                         "unable to add entry \"cn=Total,%s\"\n",
111                         ms->mss_ndn.bv_val, 0, 0 );
112                 return( -1 );
113         }
114
115         *ep = e;
116         ep = &mp->mp_next;
117         
118         /*
119          * Current conns
120          */
121         BER_BVSTR( &bv, "cn=Current" );
122         e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
123                 mi->mi_oc_monitorCounterObject, mi, NULL, NULL );
124
125         if ( e == NULL ) {
126                 Debug( LDAP_DEBUG_ANY,
127                         "monitor_subsys_conn_init: "
128                         "unable to create entry \"cn=Current,%s\"\n",
129                         ms->mss_ndn.bv_val, 0, 0 );
130                 return( -1 );
131         }
132         
133         BER_BVSTR( &bv, "0" );
134         attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, &bv );
135         
136         mp = monitor_entrypriv_create();
137         if ( mp == NULL ) {
138                 return -1;
139         }
140         e->e_private = ( void * )mp;
141         mp->mp_info = ms;
142         mp->mp_flags = ms->mss_flags \
143                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
144         mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
145
146         if ( monitor_cache_add( mi, e ) ) {
147                 Debug( LDAP_DEBUG_ANY,
148                         "monitor_subsys_conn_init: "
149                         "unable to add entry \"cn=Current,%s\"\n",
150                         ms->mss_ndn.bv_val, 0, 0 );
151                 return( -1 );
152         }
153         
154         *ep = e;
155         ep = &mp->mp_next;
156
157         monitor_cache_release( mi, e_conn );
158
159         return( 0 );
160 }
161
162 static int
163 monitor_subsys_conn_update(
164         Operation               *op,
165         SlapReply               *rs,
166         Entry                   *e )
167 {
168         monitor_info_t  *mi = ( monitor_info_t * )op->o_bd->be_private;
169
170         long                    n = -1;
171         static struct berval    total_bv = BER_BVC( "cn=total" ),
172                                 current_bv = BER_BVC( "cn=current" );
173         struct berval           rdn;
174
175         assert( mi != NULL );
176         assert( e != NULL );
177
178         dnRdn( &e->e_nname, &rdn );
179         
180         if ( dn_match( &rdn, &total_bv ) ) {
181                 n = connections_nextid();
182
183         } else if ( dn_match( &rdn, &current_bv ) ) {
184                 Connection      *c;
185                 int             connindex;
186
187                 for ( n = 0, c = connection_first( &connindex );
188                                 c != NULL;
189                                 n++, c = connection_next( c, &connindex ) ) {
190                         /* No Op */ ;
191                 }
192                 connection_done( c );
193         }
194
195         if ( n != -1 ) {
196                 Attribute       *a;
197                 char            buf[] = "+9223372036854775807L";
198                 ber_len_t       len;
199
200                 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
201                 if ( a == NULL ) {
202                         return( -1 );
203                 }
204
205                 snprintf( buf, sizeof( buf ), "%ld", n );
206                 len = strlen( buf );
207                 if ( len > a->a_vals[ 0 ].bv_len ) {
208                         a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
209                 }
210                 a->a_vals[ 0 ].bv_len = len;
211                 AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
212
213                 /* FIXME: touch modifyTimestamp? */
214         }
215
216         return SLAP_CB_CONTINUE;
217 }
218
219 static int
220 conn_create(
221         monitor_info_t          *mi,
222         Connection              *c,
223         Entry                   **ep,
224         monitor_subsys_t        *ms )
225 {
226         monitor_entry_t *mp;
227         struct tm       *tm;
228         char            buf[ BACKMONITOR_BUFSIZE ];
229         char            buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
230         char            buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
231
232         struct berval bv, ctmbv, mtmbv, bv2, bv3;
233         struct berval bv_unknown= BER_BVC("unknown");
234
235         Entry           *e;
236
237 #ifdef HACK_LOCAL_TIME
238         char            ctmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
239         char            mtmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
240 #endif
241 #ifdef HAVE_GMTIME_R
242         struct tm       tm_buf;
243 #endif /* HAVE_GMTIME_R */
244
245         assert( c != NULL );
246         assert( ep != NULL );
247
248 #ifndef HAVE_GMTIME_R
249         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
250 #endif
251
252 #ifdef HAVE_GMTIME_R
253         tm = gmtime_r( &c->c_starttime, &tm_buf );
254 #else
255         tm = gmtime( &c->c_starttime );
256 #endif
257         bv2.bv_len = lutil_gentime( buf2, sizeof( buf2 ), tm );
258         bv2.bv_val = buf2;
259 #ifdef HACK_LOCAL_TIME
260 # ifdef HAVE_LOCALTIME_R
261         tm = localtime_r( &c->c_starttime, &tm_buf );
262 # else
263         tm = localtime( &c->c_starttime );
264 # endif
265         ctmbv.bv_len = lutil_localtime( ctmbuf, sizeof( ctmbuf ), tm, -timezone );
266         ctmbv.bv_val = ctmbuf;
267 #else /* !HACK_LOCAL_TIME */
268         ctmbv = bv2;
269 #endif
270
271 #ifdef HAVE_GMTIME_R
272         tm = gmtime_r( &c->c_activitytime, &tm_buf );
273 #else
274         tm = gmtime( &c->c_activitytime );
275 #endif
276         bv3.bv_len = lutil_gentime( buf3, sizeof( buf3 ), tm );
277         bv3.bv_val = buf3;
278 #ifdef HACK_LOCAL_TIME
279 # ifdef HAVE_LOCALTIME_R
280         tm = localtime_r( &c->c_activitytime, &tm_buf );
281 # else
282         tm = localtime( &c->c_activitytime );
283 # endif /* HAVE_LOCALTIME_R */
284         mtmbv.bv_len = lutil_localtime( mtmbuf, sizeof( mtmbuf ), tm, -timezone );
285         mtmbv.bv_val = mtmbuf;
286 #else /* !HACK_LOCAL_TIME */
287         mtmbv = bv3;
288 #endif
289
290 #ifndef HAVE_GMTIME_R
291         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
292 #endif
293
294         bv.bv_len = snprintf( buf, sizeof( buf ),
295                 "cn=Connection %ld", c->c_connid );
296         bv.bv_val = buf;
297         e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv, 
298                 mi->mi_oc_monitorConnection, mi, &ctmbv, &mtmbv );
299
300         if ( e == NULL) {
301                 Debug( LDAP_DEBUG_ANY,
302                         "monitor_subsys_conn_create: "
303                         "unable to create entry "
304                         "\"cn=Connection %ld,%s\"\n",
305                         c->c_connid, 
306                         ms->mss_dn.bv_val, 0 );
307                 return( -1 );
308         }
309
310 #ifdef MONITOR_LEGACY_CONN
311         /* NOTE: this will disappear, as the exploded data
312          * has been moved to dedicated attributes */
313         bv.bv_len = snprintf( buf, sizeof( buf ),
314                         "%ld "
315                         ": %ld "
316                         ": %ld/%ld/%ld/%ld "
317                         ": %ld/%ld/%ld "
318                         ": %s%s%s%s%s%s "
319                         ": %s "
320                         ": %s "
321                         ": %s "
322                         ": %s "
323                         ": %s "
324                         ": %s "
325                         ": %s",
326                         c->c_connid,
327                         (long) c->c_protocol,
328                         c->c_n_ops_received, c->c_n_ops_executing,
329                                 c->c_n_ops_pending, c->c_n_ops_completed,
330                         
331                         /* add low-level counters here */
332                         c->c_n_get, c->c_n_read, c->c_n_write,
333                         
334                         c->c_currentber ? "r" : "",
335                         c->c_writewaiter ? "w" : "",
336                         LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
337                         LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
338                         connection_state2str( c->c_conn_state ),
339                         c->c_sasl_bind_in_progress ? "S" : "",
340                         
341                         c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
342                         
343                         c->c_listener_url.bv_val,
344                         c->c_peer_domain.bv_val,
345                         c->c_peer_name.bv_val,
346                         c->c_sock_name.bv_val,
347                         
348                         buf2,
349                         buf3 );
350         attr_merge_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
351 #endif /* MONITOR_LEGACY_CONN */
352
353         bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", c->c_connid );
354         attr_merge_one( e, mi->mi_ad_monitorConnectionNumber, &bv, NULL );
355
356         bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_protocol );
357         attr_merge_one( e, mi->mi_ad_monitorConnectionProtocol, &bv, NULL );
358
359         bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_received );
360         attr_merge_one( e, mi->mi_ad_monitorConnectionOpsReceived, &bv, NULL );
361
362         bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_executing );
363         attr_merge_one( e, mi->mi_ad_monitorConnectionOpsExecuting, &bv, NULL );
364
365         bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_pending );
366         attr_merge_one( e, mi->mi_ad_monitorConnectionOpsPending, &bv, NULL );
367
368         bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_completed );
369         attr_merge_one( e, mi->mi_ad_monitorConnectionOpsCompleted, &bv, NULL );
370
371         bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_get );
372         attr_merge_one( e, mi->mi_ad_monitorConnectionGet, &bv, NULL );
373
374         bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_read );
375         attr_merge_one( e, mi->mi_ad_monitorConnectionRead, &bv, NULL );
376
377         bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_write );
378         attr_merge_one( e, mi->mi_ad_monitorConnectionWrite, &bv, NULL );
379
380         bv.bv_len = snprintf( buf, sizeof( buf ), "%s%s%s%s%s%s",
381                         c->c_currentber ? "r" : "",
382                         c->c_writewaiter ? "w" : "",
383                         LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
384                         LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
385                         connection_state2str( c->c_conn_state ),
386                         c->c_sasl_bind_in_progress ? "S" : "" );
387         attr_merge_one( e, mi->mi_ad_monitorConnectionMask, &bv, NULL );
388
389         attr_merge_one( e, mi->mi_ad_monitorConnectionAuthzDN,
390                 &c->c_dn, &c->c_ndn );
391
392         /* NOTE: client connections leave the c_peer_* fields NULL */
393         attr_merge_one( e, mi->mi_ad_monitorConnectionListener,
394                 &c->c_listener_url, NULL );
395
396         attr_merge_one( e, mi->mi_ad_monitorConnectionPeerDomain,
397                 BER_BVISNULL( &c->c_peer_domain ) ? &bv_unknown : &c->c_peer_domain,
398                 NULL );
399
400         attr_merge_one( e, mi->mi_ad_monitorConnectionPeerAddress,
401                 BER_BVISNULL( &c->c_peer_name ) ? &bv_unknown : &c->c_peer_name,
402                 NULL );
403
404         attr_merge_one( e, mi->mi_ad_monitorConnectionLocalAddress,
405                 &c->c_sock_name, NULL );
406
407         attr_merge_one( e, mi->mi_ad_monitorConnectionStartTime, &bv2, NULL );
408
409         attr_merge_one( e, mi->mi_ad_monitorConnectionActivityTime, &bv3, NULL );
410
411         mp = monitor_entrypriv_create();
412         if ( mp == NULL ) {
413                 return LDAP_OTHER;
414         }
415         e->e_private = ( void * )mp;
416         mp->mp_info = ms;
417         mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
418
419         *ep = e;
420
421         return SLAP_CB_CONTINUE;
422 }
423
424 static int 
425 monitor_subsys_conn_create( 
426         Operation               *op,
427         SlapReply               *rs,
428         struct berval           *ndn,
429         Entry                   *e_parent,
430         Entry                   **ep )
431 {
432         monitor_info_t  *mi = ( monitor_info_t * )op->o_bd->be_private;
433
434         Connection              *c;
435         int                     connindex;
436         monitor_entry_t         *mp;
437         int                     rc = SLAP_CB_CONTINUE;
438         monitor_subsys_t        *ms;
439
440         assert( mi != NULL );
441         assert( e_parent != NULL );
442         assert( ep != NULL );
443
444         ms = (( monitor_entry_t *)e_parent->e_private)->mp_info;
445
446         *ep = NULL;
447
448         if ( ndn == NULL ) {
449                 Entry   *e = NULL,
450                         *e_tmp = NULL;
451
452                 /* create all the children of e_parent */
453                 for ( c = connection_first( &connindex );
454                                 c != NULL;
455                                 c = connection_next( c, &connindex ) )
456                 {
457                         if ( conn_create( mi, c, &e, ms ) != SLAP_CB_CONTINUE
458                                         || e == NULL )
459                         {
460                                 for ( ; e_tmp != NULL; ) {
461                                         mp = ( monitor_entry_t * )e_tmp->e_private;
462                                         e = mp->mp_next;
463
464                                         ch_free( mp );
465                                         e_tmp->e_private = NULL;
466                                         entry_free( e_tmp );
467
468                                         e_tmp = e;
469                                 }
470                                 rc = rs->sr_err = LDAP_OTHER;
471                                 break;
472                         }
473                         mp = ( monitor_entry_t * )e->e_private;
474                         mp->mp_next = e_tmp;
475                         e_tmp = e;
476                 }
477                 connection_done( c );
478                 *ep = e;
479
480         } else {
481                 unsigned long           connid;
482                 char                    *next = NULL;
483                 static struct berval    nconn_bv = BER_BVC( "cn=connection " );
484
485                 rc = LDAP_NO_SUCH_OBJECT;
486                
487                 /* create exactly the required entry;
488                  * the normalized DN must start with "cn=connection ",
489                  * followed by the connection id, followed by
490                  * the RDN separator "," */
491                 if ( ndn->bv_len <= nconn_bv.bv_len
492                                 || strncmp( ndn->bv_val, nconn_bv.bv_val, nconn_bv.bv_len ) != 0 )
493                 {
494                         return -1;
495                 }
496                 
497                 connid = strtol( &ndn->bv_val[ nconn_bv.bv_len ], &next, 10 );
498                 if ( next[ 0 ] != ',' ) {
499                         return ( rs->sr_err = LDAP_OTHER );
500                 }
501
502                 for ( c = connection_first( &connindex );
503                                 c != NULL;
504                                 c = connection_next( c, &connindex ) )
505                 {
506                         if ( c->c_connid == connid ) {
507                                 rc = conn_create( mi, c, ep, ms );
508                                 if ( rc != SLAP_CB_CONTINUE ) {
509                                         rs->sr_err = rc;
510
511                                 } else if ( *ep == NULL ) {
512                                         rc = rs->sr_err = LDAP_OTHER;
513                                 }
514
515                                 break;
516                         }
517                 }
518                 
519                 connection_done( c );
520         }
521
522         return rc;
523 }
524