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