]> git.sur5r.net Git - openldap/blob - servers/slapd/ctxcsn.c
wrap gmtime for reentrancy (ITS#6262)
[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-2009 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 int slap_serverID;
30
31 /* maxcsn->bv_val must point to a char buf[LDAP_PVT_CSNSTR_BUFSIZE] */
32 void
33 slap_get_commit_csn(
34         Operation *op,
35         struct berval *maxcsn,
36         int *foundit
37 )
38 {
39         struct slap_csn_entry *csne, *committed_csne = NULL;
40         BackendDB *be = op->o_bd->bd_self;
41         int sid = -1;
42
43         if ( maxcsn ) {
44                 assert( maxcsn->bv_val != NULL );
45                 assert( maxcsn->bv_len >= LDAP_PVT_CSNSTR_BUFSIZE );
46         }
47         if ( foundit ) {
48                 *foundit = 0;
49         }
50
51         ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex );
52
53         if ( !BER_BVISEMPTY( &op->o_csn )) {
54                 sid = slap_parse_csn_sid( &op->o_csn );
55         }
56
57         LDAP_TAILQ_FOREACH( csne, be->be_pending_csn_list, ce_csn_link ) {
58                 if ( csne->ce_opid == op->o_opid && csne->ce_connid == op->o_connid ) {
59                         csne->ce_state = SLAP_CSN_COMMIT;
60                         if ( foundit ) *foundit = 1;
61                         break;
62                 }
63         }
64
65         LDAP_TAILQ_FOREACH( csne, be->be_pending_csn_list, ce_csn_link ) {
66                 if ( sid != -1 && sid == csne->ce_sid ) {
67                         if ( csne->ce_state == SLAP_CSN_COMMIT ) committed_csne = csne;
68                         if ( csne->ce_state == SLAP_CSN_PENDING ) break;
69                 }
70         }
71
72         if ( maxcsn ) {
73                 if ( committed_csne ) {
74                         if ( committed_csne->ce_csn.bv_len < maxcsn->bv_len )
75                                 maxcsn->bv_len = committed_csne->ce_csn.bv_len;
76                         AC_MEMCPY( maxcsn->bv_val, committed_csne->ce_csn.bv_val,
77                                 maxcsn->bv_len+1 );
78                 } else {
79                         maxcsn->bv_len = 0;
80                         maxcsn->bv_val[0] = 0;
81                 }
82         }
83         ldap_pvt_thread_mutex_unlock( &be->be_pcl_mutex );
84 }
85
86 void
87 slap_rewind_commit_csn( Operation *op )
88 {
89         struct slap_csn_entry *csne;
90         BackendDB *be = op->o_bd->bd_self;
91
92         ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex );
93
94         LDAP_TAILQ_FOREACH( csne, be->be_pending_csn_list, ce_csn_link ) {
95                 if ( csne->ce_opid == op->o_opid && csne->ce_connid == op->o_connid ) {
96                         csne->ce_state = SLAP_CSN_PENDING;
97                         break;
98                 }
99         }
100
101         ldap_pvt_thread_mutex_unlock( &be->be_pcl_mutex );
102 }
103
104 void
105 slap_graduate_commit_csn( Operation *op )
106 {
107         struct slap_csn_entry *csne;
108         BackendDB *be;
109
110         if ( op == NULL ) return;
111         if ( op->o_bd == NULL ) return;
112         be = op->o_bd->bd_self;
113
114         ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex );
115
116         LDAP_TAILQ_FOREACH( csne, be->be_pending_csn_list, ce_csn_link ) {
117                 if ( csne->ce_opid == op->o_opid && csne->ce_connid == op->o_connid ) {
118                         LDAP_TAILQ_REMOVE( be->be_pending_csn_list,
119                                 csne, ce_csn_link );
120                         Debug( LDAP_DEBUG_SYNC, "slap_graduate_commit_csn: removing %p %s\n",
121                                 csne->ce_csn.bv_val, csne->ce_csn.bv_val, 0 );
122                         if ( op->o_csn.bv_val == csne->ce_csn.bv_val ) {
123                                 BER_BVZERO( &op->o_csn );
124                         }
125                         ch_free( csne->ce_csn.bv_val );
126                         ch_free( csne );
127                         break;
128                 }
129         }
130
131         ldap_pvt_thread_mutex_unlock( &be->be_pcl_mutex );
132
133         return;
134 }
135
136 void
137 slap_queue_csn(
138         Operation *op,
139         struct berval *csn )
140 {
141         struct slap_csn_entry *pending;
142         BackendDB *be = op->o_bd->bd_self;
143
144         pending = (struct slap_csn_entry *) ch_calloc( 1,
145                         sizeof( struct slap_csn_entry ));
146
147         Debug( LDAP_DEBUG_SYNC, "slap_queue_csn: queing %p %s\n", csn->bv_val, csn->bv_val, 0 );
148
149         ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex );
150
151         ber_dupbv( &pending->ce_csn, csn );
152         ber_bvreplace_x( &op->o_csn, &pending->ce_csn, op->o_tmpmemctx );
153         pending->ce_sid = slap_parse_csn_sid( csn );
154         pending->ce_connid = op->o_connid;
155         pending->ce_opid = op->o_opid;
156         pending->ce_state = SLAP_CSN_PENDING;
157         LDAP_TAILQ_INSERT_TAIL( be->be_pending_csn_list,
158                 pending, ce_csn_link );
159         ldap_pvt_thread_mutex_unlock( &be->be_pcl_mutex );
160 }
161
162 int
163 slap_get_csn(
164         Operation *op,
165         struct berval *csn,
166         int manage_ctxcsn )
167 {
168         if ( csn == NULL ) return LDAP_OTHER;
169
170         csn->bv_len = ldap_pvt_csnstr( csn->bv_val, csn->bv_len, slap_serverID, 0 );
171         if ( manage_ctxcsn )
172                 slap_queue_csn( op, csn );
173
174         return LDAP_SUCCESS;
175 }