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