]> git.sur5r.net Git - openldap/blob - servers/slurpd/rq.c
Added LDAP_LOG Messages
[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 #ifdef NEW_LOGGING
137         LDAP_LOG (( "rq", LDAP_LEVEL_WARNING, "Rq_delhead: "
138                 "Warning: attempt to delete when refcnt != 0\n" ));
139 #else
140         Debug( LDAP_DEBUG_ANY, "Warning: attempt to delete when refcnt != 0\n",
141                 0, 0, 0 );
142 #endif
143         return( -1 );
144     }
145
146     rq->rq_head = rq->rq_head->re_getnext( rq->rq_head );
147     rc = savedhead->re_free( savedhead );
148     rq->rq_nre--;       /* decrement count of Re's in queue */
149     return( rc );
150 }
151
152
153 /* 
154  * Add an entry to the tail of the replication queue.  Locking is handled
155  * internally.  When items are added to the queue, this routine wakes
156  * up any threads which are waiting for more work by signaling on the
157  * rq->rq_more condition variable.
158  */
159 static int
160 Rq_add(
161     Rq          *rq,
162     char        *buf
163 )
164 {
165     Re  *re;
166     int wasempty = 0;
167
168     /* Lock the queue */
169     rq->rq_lock( rq );
170
171     /* Create a new Re */
172     if ( Re_init( &re ) < 0 ) {
173         rq->rq_unlock( rq );
174         return -1;
175     }
176
177     /* parse buf and fill in the re struct */
178     if ( re->re_parse( re, buf ) < 0 ) {
179         re->re_free( re );
180         rq->rq_unlock( rq );
181         return -1;
182     }
183
184     /* Insert into queue */
185     if ( rq->rq_head == NULL ) {
186         rq->rq_head = re;
187         rq->rq_tail = re;
188         wasempty = 1;
189     } else {
190         rq->rq_tail->re_next = re;
191     }
192
193     /* set the sequence number */
194     re->re_seq = 0;
195     if ( !wasempty && ( rq->rq_tail->re_timestamp == re->re_timestamp )) {
196         /*
197          * Our new re has the same timestamp as the tail's timestamp.
198          * Increment the seq number in the tail and use it as our seq number.
199          */
200         re->re_seq = rq->rq_tail->re_seq + 1;
201     }
202     rq->rq_tail = re;
203
204     /* Increment count of items in queue */
205     rq->rq_nre++;
206     /* wake up any threads waiting for more work */
207     ldap_pvt_thread_cond_broadcast( &rq->rq_more );
208
209     /* ... and unlock the queue */
210     rq->rq_unlock( rq );
211
212     return 0;
213 }
214
215
216 /*
217  * Garbage-collect the replication queue.  Locking is handled internally.
218  */
219 static void
220 Rq_gc(
221     Rq  *rq
222 )
223 {
224     if ( rq == NULL ) {
225 #ifdef NEW_LOGGING
226         LDAP_LOG (( "rq", LDAP_LEVEL_DETAIL1, "Rq_gc: rq is NULL!\n" ));
227 #else
228         Debug( LDAP_DEBUG_ANY, "Rq_gc: rq is NULL!\n", 0, 0, 0 );
229 #endif
230         return;
231     }
232     rq->rq_lock( rq ); 
233     while (( rq->rq_head != NULL ) &&
234             ( rq->rq_head->re_getrefcnt( rq->rq_head ) == 0 )) {
235         rq->rq_delhead( rq );
236         rq->rq_ndel++;  /* increment count of deleted entries */
237     }
238     rq->rq_unlock( rq ); 
239     return;
240 }
241
242
243 /*
244  * For debugging: dump the contents of the replication queue to a file.
245  * Locking is handled internally.
246  */
247 static void
248 Rq_dump(
249     Rq  *rq
250 )
251 {
252     Re          *re;
253     FILE        *fp;
254     int         tmpfd;
255
256     if ( rq == NULL ) {
257 #ifdef NEW_LOGGING
258         LDAP_LOG (( "rq", LDAP_LEVEL_ARGS, "Rq_dump: rq is NULL!\n" ));
259 #else
260         Debug( LDAP_DEBUG_ANY, "Rq_dump: rq is NULL!\n", 0, 0, 0 );
261 #endif
262         return;
263     }
264
265     if (unlink(SLURPD_DUMPFILE) == -1 && errno != ENOENT) {
266 #ifdef NEW_LOGGING
267         LDAP_LOG (( "rq", LDAP_LEVEL_ERR, "Rq_dump: "
268                 "\"%s\" exists, cannot unlink\n", SLURPD_DUMPFILE ));
269 #else
270         Debug( LDAP_DEBUG_ANY, "Rq_dump: \"%s\" exists, and cannot unlink\n",
271                 SLURPD_DUMPFILE, 0, 0 );
272 #endif
273         return;
274     }
275     if (( tmpfd = open(SLURPD_DUMPFILE, O_CREAT|O_RDWR|O_EXCL, 0600)) == -1) {
276 #ifdef NEW_LOGGING
277         LDAP_LOG (( "rq", LDAP_LEVEL_ERR, "Rq_dump: "
278                 "cannot open \"%s\" for write\n", SLURPD_DUMPFILE ));
279 #else
280         Debug( LDAP_DEBUG_ANY, "Rq_dump: cannot open \"%s\" for write\n",
281                 SLURPD_DUMPFILE, 0, 0 );
282 #endif
283         return;
284     }
285     if (( fp = fdopen( tmpfd, "w" )) == NULL ) {
286 #ifdef NEW_LOGGING
287         LDAP_LOG (( "rq", LDAP_LEVEL_ERR, "Rq_dump: "
288                 "cannot fdopen \"%s\" for write\n", SLURPD_DUMPFILE ));
289 #else
290         Debug( LDAP_DEBUG_ANY, "Rq_dump: cannot fdopen \"%s\" for write\n",
291                 SLURPD_DUMPFILE, 0, 0 );
292 #endif
293         return;
294     }
295
296     rq->rq_lock( rq );
297     for ( re = rq->rq_gethead( rq ); re != NULL; re = rq->rq_getnext( re )) {
298         re->re_dump( re, fp );
299     }
300     rq->rq_unlock( rq );
301     fclose( fp );
302     return;
303 }
304
305
306 /*
307  * Write the contents of a replication queue to a file.  Returns zero if
308  * successful, -1 if not.  Handles queue locking internally.  Callers should
309  * provide an open file pointer, which should refer to a locked file.
310  */
311 static int
312 Rq_write(
313     Rq          *rq,
314     FILE        *fp
315 )
316 {
317     Re          *re;
318     time_t      now;
319
320     if ( rq == NULL ) {
321         return -1;
322     }
323
324 #ifdef NEW_LOGGING
325         LDAP_LOG (( "rq", LDAP_LEVEL_ENTRY, "Rq_write: "
326                 "re-write on-disk replication log\n" ));
327 #else
328     Debug( LDAP_DEBUG_ARGS, "re-write on-disk replication log\n",
329             0, 0, 0 );
330 #endif
331 #ifndef SEEK_SET
332 #define SEEK_SET 0
333 #endif
334     fseek( fp, 0L, SEEK_SET );  /* Go to beginning of file */
335     rq->rq_lock( rq );
336
337     for ( re = rq->rq_gethead( rq ); re != NULL; re = rq->rq_getnext( re )) {
338         if ( re->re_write( NULL, re, fp ) < 0 ) {
339             fflush( fp );
340             rq->rq_unlock( rq );
341             return -1;
342         }
343     }
344     fflush( fp );
345     sglob->srpos = ftell( fp ); /* update replog file position */
346     /* and truncate to correct len */
347     if ( ftruncate( fileno( fp ), sglob->srpos ) < 0 ) {
348 #ifdef NEW_LOGGING
349         LDAP_LOG (( "rq", LDAP_LEVEL_ERR, "Rq_write: "
350                 "Error truncating replication log: %s\n", sys_errlist[ errno ] ));
351 #else
352         Debug( LDAP_DEBUG_ANY, "Error truncating replication log: %s\n",
353                 sys_errlist[ errno ], 0, 0 );
354 #endif
355     }
356     rq->rq_ndel = 0;    /* reset count of deleted re's */
357     time( &now );
358     rq->rq_lasttrim = now;      /* reset last trim time */
359     rq->rq_unlock( rq );
360     return 0;
361 }
362
363
364 /*
365  * Check to see if the private slurpd replication log needs trimming.
366  * The current criteria are:
367  *  - The last trim was more than 5 minutes ago, *and*
368  *  - We've finished with at least 50 replication log entries since the
369  *    last time we re-wrote the replication log.
370  *
371  * Return 1 if replogfile should be trimmed, 0 if not.
372  * Any different policy should be implemented by replacing this function.
373  */
374 static int
375 Rq_needtrim(
376     Rq  *rq
377 )
378 {
379     int         rc = 0;
380     time_t      now;
381
382     if ( rq == NULL ) {
383         return 0;
384     }
385
386     rq->rq_lock( rq );
387
388     time( &now );
389
390     if ( now > ( rq->rq_lasttrim + TRIMCHECK_INTERVAL )) {
391         rc = ( rq->rq_ndel >= 50 );
392     } else {
393         rc = 0;
394     }
395     rq->rq_unlock( rq );
396     return rc;
397 }
398
399
400 /*
401  * Return counts of Re structs in the queue.
402  */
403 static int
404 Rq_getcount(
405     Rq  *rq,
406     int type
407 )
408 {
409     int count = 0;
410     Re  *re;
411
412     if ( rq == NULL ) {
413         return 0;
414     }
415
416     rq->rq_lock( rq );
417     if ( type == RQ_COUNT_ALL ) {
418         count = rq->rq_nre;
419     } else {
420         for ( re = rq->rq_gethead( rq ); re != NULL;
421                 re = rq->rq_getnext( re )) {
422             if ( type == RQ_COUNT_NZRC ) {
423                 if ( re->re_getrefcnt( re ) > 0 ) {
424                     count++;
425                 }
426             }
427         }
428     }
429     rq->rq_unlock( rq );
430     return count;
431 }
432
433
434 /* 
435  * Allocate and initialize an Rq object.
436  */
437 int
438 Rq_init(
439     Rq  **rq
440 )
441 {
442     /* Instantiate the struct */
443     (*rq) = (Rq *) malloc( sizeof( Rq ));
444     if ( *rq == NULL ) {
445         return -1;
446     }
447
448     /* Fill in all the function pointers */
449     (*rq)->rq_gethead = Rq_gethead;
450     (*rq)->rq_getnext = Rq_getnext;
451     (*rq)->rq_delhead = Rq_delhead;
452     (*rq)->rq_add = Rq_add;
453     (*rq)->rq_gc = Rq_gc;
454     (*rq)->rq_lock = Rq_lock;
455     (*rq)->rq_unlock = Rq_unlock;
456     (*rq)->rq_dump = Rq_dump;
457     (*rq)->rq_needtrim = Rq_needtrim;
458     (*rq)->rq_write = Rq_write;
459     (*rq)->rq_getcount = Rq_getcount;
460
461     /* Initialize private data */
462     ldap_pvt_thread_mutex_init( &((*rq)->rq_mutex) );
463     ldap_pvt_thread_cond_init( &((*rq)->rq_more) );
464     (*rq)->rq_head = NULL;
465     (*rq)->rq_tail = NULL;
466     (*rq)->rq_nre = 0;
467     (*rq)->rq_ndel = 0;
468     (*rq)->rq_lasttrim = (time_t) 0L;
469
470     return 0;
471 }