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