]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/operation.c
pass Operation instead of private info
[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;
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.bv_val = "0";
125         bv.bv_len = 1;
126         attr_merge_one( e, monitor_ad_desc, &bv, NULL );
127         
128         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
129         e->e_private = ( void * )mp;
130         mp->mp_next = e_tmp;
131         mp->mp_children = NULL;
132         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_OPS];
133         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_OPS].mss_flags \
134                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
135
136         if ( monitor_cache_add( mi, e ) ) {
137 #ifdef NEW_LOGGING
138                 LDAP_LOG( OPERATION, CRIT,
139                         "monitor_subsys_ops_init: "
140                         "unable to add entry 'cn=%s,%s'\n",
141                         bv_initiated.bv_val,
142                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0 );
143 #else
144                 Debug( LDAP_DEBUG_ANY,
145                         "monitor_subsys_ops_init: "
146                         "unable to add entry 'cn=%s,%s'\n%s",
147                         bv_initiated.bv_val,
148                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val,
149                         "" );
150 #endif
151                 return( -1 );
152         }
153         
154         e_tmp = e;
155         e_children = NULL;
156
157         for ( i = SLAP_OP_LAST; i-- > 0; ) {
158
159                 /*
160                  * Initiated ops
161                  */
162                 snprintf( buf, sizeof( buf ),
163                                 "dn: cn=%s,cn=%s,%s\n"
164                                 SLAPD_MONITOR_OBJECTCLASSES
165                                 "cn: %s\n",
166                                 bv_op[ i ].bv_val,
167                                 bv_initiated.bv_val,
168                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_dn.bv_val,
169                                 bv_op[ i ].bv_val );
170
171                 e = str2entry( buf );
172                 if ( e == NULL ) {
173 #ifdef NEW_LOGGING
174                         LDAP_LOG( OPERATION, CRIT,
175                                 "monitor_subsys_ops_init: "
176                                 "unable to create entry 'cn=%s,cn=%s,%s'\n",
177                                 bv_op[ i ].bv_val,
178                                 bv_initiated.bv_val,
179                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val );
180 #else
181                         Debug( LDAP_DEBUG_ANY,
182                                 "monitor_subsys_ops_init: "
183                                 "unable to create entry 'cn=%s,cn=%s,%s'\n",
184                                 bv_op[ i ].bv_val,
185                                 bv_initiated.bv_val,
186                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val );
187 #endif
188                         return( -1 );
189                 }
190         
191                 bv.bv_val = "0";
192                 bv.bv_len = 1;
193                 attr_merge_one( e, monitor_ad_desc, &bv, NULL );
194         
195                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
196                 e->e_private = ( void * )mp;
197                 mp->mp_next = e_children;
198                 mp->mp_children = NULL;
199                 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_OPS];
200                 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_OPS].mss_flags \
201                         | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
202
203                 if ( monitor_cache_add( mi, e ) ) {
204 #ifdef NEW_LOGGING
205                         LDAP_LOG( OPERATION, CRIT,
206                                 "monitor_subsys_ops_init: "
207                                 "unable to add entry 'cn=%s,cn=%s,%s'\n",
208                                 bv_op[ i ].bv_val,
209                                 bv_initiated.bv_val,
210                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val );
211 #else
212                         Debug( LDAP_DEBUG_ANY,
213                                 "monitor_subsys_ops_init: "
214                                 "unable to add entry 'cn=%s,cn=%s,%s'\n",
215                                 bv_op[ i ].bv_val,
216                                 bv_initiated.bv_val,
217                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val );
218 #endif
219                         return( -1 );
220                 }
221         
222                 e_children = e;
223         }
224
225         mp = ( struct monitorentrypriv * )e_tmp->e_private;
226         mp->mp_children = e_children;
227
228         /*
229          * Completed ops
230          */
231         snprintf( buf, sizeof( buf ),
232                         "dn: cn=%s,%s\n"
233                         SLAPD_MONITOR_OBJECTCLASSES
234                         "cn: %s\n",
235                         bv_completed.bv_val,
236                         monitor_subsys[SLAPD_MONITOR_OPS].mss_dn.bv_val,
237                         bv_completed.bv_val );
238
239         e = str2entry( buf );
240         if ( e == NULL ) {
241 #ifdef NEW_LOGGING
242                 LDAP_LOG( OPERATION, CRIT,
243                         "monitor_subsys_ops_init: "
244                         "unable to create entry 'cn=%s,%s'\n",
245                         bv_completed.bv_val,
246                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0 );
247 #else
248                 Debug( LDAP_DEBUG_ANY,
249                         "monitor_subsys_ops_init: "
250                         "unable to create entry 'cn=%s,%s'\n%s",
251                         bv_completed.bv_val,
252                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val,
253                         "" );
254 #endif
255                 return( -1 );
256         }
257
258         bv.bv_val = "0";
259         bv.bv_len = 1;
260         attr_merge_one( e, monitor_ad_desc, &bv, NULL );
261         
262         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
263         e->e_private = ( void * )mp;
264         mp->mp_next = e_tmp;
265         mp->mp_children = NULL;
266         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_OPS];
267         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_OPS].mss_flags \
268                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
269
270         if ( monitor_cache_add( mi, e ) ) {
271 #ifdef NEW_LOGGING
272                 LDAP_LOG( OPERATION, CRIT,
273                         "monitor_subsys_ops_init: "
274                         "unable to add entry 'cn=%s,%s'\n",
275                         bv_completed.bv_val,
276                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0 );
277 #else
278                 Debug( LDAP_DEBUG_ANY,
279                         "monitor_subsys_ops_init: "
280                         "unable to add entry 'cn=%s,%s'\n%s",
281                         bv_completed.bv_val,
282                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val,
283                         "" );
284 #endif
285                 return( -1 );
286         }
287         
288         e_tmp = e;
289         e_children = NULL;
290
291         for ( i = SLAP_OP_LAST; i-- > 0; ) {
292
293                 /*
294                  * Completed ops
295                  */
296                 snprintf( buf, sizeof( buf ),
297                                 "dn: cn=%s,cn=%s,%s\n"
298                                 SLAPD_MONITOR_OBJECTCLASSES
299                                 "cn: %s\n",
300                                 bv_op[ i ].bv_val,
301                                 bv_completed.bv_val,
302                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_dn.bv_val,
303                                 bv_op[ i ].bv_val );
304         
305                 e = str2entry( buf );
306                 if ( e == NULL ) {
307 #ifdef NEW_LOGGING
308                         LDAP_LOG( OPERATION, CRIT,
309                                 "monitor_subsys_ops_init: "
310                                 "unable to create entry 'cn=%s,cn=%s,%s'\n",
311                                 bv_op[ i ].bv_val,
312                                 bv_completed.bv_val,
313                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val );
314 #else
315                         Debug( LDAP_DEBUG_ANY,
316                                 "monitor_subsys_ops_init: "
317                                 "unable to create entry 'cn=%s,cn=%s,%s'\n",
318                                 bv_op[ i ].bv_val,
319                                 bv_completed.bv_val,
320                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val );
321 #endif
322                         return( -1 );
323                 }
324
325                 bv.bv_val = "0";
326                 bv.bv_len = 1;
327                 attr_merge_one( e, monitor_ad_desc, &bv, NULL );
328         
329                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
330                 e->e_private = ( void * )mp;
331                 mp->mp_next = e_children;
332                 mp->mp_children = NULL;
333                 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_OPS];
334                 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_OPS].mss_flags \
335                         | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
336
337                 if ( monitor_cache_add( mi, e ) ) {
338 #ifdef NEW_LOGGING
339                         LDAP_LOG( OPERATION, CRIT,
340                                 "monitor_subsys_ops_init: "
341                                 "unable to add entry 'cn=%s,cn=%s,%s'\n",
342                                 bv_op[ i ].bv_val,
343                                 bv_completed.bv_val,
344                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val );
345 #else
346                         Debug( LDAP_DEBUG_ANY,
347                                 "monitor_subsys_ops_init: "
348                                 "unable to add entry 'cn=%s,cn=%s,%s'\n",
349                                 bv_op[ i ].bv_val,
350                                 bv_completed.bv_val,
351                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val );
352 #endif
353                         return( -1 );
354                 }
355         
356                 e_children = e;
357         }
358
359         mp = ( struct monitorentrypriv * )e_tmp->e_private;
360         mp->mp_children = e_children;
361
362         mp = ( struct monitorentrypriv * )e_op->e_private;
363         mp->mp_children = e_tmp;
364
365         monitor_cache_release( mi, e_op );
366
367         return( 0 );
368 }
369
370 int
371 monitor_subsys_ops_update(
372         Operation               *op,
373         Entry                   *e
374 )
375 {
376         struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
377         long            n = -1;
378         char            *dn;
379
380         assert( mi );
381         assert( e );
382
383         dn = e->e_dn + 3;
384
385         if ( strncmp( dn, bv_initiated.bv_val, 
386                                 bv_initiated.bv_len ) == 0 ) {
387                 ldap_pvt_thread_mutex_lock(&num_ops_mutex);
388                 n = num_ops_initiated;
389                 ldap_pvt_thread_mutex_unlock(&num_ops_mutex);
390
391         } else if ( strncmp( dn, bv_completed.bv_val,
392                                 bv_completed.bv_len ) == 0 ) {
393                 ldap_pvt_thread_mutex_lock(&num_ops_mutex);
394                 n = num_ops_completed;
395                 ldap_pvt_thread_mutex_unlock(&num_ops_mutex);
396
397         } else {
398                 int             i;
399                 ber_len_t       len;
400                 
401                 for (i = 0; i < SLAP_OP_LAST; i++ ) {
402                         len = bv_op[ i ].bv_len;
403
404                         if ( strncmp( dn, bv_op[ i ].bv_val, len ) == 0 ) {
405                                 break;
406                         }
407                 }
408
409                 if ( i == SLAP_OP_LAST ) {
410                         return( 0 );
411                 }
412
413                 dn += len + 3 + 1;
414
415                 if ( strncmp( dn, bv_initiated.bv_val,
416                                         bv_initiated.bv_len ) == 0 ) {
417                         ldap_pvt_thread_mutex_lock(&num_ops_mutex);
418                         n = num_ops_initiated_[ i ];
419                         ldap_pvt_thread_mutex_unlock(&num_ops_mutex);
420
421                 } else if ( strncmp( dn, bv_completed.bv_val,
422                                         bv_completed.bv_len ) == 0 ) {
423                         ldap_pvt_thread_mutex_lock(&num_ops_mutex);
424                         n = num_ops_completed_[ i ];
425                         ldap_pvt_thread_mutex_unlock(&num_ops_mutex);
426
427                 } else {
428                         assert( 0 );
429                 }
430         }
431
432         if ( n != -1 ) {
433                 Attribute       *a;
434                 char            buf[16];
435
436                 a = attr_find( e->e_attrs, monitor_ad_desc );
437                 if ( a == NULL ) {
438                         return( -1 );
439                 }
440
441                 snprintf( buf, sizeof( buf ), "%ld", n );
442                 free( a->a_vals[ 0 ].bv_val );
443                 ber_str2bv( buf, 0, 1, a->a_vals );
444         }
445
446         return( 0 );
447 }
448