]> git.sur5r.net Git - openldap/blob - servers/slurpd/ri.c
af7a846e45073a4602a12db919b14890f523b295
[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 <ac/signal.h>
24
25 #include "slurp.h"
26 #include "globals.h"
27
28
29 /* External references */
30 extern void write_reject LDAP_P(( Ri *, Re *, int, char * ));
31 extern void do_nothing LDAP_P(());
32
33 /* Forward references */
34 static int ismine LDAP_P(( Ri  *, Re  * ));
35 static int isnew LDAP_P(( Ri  *, Re  * ));
36 void tsleep LDAP_P(( time_t ));
37
38
39 /*
40  * Process any unhandled replication entries in the queue.
41  */
42 static int
43 Ri_process(
44     Ri *ri
45 )
46 {
47     Rq          *rq = sglob->rq;
48     Re          *re, *new_re;
49     int         rc ;
50     char        *errmsg;
51
52 #ifdef HAVE_LINUX_THREADS
53     (void) SIGNAL( SIGSTKFLT, do_nothing );
54 #else
55     (void) SIGNAL( SIGUSR1, do_nothing );
56 #endif
57     (void) SIGNAL( SIGPIPE, SIG_IGN );
58     if ( ri == NULL ) {
59         Debug( LDAP_DEBUG_ANY, "Error: Ri_process: ri == NULL!\n", 0, 0, 0 );
60         return -1;
61     }
62
63     /*
64      * Startup code.  See if there's any work to do.  If not, wait on the
65      * rq->rq_more condition variable.
66      */
67     rq->rq_lock( rq );
68     while ( !sglob->slurpd_shutdown &&
69             (( re = rq->rq_gethead( rq )) == NULL )) {
70         /* No work - wait on condition variable */
71         pthread_cond_wait( &rq->rq_more, &rq->rq_mutex );
72     }
73
74     /*
75      * When we get here, there's work in the queue, and we have the
76      * queue locked.  re should be pointing to the head of the queue.
77      */
78     rq->rq_unlock( rq );
79     while ( !sglob->slurpd_shutdown ) {
80         if ( re != NULL ) {
81             if ( !ismine( ri, re )) {
82                 /* The Re doesn't list my host:port */
83                 Debug( LDAP_DEBUG_TRACE,
84                         "Replica %s:%d, skip repl record for %s (not mine)\n",
85                         ri->ri_hostname, ri->ri_port, re->re_dn );
86             } else if ( !isnew( ri, re )) {
87                 /* This Re is older than my saved status information */
88                 Debug( LDAP_DEBUG_TRACE,
89                         "Replica %s:%d, skip repl record for %s (old)\n",
90                         ri->ri_hostname, ri->ri_port, re->re_dn );
91             } else {
92                 rc = do_ldap( ri, re, &errmsg );
93                 switch ( rc ) {
94                 case DO_LDAP_ERR_RETRYABLE:
95                     tsleep( RETRY_SLEEP_TIME );
96                     Debug( LDAP_DEBUG_ANY,
97                             "Retrying operation for DN %s on replica %s:%d\n",
98                             re->re_dn, ri->ri_hostname, ri->ri_port );
99                     continue;
100                     break;
101                 case DO_LDAP_ERR_FATAL: {
102                     /* Non-retryable error.  Write rejection log. */
103                         int ld_errno = 0;
104                         ldap_get_option(ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &ld_errno);
105                     write_reject( ri, re, ld_errno, errmsg );
106                     /* Update status ... */
107                     (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
108                     /* ... and write to disk */
109                     (void) sglob->st->st_write( sglob->st );
110                     } break;
111                 default:
112                     /* LDAP op completed ok - update status... */
113                     (void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
114                     /* ... and write to disk */
115                     (void) sglob->st->st_write( sglob->st );
116                     break;
117                 }
118             }
119         } else {
120             Debug( LDAP_DEBUG_ANY, "Error: re is null in Ri_process\n",
121                     0, 0, 0 );
122         }
123         rq->rq_lock( rq );
124         while ( !sglob->slurpd_shutdown &&
125                 ((new_re = re->re_getnext( re )) == NULL )) {
126             if ( sglob->one_shot_mode ) {
127                 return 0;
128             }
129             /* No work - wait on condition variable */
130             pthread_cond_wait( &rq->rq_more, &rq->rq_mutex );
131         }
132         re->re_decrefcnt( re );
133         re = new_re;
134         rq->rq_unlock( rq );
135         if ( sglob->slurpd_shutdown ) {
136             return 0;
137         }
138     }
139     return 0;
140 }
141
142
143 /*
144  * Wake a replication thread which may be sleeping.
145  * Send it a SIG(STKFLT|USR1).
146  */
147 static void
148 Ri_wake(
149     Ri *ri
150
151 {
152     if ( ri == NULL ) {
153         return;
154     }
155 #ifdef HAVE_LINUX_THREADS
156     pthread_kill( ri->ri_tid, SIGSTKFLT );
157     (void) SIGNAL( SIGSTKFLT, do_nothing );
158 #else
159     pthread_kill( ri->ri_tid, SIGUSR1 );
160     (void) SIGNAL( SIGUSR1, do_nothing );
161 #endif
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 * ) malloc( 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_port = 0;
186     (*ri)->ri_ldp = NULL;
187     (*ri)->ri_bind_method = 0;
188     (*ri)->ri_bind_dn = NULL;
189     (*ri)->ri_password = NULL;
190     (*ri)->ri_principal = NULL;
191     (*ri)->ri_srvtab = NULL;
192     (*ri)->ri_curr = NULL;
193
194     return 0;
195 }
196
197
198
199
200 /*
201  * Return 1 if the hostname and port in re match the hostname and port
202  * in ri, otherwise return zero.
203  */
204 static int
205 ismine(
206     Ri  *ri,
207     Re  *re
208 )
209 {
210     Rh  *rh;
211     int i;
212
213     if ( ri == NULL || re == NULL || ri->ri_hostname == NULL ||
214             re->re_replicas == NULL ) {
215         return 0;
216     }
217     rh = re->re_replicas;
218     for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
219         if ( !strcmp( rh[ i ].rh_hostname, ri->ri_hostname) &&
220                 rh[ i ].rh_port == ri->ri_port ) {
221             return 1;
222         }
223     }
224     return 0;
225 }
226
227
228
229
230 /*
231  * Return 1 if the Re's timestamp/seq combination are greater than the
232  * timestamp and seq in the Ri's ri_stel member.  In other words, if we
233  * find replication entries in the log which we've already processed,
234  * don't process them.  If the re is "old," return 0.
235  * No check for NULL pointers is done.
236  */
237 static int
238 isnew(
239     Ri  *ri,
240     Re  *re
241 )
242 {
243     int x;
244     int ret;
245
246     /* Lock the St struct to avoid a race */
247     sglob->st->st_lock( sglob->st );
248     x = strcmp( re->re_timestamp, ri->ri_stel->last );
249     if ( x > 0 ) {
250         /* re timestamp is newer */
251         ret = 1;
252     } else if ( x < 0 ) {
253         ret = 0;
254     } else {
255         /* timestamps were equal */
256         if ( re->re_seq > ri->ri_stel->seq ) {
257             /* re seq is newer */
258             ret = 1;
259         } else {
260             ret = 0;
261         }
262     }
263     sglob->st->st_unlock( sglob->st );
264     return ret;
265 }
266
267