3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1996 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
19 * ri.c - routines used to manipulate Ri structures. An Ri (Replica
20 * information) struct contains all information about one replica
21 * instance. The Ri struct is defined in slurp.h
29 #include <ac/stdlib.h>
30 #include <ac/string.h>
31 #include <ac/signal.h>
37 /* Forward references */
38 static int ismine LDAP_P(( Ri *, Re * ));
39 static int isnew LDAP_P(( Ri *, Re * ));
43 * Process any unhandled replication entries in the queue.
51 Re *re = NULL, *new_re = NULL;
55 (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
56 (void) SIGNAL( SIGPIPE, SIG_IGN );
59 LDAP_LOG (( "ri", LDAP_LEVEL_ERR, "Ri_process: "
60 "Error: ri == NULL!\n" ));
62 Debug( LDAP_DEBUG_ANY, "Error: Ri_process: ri == NULL!\n", 0, 0, 0 );
68 * Startup code. See if there's any work to do. If not, wait on the
69 * rq->rq_more condition variable.
72 while ( !sglob->slurpd_shutdown &&
73 (( re = rq->rq_gethead( rq )) == NULL )) {
75 if ( sglob->one_shot_mode ) {
76 /* give up if in one shot mode */
80 /* wait on condition variable */
81 ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
85 * When we get here, there's work in the queue, and we have the
86 * queue locked. re should be pointing to the head of the queue.
89 while ( !sglob->slurpd_shutdown ) {
91 if ( !ismine( ri, re )) {
92 /* The Re doesn't list my host:port */
94 LDAP_LOG (( "ri", LDAP_LEVEL_DETAIL1, "Ri_process: "
95 "Replica %s:%d, skip repl record for %s (not mine)\n",
96 ri->ri_hostname, ri->ri_port, re->re_dn ));
98 Debug( LDAP_DEBUG_TRACE,
99 "Replica %s:%d, skip repl record for %s (not mine)\n",
100 ri->ri_hostname, ri->ri_port, re->re_dn );
102 } else if ( !isnew( ri, re )) {
103 /* This Re is older than my saved status information */
105 LDAP_LOG (( "ri", LDAP_LEVEL_DETAIL1, "Ri_process: "
106 "Replica %s:%d, skip repl record for %s (old)\n",
107 ri->ri_hostname, ri->ri_port, re->re_dn ));
109 Debug( LDAP_DEBUG_TRACE,
110 "Replica %s:%d, skip repl record for %s (old)\n",
111 ri->ri_hostname, ri->ri_port, re->re_dn );
114 rc = do_ldap( ri, re, &errmsg );
116 case DO_LDAP_ERR_RETRYABLE:
117 ldap_pvt_thread_sleep( RETRY_SLEEP_TIME );
119 LDAP_LOG (( "ri", LDAP_LEVEL_DETAIL1, "Ri_process: "
120 "Retrying operation for DN %s on replica %s:%d\n",
121 re->re_dn, ri->ri_hostname, ri->ri_port ));
123 Debug( LDAP_DEBUG_ANY,
124 "Retrying operation for DN %s on replica %s:%d\n",
125 re->re_dn, ri->ri_hostname, ri->ri_port );
129 case DO_LDAP_ERR_FATAL: {
130 /* Non-retryable error. Write rejection log. */
132 ldap_get_option(ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &ld_errno);
133 write_reject( ri, re, ld_errno, errmsg );
134 /* Update status ... */
135 (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
136 /* ... and write to disk */
137 (void) sglob->st->st_write( sglob->st );
140 /* LDAP op completed ok - update status... */
141 (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
142 /* ... and write to disk */
143 (void) sglob->st->st_write( sglob->st );
149 LDAP_LOG (( "ri", LDAP_LEVEL_ERR, "Ri_process: "
150 "Error: re is null in Ri_process\n" ));
152 Debug( LDAP_DEBUG_ANY, "Error: re is null in Ri_process\n",
157 while ( !sglob->slurpd_shutdown &&
158 ((new_re = re->re_getnext( re )) == NULL )) {
159 if ( sglob->one_shot_mode ) {
163 /* No work - wait on condition variable */
164 ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
166 re->re_decrefcnt( re );
169 if ( sglob->slurpd_shutdown ) {
178 * Wake a replication thread which may be sleeping.
179 * Send it a LDAP_SIGUSR1.
189 ldap_pvt_thread_kill( ri->ri_tid, LDAP_SIGUSR1 );
195 * Allocate and initialize an Ri struct.
202 (*ri) = ( Ri * ) calloc( 1, sizeof( Ri ));
207 /* Initialize member functions */
208 (*ri)->ri_process = Ri_process;
209 (*ri)->ri_wake = Ri_wake;
211 /* Initialize private data */
212 (*ri)->ri_hostname = NULL;
213 (*ri)->ri_ldp = NULL;
214 (*ri)->ri_bind_dn = NULL;
215 (*ri)->ri_password = NULL;
216 (*ri)->ri_authcId = NULL;
217 (*ri)->ri_srvtab = NULL;
218 (*ri)->ri_curr = NULL;
227 * Return 1 if the hostname and port in re match the hostname and port
228 * in ri, otherwise return zero.
239 if ( ri == NULL || re == NULL || ri->ri_hostname == NULL ||
240 re->re_replicas == NULL ) {
243 rh = re->re_replicas;
244 for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
245 if ( !strcmp( rh[ i ].rh_hostname, ri->ri_hostname) &&
246 rh[ i ].rh_port == ri->ri_port ) {
257 * Return 1 if the Re's timestamp/seq combination are greater than the
258 * timestamp and seq in the Ri's ri_stel member. In other words, if we
259 * find replication entries in the log which we've already processed,
260 * don't process them. If the re is "old," return 0.
261 * No check for NULL pointers is done.
272 /* Lock the St struct to avoid a race */
273 sglob->st->st_lock( sglob->st );
274 x = re->re_timestamp - ri->ri_stel->last;
276 /* re timestamp is newer */
278 } else if ( x < 0 ) {
281 /* timestamps were equal */
282 if ( re->re_seq > ri->ri_stel->seq ) {
283 /* re seq is newer */
289 sglob->st->st_unlock( sglob->st );