2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 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 );
74 LDAP_LOG ( SLURPD, ERR, "Ri_process: "
75 "Error: ri == NULL!\n", 0, 0, 0 );
77 Debug( LDAP_DEBUG_ANY, "Error: Ri_process: ri == NULL!\n", 0, 0, 0 );
83 * Startup code. See if there's any work to do. If not, wait on the
84 * rq->rq_more condition variable.
87 while ( !sglob->slurpd_shutdown &&
88 (( re = rq->rq_gethead( rq )) == NULL )) {
90 if ( sglob->one_shot_mode ) {
91 /* give up if in one shot mode */
95 /* wait on condition variable */
96 ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
100 * When we get here, there's work in the queue, and we have the
101 * queue locked. re should be pointing to the head of the queue.
104 while ( !sglob->slurpd_shutdown ) {
106 if ( !ismine( ri, re )) {
107 /* The Re doesn't list my host:port */
109 LDAP_LOG ( SLURPD, DETAIL1, "Ri_process: "
110 "Replica %s:%d, skip repl record for %s (not mine)\n",
111 ri->ri_hostname, ri->ri_port, re->re_dn );
113 Debug( LDAP_DEBUG_TRACE,
114 "Replica %s:%d, skip repl record for %s (not mine)\n",
115 ri->ri_hostname, ri->ri_port, re->re_dn );
117 } else if ( !isnew( ri, re )) {
118 /* This Re is older than my saved status information */
120 LDAP_LOG ( SLURPD, DETAIL1, "Ri_process: "
121 "Replica %s:%d, skip repl record for %s (old)\n",
122 ri->ri_hostname, ri->ri_port, re->re_dn );
124 Debug( LDAP_DEBUG_TRACE,
125 "Replica %s:%d, skip repl record for %s (old)\n",
126 ri->ri_hostname, ri->ri_port, re->re_dn );
129 rc = do_ldap( ri, re, &errmsg, &errfree );
131 case DO_LDAP_ERR_RETRYABLE:
132 ldap_pvt_thread_sleep( RETRY_SLEEP_TIME );
134 LDAP_LOG ( SLURPD, DETAIL1, "Ri_process: "
135 "Retrying operation for DN %s on replica %s:%d\n",
136 re->re_dn, ri->ri_hostname, ri->ri_port );
138 Debug( LDAP_DEBUG_ANY,
139 "Retrying operation for DN %s on replica %s:%d\n",
140 re->re_dn, ri->ri_hostname, ri->ri_port );
144 case DO_LDAP_ERR_FATAL: {
145 /* Non-retryable error. Write rejection log. */
147 ldap_get_option(ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &ld_errno);
148 write_reject( ri, re, ld_errno, errmsg );
149 /* Update status ... */
150 (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
151 /* ... and write to disk */
152 (void) sglob->st->st_write( sglob->st );
155 /* LDAP op completed ok - update status... */
156 (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
157 /* ... and write to disk */
158 (void) sglob->st->st_write( sglob->st );
161 if ( errfree && errmsg ) {
167 LDAP_LOG ( SLURPD, ERR, "Ri_process: "
168 "Error: re is null in Ri_process\n", 0, 0, 0 );
170 Debug( LDAP_DEBUG_ANY, "Error: re is null in Ri_process\n",
175 while ( !sglob->slurpd_shutdown &&
176 ((new_re = re->re_getnext( re )) == NULL )) {
177 if ( sglob->one_shot_mode ) {
181 /* No work - wait on condition variable */
182 ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
184 re->re_decrefcnt( re );
187 if ( sglob->slurpd_shutdown ) {
189 ldap_unbind( ri->ri_ldp );
200 * Wake a replication thread which may be sleeping.
201 * Send it a LDAP_SIGUSR1.
211 ldap_pvt_thread_kill( ri->ri_tid, LDAP_SIGUSR1 );
217 * Allocate and initialize an Ri struct.
224 (*ri) = ( Ri * ) calloc( 1, sizeof( Ri ));
229 /* Initialize member functions */
230 (*ri)->ri_process = Ri_process;
231 (*ri)->ri_wake = Ri_wake;
233 /* Initialize private data */
234 (*ri)->ri_hostname = NULL;
235 (*ri)->ri_uri = NULL;
236 (*ri)->ri_ldp = NULL;
237 (*ri)->ri_bind_dn = NULL;
238 (*ri)->ri_password = NULL;
239 (*ri)->ri_authcId = NULL;
240 (*ri)->ri_srvtab = NULL;
241 (*ri)->ri_curr = NULL;
250 * Return 1 if the hostname and port in re match the hostname and port
251 * in ri, otherwise return zero.
262 if ( ri == NULL || re == NULL || ri->ri_hostname == NULL ||
263 re->re_replicas == NULL ) {
266 rh = re->re_replicas;
267 for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
268 if ( !strcmp( rh[ i ].rh_hostname, ri->ri_hostname) &&
269 rh[ i ].rh_port == ri->ri_port ) {
280 * Return 1 if the Re's timestamp/seq combination are greater than the
281 * timestamp and seq in the Ri's ri_stel member. In other words, if we
282 * find replication entries in the log which we've already processed,
283 * don't process them. If the re is "old," return 0.
284 * No check for NULL pointers is done.
295 /* Lock the St struct to avoid a race */
296 sglob->st->st_lock( sglob->st );
297 x = re->re_timestamp - ri->ri_stel->last;
299 /* re timestamp is newer */
301 } else if ( x < 0 ) {
304 /* timestamps were equal */
305 if ( re->re_seq > ri->ri_stel->seq ) {
306 /* re seq is newer */
312 sglob->st->st_unlock( sglob->st );