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