]> git.sur5r.net Git - openldap/blob - servers/slurpd/ri.c
Minor cleanups for ITS#2620 from HEAD
[openldap] / servers / slurpd / ri.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 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             if ( ri->ri_ldp ) { 
173                 ldap_unbind( ri->ri_ldp );
174                 ri->ri_ldp = NULL;
175             }
176             return 0;
177         }
178     }
179     return 0;
180 }
181
182
183 /*
184  * Wake a replication thread which may be sleeping.
185  * Send it a LDAP_SIGUSR1.
186  */
187 static void
188 Ri_wake(
189     Ri *ri
190
191 {
192     if ( ri == NULL ) {
193         return;
194     }
195     ldap_pvt_thread_kill( ri->ri_tid, LDAP_SIGUSR1 );
196 }
197
198
199
200 /* 
201  * Allocate and initialize an Ri struct.
202  */
203 int
204 Ri_init(
205     Ri  **ri
206 )
207 {
208     (*ri) = ( Ri * ) calloc( 1, sizeof( Ri ));
209     if ( *ri == NULL ) {
210         return -1;
211     }
212
213     /* Initialize member functions */
214     (*ri)->ri_process = Ri_process;
215     (*ri)->ri_wake = Ri_wake;
216
217     /* Initialize private data */
218     (*ri)->ri_hostname = NULL;
219     (*ri)->ri_uri = NULL;
220     (*ri)->ri_ldp = NULL;
221     (*ri)->ri_bind_dn = NULL;
222     (*ri)->ri_password = NULL;
223     (*ri)->ri_authcId = NULL;
224     (*ri)->ri_srvtab = NULL;
225     (*ri)->ri_curr = NULL;
226
227     return 0;
228 }
229
230
231
232
233 /*
234  * Return 1 if the hostname and port in re match the hostname and port
235  * in ri, otherwise return zero.
236  */
237 static int
238 ismine(
239     Ri  *ri,
240     Re  *re
241 )
242 {
243     Rh  *rh;
244     int i;
245
246     if ( ri == NULL || re == NULL || ri->ri_hostname == NULL ||
247             re->re_replicas == NULL ) {
248         return 0;
249     }
250     rh = re->re_replicas;
251     for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
252         if ( !strcmp( rh[ i ].rh_hostname, ri->ri_hostname) &&
253                 rh[ i ].rh_port == ri->ri_port ) {
254             return 1;
255         }
256     }
257     return 0;
258 }
259
260
261
262
263 /*
264  * Return 1 if the Re's timestamp/seq combination are greater than the
265  * timestamp and seq in the Ri's ri_stel member.  In other words, if we
266  * find replication entries in the log which we've already processed,
267  * don't process them.  If the re is "old," return 0.
268  * No check for NULL pointers is done.
269  */
270 static int
271 isnew(
272     Ri  *ri,
273     Re  *re
274 )
275 {
276     long x;
277     int ret;
278
279     /* Lock the St struct to avoid a race */
280     sglob->st->st_lock( sglob->st );
281     x = re->re_timestamp - ri->ri_stel->last;
282     if ( x > 0 ) {
283         /* re timestamp is newer */
284         ret = 1;
285     } else if ( x < 0 ) {
286         ret = 0;
287     } else {
288         /* timestamps were equal */
289         if ( re->re_seq > ri->ri_stel->seq ) {
290             /* re seq is newer */
291             ret = 1;
292         } else {
293             ret = 0;
294         }
295     }
296     sglob->st->st_unlock( sglob->st );
297     return ret;
298 }