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
23 #include <ac/signal.h>
29 /* External references */
30 extern void write_reject LDAP_P(( Ri *, Re *, int, char * ));
31 extern void do_nothing LDAP_P(());
33 /* Forward references */
34 static int ismine LDAP_P(( Ri *, Re * ));
35 static int isnew LDAP_P(( Ri *, Re * ));
36 void tsleep LDAP_P(( time_t ));
40 * Process any unhandled replication entries in the queue.
53 #ifdef HAVE_LINUX_THREADS
54 (void) SIGNAL( SIGSTKFLT, do_nothing );
56 (void) SIGNAL( SIGUSR1, do_nothing );
58 (void) SIGNAL( SIGPIPE, SIG_IGN );
60 Debug( LDAP_DEBUG_ANY, "Error: Ri_process: ri == NULL!\n", 0, 0, 0 );
65 * Startup code. See if there's any work to do. If not, wait on the
66 * rq->rq_more condition variable.
69 while ( !sglob->slurpd_shutdown &&
70 (( re = rq->rq_gethead( rq )) == NULL )) {
71 /* No work - wait on condition variable */
72 pthread_cond_wait( &rq->rq_more, &rq->rq_mutex );
76 * When we get here, there's work in the queue, and we have the
77 * queue locked. re should be pointing to the head of the queue.
80 while ( !sglob->slurpd_shutdown ) {
82 if ( !ismine( ri, re )) {
83 /* The Re doesn't list my host:port */
84 Debug( LDAP_DEBUG_TRACE,
85 "Replica %s:%d, skip repl record for %s (not mine)\n",
86 ri->ri_hostname, ri->ri_port, re->re_dn );
87 } else if ( !isnew( ri, re )) {
88 /* This Re is older than my saved status information */
89 Debug( LDAP_DEBUG_TRACE,
90 "Replica %s:%d, skip repl record for %s (old)\n",
91 ri->ri_hostname, ri->ri_port, re->re_dn );
93 rc = do_ldap( ri, re, &errmsg );
95 case DO_LDAP_ERR_RETRYABLE:
96 tsleep( RETRY_SLEEP_TIME );
97 Debug( LDAP_DEBUG_ANY,
98 "Retrying operation for DN %s on replica %s:%d\n",
99 re->re_dn, ri->ri_hostname, ri->ri_port );
102 case DO_LDAP_ERR_FATAL: {
103 /* Non-retryable error. Write rejection log. */
105 ldap_get_option(ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &ld_errno);
106 write_reject( ri, re, ld_errno, errmsg );
107 /* 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 );
113 /* LDAP op completed ok - update status... */
114 (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
115 /* ... and write to disk */
116 (void) sglob->st->st_write( sglob->st );
121 Debug( LDAP_DEBUG_ANY, "Error: re is null in Ri_process\n",
125 while ( !sglob->slurpd_shutdown &&
126 ((new_re = re->re_getnext( re )) == NULL )) {
127 if ( sglob->one_shot_mode ) {
130 /* No work - wait on condition variable */
131 pthread_cond_wait( &rq->rq_more, &rq->rq_mutex );
133 re->re_decrefcnt( re );
136 if ( sglob->slurpd_shutdown ) {
145 * Wake a replication thread which may be sleeping.
146 * Send it a SIG(STKFLT|USR1).
156 #ifdef HAVE_LINUX_THREADS
157 pthread_kill( ri->ri_tid, SIGSTKFLT );
158 (void) SIGNAL( SIGSTKFLT, do_nothing );
160 pthread_kill( ri->ri_tid, SIGUSR1 );
161 (void) SIGNAL( SIGUSR1, do_nothing );
168 * Allocate and initialize an Ri struct.
175 (*ri) = ( Ri * ) malloc( sizeof( Ri ));
180 /* Initialize member functions */
181 (*ri)->ri_process = Ri_process;
182 (*ri)->ri_wake = Ri_wake;
184 /* Initialize private data */
185 (*ri)->ri_hostname = NULL;
187 (*ri)->ri_ldp = NULL;
188 (*ri)->ri_bind_method = 0;
189 (*ri)->ri_bind_dn = NULL;
190 (*ri)->ri_password = NULL;
191 (*ri)->ri_principal = NULL;
192 (*ri)->ri_srvtab = NULL;
193 (*ri)->ri_curr = NULL;
202 * Return 1 if the hostname and port in re match the hostname and port
203 * in ri, otherwise return zero.
214 if ( ri == NULL || re == NULL || ri->ri_hostname == NULL ||
215 re->re_replicas == NULL ) {
218 rh = re->re_replicas;
219 for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
220 if ( !strcmp( rh[ i ].rh_hostname, ri->ri_hostname) &&
221 rh[ i ].rh_port == ri->ri_port ) {
232 * Return 1 if the Re's timestamp/seq combination are greater than the
233 * timestamp and seq in the Ri's ri_stel member. In other words, if we
234 * find replication entries in the log which we've already processed,
235 * don't process them. If the re is "old," return 0.
236 * No check for NULL pointers is done.
247 /* Lock the St struct to avoid a race */
248 sglob->st->st_lock( sglob->st );
249 x = strcmp( re->re_timestamp, ri->ri_stel->last );
251 /* re timestamp is newer */
253 } else if ( x < 0 ) {
256 /* timestamps were equal */
257 if ( re->re_seq > ri->ri_stel->seq ) {
258 /* re seq is newer */
264 sglob->st->st_unlock( sglob->st );