]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/thread.c
+ Fixed slapd connectionless LDAP support
[openldap] / servers / slapd / back-monitor / thread.c
1 /* thread.c - deal with thread 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 "back-monitor.h"
29
30 #include <ldap_rq.h>
31
32 static int 
33 monitor_subsys_thread_update( 
34         Operation               *op,
35         SlapReply               *rs,
36         Entry                   *e );
37
38 /*
39  * initializes log subentry
40  */
41 int
42 monitor_subsys_thread_init(
43         BackendDB               *be,
44         monitor_subsys_t        *ms
45 )
46 {
47         monitor_info_t  *mi;
48         monitor_entry_t *mp;
49         Entry           *e, **ep, *e_thread;
50         static char     buf[ BACKMONITOR_BUFSIZE ];
51
52         ms->mss_update = monitor_subsys_thread_update;
53
54         mi = ( monitor_info_t * )be->be_private;
55
56         if ( monitor_cache_get( mi, &ms->mss_ndn, &e_thread ) ) {
57                 Debug( LDAP_DEBUG_ANY,
58                         "monitor_subsys_thread_init: unable to get entry \"%s\"\n",
59                         ms->mss_ndn.bv_val, 
60                         0, 0 );
61                 return( -1 );
62         }
63
64         mp = ( monitor_entry_t * )e_thread->e_private;
65         mp->mp_children = NULL;
66         ep = &mp->mp_children;
67
68         /*
69          * Max
70          */
71         snprintf( buf, sizeof( buf ),
72                         "dn: cn=Max,%s\n"
73                         "objectClass: %s\n"
74                         "structuralObjectClass: %s\n"
75                         "cn: Max\n"
76                         "%s: %d\n"
77                         "creatorsName: %s\n"
78                         "modifiersName: %s\n"
79                         "createTimestamp: %s\n"
80                         "modifyTimestamp: %s\n", 
81                         ms->mss_dn.bv_val,
82                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
83                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
84                         mi->mi_ad_monitoredInfo->ad_cname.bv_val,
85                         connection_pool_max,
86                         mi->mi_creatorsName.bv_val,
87                         mi->mi_creatorsName.bv_val,
88                         mi->mi_startTime.bv_val,
89                         mi->mi_startTime.bv_val );
90
91         e = str2entry( buf );
92         if ( e == NULL ) {
93                 Debug( LDAP_DEBUG_ANY,
94                         "monitor_subsys_thread_init: "
95                         "unable to create entry \"cn=Max,%s\"\n",
96                         ms->mss_ndn.bv_val, 0, 0 );
97                 return( -1 );
98         }
99         
100         mp = monitor_entrypriv_create();
101         if ( mp == NULL ) {
102                 return -1;
103         }
104         e->e_private = ( void * )mp;
105         mp->mp_info = ms;
106         mp->mp_flags = ms->mss_flags \
107                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
108
109         if ( monitor_cache_add( mi, e ) ) {
110                 Debug( LDAP_DEBUG_ANY,
111                         "monitor_subsys_thread_init: "
112                         "unable to add entry \"cn=Max,%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          * Backload
122          */
123         snprintf( buf, sizeof( buf ),
124                         "dn: cn=Backload,%s\n"
125                         "objectClass: %s\n"
126                         "structuralObjectClass: %s\n"
127                         "cn: Backload\n"
128                         "%s: 0\n"
129                         "creatorsName: %s\n"
130                         "modifiersName: %s\n"
131                         "createTimestamp: %s\n"
132                         "modifyTimestamp: %s\n",
133                         ms->mss_dn.bv_val,
134                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
135                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
136                         mi->mi_ad_monitoredInfo->ad_cname.bv_val,
137                         mi->mi_creatorsName.bv_val,
138                         mi->mi_creatorsName.bv_val,
139                         mi->mi_startTime.bv_val,
140                         mi->mi_startTime.bv_val );
141
142         e = str2entry( buf );
143         if ( e == NULL ) {
144                 Debug( LDAP_DEBUG_ANY,
145                         "monitor_subsys_thread_init: "
146                         "unable to create entry \"cn=Backload,%s\"\n",
147                         ms->mss_ndn.bv_val, 0, 0 );
148                 return( -1 );
149         }
150
151         mp = monitor_entrypriv_create();
152         if ( mp == NULL ) {
153                 return -1;
154         }
155         e->e_private = ( void * )mp;
156         mp->mp_info = ms;
157         mp->mp_flags = ms->mss_flags \
158                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
159
160         if ( monitor_cache_add( mi, e ) ) {
161                 Debug( LDAP_DEBUG_ANY,
162                         "monitor_subsys_thread_init: "
163                         "unable to add entry \"cn=Backload,%s\"\n",
164                         ms->mss_ndn.bv_val, 0, 0 );
165                 return( -1 );
166         }
167         
168         *ep = e;
169         ep = &mp->mp_next;
170
171         /*
172          * Runqueue runners
173          */
174         snprintf( buf, sizeof( buf ),
175                         "dn: cn=Runqueue,%s\n"
176                         "objectClass: %s\n"
177                         "structuralObjectClass: %s\n"
178                         "cn: Runqueue\n"
179                         "%s: 0\n"
180                         "creatorsName: %s\n"
181                         "modifiersName: %s\n"
182                         "createTimestamp: %s\n"
183                         "modifyTimestamp: %s\n",
184                         ms->mss_dn.bv_val,
185                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
186                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
187                         mi->mi_ad_monitoredInfo->ad_cname.bv_val,
188                         mi->mi_creatorsName.bv_val,
189                         mi->mi_creatorsName.bv_val,
190                         mi->mi_startTime.bv_val,
191                         mi->mi_startTime.bv_val );
192
193         e = str2entry( buf );
194         if ( e == NULL ) {
195                 Debug( LDAP_DEBUG_ANY,
196                         "monitor_subsys_thread_init: "
197                         "unable to create entry \"cn=Runqueue,%s\"\n",
198                         ms->mss_ndn.bv_val, 0, 0 );
199                 return( -1 );
200         }
201
202         mp = monitor_entrypriv_create();
203         if ( mp == NULL ) {
204                 return -1;
205         }
206         e->e_private = ( void * )mp;
207         mp->mp_info = ms;
208         mp->mp_flags = ms->mss_flags \
209                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
210
211         if ( monitor_cache_add( mi, e ) ) {
212                 Debug( LDAP_DEBUG_ANY,
213                         "monitor_subsys_thread_init: "
214                         "unable to add entry \"cn=Runqueue,%s\"\n",
215                         ms->mss_ndn.bv_val, 0, 0 );
216                 return( -1 );
217         }
218         
219         *ep = e;
220         ep = &mp->mp_next;
221
222         monitor_cache_release( mi, e_thread );
223
224         return( 0 );
225 }
226
227 static int 
228 monitor_subsys_thread_update( 
229         Operation               *op,
230         SlapReply               *rs,
231         Entry                   *e )
232 {
233         monitor_info_t  *mi = ( monitor_info_t * )op->o_bd->be_private;
234         Attribute               *a;
235         char                    buf[ BACKMONITOR_BUFSIZE ];
236         static struct berval    backload_bv = BER_BVC( "cn=backload" );
237         static struct berval    runqueue_bv = BER_BVC( "cn=runqueue" );
238         struct berval           rdn, bv;
239         ber_len_t               len;
240         int which = 0, i;
241         struct re_s *re;
242
243         assert( mi != NULL );
244
245         dnRdn( &e->e_nname, &rdn );
246         if ( dn_match( &rdn, &backload_bv ) ) {
247                 which = 1;
248
249         } else if ( dn_match( &rdn, &runqueue_bv ) ) {
250                 which = 2;
251
252         } else {
253                 return SLAP_CB_CONTINUE;
254         }
255
256         a = attr_find( e->e_attrs, mi->mi_ad_monitoredInfo );
257         if ( a == NULL ) {
258                 return rs->sr_err = LDAP_OTHER;
259         }
260
261         switch ( which ) {
262         case 1:
263                 snprintf( buf, sizeof( buf ), "%d", 
264                         ldap_pvt_thread_pool_backload( &connection_pool ) );
265                 len = strlen( buf );
266                 if ( len > a->a_vals[ 0 ].bv_len ) {
267                         a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
268                 }
269                 a->a_vals[ 0 ].bv_len = len;
270                 AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
271                 break;
272
273         case 2:
274                 for ( i = 0; !BER_BVISNULL( &a->a_vals[ i ] ); i++ ) {
275                         ch_free( a->a_vals[i].bv_val );
276                         BER_BVZERO( &a->a_vals[ i ] );
277                 }
278                 bv.bv_val = buf;
279                 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
280                 LDAP_STAILQ_FOREACH( re, &slapd_rq.run_list, rnext ) {
281                         bv.bv_len = snprintf( buf, sizeof( buf ), "%s(%s)",
282                                 re->tname, re->tspec );
283                         value_add_one( &a->a_vals, &bv );
284                 }
285                 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
286
287                 /* don't leave 'round attributes with no values */
288                 if ( BER_BVISNULL( &a->a_vals[ 0 ] ) ) {
289                         BER_BVSTR( &bv, "()" );
290                         value_add_one( &a->a_vals, &bv );
291                 }
292                 break;
293         }
294
295         /* FIXME: touch modifyTimestamp? */
296
297         return SLAP_CB_CONTINUE;
298 }
299