]> git.sur5r.net Git - openldap/blob - servers/slapd/ctxcsn.c
ITS#2764, #2781 revert backend.c patch, just catch the NULL referral
[openldap] / servers / slapd / ctxcsn.c
1 /* ctxcsn.c -- Context CSN Management Routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003 The OpenLDAP Foundation.
6  * Portions Copyright 2003 IBM Corporation.
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 the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21
22 #include <ac/string.h>
23 #include <ac/socket.h>
24
25 #include "ldap_pvt.h"
26 #include "lutil.h"
27 #include "slap.h"
28 #include "lutil_ldap.h"
29
30 const struct berval slap_ldapsync_bv = BER_BVC("ldapsync");
31 const struct berval slap_ldapsync_cn_bv = BER_BVC("cn=ldapsync");
32
33 void
34 slap_get_commit_csn( Operation *op, struct berval *csn )
35 {
36         struct slap_csn_entry *csne = NULL, *committed_csne = NULL;
37         int i = 0;
38
39         csn->bv_val = NULL;
40         csn->bv_len = 0;
41
42         ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
43
44         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
45                 if ( csne->opid == op->o_opid && csne->connid == op->o_connid ) break;
46         }
47
48         if ( csne ) {
49                 csne->state = SLAP_CSN_COMMIT;
50         }
51
52         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
53                 if ( csne->state == SLAP_CSN_COMMIT ) committed_csne = csne;
54                 if ( csne->state == SLAP_CSN_PENDING ) break;
55         }
56
57         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
58
59         if ( committed_csne ) {
60                 ber_dupbv( csn, committed_csne->csn );
61         }
62 }
63
64 void
65 slap_rewind_commit_csn( Operation *op )
66 {
67         struct slap_csn_entry *csne = NULL;
68
69         ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
70
71         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
72                 if ( csne->opid == op->o_opid && csne->connid == op->o_connid ) break;
73         }
74
75         if ( csne ) {
76                 csne->state = SLAP_CSN_PENDING;
77         }
78         
79         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
80 }
81
82 void
83 slap_graduate_commit_csn( Operation *op )
84 {
85         struct slap_csn_entry *csne = NULL;
86
87         if ( op == NULL )
88                 return;
89
90         if ( op->o_bd == NULL )
91                 return;
92
93         ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
94
95         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
96                 if ( csne->opid == op->o_opid && csne->connid == op->o_connid ) break;
97         }
98
99         if ( csne ) {
100                 LDAP_TAILQ_REMOVE( &op->o_bd->be_pending_csn_list, csne, csn_link );
101                 ch_free( csne->csn->bv_val );
102                 ch_free( csne->csn );
103                 ch_free( csne );
104         }
105
106         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
107
108         return;
109 }
110
111 static struct berval ocbva[] = {
112         BER_BVC("top"),
113         BER_BVC("subentry"),
114         BER_BVC("syncProviderSubentry"),
115         {0,NULL}
116 };
117
118 Entry *
119 slap_create_context_csn_entry(
120         Backend *be,
121         struct berval *context_csn
122 )
123 {
124         Entry* e;
125         int rc;
126
127         struct berval bv;
128
129         e = (Entry *) ch_calloc( 1, sizeof( Entry ));
130
131         attr_merge( e, slap_schema.si_ad_objectClass,
132                 ocbva, NULL );
133         attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
134                 &ocbva[1], NULL );
135         attr_merge_one( e, slap_schema.si_ad_cn,
136                 (struct berval *)&slap_ldapsync_bv, NULL );
137
138         if ( context_csn ) {
139                 attr_merge_one( e, slap_schema.si_ad_contextCSN,
140                         context_csn, NULL );
141         }
142
143         bv.bv_val = "{}";
144         bv.bv_len = sizeof("{}")-1;
145         attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
146
147         build_new_dn( &e->e_name, &be->be_nsuffix[0],
148                 (struct berval *)&slap_ldapsync_cn_bv, NULL );
149         ber_dupbv( &e->e_nname, &e->e_name );
150
151         return e;
152 }
153
154 static int
155 slap_contextcsn_callback(
156         Operation* op,
157         SlapReply* rs
158 )
159 {
160         if ( rs->sr_type != REP_SEARCH ) {
161                 *((int*)op->o_callback->sc_private) = 0;
162         } else {
163                 *((int*)op->o_callback->sc_private) = 1;
164         }
165         return LDAP_SUCCESS;
166 }
167
168 int
169 slap_get_csn(
170         Operation *op,
171         char *csnbuf,
172         int     len,
173         struct berval *csn,
174         int manage_ctxcsn
175 )
176 {
177         struct slap_csn_entry *pending;
178
179         if ( csn == NULL ) return LDAP_OTHER;
180
181         csn->bv_len = lutil_csnstr( csnbuf, len, 0, 0 );
182         csn->bv_val = csnbuf;
183
184         if ( manage_ctxcsn ) {
185                 pending = (struct slap_csn_entry *) ch_calloc( 1,
186                         sizeof( struct slap_csn_entry ));
187                 ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
188                 ber_dupbv( &op->o_sync_csn, csn );
189                 pending->csn = ber_dupbv( NULL, csn );
190                 pending->connid = op->o_connid;
191                 pending->opid = op->o_opid;
192                 pending->state = SLAP_CSN_PENDING;
193                 LDAP_TAILQ_INSERT_TAIL( &op->o_bd->be_pending_csn_list,
194                         pending, csn_link );
195                 ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
196         }
197
198         return LDAP_SUCCESS;
199 }