3 * Copyright (c) 1996 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
15 * ri.c - routines used to manipulate Ri structures. An Ri (Replica
16 * information) struct contains all information about one replica
17 * instance. The Ri struct is defined in slurp.h
25 #include <ac/stdlib.h>
26 #include <ac/string.h>
27 #include <ac/signal.h>
33 /* Forward references */
34 static int ismine LDAP_P(( Ri *, Re * ));
35 static int isnew LDAP_P(( Ri *, Re * ));
39 * Process any unhandled replication entries in the queue.
51 (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
52 (void) SIGNAL( SIGPIPE, SIG_IGN );
54 Debug( LDAP_DEBUG_ANY, "Error: Ri_process: ri == NULL!\n", 0, 0, 0 );
59 * Startup code. See if there's any work to do. If not, wait on the
60 * rq->rq_more condition variable.
63 while ( !sglob->slurpd_shutdown &&
64 (( re = rq->rq_gethead( rq )) == NULL )) {
65 /* No work - wait on condition variable */
66 ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
70 * When we get here, there's work in the queue, and we have the
71 * queue locked. re should be pointing to the head of the queue.
74 while ( !sglob->slurpd_shutdown ) {
76 if ( !ismine( ri, re )) {
77 /* The Re doesn't list my host:port */
78 Debug( LDAP_DEBUG_TRACE,
79 "Replica %s:%d, skip repl record for %s (not mine)\n",
80 ri->ri_hostname, ri->ri_port, re->re_dn );
81 } else if ( !isnew( ri, re )) {
82 /* This Re is older than my saved status information */
83 Debug( LDAP_DEBUG_TRACE,
84 "Replica %s:%d, skip repl record for %s (old)\n",
85 ri->ri_hostname, ri->ri_port, re->re_dn );
87 rc = do_ldap( ri, re, &errmsg );
89 case DO_LDAP_ERR_RETRYABLE:
90 ldap_pvt_thread_sleep( RETRY_SLEEP_TIME );
91 Debug( LDAP_DEBUG_ANY,
92 "Retrying operation for DN %s on replica %s:%d\n",
93 re->re_dn, ri->ri_hostname, ri->ri_port );
96 case DO_LDAP_ERR_FATAL: {
97 /* Non-retryable error. Write rejection log. */
99 ldap_get_option(ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &ld_errno);
100 write_reject( ri, re, ld_errno, errmsg );
101 /* Update status ... */
102 (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
103 /* ... and write to disk */
104 (void) sglob->st->st_write( sglob->st );
107 /* LDAP op completed ok - update status... */
108 (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
109 /* ... and write to disk */
110 (void) sglob->st->st_write( sglob->st );
115 Debug( LDAP_DEBUG_ANY, "Error: re is null in Ri_process\n",
119 while ( !sglob->slurpd_shutdown &&
120 ((new_re = re->re_getnext( re )) == NULL )) {
121 if ( sglob->one_shot_mode ) {
125 /* No work - wait on condition variable */
126 ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
128 re->re_decrefcnt( re );
131 if ( sglob->slurpd_shutdown ) {
140 * Wake a replication thread which may be sleeping.
141 * Send it a LDAP_SIGUSR1.
151 ldap_pvt_thread_kill( ri->ri_tid, LDAP_SIGUSR1 );
157 * Allocate and initialize an Ri struct.
164 (*ri) = ( Ri * ) calloc( 1, sizeof( Ri ));
169 /* Initialize member functions */
170 (*ri)->ri_process = Ri_process;
171 (*ri)->ri_wake = Ri_wake;
173 /* Initialize private data */
174 (*ri)->ri_hostname = NULL;
175 (*ri)->ri_ldp = NULL;
176 (*ri)->ri_bind_dn = NULL;
177 (*ri)->ri_password = NULL;
178 (*ri)->ri_authcId = NULL;
179 (*ri)->ri_srvtab = NULL;
180 (*ri)->ri_curr = NULL;
189 * Return 1 if the hostname and port in re match the hostname and port
190 * in ri, otherwise return zero.
201 if ( ri == NULL || re == NULL || ri->ri_hostname == NULL ||
202 re->re_replicas == NULL ) {
205 rh = re->re_replicas;
206 for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
207 if ( !strcmp( rh[ i ].rh_hostname, ri->ri_hostname) &&
208 rh[ i ].rh_port == ri->ri_port ) {
219 * Return 1 if the Re's timestamp/seq combination are greater than the
220 * timestamp and seq in the Ri's ri_stel member. In other words, if we
221 * find replication entries in the log which we've already processed,
222 * don't process them. If the re is "old," return 0.
223 * No check for NULL pointers is done.
234 /* Lock the St struct to avoid a race */
235 sglob->st->st_lock( sglob->st );
236 x = strcmp( re->re_timestamp, ri->ri_stel->last );
238 /* re timestamp is newer */
240 } else if ( x < 0 ) {
243 /* timestamps were equal */
244 if ( re->re_seq > ri->ri_stel->seq ) {
245 /* re seq is newer */
251 sglob->st->st_unlock( sglob->st );