]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/conn.c
8bb3eb8f28d4fceb096eabe0a34cdb9000c52f20
[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         Entry                   *e
178 )
179 {
180         monitor_info_t  *mi = ( monitor_info_t * )op->o_bd->be_private;
181
182         long                    n = -1;
183         static struct berval    total_bv = BER_BVC( "cn=total" ),
184                                 current_bv = BER_BVC( "cn=current" );
185         struct berval           rdn;
186
187         assert( mi );
188         assert( e );
189
190         dnRdn( &e->e_nname, &rdn );
191         
192         if ( dn_match( &rdn, &total_bv ) ) {
193                 n = connections_nextid();
194
195         } else if ( dn_match( &rdn, &current_bv ) ) {
196                 Connection      *c;
197                 int             connindex;
198
199                 for ( n = 0, c = connection_first( &connindex );
200                                 c != NULL;
201                                 n++, c = connection_next( c, &connindex ) ) {
202                         /* No Op */ ;
203                 }
204                 connection_done( c );
205         }
206
207         if ( n != -1 ) {
208                 Attribute       *a;
209                 char            buf[] = "+9223372036854775807L";
210                 ber_len_t       len;
211
212                 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
213                 if ( a == NULL ) {
214                         return( -1 );
215                 }
216
217                 snprintf( buf, sizeof( buf ), "%ld", n );
218                 len = strlen( buf );
219                 if ( len > a->a_vals[ 0 ].bv_len ) {
220                         a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
221                 }
222                 a->a_vals[ 0 ].bv_len = len;
223                 AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
224
225                 /* FIXME: touch modifyTimestamp? */
226         }
227
228         return( 0 );
229 }
230
231 static int
232 conn_create(
233         monitor_info_t          *mi,
234         Connection              *c,
235         Entry                   **ep,
236         monitor_subsys_t        *ms
237 )
238 {
239         monitor_entry_t *mp;
240         struct tm               *ltm;
241         char                    buf[ BACKMONITOR_BUFSIZE ];
242         char                    buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
243         char                    buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
244
245         struct berval           bv;
246
247         Entry                   *e;
248
249         struct tm       *ctm;
250         char            ctmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
251         struct tm       *mtm;
252         char            mtmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
253 #ifdef HAVE_GMTIME_R
254         struct tm       tm_buf;
255 #endif /* HAVE_GMTIME_R */
256
257         assert( c != NULL );
258         assert( ep != NULL );
259
260 #ifndef HAVE_GMTIME_R
261         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
262 #endif
263 #ifdef HACK_LOCAL_TIME
264 # ifdef HAVE_LOCALTIME_R
265         ctm = localtime_r( &c->c_starttime, &tm_buf );
266         lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
267         mtm = localtime_r( &c->c_activitytime, &tm_buf );
268         lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
269 # else
270         ctm = localtime( &c->c_starttime );
271         lutil_localtime( ctmbuf, sizeof( ctmbuf ), ctm, -timezone );
272         mtm = localtime( &c->c_activitytime );
273         lutil_localtime( mtmbuf, sizeof( mtmbuf ), mtm, -timezone );
274 # endif /* HAVE_LOCALTIME_R */
275 #else /* !HACK_LOCAL_TIME */
276 # ifdef HAVE_GMTIME_R
277         ctm = gmtime_r( &c->c_starttime, &tm_buf );
278         lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
279         mtm = gmtime_r( &c->c_activitytime, &tm_buf );
280         lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
281 # else
282         ctm = gmtime( &c->c_starttime );
283         lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
284         mtm = gmtime( &c->c_activitytime );
285         lutil_gentime( mtmbuf, sizeof( mtmbuf ), mtm );
286 # endif /* HAVE_GMTIME_R */
287 #endif /* !HACK_LOCAL_TIME */
288 #ifndef HAVE_GMTIME_R
289         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
290 #endif
291
292 #ifndef HAVE_GMTIME_R
293         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
294 #endif
295
296 #ifdef HAVE_GMTIME_R
297         ltm = gmtime_r( &c->c_starttime, &tm_buf );
298 #else
299         ltm = gmtime( &c->c_starttime );
300 #endif
301         lutil_gentime( buf2, sizeof( buf2 ), ltm );
302
303 #ifdef HAVE_GMTIME_R
304         ltm = gmtime_r( &c->c_activitytime, &tm_buf );
305 #else
306         ltm = gmtime( &c->c_activitytime );
307 #endif
308         lutil_gentime( buf3, sizeof( buf3 ), ltm );
309
310 #ifndef HAVE_GMTIME_R
311         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
312 #endif /* HAVE_GMTIME_R */
313
314         snprintf( buf, sizeof( buf ),
315                 "dn: cn=Connection %ld,%s\n"
316                 "objectClass: %s\n"
317                 "structuralObjectClass: %s\n"
318                 "cn: Connection %ld\n"
319
320 #ifdef MONITOR_LEGACY_CONN
321                 /* NOTE: this will disappear, as the exploded data
322                  * has been moved to dedicated attributes */
323                 "%s: "
324                         "%ld "
325                         ": %ld "
326                         ": %ld/%ld/%ld/%ld "
327                         ": %ld/%ld/%ld "
328                         ": %s%s%s%s%s%s "
329                         ": %s "
330                         ": %s "
331                         ": %s "
332                         ": %s "
333                         ": %s "
334                         ": %s "
335                         ": %s\n"
336 #endif /* MONITOR_LEGACY_CONN */
337
338                 "%s: %lu\n"
339                 "%s: %ld\n"
340
341                 "%s: %ld\n"
342                 "%s: %ld\n"
343                 "%s: %ld\n"
344                 "%s: %ld\n"
345
346                 "%s: %ld\n"
347                 "%s: %ld\n"
348                 "%s: %ld\n"
349
350                 "%s: %s%s%s%s%s%s\n"
351
352                 "%s: %s\n"
353
354                 "%s: %s\n"
355                 "%s: %s\n"
356                 "%s: %s\n"
357                 "%s: %s\n"
358
359                 "%s: %s\n"
360                 "%s: %s\n"
361
362                 "creatorsName: %s\n"
363                 "modifiersName: %s\n"
364                 "createTimestamp: %s\n"
365                 "modifyTimestamp: %s\n",
366                 c->c_connid, ms->mss_dn.bv_val,
367                 mi->mi_oc_monitorConnection->soc_cname.bv_val,
368                 mi->mi_oc_monitorConnection->soc_cname.bv_val,
369                 c->c_connid,
370
371 #ifdef MONITOR_LEGACY_CONN
372                 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
373                         c->c_connid,
374                         (long) c->c_protocol,
375                         c->c_n_ops_received, c->c_n_ops_executing,
376                                 c->c_n_ops_pending, c->c_n_ops_completed,
377                         
378                         /* add low-level counters here */
379                         c->c_n_get, c->c_n_read, c->c_n_write,
380                         
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                         
388                         c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
389                         
390                         c->c_listener_url.bv_val,
391                         c->c_peer_domain.bv_val,
392                         c->c_peer_name.bv_val,
393                         c->c_sock_name.bv_val,
394                         
395                         buf2,
396                         buf3,
397 #endif /* MONITOR_LEGACY_CONN */
398
399                 mi->mi_ad_monitorConnectionNumber->ad_cname.bv_val,
400                         c->c_connid,
401                 mi->mi_ad_monitorConnectionProtocol->ad_cname.bv_val,
402                         (long)c->c_protocol,
403
404                 mi->mi_ad_monitorConnectionOpsReceived->ad_cname.bv_val,
405                         c->c_n_ops_received,
406                 mi->mi_ad_monitorConnectionOpsExecuting->ad_cname.bv_val,
407                         c->c_n_ops_executing,
408                 mi->mi_ad_monitorConnectionOpsPending->ad_cname.bv_val,
409                         c->c_n_ops_pending,
410                 mi->mi_ad_monitorConnectionOpsCompleted->ad_cname.bv_val,
411                         c->c_n_ops_completed,
412
413                 mi->mi_ad_monitorConnectionGet->ad_cname.bv_val,
414                         c->c_n_get,
415                 mi->mi_ad_monitorConnectionRead->ad_cname.bv_val,
416                         c->c_n_read,
417                 mi->mi_ad_monitorConnectionWrite->ad_cname.bv_val,
418                         c->c_n_write,
419
420                 mi->mi_ad_monitorConnectionMask->ad_cname.bv_val,
421                         c->c_currentber ? "r" : "",
422                         c->c_writewaiter ? "w" : "",
423                         LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
424                         LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
425                         connection_state2str( c->c_conn_state ),
426                         c->c_sasl_bind_in_progress ? "S" : "",
427                 
428                 mi->mi_ad_monitorConnectionAuthzDN->ad_cname.bv_val,
429                         c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
430
431                 mi->mi_ad_monitorConnectionListener->ad_cname.bv_val,
432                         c->c_listener_url.bv_val,
433                 mi->mi_ad_monitorConnectionPeerDomain->ad_cname.bv_val,
434                         c->c_peer_domain.bv_val,
435                 mi->mi_ad_monitorConnectionLocalAddress->ad_cname.bv_val,
436                         c->c_peer_name.bv_val,
437                 mi->mi_ad_monitorConnectionPeerAddress->ad_cname.bv_val,
438                         c->c_sock_name.bv_val,
439
440                 mi->mi_ad_monitorConnectionStartTime->ad_cname.bv_val,
441                         buf2,
442                 mi->mi_ad_monitorConnectionActivityTime->ad_cname.bv_val,
443                         buf3,
444
445                 mi->mi_creatorsName.bv_val,
446                 mi->mi_creatorsName.bv_val,
447                 ctmbuf,
448                 mtmbuf );
449                 
450         e = str2entry( buf );
451
452         if ( e == NULL) {
453                 Debug( LDAP_DEBUG_ANY,
454                         "monitor_subsys_conn_create: "
455                         "unable to create entry "
456                         "\"cn=Connection %ld,%s\" entry\n",
457                         c->c_connid, 
458                         ms->mss_dn.bv_val, 0 );
459                 return( -1 );
460         }
461
462         mp = monitor_entrypriv_create();
463         if ( mp == NULL ) {
464                 return -1;
465         }
466         e->e_private = ( void * )mp;
467         mp->mp_info = ms;
468         mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
469
470         *ep = e;
471
472         return( 0 );
473 }
474
475 int 
476 monitor_subsys_conn_create( 
477         Operation               *op,
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 = 0;
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 = -1;
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 -1;
548                 }
549
550                 for ( c = connection_first( &connindex );
551                                 c != NULL;
552                                 c = connection_next( c, &connindex )) {
553                         if ( c->c_connid == connid ) {
554                                 if ( conn_create( mi, c, ep, ms ) || *ep == NULL ) {
555                                         rc = -1;
556                                 }
557
558                                 break;
559                         }
560                 }
561                 
562                 connection_done(c);
563         }
564
565         return rc;
566 }
567