]> git.sur5r.net Git - openldap/blob - servers/slapd/ctxcsn.c
#ifdef for slap_auxprop_store
[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 )
46                         break;
47         }
48
49         if ( csne ) {
50                 csne->state = SLAP_CSN_COMMIT;
51         }
52
53         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
54                 if ( csne->state == SLAP_CSN_COMMIT )
55                         committed_csne = csne;
56                 if ( csne->state == SLAP_CSN_PENDING )
57                         break;
58         }
59
60         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
61
62         if ( committed_csne ) {
63                 ber_dupbv( csn, committed_csne->csn );
64         }
65 }
66
67 void
68 slap_rewind_commit_csn( Operation *op )
69 {
70         struct slap_csn_entry *csne = NULL;
71
72         ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
73
74         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
75                 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
76                         break;
77         }
78
79         if ( csne ) {
80                 csne->state = SLAP_CSN_PENDING;
81         }
82         
83         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
84 }
85
86 void
87 slap_graduate_commit_csn( Operation *op )
88 {
89         struct slap_csn_entry *csne = NULL;
90
91         if ( op == NULL )
92                 return;
93
94         if ( op->o_bd == NULL )
95                 return;
96
97         ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
98
99         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
100                 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
101                         break;
102         }
103
104         if ( csne ) {
105                 LDAP_TAILQ_REMOVE( &op->o_bd->be_pending_csn_list, csne, csn_link );
106                 ch_free( csne->csn->bv_val );
107                 ch_free( csne->csn );
108                 ch_free( csne );
109         }
110
111         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
112
113         return;
114 }
115
116 static struct berval ocbva[] = {
117         BER_BVC("top"),
118         BER_BVC("subentry"),
119         BER_BVC("syncProviderSubentry"),
120         {0,NULL}
121 };
122
123 Entry *
124 slap_create_context_csn_entry(
125         Backend *be,
126         struct berval *context_csn
127 )
128 {
129         Entry* e;
130         int rc;
131
132         struct berval bv;
133
134         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
135
136         attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
137
138         attr_merge_one( e, slap_schema.si_ad_structuralObjectClass, &ocbva[1], NULL );
139
140         attr_merge_one( e, slap_schema.si_ad_cn, (struct berval *)&slap_ldapsync_bv, NULL );
141
142         if ( context_csn ) {
143                 attr_merge_one( e, slap_schema.si_ad_contextCSN,
144                         context_csn, NULL );
145         }
146
147         bv.bv_val = "{}";
148         bv.bv_len = sizeof("{}")-1;
149         attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
150
151         build_new_dn( &e->e_name, &be->be_nsuffix[0], (struct berval *)&slap_ldapsync_cn_bv, NULL );
152         ber_dupbv( &e->e_nname, &e->e_name );
153
154         return e;
155 }
156
157 static int
158 slap_contextcsn_callback(
159         Operation* op,
160         SlapReply* rs
161 )
162 {
163         if ( rs->sr_type != REP_SEARCH ) {
164                 *((int*)op->o_callback->sc_private) = 0;
165         } else {
166                 *((int*)op->o_callback->sc_private) = 1;
167         }
168         return LDAP_SUCCESS;
169 }
170
171 int
172 slap_get_csn(
173         Operation *op,
174         char *csnbuf,
175         int     len,
176         struct berval *csn,
177         int manage_ctxcsn
178 )
179 {
180         struct  slap_csn_entry *pending;
181
182         if ( csn == NULL )
183                 return LDAP_OTHER;
184
185         csn->bv_len = lutil_csnstr( csnbuf, len, 0, 0 );
186         csn->bv_val = csnbuf;
187
188         if ( manage_ctxcsn ) {
189                 pending = (struct slap_csn_entry *) ch_calloc( 1, sizeof( struct slap_csn_entry ));
190                 ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
191                 ber_dupbv( &op->o_sync_csn, csn );
192                 pending->csn = ber_dupbv( NULL, csn );
193                 pending->connid = op->o_connid;
194                 pending->opid = op->o_opid;
195                 pending->state = SLAP_CSN_PENDING;
196                 LDAP_TAILQ_INSERT_TAIL( &op->o_bd->be_pending_csn_list, pending, csn_link );
197                 ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
198         }
199
200         return LDAP_SUCCESS;
201 }