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