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