]> git.sur5r.net Git - openldap/blob - servers/slurpd/rq.c
Added a "dnattr" case for ACIs (still need to check the ACI OID).
[openldap] / servers / slurpd / rq.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright (c) 1996 Regents of the University of Michigan.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted
7  * provided that this notice is preserved and that due credit is given
8  * to the University of Michigan at Ann Arbor. The name of the University
9  * may not be used to endorse or promote products derived from this
10  * software without specific prior written permission. This software
11  * is provided ``as is'' without express or implied warranty.
12  */
13
14 /*
15  * rq.c - routines used to manage the queue of replication entries.
16  * An Rq (Replication queue) struct contains a linked list of Re
17  * (Replication entry) structures.
18  *
19  * Routines wishing to access the replication queue should do so through
20  * the Rq struct's member functions, e.g. rq->rq_gethead() and friends.
21  * For example, Re structs should be added to the queue by calling 
22  * the rq_add() member function.
23  *
24  * Access to the queue is serialized by a mutex.  Member functions which do
25  * not do their own locking should only be called after locking the queue
26  * using the rq_lock() member function.  The queue should be unlocked with
27  * the rq_unlock() member function.
28  *
29  * Note that some member functions handle their own locking internally.
30  * Callers should not lock the queue before calling these functions.
31  * See the comment block for each function below.
32  *
33  */
34
35 #include "portable.h"
36
37 #include <stdio.h>
38
39 #include <ac/stdlib.h>
40 #include <ac/string.h>
41 #include <ac/unistd.h>          /* get ftruncate() */
42
43 #include "slurp.h"
44 #include "globals.h"
45
46
47 /*
48  * Lock the replication queue.
49  */
50 static int
51 Rq_lock(
52     Rq  *rq
53 )
54 {
55     return( ldap_pvt_thread_mutex_lock( &rq->rq_mutex ));
56 }
57
58
59 /*
60  * Unlock the replication queue.
61  */
62 static int
63 Rq_unlock(
64     Rq  *rq
65 )
66 {
67     return( ldap_pvt_thread_mutex_unlock( &rq->rq_mutex ));
68 }
69
70
71
72 /*
73  * Return the head of the queue.  Callers should lock the queue before
74  * calling this routine.
75  */
76 static Re *
77 Rq_gethead(
78     Rq  *rq
79 )
80 {
81     return( rq == NULL ? NULL : rq->rq_head );
82 }
83
84
85 /*
86  * Return the next item in the queue.  Callers should lock the queue before
87  * calling this routine.
88  */
89 static Re *
90 Rq_getnext(
91     Re  *re
92 )
93 {
94     if ( re == NULL ) {
95         return NULL;
96     } else {
97         return( re->re_getnext( re ));
98     }
99 }
100
101
102 /*
103  * Delete the item at the head of the list.  The queue should be locked
104  * by the caller before calling this routine.
105  */
106 static int
107 Rq_delhead(
108     Rq  *rq
109 )
110 {
111     Re  *savedhead;
112     int rc;
113
114     if ( rq == NULL ) {
115         return( -1 );
116     }
117
118     savedhead = rq->rq_head;
119     if ( savedhead == NULL ) {
120         return( 0 );
121     }
122
123     if ( savedhead->re_getrefcnt( savedhead ) != 0 ) {
124         Debug( LDAP_DEBUG_ANY, "Warning: attempt to delete when refcnt != 0\n",
125                 0, 0, 0 );
126         return( -1 );
127     }
128
129     rq->rq_head = rq->rq_head->re_getnext( rq->rq_head );
130     rc = savedhead->re_free( savedhead );
131     rq->rq_nre--;       /* decrement count of Re's in queue */
132     return( rc );
133 }
134
135
136 /* 
137  * Add an entry to the tail of the replication queue.  Locking is handled
138  * internally.  When items are added to the queue, this routine wakes
139  * up any threads which are waiting for more work by signaling on the
140  * rq->rq_more condition variable.
141  */
142 static int
143 Rq_add(
144     Rq          *rq,
145     char        *buf
146 )
147 {
148     Re  *re;
149     int wasempty = 0;
150
151     /* Lock the queue */
152     rq->rq_lock( rq );
153
154     /* Create a new Re */
155     if ( Re_init( &re ) < 0 ) {
156         rq->rq_unlock( rq );
157         return -1;
158     }
159
160     /* parse buf and fill in the re struct */
161     if ( re->re_parse( re, buf ) < 0 ) {
162         re->re_free( re );
163         rq->rq_unlock( rq );
164         return -1;
165     }
166
167     /* Insert into queue */
168     if ( rq->rq_head == NULL ) {
169         rq->rq_head = re;
170         rq->rq_tail = re;
171         wasempty = 1;
172     } else {
173         rq->rq_tail->re_next = re;
174     }
175
176     /* set the sequence number */
177     re->re_seq = 0;
178     if ( !wasempty && !strcmp(rq->rq_tail->re_timestamp, re->re_timestamp )) {
179         /*
180          * Our new re has the same timestamp as the tail's timestamp.
181          * Increment the seq number in the tail and use it as our seq number.
182          */
183         re->re_seq = rq->rq_tail->re_seq + 1;
184     }
185     rq->rq_tail = re;
186
187     /* Increment count of items in queue */
188     rq->rq_nre++;
189     /* wake up any threads waiting for more work */
190     ldap_pvt_thread_cond_broadcast( &rq->rq_more );
191
192     /* ... and unlock the queue */
193     rq->rq_unlock( rq );
194
195     return 0;
196 }
197
198
199 /*
200  * Garbage-collect the replication queue.  Locking is handled internally.
201  */
202 static void
203 Rq_gc(
204     Rq  *rq
205 )
206 {
207     if ( rq == NULL ) {
208         Debug( LDAP_DEBUG_ANY, "Rq_gc: rq is NULL!\n", 0, 0, 0 );
209         return;
210     }
211     rq->rq_lock( rq ); 
212     while (( rq->rq_head != NULL ) &&
213             ( rq->rq_head->re_getrefcnt( rq->rq_head ) == 0 )) {
214         rq->rq_delhead( rq );
215         rq->rq_ndel++;  /* increment count of deleted entries */
216     }
217     rq->rq_unlock( rq ); 
218     return;
219 }
220
221
222 /*
223  * For debugging: dump the contents of the replication queue to a file.
224  * Locking is handled internally.
225  */
226 static void
227 Rq_dump(
228     Rq  *rq
229 )
230 {
231     Re          *re;
232     FILE        *fp;
233
234     if ( rq == NULL ) {
235         Debug( LDAP_DEBUG_ANY, "Rq_dump: rq is NULL!\n", 0, 0, 0 );
236         return;
237     }
238
239     if (( fp = fopen( SLURPD_DUMPFILE, "w" )) == NULL ) {
240         Debug( LDAP_DEBUG_ANY, "Rq_dump: cannot open \"%s\" for write\n",
241                 SLURPD_DUMPFILE, 0, 0 );
242         return;
243     }
244
245     rq->rq_lock( rq );
246     for ( re = rq->rq_gethead( rq ); re != NULL; re = rq->rq_getnext( re )) {
247         re->re_dump( re, fp );
248     }
249     rq->rq_unlock( rq );
250     fclose( fp );
251     return;
252 }
253
254
255 /*
256  * Write the contents of a replication queue to a file.  Returns zero if
257  * successful, -1 if not.  Handles queue locking internally.  Callers should
258  * provide an open file pointer, which should refer to a locked file.
259  */
260 static int
261 Rq_write(
262     Rq          *rq,
263     FILE        *fp
264 )
265 {
266     Re          *re;
267     time_t      now;
268
269     if ( rq == NULL ) {
270         return -1;
271     }
272
273     Debug( LDAP_DEBUG_ARGS, "re-write on-disk replication log\n",
274             0, 0, 0 );
275 #ifndef SEEK_SET
276 #define SEEK_SET 0
277 #endif
278     fseek( fp, 0L, SEEK_SET );  /* Go to beginning of file */
279     rq->rq_lock( rq );
280
281     for ( re = rq->rq_gethead( rq ); re != NULL; re = rq->rq_getnext( re )) {
282         if ( re->re_write( NULL, re, fp ) < 0 ) {
283             fflush( fp );
284             rq->rq_unlock( rq );
285             return -1;
286         }
287     }
288     fflush( fp );
289     sglob->srpos = ftell( fp ); /* update replog file position */
290     /* and truncate to correct len */
291     if ( ftruncate( fileno( fp ), sglob->srpos ) < 0 ) {
292         Debug( LDAP_DEBUG_ANY, "Error truncating replication log: %s\n",
293                 sys_errlist[ errno ], 0, 0 );
294     }
295     rq->rq_ndel = 0;    /* reset count of deleted re's */
296     time( &now );
297     rq->rq_lasttrim = now;      /* reset last trim time */
298     rq->rq_unlock( rq );
299     return 0;
300 }
301
302
303 /*
304  * Check to see if the private slurpd replication log needs trimming.
305  * The current criteria are:
306  *  - The last trim was more than 5 minutes ago, *and*
307  *  - We've finished with at least 50 replication log entries since the
308  *    last time we re-wrote the replication log.
309  *
310  * Return 1 if replogfile should be trimmed, 0 if not.
311  * Any different policy should be implemented by replacing this function.
312  */
313 static int
314 Rq_needtrim(
315     Rq  *rq
316 )
317 {
318     int         rc = 0;
319     time_t      now;
320
321     if ( rq == NULL ) {
322         return 0;
323     }
324
325     rq->rq_lock( rq );
326
327     time( &now );
328
329     if ( now > ( rq->rq_lasttrim + TRIMCHECK_INTERVAL )) {
330         rc = ( rq->rq_ndel >= 50 );
331     } else {
332         rc = 0;
333     }
334     rq->rq_unlock( rq );
335     return rc;
336 }
337
338
339 /*
340  * Return counts of Re structs in the queue.
341  */
342 static int
343 Rq_getcount(
344     Rq  *rq,
345     int type
346 )
347 {
348     int count = 0;
349     Re  *re;
350
351     if ( rq == NULL ) {
352         return 0;
353     }
354
355     rq->rq_lock( rq );
356     if ( type == RQ_COUNT_ALL ) {
357         count = rq->rq_nre;
358     } else {
359         for ( re = rq->rq_gethead( rq ); re != NULL;
360                 re = rq->rq_getnext( re )) {
361             if ( type == RQ_COUNT_NZRC ) {
362                 if ( re->re_getrefcnt( re ) > 1 ) {
363                     count++;
364                 }
365             }
366         }
367     }
368     rq->rq_unlock( rq );
369     return count;
370 }
371
372
373 /* 
374  * Allocate and initialize an Rq object.
375  */
376 int
377 Rq_init(
378     Rq  **rq
379 )
380 {
381     /* Instantiate the struct */
382     (*rq) = (Rq *) malloc( sizeof( Rq ));
383     if ( *rq == NULL ) {
384         return -1;
385     }
386
387     /* Fill in all the function pointers */
388     (*rq)->rq_gethead = Rq_gethead;
389     (*rq)->rq_getnext = Rq_getnext;
390     (*rq)->rq_delhead = Rq_delhead;
391     (*rq)->rq_add = Rq_add;
392     (*rq)->rq_gc = Rq_gc;
393     (*rq)->rq_lock = Rq_lock;
394     (*rq)->rq_unlock = Rq_unlock;
395     (*rq)->rq_dump = Rq_dump;
396     (*rq)->rq_needtrim = Rq_needtrim;
397     (*rq)->rq_write = Rq_write;
398     (*rq)->rq_getcount = Rq_getcount;
399
400     /* Initialize private data */
401     ldap_pvt_thread_mutex_init( &((*rq)->rq_mutex) );
402     ldap_pvt_thread_cond_init( &((*rq)->rq_more) );
403     (*rq)->rq_head = NULL;
404     (*rq)->rq_tail = NULL;
405     (*rq)->rq_nre = 0;
406     (*rq)->rq_ndel = 0;
407     (*rq)->rq_lasttrim = (time_t) 0L;
408
409     return 0;
410 }