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