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