3 * Copyright (c) 1996 Regents of the University of Michigan.
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.
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.
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.
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.
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.
40 #include <ac/stdlib.h>
41 #include <ac/string.h>
42 #include <ac/unistd.h> /* get ftruncate() */
44 #ifdef HAVE_SYS_TYPES_H
45 #include <sys/types.h>
56 * Lock the replication queue.
63 return( ldap_pvt_thread_mutex_lock( &rq->rq_mutex ));
68 * Unlock the replication queue.
75 return( ldap_pvt_thread_mutex_unlock( &rq->rq_mutex ));
81 * Return the head of the queue. Callers should lock the queue before
82 * calling this routine.
89 return( rq == NULL ? NULL : rq->rq_head );
94 * Return the next item in the queue. Callers should lock the queue before
95 * calling this routine.
105 return( re->re_getnext( re ));
111 * Delete the item at the head of the list. The queue should be locked
112 * by the caller before calling this routine.
126 savedhead = rq->rq_head;
127 if ( savedhead == NULL ) {
131 if ( savedhead->re_getrefcnt( savedhead ) != 0 ) {
132 Debug( LDAP_DEBUG_ANY, "Warning: attempt to delete when refcnt != 0\n",
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 */
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.
162 /* Create a new Re */
163 if ( Re_init( &re ) < 0 ) {
168 /* parse buf and fill in the re struct */
169 if ( re->re_parse( re, buf ) < 0 ) {
175 /* Insert into queue */
176 if ( rq->rq_head == NULL ) {
181 rq->rq_tail->re_next = re;
184 /* set the sequence number */
186 if ( !wasempty && !strcmp(rq->rq_tail->re_timestamp, re->re_timestamp )) {
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.
191 re->re_seq = rq->rq_tail->re_seq + 1;
195 /* Increment count of items in queue */
197 /* wake up any threads waiting for more work */
198 ldap_pvt_thread_cond_broadcast( &rq->rq_more );
200 /* ... and unlock the queue */
208 * Garbage-collect the replication queue. Locking is handled internally.
216 Debug( LDAP_DEBUG_ANY, "Rq_gc: rq is NULL!\n", 0, 0, 0 );
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 */
231 * For debugging: dump the contents of the replication queue to a file.
232 * Locking is handled internally.
244 Debug( LDAP_DEBUG_ANY, "Rq_dump: rq is NULL!\n", 0, 0, 0 );
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 );
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 );
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 );
265 for ( re = rq->rq_gethead( rq ); re != NULL; re = rq->rq_getnext( re )) {
266 re->re_dump( re, fp );
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.
292 Debug( LDAP_DEBUG_ARGS, "re-write on-disk replication log\n",
297 fseek( fp, 0L, SEEK_SET ); /* Go to beginning of file */
300 for ( re = rq->rq_gethead( rq ); re != NULL; re = rq->rq_getnext( re )) {
301 if ( re->re_write( NULL, re, fp ) < 0 ) {
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 );
314 rq->rq_ndel = 0; /* reset count of deleted re's */
316 rq->rq_lasttrim = now; /* reset last trim time */
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.
329 * Return 1 if replogfile should be trimmed, 0 if not.
330 * Any different policy should be implemented by replacing this function.
348 if ( now > ( rq->rq_lasttrim + TRIMCHECK_INTERVAL )) {
349 rc = ( rq->rq_ndel >= 50 );
359 * Return counts of Re structs in the queue.
375 if ( type == RQ_COUNT_ALL ) {
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 ) {
393 * Allocate and initialize an Rq object.
400 /* Instantiate the struct */
401 (*rq) = (Rq *) malloc( sizeof( Rq ));
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;
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;
426 (*rq)->rq_lasttrim = (time_t) 0L;