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