]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/thread.c
tolerate that config_get_vals() returns success with no values (ITS#4341)
[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         struct berval bv;
52
53         ms->mss_update = monitor_subsys_thread_update;
54
55         mi = ( monitor_info_t * )be->be_private;
56
57         if ( monitor_cache_get( mi, &ms->mss_ndn, &e_thread ) ) {
58                 Debug( LDAP_DEBUG_ANY,
59                         "monitor_subsys_thread_init: unable to get entry \"%s\"\n",
60                         ms->mss_ndn.bv_val, 
61                         0, 0 );
62                 return( -1 );
63         }
64
65         mp = ( monitor_entry_t * )e_thread->e_private;
66         mp->mp_children = NULL;
67         ep = &mp->mp_children;
68
69         /*
70          * Max
71          */
72         BER_BVSTR( &bv, "cn=Max" );
73         e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
74                 mi->mi_oc_monitoredObject, mi, NULL, NULL );
75         if ( e == NULL ) {
76                 Debug( LDAP_DEBUG_ANY,
77                         "monitor_subsys_thread_init: "
78                         "unable to create entry \"cn=Max,%s\"\n",
79                         ms->mss_ndn.bv_val, 0, 0 );
80                 return( -1 );
81         }
82         bv.bv_len = snprintf( buf, sizeof( buf ), "%d", connection_pool_max );
83         bv.bv_val = buf;
84         attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
85         
86         mp = monitor_entrypriv_create();
87         if ( mp == NULL ) {
88                 return -1;
89         }
90         e->e_private = ( void * )mp;
91         mp->mp_info = ms;
92         mp->mp_flags = ms->mss_flags \
93                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
94
95         if ( monitor_cache_add( mi, e ) ) {
96                 Debug( LDAP_DEBUG_ANY,
97                         "monitor_subsys_thread_init: "
98                         "unable to add entry \"cn=Max,%s\"\n",
99                         ms->mss_ndn.bv_val, 0, 0 );
100                 return( -1 );
101         }
102         
103         *ep = e;
104         ep = &mp->mp_next;
105
106         /*
107          * Backload
108          */
109         BER_BVSTR( &bv, "cn=Backload" );
110         e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
111                 mi->mi_oc_monitoredObject, mi, NULL, NULL );
112         if ( e == NULL ) {
113                 Debug( LDAP_DEBUG_ANY,
114                         "monitor_subsys_thread_init: "
115                         "unable to create entry \"cn=Backload,%s\"\n",
116                         ms->mss_ndn.bv_val, 0, 0 );
117                 return( -1 );
118         }
119         BER_BVSTR( &bv, "0" );
120         attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
121
122         mp = monitor_entrypriv_create();
123         if ( mp == NULL ) {
124                 return -1;
125         }
126         e->e_private = ( void * )mp;
127         mp->mp_info = ms;
128         mp->mp_flags = ms->mss_flags \
129                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
130
131         if ( monitor_cache_add( mi, e ) ) {
132                 Debug( LDAP_DEBUG_ANY,
133                         "monitor_subsys_thread_init: "
134                         "unable to add entry \"cn=Backload,%s\"\n",
135                         ms->mss_ndn.bv_val, 0, 0 );
136                 return( -1 );
137         }
138         
139         *ep = e;
140         ep = &mp->mp_next;
141
142         /*
143          * Runqueue runners
144          */
145         BER_BVSTR( &bv, "cn=Runqueue" );
146         e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
147                 mi->mi_oc_monitoredObject, mi, NULL, NULL );
148         if ( e == NULL ) {
149                 Debug( LDAP_DEBUG_ANY,
150                         "monitor_subsys_thread_init: "
151                         "unable to create entry \"cn=Runqueue,%s\"\n",
152                         ms->mss_ndn.bv_val, 0, 0 );
153                 return( -1 );
154         }
155         BER_BVSTR( &bv, "0" );
156         attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
157
158         mp = monitor_entrypriv_create();
159         if ( mp == NULL ) {
160                 return -1;
161         }
162         e->e_private = ( void * )mp;
163         mp->mp_info = ms;
164         mp->mp_flags = ms->mss_flags \
165                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
166
167         if ( monitor_cache_add( mi, e ) ) {
168                 Debug( LDAP_DEBUG_ANY,
169                         "monitor_subsys_thread_init: "
170                         "unable to add entry \"cn=Runqueue,%s\"\n",
171                         ms->mss_ndn.bv_val, 0, 0 );
172                 return( -1 );
173         }
174         
175         *ep = e;
176         ep = &mp->mp_next;
177
178         monitor_cache_release( mi, e_thread );
179
180         return( 0 );
181 }
182
183 static int 
184 monitor_subsys_thread_update( 
185         Operation               *op,
186         SlapReply               *rs,
187         Entry                   *e )
188 {
189         monitor_info_t  *mi = ( monitor_info_t * )op->o_bd->be_private;
190         Attribute               *a;
191         char                    buf[ BACKMONITOR_BUFSIZE ];
192         static struct berval    backload_bv = BER_BVC( "cn=backload" );
193         static struct berval    runqueue_bv = BER_BVC( "cn=runqueue" );
194         struct berval           rdn, bv;
195         ber_len_t               len;
196         int which = 0, i;
197         struct re_s *re;
198
199         assert( mi != NULL );
200
201         dnRdn( &e->e_nname, &rdn );
202         if ( dn_match( &rdn, &backload_bv ) ) {
203                 which = 1;
204
205         } else if ( dn_match( &rdn, &runqueue_bv ) ) {
206                 which = 2;
207
208         } else {
209                 return SLAP_CB_CONTINUE;
210         }
211
212         a = attr_find( e->e_attrs, mi->mi_ad_monitoredInfo );
213         if ( a == NULL ) {
214                 return rs->sr_err = LDAP_OTHER;
215         }
216
217         switch ( which ) {
218         case 1:
219                 snprintf( buf, sizeof( buf ), "%d", 
220                         ldap_pvt_thread_pool_backload( &connection_pool ) );
221                 len = strlen( buf );
222                 if ( len > a->a_vals[ 0 ].bv_len ) {
223                         a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
224                 }
225                 a->a_vals[ 0 ].bv_len = len;
226                 AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
227                 break;
228
229         case 2:
230                 for ( i = 0; !BER_BVISNULL( &a->a_vals[ i ] ); i++ ) {
231                         ch_free( a->a_vals[i].bv_val );
232                         BER_BVZERO( &a->a_vals[ i ] );
233                 }
234                 bv.bv_val = buf;
235                 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
236                 LDAP_STAILQ_FOREACH( re, &slapd_rq.run_list, rnext ) {
237                         bv.bv_len = snprintf( buf, sizeof( buf ), "%s(%s)",
238                                 re->tname, re->tspec );
239                         value_add_one( &a->a_vals, &bv );
240                 }
241                 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
242
243                 /* don't leave 'round attributes with no values */
244                 if ( BER_BVISNULL( &a->a_vals[ 0 ] ) ) {
245                         BER_BVSTR( &bv, "()" );
246                         value_add_one( &a->a_vals, &bv );
247                 }
248                 break;
249         }
250
251         /* FIXME: touch modifyTimestamp? */
252
253         return SLAP_CB_CONTINUE;
254 }
255