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