]> git.sur5r.net Git - openldap/blob - servers/slurpd/ri.c
8bd14eaa0040f1302470e2c6a57ff3b466e55c8e
[openldap] / servers / slurpd / ri.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2007 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1996 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25 /* ACKNOWLEDGEMENTS:
26  * This work was originally developed by the University of Michigan
27  * (as part of U-MICH LDAP).
28  */
29
30 /*
31  * ri.c - routines used to manipulate Ri structures.  An Ri (Replica
32  * information) struct contains all information about one replica
33  * instance.  The Ri struct is defined in slurp.h
34  */
35
36
37 #include "portable.h"
38
39 #include <stdio.h>
40
41 #include <ac/stdlib.h>
42 #include <ac/string.h>
43 #include <ac/signal.h>
44
45 #include "slurp.h"
46 #include "globals.h"
47
48
49 /* Forward references */
50 static int ismine LDAP_P(( Ri  *, Re  * ));
51 static int isnew LDAP_P(( Ri  *, Re  * ));
52
53
54 /*
55  * Process any unhandled replication entries in the queue.
56  */
57 static int
58 Ri_process(
59     Ri *ri
60 )
61 {
62     Rq          *rq = sglob->rq;
63     Re          *re = NULL, *new_re = NULL;
64     int         rc ;
65     char        *errmsg;
66     int         errfree;
67
68     (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
69 #ifdef SIGPIPE
70     (void) SIGNAL( SIGPIPE, SIG_IGN );
71 #endif
72     if ( ri == NULL ) {
73         Debug( LDAP_DEBUG_ANY, "Error: Ri_process: ri == NULL!\n", 0, 0, 0 );
74         return -1;
75     }
76
77     /*
78      * Startup code.  See if there's any work to do.  If not, wait on the
79      * rq->rq_more condition variable.
80      */
81     rq->rq_lock( rq );
82     while ( !sglob->slurpd_shutdown &&
83             (( re = rq->rq_gethead( rq )) == NULL )) {
84         /* No work */
85         if ( sglob->one_shot_mode ) {
86             /* give up if in one shot mode */
87             rq->rq_unlock( rq );
88             return 0;
89         }
90         /* wait on condition variable */
91         ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
92     }
93
94     /*
95      * When we get here, there's work in the queue, and we have the
96      * queue locked.  re should be pointing to the head of the queue.
97      */
98     rq->rq_unlock( rq );
99     while ( !sglob->slurpd_shutdown ) {
100         if ( re != NULL ) {
101             if ( !ismine( ri, re )) {
102                 /* The Re doesn't list my host:port */
103                 Debug( LDAP_DEBUG_TRACE,
104                         "Replica %s:%d, skip repl record for %s (not mine)\n",
105                         ri->ri_hostname, ri->ri_port, re->re_dn );
106             } else if ( !isnew( ri, re )) {
107                 /* This Re is older than my saved status information */
108                 Debug( LDAP_DEBUG_TRACE,
109                         "Replica %s:%d, skip repl record for %s (old)\n",
110                         ri->ri_hostname, ri->ri_port, re->re_dn );
111             } else {
112                 rc = do_ldap( ri, re, &errmsg, &errfree );
113                 switch ( rc ) {
114                 case DO_LDAP_ERR_RETRYABLE:
115                     ldap_pvt_thread_sleep( RETRY_SLEEP_TIME );
116                     Debug( LDAP_DEBUG_ANY,
117                             "Retrying operation for DN %s on replica %s:%d\n",
118                             re->re_dn, ri->ri_hostname, ri->ri_port );
119                     continue;
120                     break;
121                 case DO_LDAP_ERR_FATAL: {
122                     /* Non-retryable error.  Write rejection log. */
123                         int ld_errno = 0;
124                         ldap_get_option(ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &ld_errno);
125                     write_reject( ri, re, ld_errno, errmsg );
126                     /* Update status ... */
127                     (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
128                     /* ... and write to disk */
129                     (void) sglob->st->st_write( sglob->st );
130                     } break;
131                 default:
132                     /* LDAP op completed ok - update status... */
133                     (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
134                     /* ... and write to disk */
135                     (void) sglob->st->st_write( sglob->st );
136                     break;
137                 }
138                 if ( errfree && errmsg ) {
139                     ch_free( errmsg );
140                 }
141             }
142         } else {
143             Debug( LDAP_DEBUG_ANY, "Error: re is null in Ri_process\n",
144                     0, 0, 0 );
145         }
146         rq->rq_lock( rq );
147         while ( !sglob->slurpd_shutdown &&
148                 ((new_re = re->re_getnext( re )) == NULL )) {
149             if ( sglob->one_shot_mode ) {
150                 rq->rq_unlock( rq );
151                 return 0;
152             }
153             /* No work - wait on condition variable */
154             ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
155         }
156         re->re_decrefcnt( re );
157         re = new_re;
158         rq->rq_unlock( rq );
159         if ( sglob->slurpd_shutdown ) {
160             if ( ri->ri_ldp ) {
161                 ldap_unbind_ext( ri->ri_ldp, NULL, NULL );
162                 ri->ri_ldp = NULL;
163             }
164             return 0;
165         }
166     }
167     return 0;
168 }
169
170
171 /*
172  * Wake a replication thread which may be sleeping.
173  * Send it a LDAP_SIGUSR1.
174  */
175 static void
176 Ri_wake(
177     Ri *ri
178
179 {
180     if ( ri == NULL ) {
181         return;
182     }
183     ldap_pvt_thread_kill( ri->ri_tid, LDAP_SIGUSR1 );
184 }
185
186
187
188 /* 
189  * Allocate and initialize an Ri struct.
190  */
191 int
192 Ri_init(
193     Ri  **ri
194 )
195 {
196     (*ri) = ( Ri * ) calloc( 1, sizeof( Ri ));
197     if ( *ri == NULL ) {
198         return -1;
199     }
200
201     /* Initialize member functions */
202     (*ri)->ri_process = Ri_process;
203     (*ri)->ri_wake = Ri_wake;
204
205     /* Initialize private data */
206     (*ri)->ri_hostname = NULL;
207     (*ri)->ri_uri = NULL;
208     (*ri)->ri_ldp = NULL;
209     (*ri)->ri_bind_dn = NULL;
210     (*ri)->ri_password = NULL;
211     (*ri)->ri_authcId = NULL;
212     (*ri)->ri_srvtab = NULL;
213     (*ri)->ri_curr = NULL;
214
215     return 0;
216 }
217
218
219
220
221 /*
222  * Return 1 if the hostname and port in re match the hostname and port
223  * in ri, otherwise return zero.
224  */
225 static int
226 ismine(
227     Ri  *ri,
228     Re  *re
229 )
230 {
231     Rh  *rh;
232     int i;
233
234     if ( ri == NULL || re == NULL || ri->ri_hostname == NULL ||
235             re->re_replicas == NULL ) {
236         return 0;
237     }
238     rh = re->re_replicas;
239     for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
240         if ( !strcmp( rh[ i ].rh_hostname, ri->ri_hostname) &&
241                 rh[ i ].rh_port == ri->ri_port ) {
242             return 1;
243         }
244     }
245     return 0;
246 }
247
248
249
250
251 /*
252  * Return 1 if the Re's timestamp/seq combination are greater than the
253  * timestamp and seq in the Ri's ri_stel member.  In other words, if we
254  * find replication entries in the log which we've already processed,
255  * don't process them.  If the re is "old," return 0.
256  * No check for NULL pointers is done.
257  */
258 static int
259 isnew(
260     Ri  *ri,
261     Re  *re
262 )
263 {
264     long x;
265     int ret;
266
267     /* Lock the St struct to avoid a race */
268     sglob->st->st_lock( sglob->st );
269     x = re->re_timestamp - ri->ri_stel->last;
270     if ( x > 0 ) {
271         /* re timestamp is newer */
272         ret = 1;
273     } else if ( x < 0 ) {
274         ret = 0;
275     } else {
276         /* timestamps were equal */
277         if ( re->re_seq > ri->ri_stel->seq ) {
278             /* re seq is newer */
279             ret = 1;
280         } else {
281             ret = 0;
282         }
283     }
284     sglob->st->st_unlock( sglob->st );
285     return ret;
286 }