]> git.sur5r.net Git - openldap/blob - servers/slurpd/ri.c
Resync with HEAD
[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     (void) SIGNAL( SIGPIPE, SIG_IGN );
57     if ( ri == NULL ) {
58 #ifdef NEW_LOGGING
59         LDAP_LOG (( "ri", LDAP_LEVEL_ERR, "Ri_process: "
60                 "Error: ri == NULL!\n" ));
61 #else
62         Debug( LDAP_DEBUG_ANY, "Error: Ri_process: ri == NULL!\n", 0, 0, 0 );
63 #endif
64         return -1;
65     }
66
67     /*
68      * Startup code.  See if there's any work to do.  If not, wait on the
69      * rq->rq_more condition variable.
70      */
71     rq->rq_lock( rq );
72     while ( !sglob->slurpd_shutdown &&
73             (( re = rq->rq_gethead( rq )) == NULL )) {
74         /* No work */
75         if ( sglob->one_shot_mode ) {
76             /* give up if in one shot mode */
77             rq->rq_unlock( rq );
78             return 0;
79         }
80         /* wait on condition variable */
81         ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
82     }
83
84     /*
85      * When we get here, there's work in the queue, and we have the
86      * queue locked.  re should be pointing to the head of the queue.
87      */
88     rq->rq_unlock( rq );
89     while ( !sglob->slurpd_shutdown ) {
90         if ( re != NULL ) {
91             if ( !ismine( ri, re )) {
92                 /* The Re doesn't list my host:port */
93 #ifdef NEW_LOGGING
94                 LDAP_LOG (( "ri", LDAP_LEVEL_DETAIL1, "Ri_process: "
95                         "Replica %s:%d, skip repl record for %s (not mine)\n",
96                         ri->ri_hostname, ri->ri_port, re->re_dn ));
97 #else
98                 Debug( LDAP_DEBUG_TRACE,
99                         "Replica %s:%d, skip repl record for %s (not mine)\n",
100                         ri->ri_hostname, ri->ri_port, re->re_dn );
101 #endif
102             } else if ( !isnew( ri, re )) {
103                 /* This Re is older than my saved status information */
104 #ifdef NEW_LOGGING
105                 LDAP_LOG (( "ri", LDAP_LEVEL_DETAIL1, "Ri_process: "
106                         "Replica %s:%d, skip repl record for %s (old)\n",
107                         ri->ri_hostname, ri->ri_port, re->re_dn ));
108 #else
109                 Debug( LDAP_DEBUG_TRACE,
110                         "Replica %s:%d, skip repl record for %s (old)\n",
111                         ri->ri_hostname, ri->ri_port, re->re_dn );
112 #endif
113             } else {
114                 rc = do_ldap( ri, re, &errmsg );
115                 switch ( rc ) {
116                 case DO_LDAP_ERR_RETRYABLE:
117                     ldap_pvt_thread_sleep( RETRY_SLEEP_TIME );
118 #ifdef NEW_LOGGING
119                         LDAP_LOG (( "ri", LDAP_LEVEL_DETAIL1, "Ri_process: "
120                                 "Retrying operation for DN %s on replica %s:%d\n",
121                             re->re_dn, ri->ri_hostname, ri->ri_port ));
122 #else
123                     Debug( LDAP_DEBUG_ANY,
124                             "Retrying operation for DN %s on replica %s:%d\n",
125                             re->re_dn, ri->ri_hostname, ri->ri_port );
126 #endif
127                     continue;
128                     break;
129                 case DO_LDAP_ERR_FATAL: {
130                     /* Non-retryable error.  Write rejection log. */
131                         int ld_errno = 0;
132                         ldap_get_option(ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &ld_errno);
133                     write_reject( ri, re, ld_errno, errmsg );
134                     /* Update status ... */
135                     (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
136                     /* ... and write to disk */
137                     (void) sglob->st->st_write( sglob->st );
138                     } break;
139                 default:
140                     /* LDAP op completed ok - update status... */
141                     (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
142                     /* ... and write to disk */
143                     (void) sglob->st->st_write( sglob->st );
144                     break;
145                 }
146             }
147         } else {
148 #ifdef NEW_LOGGING
149                 LDAP_LOG (( "ri", LDAP_LEVEL_ERR, "Ri_process: "
150                         "Error: re is null in Ri_process\n" ));
151 #else
152             Debug( LDAP_DEBUG_ANY, "Error: re is null in Ri_process\n",
153                     0, 0, 0 );
154 #endif
155         }
156         rq->rq_lock( rq );
157         while ( !sglob->slurpd_shutdown &&
158                 ((new_re = re->re_getnext( re )) == NULL )) {
159             if ( sglob->one_shot_mode ) {
160                 rq->rq_unlock( rq );
161                 return 0;
162             }
163             /* No work - wait on condition variable */
164             ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
165         }
166         re->re_decrefcnt( re );
167         re = new_re;
168         rq->rq_unlock( rq );
169         if ( sglob->slurpd_shutdown ) {
170             return 0;
171         }
172     }
173     return 0;
174 }
175
176
177 /*
178  * Wake a replication thread which may be sleeping.
179  * Send it a LDAP_SIGUSR1.
180  */
181 static void
182 Ri_wake(
183     Ri *ri
184
185 {
186     if ( ri == NULL ) {
187         return;
188     }
189     ldap_pvt_thread_kill( ri->ri_tid, LDAP_SIGUSR1 );
190 }
191
192
193
194 /* 
195  * Allocate and initialize an Ri struct.
196  */
197 int
198 Ri_init(
199     Ri  **ri
200 )
201 {
202     (*ri) = ( Ri * ) calloc( 1, sizeof( Ri ));
203     if ( *ri == NULL ) {
204         return -1;
205     }
206
207     /* Initialize member functions */
208     (*ri)->ri_process = Ri_process;
209     (*ri)->ri_wake = Ri_wake;
210
211     /* Initialize private data */
212     (*ri)->ri_hostname = NULL;
213     (*ri)->ri_ldp = NULL;
214     (*ri)->ri_bind_dn = NULL;
215     (*ri)->ri_password = NULL;
216     (*ri)->ri_authcId = NULL;
217     (*ri)->ri_srvtab = NULL;
218     (*ri)->ri_curr = NULL;
219
220     return 0;
221 }
222
223
224
225
226 /*
227  * Return 1 if the hostname and port in re match the hostname and port
228  * in ri, otherwise return zero.
229  */
230 static int
231 ismine(
232     Ri  *ri,
233     Re  *re
234 )
235 {
236     Rh  *rh;
237     int i;
238
239     if ( ri == NULL || re == NULL || ri->ri_hostname == NULL ||
240             re->re_replicas == NULL ) {
241         return 0;
242     }
243     rh = re->re_replicas;
244     for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
245         if ( !strcmp( rh[ i ].rh_hostname, ri->ri_hostname) &&
246                 rh[ i ].rh_port == ri->ri_port ) {
247             return 1;
248         }
249     }
250     return 0;
251 }
252
253
254
255
256 /*
257  * Return 1 if the Re's timestamp/seq combination are greater than the
258  * timestamp and seq in the Ri's ri_stel member.  In other words, if we
259  * find replication entries in the log which we've already processed,
260  * don't process them.  If the re is "old," return 0.
261  * No check for NULL pointers is done.
262  */
263 static int
264 isnew(
265     Ri  *ri,
266     Re  *re
267 )
268 {
269     long x;
270     int ret;
271
272     /* Lock the St struct to avoid a race */
273     sglob->st->st_lock( sglob->st );
274     x = re->re_timestamp - ri->ri_stel->last;
275     if ( x > 0 ) {
276         /* re timestamp is newer */
277         ret = 1;
278     } else if ( x < 0 ) {
279         ret = 0;
280     } else {
281         /* timestamps were equal */
282         if ( re->re_seq > ri->ri_stel->seq ) {
283             /* re seq is newer */
284             ret = 1;
285         } else {
286             ret = 0;
287         }
288     }
289     sglob->st->st_unlock( sglob->st );
290     return ret;
291 }