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