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