]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/operation.c
remove ch_malloc/ch_free
[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_op[] = {
44                 BER_BVC( "Bind" ),
45                 BER_BVC( "Unbind" ),
46                 BER_BVC( "Add" ),
47                 BER_BVC( "Delete" ),
48                 BER_BVC( "Modrdn" ),
49                 BER_BVC( "Modify" ),
50                 BER_BVC( "Compare" ),
51                 BER_BVC( "Search" ),
52                 BER_BVC( "Abandon" ),
53                 BER_BVC( "Extended" )
54         };
55
56 int
57 monitor_subsys_ops_init(
58         BackendDB               *be
59 )
60 {
61         struct monitorinfo      *mi;
62         
63         Entry                   *e, *e_tmp, *e_op;
64         struct monitorentrypriv *mp;
65         char                    buf[ BACKMONITOR_BUFSIZE ];
66         int                     i;
67
68         assert( be != NULL );
69
70         mi = ( struct monitorinfo * )be->be_private;
71
72         if ( monitor_cache_get( mi,
73                         &monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn, &e_op ) ) {
74 #ifdef NEW_LOGGING
75                 LDAP_LOG( OPERATION, CRIT,
76                         "monitor_subsys_ops_init: "
77                         "unable to get entry '%s'\n",
78                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0, 0 );
79 #else
80                 Debug( LDAP_DEBUG_ANY,
81                         "monitor_subsys_ops_init: "
82                         "unable to get entry '%s'\n%s%s",
83                         monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 
84                         "", "" );
85 #endif
86                 return( -1 );
87         }
88
89         e_tmp = NULL;
90
91         for ( i = SLAP_OP_LAST; i-- > 0; ) {
92
93                 /*
94                  * Initiated ops
95                  */
96                 snprintf( buf, sizeof( buf ),
97                                 "dn: cn=%s,%s\n"
98                                 "objectClass: %s\n"
99                                 "structuralObjectClass: %s\n"
100                                 "cn: %s\n"
101                                 "%s: 0\n"
102                                 "%s: 0\n",
103                                 bv_op[ i ].bv_val,
104                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_dn.bv_val,
105                                 mi->oc_monitorOperation->soc_cname.bv_val,
106                                 mi->oc_monitorOperation->soc_cname.bv_val,
107                                 bv_op[ i ].bv_val,
108                                 mi->ad_monitorOpInitiated->ad_cname.bv_val,
109                                 mi->ad_monitorOpCompleted->ad_cname.bv_val );
110
111                 e = str2entry( buf );
112                 if ( e == NULL ) {
113 #ifdef NEW_LOGGING
114                         LDAP_LOG( OPERATION, CRIT,
115                                 "monitor_subsys_ops_init: "
116                                 "unable to create entry 'cn=%s,%s'\n",
117                                 bv_op[ i ].bv_val,
118                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0 );
119 #else
120                         Debug( LDAP_DEBUG_ANY,
121                                 "monitor_subsys_ops_init: "
122                                 "unable to create entry 'cn=%s,%s'\n",
123                                 bv_op[ i ].bv_val,
124                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0 );
125 #endif
126                         return( -1 );
127                 }
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_op[ i ].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",
148                                 bv_op[ i ].bv_val,
149                                 monitor_subsys[SLAPD_MONITOR_OPS].mss_ndn.bv_val, 0 );
150 #endif
151                         return( -1 );
152                 }
153         
154                 e_tmp = e;
155         }
156
157         mp = ( struct monitorentrypriv * )e_op->e_private;
158         mp->mp_children = e_tmp;
159
160         monitor_cache_release( mi, e_op );
161
162         return( 0 );
163 }
164
165 int
166 monitor_subsys_ops_update(
167         Operation               *op,
168         Entry                   *e
169 )
170 {
171         struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
172         long            nInitiated = -1, nCompleted = -1;
173         char            *rdnvalue;
174         int             i;
175         ber_len_t       len;
176         Attribute       *a;
177         char            buf[] = "+9223372036854775807L";
178
179         assert( mi );
180         assert( e );
181
182         rdnvalue = e->e_dn + ( sizeof( "cn=" ) - 1 );
183
184         for (i = 0; i < SLAP_OP_LAST; i++ ) {
185                 len = bv_op[ i ].bv_len;
186
187                 if ( strncmp( rdnvalue, bv_op[ i ].bv_val, len ) == 0 ) {
188                         nInitiated = num_ops_initiated_[ i ];
189                         nCompleted = num_ops_completed_[ i ];
190                         break;
191                 }
192         }
193
194         if ( i == SLAP_OP_LAST ) {
195                 return( 0 );
196         }
197
198         a = attr_find( e->e_attrs, mi->ad_monitorOpInitiated );
199         assert ( a != NULL );
200         snprintf( buf, sizeof( buf ), "%ld", nInitiated );
201         free( a->a_vals[ 0 ].bv_val );
202         ber_str2bv( buf, 0, 1, &a->a_vals[ 0 ] );
203
204         a = attr_find( e->e_attrs, mi->ad_monitorOpCompleted );
205         assert ( a != NULL );
206         snprintf( buf, sizeof( buf ), "%ld", nCompleted );
207         free( a->a_vals[ 0 ].bv_val );
208         ber_str2bv( buf, 0, 1, &a->a_vals[ 0 ] );
209
210         return( 0 );
211 }
212