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 )) {
66 if ( sglob->one_shot_mode ) {
67 /* give up if in one shot mode */
71 /* wait on condition variable */
72 ldap_pvt_thread_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 ldap_pvt_thread_sleep( 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 ) {
131 /* No work - wait on condition variable */
132 ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
134 re->re_decrefcnt( re );
137 if ( sglob->slurpd_shutdown ) {
146 * Wake a replication thread which may be sleeping.
147 * Send it a LDAP_SIGUSR1.
157 ldap_pvt_thread_kill( ri->ri_tid, LDAP_SIGUSR1 );
163 * Allocate and initialize an Ri struct.
170 (*ri) = ( Ri * ) calloc( 1, sizeof( Ri ));
175 /* Initialize member functions */
176 (*ri)->ri_process = Ri_process;
177 (*ri)->ri_wake = Ri_wake;
179 /* Initialize private data */
180 (*ri)->ri_hostname = NULL;
181 (*ri)->ri_ldp = NULL;
182 (*ri)->ri_bind_dn = NULL;
183 (*ri)->ri_password = NULL;
184 (*ri)->ri_authcId = NULL;
185 (*ri)->ri_srvtab = NULL;
186 (*ri)->ri_curr = NULL;
195 * Return 1 if the hostname and port in re match the hostname and port
196 * in ri, otherwise return zero.
207 if ( ri == NULL || re == NULL || ri->ri_hostname == NULL ||
208 re->re_replicas == NULL ) {
211 rh = re->re_replicas;
212 for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
213 if ( !strcmp( rh[ i ].rh_hostname, ri->ri_hostname) &&
214 rh[ i ].rh_port == ri->ri_port ) {
225 * Return 1 if the Re's timestamp/seq combination are greater than the
226 * timestamp and seq in the Ri's ri_stel member. In other words, if we
227 * find replication entries in the log which we've already processed,
228 * don't process them. If the re is "old," return 0.
229 * No check for NULL pointers is done.
240 /* Lock the St struct to avoid a race */
241 sglob->st->st_lock( sglob->st );
242 x = strcmp( re->re_timestamp, ri->ri_stel->last );
244 /* re timestamp is newer */
246 } else if ( x < 0 ) {
249 /* timestamps were equal */
250 if ( re->re_seq > ri->ri_stel->seq ) {
251 /* re seq is newer */
257 sglob->st->st_unlock( sglob->st );