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