]> git.sur5r.net Git - openldap/blob - servers/slapd/ctxcsn.c
71c7188bd3c9f2fd79a5863914ac57df545617b5
[openldap] / servers / slapd / ctxcsn.c
1 /* $OpenLDAP$ */
2 /*
3  * Context CSN Management Routines
4  */
5 /* Copyright (c) 2003 by International Business Machines, Inc.
6  *
7  * International Business Machines, Inc. (hereinafter called IBM) grants
8  * permission under its copyrights to use, copy, modify, and distribute this
9  * Software with or without fee, provided that the above copyright notice and
10  * all paragraphs of this notice appear in all copies, and that the name of IBM
11  * not be used in connection with the marketing of any product incorporating
12  * the Software or modifications thereof, without specific, written prior
13  * permission.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17  * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
18  * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
19  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
20  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/string.h>
28 #include <ac/socket.h>
29
30 #include "ldap_pvt.h"
31 #include "lutil.h"
32 #include "slap.h"
33 #include "lutil_ldap.h"
34
35 const struct berval slap_ldapsync_bv = BER_BVC("ldapsync");
36 const struct berval slap_ldapsync_cn_bv = BER_BVC("cn=ldapsync");
37
38 void
39 slap_get_commit_csn( Operation *op, struct berval *csn )
40 {
41         struct slap_csn_entry *csne = NULL, *committed_csne = NULL;
42         int i = 0;
43
44         csn->bv_val = NULL;
45         csn->bv_len = 0;
46
47         ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
48
49         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
50                 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
51                         break;
52         }
53
54         if ( csne ) {
55                 csne->state = SLAP_CSN_COMMIT;
56         }
57
58         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
59                 if ( csne->state == SLAP_CSN_COMMIT )
60                         committed_csne = csne;
61                 if ( csne->state == SLAP_CSN_PENDING )
62                         break;
63         }
64
65         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
66
67         if ( committed_csne ) {
68                 ber_dupbv( csn, committed_csne->csn );
69         }
70 }
71
72 void
73 slap_rewind_commit_csn( Operation *op )
74 {
75         struct slap_csn_entry *csne = NULL;
76
77         ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
78
79         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
80                 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
81                         break;
82         }
83
84         if ( csne ) {
85                 csne->state = SLAP_CSN_PENDING;
86         }
87         
88         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
89 }
90
91 void
92 slap_graduate_commit_csn( Operation *op )
93 {
94         struct slap_csn_entry *csne = NULL;
95
96         if ( op == NULL )
97                 return;
98
99         if ( op->o_bd == NULL )
100                 return;
101
102         ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
103
104         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
105                 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
106                         break;
107         }
108
109         if ( csne ) {
110                 LDAP_TAILQ_REMOVE( &op->o_bd->be_pending_csn_list, csne, csn_link );
111                 ch_free( csne->csn->bv_val );
112                 ch_free( csne->csn );
113                 ch_free( csne );
114         }
115
116         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
117
118         return;
119 }
120
121 static struct berval ocbva[] = {
122         BER_BVC("top"),
123         BER_BVC("subentry"),
124         BER_BVC("syncProviderSubentry"),
125         {0,NULL}
126 };
127
128 Entry *
129 slap_create_context_csn_entry(
130         Backend *be,
131         struct berval *context_csn
132 )
133 {
134         Entry* e;
135         int rc;
136
137         struct berval bv;
138
139         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
140
141         attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
142
143         attr_merge_one( e, slap_schema.si_ad_structuralObjectClass, &ocbva[1], NULL );
144
145         attr_merge_one( e, slap_schema.si_ad_cn, (struct berval *)&slap_ldapsync_bv, NULL );
146
147         if ( context_csn ) {
148                 attr_merge_one( e, slap_schema.si_ad_contextCSN,
149                         context_csn, NULL );
150         }
151
152         bv.bv_val = "{}";
153         bv.bv_len = sizeof("{}")-1;
154         attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
155
156         build_new_dn( &e->e_name, &be->be_nsuffix[0], (struct berval *)&slap_ldapsync_cn_bv, NULL );
157         ber_dupbv( &e->e_nname, &e->e_name );
158
159         return e;
160 }
161
162 static int
163 slap_contextcsn_callback(
164         Operation* op,
165         SlapReply* rs
166 )
167 {
168         if ( rs->sr_type != REP_SEARCH ) {
169                 *((int*)op->o_callback->sc_private) = 0;
170         } else {
171                 *((int*)op->o_callback->sc_private) = 1;
172         }
173         return LDAP_SUCCESS;
174 }
175
176 int
177 slap_get_csn(
178         Operation *op,
179         char *csnbuf,
180         int     len,
181         struct berval *csn,
182         int manage_ctxcsn
183 )
184 {
185         struct  slap_csn_entry *pending;
186
187         if ( csn == NULL )
188                 return LDAP_OTHER;
189
190         csn->bv_len = lutil_csnstr( csnbuf, len, 0, 0 );
191         csn->bv_val = csnbuf;
192
193         if ( manage_ctxcsn ) {
194                 pending = (struct slap_csn_entry *) ch_calloc( 1, sizeof( struct slap_csn_entry ));
195                 ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
196                 ber_dupbv( &op->o_sync_csn, csn );
197                 pending->csn = ber_dupbv( NULL, csn );
198                 pending->connid = op->o_connid;
199                 pending->opid = op->o_opid;
200                 pending->state = SLAP_CSN_PENDING;
201                 LDAP_TAILQ_INSERT_TAIL( &op->o_bd->be_pending_csn_list, pending, csn_link );
202                 ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
203         }
204
205         return LDAP_SUCCESS;
206 }