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