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