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