]> git.sur5r.net Git - openldap/blob - servers/slurpd/ri.c
f22da316265c2d6e284f0b16ff01dd0abb0379fc
[openldap] / servers / slurpd / ri.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright (c) 1996 Regents of the University of Michigan.
4  * All rights reserved.
5  *
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.
12  */
13
14 /*
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
18  */
19
20
21 #include "portable.h"
22
23 #include <stdio.h>
24
25 #include <ac/stdlib.h>
26 #include <ac/string.h>
27 #include <ac/signal.h>
28
29 #include "slurp.h"
30 #include "globals.h"
31
32
33 /* Forward references */
34 static int ismine LDAP_P(( Ri  *, Re  * ));
35 static int isnew LDAP_P(( Ri  *, Re  * ));
36
37
38 /*
39  * Process any unhandled replication entries in the queue.
40  */
41 static int
42 Ri_process(
43     Ri *ri
44 )
45 {
46     Rq          *rq = sglob->rq;
47     Re          *re, *new_re;
48     int         rc ;
49     char        *errmsg;
50
51     (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
52     (void) SIGNAL( SIGPIPE, SIG_IGN );
53     if ( ri == NULL ) {
54         Debug( LDAP_DEBUG_ANY, "Error: Ri_process: ri == NULL!\n", 0, 0, 0 );
55         return -1;
56     }
57
58     /*
59      * Startup code.  See if there's any work to do.  If not, wait on the
60      * rq->rq_more condition variable.
61      */
62     rq->rq_lock( rq );
63     while ( !sglob->slurpd_shutdown &&
64             (( re = rq->rq_gethead( rq )) == NULL )) {
65         /* No work */
66         if ( sglob->one_shot_mode ) {
67             /* give up if in one shot mode */
68             rq->rq_unlock( rq );
69             return 0;
70         }
71         /* wait on condition variable */
72         ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
73     }
74
75     /*
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.
78      */
79     rq->rq_unlock( rq );
80     while ( !sglob->slurpd_shutdown ) {
81         if ( re != NULL ) {
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 );
92             } else {
93                 rc = do_ldap( ri, re, &errmsg );
94                 switch ( rc ) {
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 );
100                     continue;
101                     break;
102                 case DO_LDAP_ERR_FATAL: {
103                     /* Non-retryable error.  Write rejection log. */
104                         int ld_errno = 0;
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 );
111                     } break;
112                 default:
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 );
117                     break;
118                 }
119             }
120         } else {
121             Debug( LDAP_DEBUG_ANY, "Error: re is null in Ri_process\n",
122                     0, 0, 0 );
123         }
124         rq->rq_lock( rq );
125         while ( !sglob->slurpd_shutdown &&
126                 ((new_re = re->re_getnext( re )) == NULL )) {
127             if ( sglob->one_shot_mode ) {
128                 rq->rq_unlock( rq );
129                 return 0;
130             }
131             /* No work - wait on condition variable */
132             ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
133         }
134         re->re_decrefcnt( re );
135         re = new_re;
136         rq->rq_unlock( rq );
137         if ( sglob->slurpd_shutdown ) {
138             return 0;
139         }
140     }
141     return 0;
142 }
143
144
145 /*
146  * Wake a replication thread which may be sleeping.
147  * Send it a LDAP_SIGUSR1.
148  */
149 static void
150 Ri_wake(
151     Ri *ri
152
153 {
154     if ( ri == NULL ) {
155         return;
156     }
157     ldap_pvt_thread_kill( ri->ri_tid, LDAP_SIGUSR1 );
158 }
159
160
161
162 /* 
163  * Allocate and initialize an Ri struct.
164  */
165 int
166 Ri_init(
167     Ri  **ri
168 )
169 {
170     (*ri) = ( Ri * ) calloc( 1, sizeof( Ri ));
171     if ( *ri == NULL ) {
172         return -1;
173     }
174
175     /* Initialize member functions */
176     (*ri)->ri_process = Ri_process;
177     (*ri)->ri_wake = Ri_wake;
178
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;
187
188     return 0;
189 }
190
191
192
193
194 /*
195  * Return 1 if the hostname and port in re match the hostname and port
196  * in ri, otherwise return zero.
197  */
198 static int
199 ismine(
200     Ri  *ri,
201     Re  *re
202 )
203 {
204     Rh  *rh;
205     int i;
206
207     if ( ri == NULL || re == NULL || ri->ri_hostname == NULL ||
208             re->re_replicas == NULL ) {
209         return 0;
210     }
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 ) {
215             return 1;
216         }
217     }
218     return 0;
219 }
220
221
222
223
224 /*
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.
230  */
231 static int
232 isnew(
233     Ri  *ri,
234     Re  *re
235 )
236 {
237     int x;
238     int ret;
239
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 );
243     if ( x > 0 ) {
244         /* re timestamp is newer */
245         ret = 1;
246     } else if ( x < 0 ) {
247         ret = 0;
248     } else {
249         /* timestamps were equal */
250         if ( re->re_seq > ri->ri_stel->seq ) {
251             /* re seq is newer */
252             ret = 1;
253         } else {
254             ret = 0;
255         }
256     }
257     sglob->st->st_unlock( sglob->st );
258     return ret;
259 }