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.
55 (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
56 (void) SIGNAL( SIGPIPE, SIG_IGN );
58 Debug( LDAP_DEBUG_ANY, "Error: Ri_process: ri == NULL!\n", 0, 0, 0 );
63 * Startup code. See if there's any work to do. If not, wait on the
64 * rq->rq_more condition variable.
67 while ( !sglob->slurpd_shutdown &&
68 (( re = rq->rq_gethead( rq )) == NULL )) {
70 if ( sglob->one_shot_mode ) {
71 /* give up if in one shot mode */
75 /* wait on condition variable */
76 ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
80 * When we get here, there's work in the queue, and we have the
81 * queue locked. re should be pointing to the head of the queue.
84 while ( !sglob->slurpd_shutdown ) {
86 if ( !ismine( ri, re )) {
87 /* The Re doesn't list my host:port */
88 Debug( LDAP_DEBUG_TRACE,
89 "Replica %s:%d, skip repl record for %s (not mine)\n",
90 ri->ri_hostname, ri->ri_port, re->re_dn );
91 } else if ( !isnew( ri, re )) {
92 /* This Re is older than my saved status information */
93 Debug( LDAP_DEBUG_TRACE,
94 "Replica %s:%d, skip repl record for %s (old)\n",
95 ri->ri_hostname, ri->ri_port, re->re_dn );
97 rc = do_ldap( ri, re, &errmsg );
99 case DO_LDAP_ERR_RETRYABLE:
100 ldap_pvt_thread_sleep( RETRY_SLEEP_TIME );
101 Debug( LDAP_DEBUG_ANY,
102 "Retrying operation for DN %s on replica %s:%d\n",
103 re->re_dn, ri->ri_hostname, ri->ri_port );
106 case DO_LDAP_ERR_FATAL: {
107 /* Non-retryable error. Write rejection log. */
109 ldap_get_option(ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &ld_errno);
110 write_reject( ri, re, ld_errno, errmsg );
111 /* Update status ... */
112 (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
113 /* ... and write to disk */
114 (void) sglob->st->st_write( sglob->st );
117 /* LDAP op completed ok - update status... */
118 (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
119 /* ... and write to disk */
120 (void) sglob->st->st_write( sglob->st );
125 Debug( LDAP_DEBUG_ANY, "Error: re is null in Ri_process\n",
129 while ( !sglob->slurpd_shutdown &&
130 ((new_re = re->re_getnext( re )) == NULL )) {
131 if ( sglob->one_shot_mode ) {
135 /* No work - wait on condition variable */
136 ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
138 re->re_decrefcnt( re );
141 if ( sglob->slurpd_shutdown ) {
150 * Wake a replication thread which may be sleeping.
151 * Send it a LDAP_SIGUSR1.
161 ldap_pvt_thread_kill( ri->ri_tid, LDAP_SIGUSR1 );
167 * Allocate and initialize an Ri struct.
174 (*ri) = ( Ri * ) calloc( 1, sizeof( Ri ));
179 /* Initialize member functions */
180 (*ri)->ri_process = Ri_process;
181 (*ri)->ri_wake = Ri_wake;
183 /* Initialize private data */
184 (*ri)->ri_hostname = NULL;
185 (*ri)->ri_ldp = NULL;
186 (*ri)->ri_bind_dn = NULL;
187 (*ri)->ri_password = NULL;
188 (*ri)->ri_authcId = NULL;
189 (*ri)->ri_srvtab = NULL;
190 (*ri)->ri_curr = NULL;
199 * Return 1 if the hostname and port in re match the hostname and port
200 * in ri, otherwise return zero.
211 if ( ri == NULL || re == NULL || ri->ri_hostname == NULL ||
212 re->re_replicas == NULL ) {
215 rh = re->re_replicas;
216 for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
217 if ( !strcmp( rh[ i ].rh_hostname, ri->ri_hostname) &&
218 rh[ i ].rh_port == ri->ri_port ) {
229 * Return 1 if the Re's timestamp/seq combination are greater than the
230 * timestamp and seq in the Ri's ri_stel member. In other words, if we
231 * find replication entries in the log which we've already processed,
232 * don't process them. If the re is "old," return 0.
233 * No check for NULL pointers is done.
244 /* Lock the St struct to avoid a race */
245 sglob->st->st_lock( sglob->st );
246 x = re->re_timestamp - ri->ri_stel->last;
248 /* re timestamp is newer */
250 } else if ( x < 0 ) {
253 /* timestamps were equal */
254 if ( re->re_seq > ri->ri_stel->seq ) {
255 /* re seq is newer */
261 sglob->st->st_unlock( sglob->st );