2 * Copyright (c) 1996 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
14 * ri.c - routines used to manipulate Ri structures. An Ri (Replica
15 * information) struct contains all information about one replica
16 * instance. The Ri struct is defined in slurp.h
24 #include <ac/signal.h>
30 /* Forward references */
31 static int ismine LDAP_P(( Ri *, Re * ));
32 static int isnew LDAP_P(( Ri *, Re * ));
36 * Process any unhandled replication entries in the queue.
48 #ifdef HAVE_LINUX_THREADS
49 (void) SIGNAL( SIGSTKFLT, do_nothing );
51 (void) SIGNAL( SIGUSR1, do_nothing );
53 (void) SIGNAL( SIGPIPE, SIG_IGN );
55 Debug( LDAP_DEBUG_ANY, "Error: Ri_process: ri == NULL!\n", 0, 0, 0 );
60 * Startup code. See if there's any work to do. If not, wait on the
61 * rq->rq_more condition variable.
64 while ( !sglob->slurpd_shutdown &&
65 (( re = rq->rq_gethead( rq )) == NULL )) {
66 /* No work - wait on condition variable */
67 pthread_cond_wait( &rq->rq_more, &rq->rq_mutex );
71 * When we get here, there's work in the queue, and we have the
72 * queue locked. re should be pointing to the head of the queue.
75 while ( !sglob->slurpd_shutdown ) {
77 if ( !ismine( ri, re )) {
78 /* The Re doesn't list my host:port */
79 Debug( LDAP_DEBUG_TRACE,
80 "Replica %s:%d, skip repl record for %s (not mine)\n",
81 ri->ri_hostname, ri->ri_port, re->re_dn );
82 } else if ( !isnew( ri, re )) {
83 /* This Re is older than my saved status information */
84 Debug( LDAP_DEBUG_TRACE,
85 "Replica %s:%d, skip repl record for %s (old)\n",
86 ri->ri_hostname, ri->ri_port, re->re_dn );
88 rc = do_ldap( ri, re, &errmsg );
90 case DO_LDAP_ERR_RETRYABLE:
91 tsleep( RETRY_SLEEP_TIME );
92 Debug( LDAP_DEBUG_ANY,
93 "Retrying operation for DN %s on replica %s:%d\n",
94 re->re_dn, ri->ri_hostname, ri->ri_port );
97 case DO_LDAP_ERR_FATAL: {
98 /* Non-retryable error. Write rejection log. */
100 ldap_get_option(ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &ld_errno);
101 write_reject( ri, re, ld_errno, errmsg );
102 /* Update status ... */
103 (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
104 /* ... and write to disk */
105 (void) sglob->st->st_write( sglob->st );
108 /* LDAP op completed ok - update status... */
109 (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
110 /* ... and write to disk */
111 (void) sglob->st->st_write( sglob->st );
116 Debug( LDAP_DEBUG_ANY, "Error: re is null in Ri_process\n",
120 while ( !sglob->slurpd_shutdown &&
121 ((new_re = re->re_getnext( re )) == NULL )) {
122 if ( sglob->one_shot_mode ) {
126 /* No work - wait on condition variable */
127 pthread_cond_wait( &rq->rq_more, &rq->rq_mutex );
129 re->re_decrefcnt( re );
132 if ( sglob->slurpd_shutdown ) {
141 * Wake a replication thread which may be sleeping.
142 * Send it a SIG(STKFLT|USR1).
152 #ifdef HAVE_LINUX_THREADS
153 pthread_kill( ri->ri_tid, SIGSTKFLT );
154 (void) SIGNAL( SIGSTKFLT, do_nothing );
156 pthread_kill( ri->ri_tid, SIGUSR1 );
157 (void) SIGNAL( SIGUSR1, do_nothing );
164 * Allocate and initialize an Ri struct.
171 (*ri) = ( Ri * ) malloc( sizeof( Ri ));
176 /* Initialize member functions */
177 (*ri)->ri_process = Ri_process;
178 (*ri)->ri_wake = Ri_wake;
180 /* Initialize private data */
181 (*ri)->ri_hostname = NULL;
183 (*ri)->ri_ldp = NULL;
184 (*ri)->ri_bind_method = 0;
185 (*ri)->ri_bind_dn = NULL;
186 (*ri)->ri_password = NULL;
187 (*ri)->ri_principal = NULL;
188 (*ri)->ri_srvtab = NULL;
189 (*ri)->ri_curr = NULL;
198 * Return 1 if the hostname and port in re match the hostname and port
199 * in ri, otherwise return zero.
210 if ( ri == NULL || re == NULL || ri->ri_hostname == NULL ||
211 re->re_replicas == NULL ) {
214 rh = re->re_replicas;
215 for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
216 if ( !strcmp( rh[ i ].rh_hostname, ri->ri_hostname) &&
217 rh[ i ].rh_port == ri->ri_port ) {
228 * Return 1 if the Re's timestamp/seq combination are greater than the
229 * timestamp and seq in the Ri's ri_stel member. In other words, if we
230 * find replication entries in the log which we've already processed,
231 * don't process them. If the re is "old," return 0.
232 * No check for NULL pointers is done.
243 /* Lock the St struct to avoid a race */
244 sglob->st->st_lock( sglob->st );
245 x = strcmp( re->re_timestamp, ri->ri_stel->last );
247 /* re timestamp is newer */
249 } else if ( x < 0 ) {
252 /* timestamps were equal */
253 if ( re->re_seq > ri->ri_stel->seq ) {
254 /* re seq is newer */
260 sglob->st->st_unlock( sglob->st );