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