]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/operation.c
more (mostly blind) fixes
[openldap] / servers / slapd / back-monitor / operation.c
1 /* operation.c - deal with operation subsystem */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
8  * 
9  * This work has beed deveolped for the OpenLDAP Foundation 
10  * in the hope that it may be useful to the Open Source community, 
11  * but WITHOUT ANY WARRANTY.
12  * 
13  * Permission is granted to anyone to use this software for any purpose
14  * on any computer system, and to alter it and redistribute it, subject
15  * to the following restrictions:
16  * 
17  * 1. The author and SysNet s.n.c. are not responsible for the consequences
18  *    of use of this software, no matter how awful, even if they arise from
19  *    flaws in it.
20  * 
21  * 2. The origin of this software must not be misrepresented, either by
22  *    explicit claim or by omission.  Since few users ever read sources,
23  *    credits should appear in the documentation.
24  * 
25  * 3. Altered versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.  Since few users
27  *    ever read sources, credits should appear in the documentation.
28  *    SysNet s.n.c. cannot be responsible for the consequences of the
29  *    alterations.
30  * 
31  * 4. This notice may not be removed or altered.
32  */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37
38 #include "slap.h"
39 #include "back-monitor.h"
40 #include "lber_pvt.h"
41
42 static struct berval 
43         bv_initiated = BER_BVC( "Initiated" ),
44         bv_completed = BER_BVC( "Completed" ),
45         bv_op[] = {
46                 BER_BVC( "Bind" ),
47                 BER_BVC( "Unbind" ),
48                 BER_BVC( "Add" ),
49                 BER_BVC( "Delete" ),
50                 BER_BVC( "Modrdn" ),
51                 BER_BVC( "Modify" ),
52                 BER_BVC( "Compare" ),
53                 BER_BVC( "Search" ),
54                 BER_BVC( "Abandon" ),
55                 BER_BVC( "Extended" )
56         };
57
58 int
59 monitor_subsys_ops_init(
60         BackendDB               *be
61 )
62 {
63         struct monitorinfo      *mi;
64         
65         Entry                   *e, *e_tmp, *e_op, *e_children;
66         struct monitorentrypriv *mp;
67         char                    buf[1024];
68         struct berval           bv[2];
69         int                     i;
70
71         assert( be != NULL );
72
73         mi = ( struct monitorinfo * )be->be_private;
74
75         if ( monitor_cache_get( mi,
76                         &monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn, &e_op ) ) {
77 #ifdef NEW_LOGGING
78                 LDAP_LOG( OPERATION, CRIT,
79                         "monitor_subsys_ops_init: "
80                         "unable to get entry '%s'\n",
81                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0, 0 );
82 #else
83                 Debug( LDAP_DEBUG_ANY,
84                         "monitor_subsys_ops_init: "
85                         "unable to get entry '%s'\n%s%s",
86                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 
87                         "", "" );
88 #endif
89                 return( -1 );
90         }
91
92         e_tmp = NULL;
93
94         /*
95          * Initiated ops
96          */
97         snprintf( buf, sizeof( buf ),
98                         "dn: cn=%s,%s\n"
99                         SLAPD_MONITOR_OBJECTCLASSES
100                         "cn: %s\n",
101                         bv_initiated.bv_val,
102                         monitor_subsys[SLAPD_MONITOR_OPS].mss_dn.bv_val,
103                         bv_initiated.bv_val );
104
105         e = str2entry( buf );
106         if ( e == NULL ) {
107 #ifdef NEW_LOGGING
108                 LDAP_LOG( OPERATION, CRIT,
109                         "monitor_subsys_ops_init: "
110                         "unable to create entry 'cn=%s,%s'\n",
111                         bv_initiated.bv_val,
112                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0 );
113 #else
114                 Debug( LDAP_DEBUG_ANY,
115                         "monitor_subsys_ops_init: "
116                         "unable to create entry 'cn=%s,%s'\n%s",
117                         bv_initiated.bv_val,
118                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val,
119                         "" );
120 #endif
121                 return( -1 );
122         }
123         
124         bv[1].bv_val = NULL;
125         bv[0].bv_val = "0";
126         bv[0].bv_len = 1;
127         attr_mergeit( e, monitor_ad_desc, bv );
128         
129         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
130         e->e_private = ( void * )mp;
131         mp->mp_next = e_tmp;
132         mp->mp_children = NULL;
133         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_OPS];
134         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_OPS].mss_flags \
135                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
136
137         if ( monitor_cache_add( mi, e ) ) {
138 #ifdef NEW_LOGGING
139                 LDAP_LOG( OPERATION, CRIT,
140                         "monitor_subsys_ops_init: "
141                         "unable to add entry 'cn=%s,%s'\n",
142                         bv_initiated.bv_val,
143                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0 );
144 #else
145                 Debug( LDAP_DEBUG_ANY,
146                         "monitor_subsys_ops_init: "
147                         "unable to add entry 'cn=%s,%s'\n%s",
148                         bv_initiated.bv_val,
149                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val,
150                         "" );
151 #endif
152                 return( -1 );
153         }
154         
155         e_tmp = e;
156         e_children = NULL;
157
158         for ( i = SLAP_OP_LAST; i-- > 0; ) {
159
160                 /*
161                  * Initiated ops
162                  */
163                 snprintf( buf, sizeof( buf ),
164                                 "dn: cn=%s,cn=%s,%s\n"
165                                 SLAPD_MONITOR_OBJECTCLASSES
166                                 "cn: %s\n",
167                                 bv_op[ i ].bv_val,
168                                 bv_initiated.bv_val,
169                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_dn.bv_val,
170                                 bv_op[ i ].bv_val );
171
172                 e = str2entry( buf );
173                 if ( e == NULL ) {
174 #ifdef NEW_LOGGING
175                         LDAP_LOG( OPERATION, CRIT,
176                                 "monitor_subsys_ops_init: "
177                                 "unable to create entry 'cn=%s,cn=%s,%s'\n",
178                                 bv_op[ i ].bv_val,
179                                 bv_initiated.bv_val,
180                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val );
181 #else
182                         Debug( LDAP_DEBUG_ANY,
183                                 "monitor_subsys_ops_init: "
184                                 "unable to create entry 'cn=%s,cn=%s,%s'\n",
185                                 bv_op[ i ].bv_val,
186                                 bv_initiated.bv_val,
187                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val );
188 #endif
189                         return( -1 );
190                 }
191         
192                 bv[1].bv_val = NULL;
193                 bv[0].bv_val = "0";
194                 bv[0].bv_len = 1;
195                 attr_mergeit( e, monitor_ad_desc, bv );
196         
197                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
198                 e->e_private = ( void * )mp;
199                 mp->mp_next = e_children;
200                 mp->mp_children = NULL;
201                 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_OPS];
202                 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_OPS].mss_flags \
203                         | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
204
205                 if ( monitor_cache_add( mi, e ) ) {
206 #ifdef NEW_LOGGING
207                         LDAP_LOG( OPERATION, CRIT,
208                                 "monitor_subsys_ops_init: "
209                                 "unable to add entry 'cn=%s,cn=%s,%s'\n",
210                                 bv_op[ i ].bv_val,
211                                 bv_initiated.bv_val,
212                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val );
213 #else
214                         Debug( LDAP_DEBUG_ANY,
215                                 "monitor_subsys_ops_init: "
216                                 "unable to add entry 'cn=%s,cn=%s,%s'\n",
217                                 bv_op[ i ].bv_val,
218                                 bv_initiated.bv_val,
219                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val );
220 #endif
221                         return( -1 );
222                 }
223         
224                 e_children = e;
225         }
226
227         mp = ( struct monitorentrypriv * )e_tmp->e_private;
228         mp->mp_children = e_children;
229
230         /*
231          * Completed ops
232          */
233         snprintf( buf, sizeof( buf ),
234                         "dn: cn=%s,%s\n"
235                         SLAPD_MONITOR_OBJECTCLASSES
236                         "cn: %s\n",
237                         bv_completed.bv_val,
238                         monitor_subsys[SLAPD_MONITOR_OPS].mss_dn.bv_val,
239                         bv_completed.bv_val );
240
241         e = str2entry( buf );
242         if ( e == NULL ) {
243 #ifdef NEW_LOGGING
244                 LDAP_LOG( OPERATION, CRIT,
245                         "monitor_subsys_ops_init: "
246                         "unable to create entry 'cn=%s,%s'\n",
247                         bv_completed.bv_val,
248                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0 );
249 #else
250                 Debug( LDAP_DEBUG_ANY,
251                         "monitor_subsys_ops_init: "
252                         "unable to create entry 'cn=%s,%s'\n%s",
253                         bv_completed.bv_val,
254                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val,
255                         "" );
256 #endif
257                 return( -1 );
258         }
259
260         bv[0].bv_val = "0";
261         bv[0].bv_len = 1;
262         attr_mergeit( e, monitor_ad_desc, bv );
263         
264         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
265         e->e_private = ( void * )mp;
266         mp->mp_next = e_tmp;
267         mp->mp_children = NULL;
268         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_OPS];
269         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_OPS].mss_flags \
270                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
271
272         if ( monitor_cache_add( mi, e ) ) {
273 #ifdef NEW_LOGGING
274                 LDAP_LOG( OPERATION, CRIT,
275                         "monitor_subsys_ops_init: "
276                         "unable to add entry 'cn=%s,%s'\n",
277                         bv_completed.bv_val,
278                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0 );
279 #else
280                 Debug( LDAP_DEBUG_ANY,
281                         "monitor_subsys_ops_init: "
282                         "unable to add entry 'cn=%s,%s'\n%s",
283                         bv_completed.bv_val,
284                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val,
285                         "" );
286 #endif
287                 return( -1 );
288         }
289         
290         e_tmp = e;
291         e_children = NULL;
292
293         for ( i = SLAP_OP_LAST; i-- > 0; ) {
294
295                 /*
296                  * Completed ops
297                  */
298                 snprintf( buf, sizeof( buf ),
299                                 "dn: cn=%s,cn=%s,%s\n"
300                                 SLAPD_MONITOR_OBJECTCLASSES
301                                 "cn: %s\n",
302                                 bv_op[ i ].bv_val,
303                                 bv_completed.bv_val,
304                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_dn.bv_val,
305                                 bv_op[ i ].bv_val );
306         
307                 e = str2entry( buf );
308                 if ( e == NULL ) {
309 #ifdef NEW_LOGGING
310                         LDAP_LOG( OPERATION, CRIT,
311                                 "monitor_subsys_ops_init: "
312                                 "unable to create entry 'cn=%s,cn=%s,%s'\n",
313                                 bv_op[ i ].bv_val,
314                                 bv_completed.bv_val,
315                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val );
316 #else
317                         Debug( LDAP_DEBUG_ANY,
318                                 "monitor_subsys_ops_init: "
319                                 "unable to create entry 'cn=%s,cn=%s,%s'\n",
320                                 bv_op[ i ].bv_val,
321                                 bv_completed.bv_val,
322                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val );
323 #endif
324                         return( -1 );
325                 }
326
327                 bv[0].bv_val = "0";
328                 bv[0].bv_len = 1;
329                 attr_mergeit( e, monitor_ad_desc, bv );
330         
331                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
332                 e->e_private = ( void * )mp;
333                 mp->mp_next = e_children;
334                 mp->mp_children = NULL;
335                 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_OPS];
336                 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_OPS].mss_flags \
337                         | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
338
339                 if ( monitor_cache_add( mi, e ) ) {
340 #ifdef NEW_LOGGING
341                         LDAP_LOG( OPERATION, CRIT,
342                                 "monitor_subsys_ops_init: "
343                                 "unable to add entry 'cn=%s,cn=%s,%s'\n",
344                                 bv_op[ i ].bv_val,
345                                 bv_completed.bv_val,
346                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val );
347 #else
348                         Debug( LDAP_DEBUG_ANY,
349                                 "monitor_subsys_ops_init: "
350                                 "unable to add entry 'cn=%s,cn=%s,%s'\n",
351                                 bv_op[ i ].bv_val,
352                                 bv_completed.bv_val,
353                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val );
354 #endif
355                         return( -1 );
356                 }
357         
358                 e_children = e;
359         }
360
361         mp = ( struct monitorentrypriv * )e_tmp->e_private;
362         mp->mp_children = e_children;
363
364         mp = ( struct monitorentrypriv * )e_op->e_private;
365         mp->mp_children = e_tmp;
366
367         monitor_cache_release( mi, e_op );
368
369         return( 0 );
370 }
371
372 int
373 monitor_subsys_ops_update(
374         struct monitorinfo      *mi,
375         Entry                   *e
376 )
377 {
378         long            n = -1;
379         char            *dn;
380
381         assert( mi );
382         assert( e );
383
384         dn = e->e_dn + 3;
385
386         if ( strncmp( dn, bv_initiated.bv_val, 
387                                 bv_initiated.bv_len ) == 0 ) {
388                 ldap_pvt_thread_mutex_lock(&num_ops_mutex);
389                 n = num_ops_initiated;
390                 ldap_pvt_thread_mutex_unlock(&num_ops_mutex);
391
392         } else if ( strncmp( dn, bv_completed.bv_val,
393                                 bv_completed.bv_len ) == 0 ) {
394                 ldap_pvt_thread_mutex_lock(&num_ops_mutex);
395                 n = num_ops_completed;
396                 ldap_pvt_thread_mutex_unlock(&num_ops_mutex);
397
398         } else {
399                 int             i;
400                 ber_len_t       len;
401                 
402                 for (i = 0; i < SLAP_OP_LAST; i++ ) {
403                         len = bv_op[ i ].bv_len;
404
405                         if ( strncmp( dn, bv_op[ i ].bv_val, len ) == 0 ) {
406                                 break;
407                         }
408                 }
409
410                 if ( i == SLAP_OP_LAST ) {
411                         return( 0 );
412                 }
413
414                 dn += len + 3 + 1;
415
416                 if ( strncmp( dn, bv_initiated.bv_val,
417                                         bv_initiated.bv_len ) == 0 ) {
418                         ldap_pvt_thread_mutex_lock(&num_ops_mutex);
419                         n = num_ops_initiated_[ i ];
420                         ldap_pvt_thread_mutex_unlock(&num_ops_mutex);
421
422                 } else if ( strncmp( dn, bv_completed.bv_val,
423                                         bv_completed.bv_len ) == 0 ) {
424                         ldap_pvt_thread_mutex_lock(&num_ops_mutex);
425                         n = num_ops_completed_[ i ];
426                         ldap_pvt_thread_mutex_unlock(&num_ops_mutex);
427
428                 } else {
429                         assert( 0 );
430                 }
431         }
432
433         if ( n != -1 ) {
434                 Attribute       *a;
435                 char            buf[16];
436
437                 a = attr_find( e->e_attrs, monitor_ad_desc );
438                 if ( a == NULL ) {
439                         return( -1 );
440                 }
441
442                 snprintf( buf, sizeof( buf ), "%ld", n );
443                 free( a->a_vals[ 0 ].bv_val );
444                 ber_str2bv( buf, 0, 1, a->a_vals );
445         }
446
447         return( 0 );
448 }
449