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