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 );
57 (void) SIGNAL( SIGPIPE, SIG_IGN );
61 LDAP_LOG ( SLURPD, ERR, "Ri_process: "
62 "Error: ri == NULL!\n", 0, 0, 0 );
64 Debug( LDAP_DEBUG_ANY, "Error: Ri_process: ri == NULL!\n", 0, 0, 0 );
70 * Startup code. See if there's any work to do. If not, wait on the
71 * rq->rq_more condition variable.
74 while ( !sglob->slurpd_shutdown &&
75 (( re = rq->rq_gethead( rq )) == NULL )) {
77 if ( sglob->one_shot_mode ) {
78 /* give up if in one shot mode */
82 /* wait on condition variable */
83 ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
87 * When we get here, there's work in the queue, and we have the
88 * queue locked. re should be pointing to the head of the queue.
91 while ( !sglob->slurpd_shutdown ) {
93 if ( !ismine( ri, re )) {
94 /* The Re doesn't list my host:port */
96 LDAP_LOG ( SLURPD, DETAIL1, "Ri_process: "
97 "Replica %s:%d, skip repl record for %s (not mine)\n",
98 ri->ri_hostname, ri->ri_port, re->re_dn );
100 Debug( LDAP_DEBUG_TRACE,
101 "Replica %s:%d, skip repl record for %s (not mine)\n",
102 ri->ri_hostname, ri->ri_port, re->re_dn );
104 } else if ( !isnew( ri, re )) {
105 /* This Re is older than my saved status information */
107 LDAP_LOG ( SLURPD, DETAIL1, "Ri_process: "
108 "Replica %s:%d, skip repl record for %s (old)\n",
109 ri->ri_hostname, ri->ri_port, re->re_dn );
111 Debug( LDAP_DEBUG_TRACE,
112 "Replica %s:%d, skip repl record for %s (old)\n",
113 ri->ri_hostname, ri->ri_port, re->re_dn );
116 rc = do_ldap( ri, re, &errmsg );
118 case DO_LDAP_ERR_RETRYABLE:
119 ldap_pvt_thread_sleep( RETRY_SLEEP_TIME );
121 LDAP_LOG ( SLURPD, DETAIL1, "Ri_process: "
122 "Retrying operation for DN %s on replica %s:%d\n",
123 re->re_dn, ri->ri_hostname, ri->ri_port );
125 Debug( LDAP_DEBUG_ANY,
126 "Retrying operation for DN %s on replica %s:%d\n",
127 re->re_dn, ri->ri_hostname, ri->ri_port );
131 case DO_LDAP_ERR_FATAL: {
132 /* Non-retryable error. Write rejection log. */
134 ldap_get_option(ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &ld_errno);
135 write_reject( ri, re, ld_errno, errmsg );
136 /* Update status ... */
137 (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
138 /* ... and write to disk */
139 (void) sglob->st->st_write( sglob->st );
142 /* LDAP op completed ok - update status... */
143 (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
144 /* ... and write to disk */
145 (void) sglob->st->st_write( sglob->st );
151 LDAP_LOG ( SLURPD, ERR, "Ri_process: "
152 "Error: re is null in Ri_process\n", 0, 0, 0 );
154 Debug( LDAP_DEBUG_ANY, "Error: re is null in Ri_process\n",
159 while ( !sglob->slurpd_shutdown &&
160 ((new_re = re->re_getnext( re )) == NULL )) {
161 if ( sglob->one_shot_mode ) {
165 /* No work - wait on condition variable */
166 ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
168 re->re_decrefcnt( re );
171 if ( sglob->slurpd_shutdown ) {
180 * Wake a replication thread which may be sleeping.
181 * Send it a LDAP_SIGUSR1.
191 ldap_pvt_thread_kill( ri->ri_tid, LDAP_SIGUSR1 );
197 * Allocate and initialize an Ri struct.
204 (*ri) = ( Ri * ) calloc( 1, sizeof( Ri ));
209 /* Initialize member functions */
210 (*ri)->ri_process = Ri_process;
211 (*ri)->ri_wake = Ri_wake;
213 /* Initialize private data */
214 (*ri)->ri_hostname = NULL;
215 (*ri)->ri_ldp = NULL;
216 (*ri)->ri_bind_dn = NULL;
217 (*ri)->ri_password = NULL;
218 (*ri)->ri_authcId = NULL;
219 (*ri)->ri_srvtab = NULL;
220 (*ri)->ri_curr = NULL;
229 * Return 1 if the hostname and port in re match the hostname and port
230 * in ri, otherwise return zero.
241 if ( ri == NULL || re == NULL || ri->ri_hostname == NULL ||
242 re->re_replicas == NULL ) {
245 rh = re->re_replicas;
246 for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
247 if ( !strcmp( rh[ i ].rh_hostname, ri->ri_hostname) &&
248 rh[ i ].rh_port == ri->ri_port ) {
259 * Return 1 if the Re's timestamp/seq combination are greater than the
260 * timestamp and seq in the Ri's ri_stel member. In other words, if we
261 * find replication entries in the log which we've already processed,
262 * don't process them. If the re is "old," return 0.
263 * No check for NULL pointers is done.
274 /* Lock the St struct to avoid a race */
275 sglob->st->st_lock( sglob->st );
276 x = re->re_timestamp - ri->ri_stel->last;
278 /* re timestamp is newer */
280 } else if ( x < 0 ) {
283 /* timestamps were equal */
284 if ( re->re_seq > ri->ri_stel->seq ) {
285 /* re seq is newer */
291 sglob->st->st_unlock( sglob->st );