]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/operation.c
Happy new year!
[openldap] / servers / slapd / back-monitor / operation.c
1 /* operation.c - deal with operation 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 #include "lber_pvt.h"
30
31 struct monitor_ops_t {
32         struct berval   rdn;
33         struct berval   nrdn;
34 } monitor_op[] = {
35         { BER_BVC( "cn=Bind" ),         BER_BVNULL },
36         { BER_BVC( "cn=Unbind" ),       BER_BVNULL },
37         { BER_BVC( "cn=Add" ),          BER_BVNULL },
38         { BER_BVC( "cn=Delete" ),       BER_BVNULL },
39         { BER_BVC( "cn=Modrdn" ),       BER_BVNULL },
40         { BER_BVC( "cn=Modify" ),       BER_BVNULL },
41         { BER_BVC( "cn=Compare" ),      BER_BVNULL },
42         { BER_BVC( "cn=Search" ),       BER_BVNULL },
43         { BER_BVC( "cn=Abandon" ),      BER_BVNULL },
44         { BER_BVC( "cn=Extended" ),     BER_BVNULL },
45         { BER_BVNULL,                   BER_BVNULL }
46 };
47
48 static int
49 monitor_subsys_ops_destroy(
50         BackendDB               *be,
51         monitor_subsys_t        *ms );
52
53 static int
54 monitor_subsys_ops_update(
55         Operation               *op,
56         SlapReply               *rs,
57         Entry                   *e );
58
59 int
60 monitor_subsys_ops_init(
61         BackendDB               *be,
62         monitor_subsys_t        *ms )
63 {
64         monitor_info_t  *mi;
65         
66         Entry           *e_op, **ep;
67         monitor_entry_t *mp;
68         char            buf[ BACKMONITOR_BUFSIZE ];
69         int             i;
70         struct berval   bv_zero = BER_BVC( "0" );
71
72         assert( be != NULL );
73
74         ms->mss_destroy = monitor_subsys_ops_destroy;
75         ms->mss_update = monitor_subsys_ops_update;
76
77         mi = ( monitor_info_t * )be->be_private;
78
79         if ( monitor_cache_get( mi,
80                         &ms->mss_ndn, &e_op ) )
81         {
82                 Debug( LDAP_DEBUG_ANY,
83                         "monitor_subsys_ops_init: "
84                         "unable to get entry \"%s\"\n",
85                         ms->mss_ndn.bv_val, 
86                         0, 0 );
87                 return( -1 );
88         }
89
90         attr_merge_one( e_op, mi->mi_ad_monitorOpInitiated, &bv_zero, &bv_zero );
91         attr_merge_one( e_op, mi->mi_ad_monitorOpCompleted, &bv_zero, &bv_zero );
92
93         mp = ( monitor_entry_t * )e_op->e_private;
94         mp->mp_children = NULL;
95         ep = &mp->mp_children;
96
97         for ( i = 0; i < SLAP_OP_LAST; i++ ) {
98                 struct berval   rdn;
99                 Entry           *e;
100
101                 /*
102                  * Initiated ops
103                  */
104                 snprintf( buf, sizeof( buf ),
105                                 "dn: %s,%s\n"
106                                 "objectClass: %s\n"
107                                 "structuralObjectClass: %s\n"
108                                 "cn: %s\n"
109                                 "%s: 0\n"
110                                 "%s: 0\n"
111                                 "creatorsName: %s\n"
112                                 "modifiersName: %s\n"
113                                 "createTimestamp: %s\n"
114                                 "modifyTimestamp: %s\n",
115                                 monitor_op[ i ].rdn.bv_val,
116                                 ms->mss_dn.bv_val,
117                                 mi->mi_oc_monitorOperation->soc_cname.bv_val,
118                                 mi->mi_oc_monitorOperation->soc_cname.bv_val,
119                                 &monitor_op[ i ].rdn.bv_val[ STRLENOF( "cn=" ) ],
120                                 mi->mi_ad_monitorOpInitiated->ad_cname.bv_val,
121                                 mi->mi_ad_monitorOpCompleted->ad_cname.bv_val,
122                                 mi->mi_creatorsName.bv_val,
123                                 mi->mi_creatorsName.bv_val,
124                                 mi->mi_startTime.bv_val,
125                                 mi->mi_startTime.bv_val );
126
127                 e = str2entry( buf );
128                 if ( e == NULL ) {
129                         Debug( LDAP_DEBUG_ANY,
130                                 "monitor_subsys_ops_init: "
131                                 "unable to create entry \"%s,%s\"\n",
132                                 monitor_op[ i ].rdn.bv_val,
133                                 ms->mss_ndn.bv_val, 0 );
134                         return( -1 );
135                 }
136         
137                 /* steal normalized RDN */
138                 dnRdn( &e->e_nname, &rdn );
139                 ber_dupbv( &monitor_op[ i ].nrdn, &rdn );
140         
141                 mp = monitor_entrypriv_create();
142                 if ( mp == NULL ) {
143                         return -1;
144                 }
145                 e->e_private = ( void * )mp;
146                 mp->mp_info = ms;
147                 mp->mp_flags = ms->mss_flags \
148                         | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
149
150                 if ( monitor_cache_add( mi, e ) ) {
151                         Debug( LDAP_DEBUG_ANY,
152                                 "monitor_subsys_ops_init: "
153                                 "unable to add entry \"%s,%s\"\n",
154                                 monitor_op[ i ].rdn.bv_val,
155                                 ms->mss_ndn.bv_val, 0 );
156                         return( -1 );
157                 }
158
159                 *ep = e;
160                 ep = &mp->mp_next;
161         }
162
163         monitor_cache_release( mi, e_op );
164
165         return( 0 );
166 }
167
168 static int
169 monitor_subsys_ops_destroy(
170         BackendDB               *be,
171         monitor_subsys_t        *ms )
172 {
173         int             i;
174
175         for ( i = 0; i < SLAP_OP_LAST; i++ ) {
176                 if ( !BER_BVISNULL( &monitor_op[ i ].nrdn ) ) {
177                         ch_free( monitor_op[ i ].nrdn.bv_val );
178                 }
179         }
180
181         return 0;
182 }
183
184 static int
185 monitor_subsys_ops_update(
186         Operation               *op,
187         SlapReply               *rs,
188         Entry                   *e )
189 {
190         monitor_info_t          *mi = ( monitor_info_t * )op->o_bd->be_private;
191
192         ldap_pvt_mp_t           nInitiated = LDAP_PVT_MP_INIT,
193                                 nCompleted = LDAP_PVT_MP_INIT;
194         struct berval           rdn;
195         int                     i;
196         Attribute               *a;
197         static struct berval    bv_ops = BER_BVC( "cn=operations" );
198
199         assert( mi != NULL );
200         assert( e != NULL );
201
202         dnRdn( &e->e_nname, &rdn );
203
204         if ( dn_match( &rdn, &bv_ops ) ) {
205                 ldap_pvt_mp_init( nInitiated );
206                 ldap_pvt_mp_init( nCompleted );
207
208                 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
209                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
210                         ldap_pvt_mp_add( nInitiated, slap_counters.sc_ops_initiated_[ i ] );
211                         ldap_pvt_mp_add( nCompleted, slap_counters.sc_ops_completed_[ i ] );
212                 }
213                 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
214                 
215         } else {
216                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
217                         if ( dn_match( &rdn, &monitor_op[ i ].nrdn ) )
218                         {
219                                 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
220                                 ldap_pvt_mp_init_set( nInitiated, slap_counters.sc_ops_initiated_[ i ] );
221                                 ldap_pvt_mp_init_set( nCompleted, slap_counters.sc_ops_completed_[ i ] );
222                                 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
223                                 break;
224                         }
225                 }
226
227                 if ( i == SLAP_OP_LAST ) {
228                         /* not found ... */
229                         return( 0 );
230                 }
231         }
232
233         a = attr_find( e->e_attrs, mi->mi_ad_monitorOpInitiated );
234         assert ( a != NULL );
235
236         /* NOTE: no minus sign is allowed in the counters... */
237         UI2BV( &a->a_vals[ 0 ], nInitiated );
238         ldap_pvt_mp_clear( nInitiated );
239         
240         a = attr_find( e->e_attrs, mi->mi_ad_monitorOpCompleted );
241         assert ( a != NULL );
242
243         /* NOTE: no minus sign is allowed in the counters... */
244         UI2BV( &a->a_vals[ 0 ], nCompleted );
245         ldap_pvt_mp_clear( nCompleted );
246
247         /* FIXME: touch modifyTimestamp? */
248
249         return SLAP_CB_CONTINUE;
250 }
251