2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1996 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
26 * This work was originally developed by the University of Michigan
27 * (as part of U-MICH LDAP).
31 * ri.c - routines used to manipulate Ri structures. An Ri (Replica
32 * information) struct contains all information about one replica
33 * instance. The Ri struct is defined in slurp.h
41 #include <ac/stdlib.h>
42 #include <ac/string.h>
43 #include <ac/signal.h>
49 /* Forward references */
50 static int ismine LDAP_P(( Ri *, Re * ));
51 static int isnew LDAP_P(( Ri *, Re * ));
55 * Process any unhandled replication entries in the queue.
63 Re *re = NULL, *new_re = NULL;
68 (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
70 (void) SIGNAL( SIGPIPE, SIG_IGN );
73 Debug( LDAP_DEBUG_ANY, "Error: Ri_process: ri == NULL!\n", 0, 0, 0 );
78 * Startup code. See if there's any work to do. If not, wait on the
79 * rq->rq_more condition variable.
82 while ( !sglob->slurpd_shutdown &&
83 (( re = rq->rq_gethead( rq )) == NULL )) {
85 if ( sglob->one_shot_mode ) {
86 /* give up if in one shot mode */
90 /* wait on condition variable */
91 ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
95 * When we get here, there's work in the queue, and we have the
96 * queue locked. re should be pointing to the head of the queue.
99 while ( !sglob->slurpd_shutdown ) {
101 if ( !ismine( ri, re )) {
102 /* The Re doesn't list my host:port */
103 Debug( LDAP_DEBUG_TRACE,
104 "Replica %s:%d, skip repl record for %s (not mine)\n",
105 ri->ri_hostname, ri->ri_port, re->re_dn );
106 } else if ( !isnew( ri, re )) {
107 /* This Re is older than my saved status information */
108 Debug( LDAP_DEBUG_TRACE,
109 "Replica %s:%d, skip repl record for %s (old)\n",
110 ri->ri_hostname, ri->ri_port, re->re_dn );
112 rc = do_ldap( ri, re, &errmsg, &errfree );
114 case DO_LDAP_ERR_RETRYABLE:
115 ldap_pvt_thread_sleep( RETRY_SLEEP_TIME );
116 Debug( LDAP_DEBUG_ANY,
117 "Retrying operation for DN %s on replica %s:%d\n",
118 re->re_dn, ri->ri_hostname, ri->ri_port );
121 case DO_LDAP_ERR_FATAL: {
122 /* Non-retryable error. Write rejection log. */
124 ldap_get_option(ri->ri_ldp, LDAP_OPT_RESULT_CODE, &ld_errno);
125 write_reject( ri, re, ld_errno, errmsg );
126 /* Update status ... */
127 (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
128 /* ... and write to disk */
129 (void) sglob->st->st_write( sglob->st );
132 /* LDAP op completed ok - update status... */
133 (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
134 /* ... and write to disk */
135 (void) sglob->st->st_write( sglob->st );
138 if ( errfree && errmsg ) {
143 Debug( LDAP_DEBUG_ANY, "Error: re is null in Ri_process\n",
147 while ( !sglob->slurpd_shutdown &&
148 ((new_re = re->re_getnext( re )) == NULL )) {
149 if ( sglob->one_shot_mode ) {
153 /* No work - wait on condition variable */
154 ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
156 re->re_decrefcnt( re );
159 if ( sglob->slurpd_shutdown ) {
161 ldap_unbind_ext( ri->ri_ldp, NULL, NULL );
172 * Wake a replication thread which may be sleeping.
173 * Send it a LDAP_SIGUSR1.
183 ldap_pvt_thread_kill( ri->ri_tid, LDAP_SIGUSR1 );
189 * Allocate and initialize an Ri struct.
196 (*ri) = ( Ri * ) calloc( 1, sizeof( Ri ));
201 /* Initialize member functions */
202 (*ri)->ri_process = Ri_process;
203 (*ri)->ri_wake = Ri_wake;
205 /* Initialize private data */
206 (*ri)->ri_hostname = NULL;
207 (*ri)->ri_uri = NULL;
208 (*ri)->ri_ldp = NULL;
209 (*ri)->ri_bind_dn = NULL;
210 (*ri)->ri_password = NULL;
211 (*ri)->ri_authcId = NULL;
212 (*ri)->ri_srvtab = NULL;
213 (*ri)->ri_curr = NULL;
222 * Return 1 if the hostname and port in re match the hostname and port
223 * in ri, otherwise return zero.
234 if ( ri == NULL || re == NULL || ri->ri_hostname == NULL ||
235 re->re_replicas == NULL ) {
238 rh = re->re_replicas;
239 for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
240 if ( !strcmp( rh[ i ].rh_hostname, ri->ri_hostname) &&
241 rh[ i ].rh_port == ri->ri_port ) {
252 * Return 1 if the Re's timestamp/seq combination are greater than the
253 * timestamp and seq in the Ri's ri_stel member. In other words, if we
254 * find replication entries in the log which we've already processed,
255 * don't process them. If the re is "old," return 0.
256 * No check for NULL pointers is done.
267 /* Lock the St struct to avoid a race */
268 sglob->st->st_lock( sglob->st );
269 x = re->re_timestamp - ri->ri_stel->last;
271 /* re timestamp is newer */
273 } else if ( x < 0 ) {
276 /* timestamps were equal */
277 if ( re->re_seq > ri->ri_stel->seq ) {
278 /* re seq is newer */
284 sglob->st->st_unlock( sglob->st );