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