]> git.sur5r.net Git - openldap/blob - servers/slapd/ctxcsn.c
Force a refresh if the search base has changed
[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-2004 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 "lutil.h"
26 #include "slap.h"
27 #include "lutil_ldap.h"
28
29 const struct berval slap_ldapsync_bv = BER_BVC("ldapsync");
30 const struct berval slap_ldapsync_cn_bv = BER_BVC("cn=ldapsync");
31
32 void
33 slap_get_commit_csn( Operation *op, struct berval *csn )
34 {
35         struct slap_csn_entry *csne, *committed_csne = NULL;
36         int i = 0;
37
38         csn->bv_val = NULL;
39         csn->bv_len = 0;
40
41         ldap_pvt_thread_mutex_lock( op->o_bd->be_pcl_mutexp );
42
43         LDAP_TAILQ_FOREACH( csne, op->o_bd->be_pending_csn_list, ce_csn_link ) {
44                 if ( csne->ce_opid == op->o_opid && csne->ce_connid == op->o_connid ) {
45                         csne->ce_state = SLAP_CSN_COMMIT;
46                         break;
47                 }
48         }
49
50         LDAP_TAILQ_FOREACH( csne, op->o_bd->be_pending_csn_list, ce_csn_link ) {
51                 if ( csne->ce_state == SLAP_CSN_COMMIT ) committed_csne = csne;
52                 if ( csne->ce_state == SLAP_CSN_PENDING ) break;
53         }
54
55         if ( committed_csne ) ber_dupbv_x( csn, committed_csne->ce_csn, op->o_tmpmemctx );
56         ldap_pvt_thread_mutex_unlock( op->o_bd->be_pcl_mutexp );
57 }
58
59 void
60 slap_rewind_commit_csn( Operation *op )
61 {
62         struct slap_csn_entry *csne;
63
64         ldap_pvt_thread_mutex_lock( op->o_bd->be_pcl_mutexp );
65
66         LDAP_TAILQ_FOREACH( csne, op->o_bd->be_pending_csn_list, ce_csn_link ) {
67                 if ( csne->ce_opid == op->o_opid && csne->ce_connid == op->o_connid ) {
68                         csne->ce_state = SLAP_CSN_PENDING;
69                         break;
70                 }
71         }
72
73         ldap_pvt_thread_mutex_unlock( op->o_bd->be_pcl_mutexp );
74 }
75
76 void
77 slap_graduate_commit_csn( Operation *op )
78 {
79         struct slap_csn_entry *csne;
80
81         if ( op == NULL ) return;
82         if ( op->o_bd == NULL ) return;
83
84         ldap_pvt_thread_mutex_lock( op->o_bd->be_pcl_mutexp );
85
86         LDAP_TAILQ_FOREACH( csne, op->o_bd->be_pending_csn_list, ce_csn_link ) {
87                 if ( csne->ce_opid == op->o_opid && csne->ce_connid == op->o_connid ) {
88                         LDAP_TAILQ_REMOVE( op->o_bd->be_pending_csn_list,
89                                 csne, ce_csn_link );
90                         ch_free( csne->ce_csn->bv_val );
91                         ch_free( csne->ce_csn );
92                         ch_free( csne );
93                         break;
94                 }
95         }
96
97         ldap_pvt_thread_mutex_unlock( op->o_bd->be_pcl_mutexp );
98
99         return;
100 }
101
102 static struct berval ocbva[] = {
103         BER_BVC("top"),
104         BER_BVC("subentry"),
105         BER_BVC("syncProviderSubentry"),
106         BER_BVNULL
107 };
108
109 Entry *
110 slap_create_context_csn_entry(
111         Backend *be,
112         struct berval *context_csn )
113 {
114         Entry* e;
115         int rc;
116
117         struct berval bv;
118
119         e = (Entry *) ch_calloc( 1, sizeof( Entry ));
120
121         attr_merge( e, slap_schema.si_ad_objectClass,
122                 ocbva, NULL );
123         attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
124                 &ocbva[1], NULL );
125         attr_merge_one( e, slap_schema.si_ad_cn,
126                 (struct berval *)&slap_ldapsync_bv, NULL );
127
128         if ( context_csn ) {
129                 attr_merge_one( e, slap_schema.si_ad_contextCSN,
130                         context_csn, NULL );
131         }
132
133         BER_BVSTR( &bv, "{}" );
134         attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
135
136         build_new_dn( &e->e_name, &be->be_nsuffix[0],
137                 (struct berval *)&slap_ldapsync_cn_bv, NULL );
138         ber_dupbv( &e->e_nname, &e->e_name );
139
140         return e;
141 }
142
143 void
144 slap_queue_csn(
145         Operation *op,
146         struct berval *csn )
147 {
148         struct slap_csn_entry *pending;
149
150         pending = (struct slap_csn_entry *) ch_calloc( 1,
151                         sizeof( struct slap_csn_entry ));
152         ldap_pvt_thread_mutex_lock( op->o_bd->be_pcl_mutexp );
153
154         pending->ce_csn = ber_dupbv( NULL, csn );
155         pending->ce_connid = op->o_connid;
156         pending->ce_opid = op->o_opid;
157         pending->ce_state = SLAP_CSN_PENDING;
158         LDAP_TAILQ_INSERT_TAIL( op->o_bd->be_pending_csn_list,
159                 pending, ce_csn_link );
160         ldap_pvt_thread_mutex_unlock( op->o_bd->be_pcl_mutexp );
161 }
162
163 int
164 slap_get_csn(
165         Operation *op,
166         char *csnbuf,
167         int     len,
168         struct berval *csn,
169         int manage_ctxcsn )
170 {
171         if ( csn == NULL ) return LDAP_OTHER;
172
173         csn->bv_len = lutil_csnstr( csnbuf, len, 0, 0 );
174         csn->bv_val = csnbuf;
175
176         if ( manage_ctxcsn )
177                 slap_queue_csn( op, csn );
178
179         return LDAP_SUCCESS;
180 }