1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-27 02:19:00 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2004-01-02 03:15:16 +08:00
|
|
|
* Copyright 1998-2004 The OpenLDAP Foundation.
|
2003-11-27 02:19:00 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
2001-01-21 01:49:05 +08:00
|
|
|
*/
|
2003-11-27 02:19:00 +08:00
|
|
|
/* Portions Copyright (c) 1996 Regents of the University of Michigan.
|
1998-08-09 08:43:13 +08:00
|
|
|
* 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.
|
|
|
|
*/
|
2003-11-27 02:19:00 +08:00
|
|
|
/* ACKNOWLEDGEMENTS:
|
|
|
|
* This work was originally developed by the University of Michigan
|
|
|
|
* (as part of U-MICH LDAP).
|
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* fm.c - file management routines.
|
|
|
|
*/
|
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include "portable.h"
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <stdio.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
|
1999-06-03 08:37:44 +08:00
|
|
|
#include <ac/stdlib.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/signal.h>
|
2003-03-24 22:57:10 +08:00
|
|
|
#include <ac/socket.h>
|
2003-05-15 19:45:57 +08:00
|
|
|
#include <ac/unistd.h>
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
#include "slurp.h"
|
|
|
|
#include "globals.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Forward references
|
|
|
|
*/
|
1998-10-25 09:41:42 +08:00
|
|
|
static char *get_record LDAP_P(( FILE * ));
|
|
|
|
static void populate_queue LDAP_P(( char *f ));
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Main file manager routine. Watches for new data to be appended to the
|
|
|
|
* slapd replication log. When new data is appended, fm does the following:
|
|
|
|
* - appends the data to slurpd's private copy of the replication log.
|
|
|
|
* - truncates the slapd replog
|
|
|
|
* - adds items to the internal queue of replication work to do
|
|
|
|
* - signals the replication threads to let them know new work has arrived.
|
|
|
|
*/
|
Protoized, moved extern definitions to .h files, fixed related bugs.
Most function and variable definitions are now preceded by its extern
definition, for error checking. Retyped a number of functions, usually
to return void. Fixed a number of printf format errors.
API changes (in ldap/include):
Added avl_dup_ok, avl_prefixapply, removed ber_fatten (probably typo
for ber_flatten), retyped ldap_sort_strcasecmp, grew lutil.h.
A number of `extern' declarations are left (some added by protoize), to
be cleaned away later. Mostly strdup(), strcasecmp(), mktemp(), optind,
optarg, errno.
1998-11-16 06:40:11 +08:00
|
|
|
void *
|
1998-08-09 08:43:13 +08:00
|
|
|
fm(
|
|
|
|
void *arg
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rc;
|
2003-03-24 22:57:10 +08:00
|
|
|
int i;
|
|
|
|
fd_set readfds;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/* Set up our signal handlers:
|
|
|
|
* SIG{TERM,INT,HUP} causes a shutdown
|
|
|
|
*/
|
2003-03-03 19:47:16 +08:00
|
|
|
(void) SIGNAL( SIGTERM, slurp_set_shutdown );
|
|
|
|
(void) SIGNAL( SIGINT, slurp_set_shutdown );
|
1999-03-02 06:37:05 +08:00
|
|
|
#ifdef SIGHUP
|
2003-03-03 19:47:16 +08:00
|
|
|
(void) SIGNAL( SIGHUP, slurp_set_shutdown );
|
|
|
|
#endif
|
2003-03-24 15:49:59 +08:00
|
|
|
#if defined(SIGBREAK) && defined(HAVE_NT_SERVICE_MANAGER)
|
|
|
|
(void) SIGNAL( SIGBREAK, do_nothing );
|
1999-03-02 06:37:05 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( sglob->one_shot_mode ) {
|
|
|
|
if ( file_nonempty( sglob->slapd_replogfile )) {
|
|
|
|
populate_queue( sglob->slapd_replogfile );
|
|
|
|
}
|
|
|
|
printf( "Processing in one-shot mode:\n" );
|
|
|
|
printf( "%d total replication records in file,\n",
|
|
|
|
sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_ALL ));
|
|
|
|
printf( "%d replication records to process.\n",
|
|
|
|
sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_NZRC ));
|
Protoized, moved extern definitions to .h files, fixed related bugs.
Most function and variable definitions are now preceded by its extern
definition, for error checking. Retyped a number of functions, usually
to return void. Fixed a number of printf format errors.
API changes (in ldap/include):
Added avl_dup_ok, avl_prefixapply, removed ber_fatten (probably typo
for ber_flatten), retyped ldap_sort_strcasecmp, grew lutil.h.
A number of `extern' declarations are left (some added by protoize), to
be cleaned away later. Mostly strdup(), strcasecmp(), mktemp(), optind,
optarg, errno.
1998-11-16 06:40:11 +08:00
|
|
|
return NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* There may be some leftover replication records in our own
|
|
|
|
* copy of the replication log. If any exist, add them to the
|
|
|
|
* queue.
|
|
|
|
*/
|
|
|
|
if ( file_nonempty( sglob->slurpd_replogfile )) {
|
|
|
|
populate_queue( sglob->slurpd_replogfile );
|
|
|
|
}
|
|
|
|
|
2003-03-24 22:57:10 +08:00
|
|
|
FD_ZERO( &readfds );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
while ( !sglob->slurpd_shutdown ) {
|
|
|
|
if ( file_nonempty( sglob->slapd_replogfile )) {
|
|
|
|
/* New work found - copy to slurpd replog file */
|
2002-04-16 01:57:44 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG ( SLURPD, ARGS,
|
|
|
|
"fm: new work in %s\n", sglob->slapd_replogfile, 0, 0 );
|
2002-04-16 01:57:44 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ARGS, "new work in %s\n",
|
|
|
|
sglob->slapd_replogfile, 0, 0 );
|
2002-04-16 01:57:44 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
if (( rc = copy_replog( sglob->slapd_replogfile,
|
|
|
|
sglob->slurpd_replogfile )) == 0 ) {
|
|
|
|
populate_queue( sglob->slurpd_replogfile );
|
|
|
|
} else {
|
|
|
|
if ( rc < 0 ) {
|
2002-04-16 01:57:44 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG ( SLURPD, CRIT,
|
|
|
|
"fm: Fatal error while copying replication log\n" , 0, 0, 0);
|
2002-04-16 01:57:44 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"Fatal error while copying replication log\n",
|
|
|
|
0, 0, 0 );
|
2002-04-16 01:57:44 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
sglob->slurpd_shutdown = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2003-03-24 22:57:10 +08:00
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
FD_SET( sglob->wake_sds[0], &readfds );
|
|
|
|
tv.tv_sec = sglob->no_work_interval;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
rc = select( sglob->wake_sds[0]+1, &readfds, NULL, NULL, &tv );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Garbage-collect queue */
|
|
|
|
sglob->rq->rq_gc( sglob->rq );
|
|
|
|
|
|
|
|
/* Trim replication log file, if needed */
|
|
|
|
if ( sglob->rq->rq_needtrim( sglob->rq )) {
|
|
|
|
FILE *fp, *lfp;
|
|
|
|
if (( rc = acquire_lock( sglob->slurpd_replogfile, &fp,
|
|
|
|
&lfp )) < 0 ) {
|
2002-04-16 01:57:44 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG ( SLURPD, ERR,
|
2002-04-16 01:57:44 +08:00
|
|
|
"fm: Error: cannot acquire lock on \"%s\" for trimming\n",
|
2002-07-12 04:33:24 +08:00
|
|
|
sglob->slurpd_replogfile, 0, 0 );
|
2002-04-16 01:57:44 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"Error: cannot acquire lock on \"%s\" for trimming\n",
|
|
|
|
sglob->slurpd_replogfile, 0, 0 );
|
2002-04-16 01:57:44 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
|
|
|
sglob->rq->rq_write( sglob->rq, fp );
|
|
|
|
(void) relinquish_lock( sglob->slurpd_replogfile, fp, lfp );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-03-24 22:57:10 +08:00
|
|
|
sglob->rq->rq_lock( sglob->rq ); /* lock queue */
|
|
|
|
ldap_pvt_thread_cond_broadcast( &(sglob->rq->rq_more) ); /* wake repl threads */
|
|
|
|
for ( i = 0; i < sglob->num_replicas; i++ ) {
|
|
|
|
(sglob->replicas[ i ])->ri_wake( sglob->replicas[ i ]);
|
|
|
|
}
|
|
|
|
sglob->rq->rq_unlock( sglob->rq ); /* unlock queue */
|
2002-04-16 01:57:44 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG ( SLURPD, RESULTS, "fm: exiting\n", 0, 0, 0 );
|
2002-04-16 01:57:44 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ARGS, "fm: exiting\n", 0, 0, 0 );
|
2002-04-16 01:57:44 +08:00
|
|
|
#endif
|
Protoized, moved extern definitions to .h files, fixed related bugs.
Most function and variable definitions are now preceded by its extern
definition, for error checking. Retyped a number of functions, usually
to return void. Fixed a number of printf format errors.
API changes (in ldap/include):
Added avl_dup_ok, avl_prefixapply, removed ber_fatten (probably typo
for ber_flatten), retyped ldap_sort_strcasecmp, grew lutil.h.
A number of `extern' declarations are left (some added by protoize), to
be cleaned away later. Mostly strdup(), strcasecmp(), mktemp(), optind,
optarg, errno.
1998-11-16 06:40:11 +08:00
|
|
|
return NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set a global flag which signals that we're shutting down.
|
|
|
|
*/
|
2003-03-03 19:47:16 +08:00
|
|
|
RETSIGTYPE
|
|
|
|
slurp_set_shutdown(int sig)
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
sglob->slurpd_shutdown = 1; /* set flag */
|
2003-03-24 22:57:10 +08:00
|
|
|
tcp_write( sglob->wake_sds[1], "0", 1); /* wake up file mgr */
|
|
|
|
|
2003-03-03 19:47:16 +08:00
|
|
|
(void) SIGNAL_REINSTALL( sig, slurp_set_shutdown ); /* reinstall handlers */
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A do-nothing signal handler.
|
|
|
|
*/
|
1998-10-25 09:41:42 +08:00
|
|
|
RETSIGTYPE
|
1999-03-02 06:37:05 +08:00
|
|
|
do_nothing(int sig)
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
2000-07-22 08:37:51 +08:00
|
|
|
(void) SIGNAL_REINSTALL( sig, do_nothing );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the slurpd replication log, seek to our last known position, and
|
|
|
|
* process any pending replication entries.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
populate_queue(
|
|
|
|
char *f
|
|
|
|
)
|
|
|
|
{
|
|
|
|
FILE *fp, *lfp;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if ( acquire_lock( f, &fp, &lfp ) < 0 ) {
|
2002-04-16 01:57:44 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG ( SLURPD, ERR,
|
2002-04-16 01:57:44 +08:00
|
|
|
"populate_queue: error: can't lock file \"%s\": %s\n",
|
2002-07-12 04:33:24 +08:00
|
|
|
f, sys_errlist[ errno ], 0 );
|
2002-04-16 01:57:44 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"error: can't lock file \"%s\": %s\n",
|
|
|
|
f, sys_errlist[ errno ], 0 );
|
2002-04-16 01:57:44 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read replication records from fp and append them the
|
|
|
|
* the queue.
|
|
|
|
*/
|
|
|
|
if ( fseek( fp, sglob->srpos, 0 ) < 0 ) {
|
2002-04-16 01:57:44 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG ( SLURPD, ERR,
|
2002-04-16 01:57:44 +08:00
|
|
|
"populate_queue: error: can't seek to offset %ld in file \"%s\"\n",
|
2002-07-12 04:33:24 +08:00
|
|
|
sglob->srpos, f, 0 );
|
2002-04-16 01:57:44 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"error: can't seek to offset %ld in file \"%s\"\n",
|
|
|
|
sglob->srpos, f, 0 );
|
2002-04-16 01:57:44 +08:00
|
|
|
#endif
|
1998-11-12 03:17:25 +08:00
|
|
|
} else {
|
1998-08-09 08:43:13 +08:00
|
|
|
while (( p = get_record( fp )) != NULL ) {
|
|
|
|
if ( sglob->rq->rq_add( sglob->rq, p ) < 0 ) {
|
|
|
|
char *t;
|
|
|
|
/* Print an error message. Only print first line. */
|
|
|
|
if (( t = strchr( p, '\n' )) != NULL ) {
|
|
|
|
*t = '\0';
|
|
|
|
}
|
2002-04-16 01:57:44 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG ( SLURPD, ERR,
|
2002-04-16 01:57:44 +08:00
|
|
|
"populate_queue: error: malformed replog entry "
|
2002-07-12 04:33:24 +08:00
|
|
|
"(begins with \"%s\")\n", p, 0, 0 );
|
2002-04-16 01:57:44 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"error: malformed replog entry (begins with \"%s\")\n",
|
|
|
|
p, 0, 0 );
|
2002-04-16 01:57:44 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
free( p );
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_yield();
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
sglob->srpos = ftell( fp );
|
1998-11-12 03:17:25 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
(void) relinquish_lock( f, fp, lfp );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the next "record" from the file pointed to by fp. A "record"
|
|
|
|
* is delimited by two consecutive newlines. Returns NULL on EOF.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
get_record(
|
|
|
|
FILE *fp
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
static char line[BUFSIZ];
|
|
|
|
char *buf = NULL;
|
|
|
|
static int lcur, lmax;
|
|
|
|
|
|
|
|
lcur = lmax = 0;
|
|
|
|
|
|
|
|
while (( fgets( line, sizeof(line), fp ) != NULL ) &&
|
|
|
|
(( len = strlen( line )) > 1 )) {
|
1999-07-30 05:25:39 +08:00
|
|
|
|
|
|
|
while ( lcur + len + 1 > lmax ) {
|
|
|
|
lmax += BUFSIZ;
|
|
|
|
buf = (char *) ch_realloc( buf, lmax );
|
|
|
|
}
|
|
|
|
strcpy( buf + lcur, line );
|
|
|
|
lcur += len;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
return( buf );
|
|
|
|
}
|