#include "portable.h"
#include <stdio.h>
#include <ac/stdlib.h>
#include <ac/string.h>
#include <ac/signal.h>
#include <ac/socket.h>
#include <ac/unistd.h>
#include "slurp.h"
#include "globals.h"
static char *get_record LDAP_P(( FILE * ));
static void populate_queue LDAP_P(( char *f ));
void *
fm(
void *arg
)
{
int rc;
int i;
fd_set readfds;
(void) SIGNAL( SIGTERM, slurp_set_shutdown );
(void) SIGNAL( SIGINT, slurp_set_shutdown );
#ifdef SIGHUP
(void) SIGNAL( SIGHUP, slurp_set_shutdown );
#endif
#if defined(SIGBREAK) && defined(HAVE_NT_SERVICE_MANAGER)
(void) SIGNAL( SIGBREAK, do_nothing );
#endif
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 ));
return NULL;
}
if ( file_nonempty( sglob->slurpd_replogfile )) {
populate_queue( sglob->slurpd_replogfile );
}
FD_ZERO( &readfds );
while ( !sglob->slurpd_shutdown ) {
if ( file_nonempty( sglob->slapd_replogfile )) {
Debug( LDAP_DEBUG_ARGS, "new work in %s\n",
sglob->slapd_replogfile, 0, 0 );
if (( rc = copy_replog( sglob->slapd_replogfile,
sglob->slurpd_replogfile )) == 0 ) {
populate_queue( sglob->slurpd_replogfile );
} else {
if ( rc < 0 ) {
Debug( LDAP_DEBUG_ANY,
"Fatal error while copying replication log\n",
0, 0, 0 );
sglob->slurpd_shutdown = 1;
}
}
} else {
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 );
}
sglob->rq->rq_gc( sglob->rq );
if ( sglob->rq->rq_needtrim( sglob->rq )) {
FILE *fp, *lfp;
if (( rc = acquire_lock( sglob->slurpd_replogfile, &fp,
&lfp )) < 0 ) {
Debug( LDAP_DEBUG_ANY,
"Error: cannot acquire lock on \"%s\" for trimming\n",
sglob->slurpd_replogfile, 0, 0 );
} else {
sglob->rq->rq_write( sglob->rq, fp );
(void) relinquish_lock( sglob->slurpd_replogfile, fp, lfp );
}
}
}
sglob->rq->rq_lock( sglob->rq );
ldap_pvt_thread_cond_broadcast( &(sglob->rq->rq_more) );
for ( i = 0; i < sglob->num_replicas; i++ ) {
(sglob->replicas[ i ])->ri_wake( sglob->replicas[ i ]);
}
sglob->rq->rq_unlock( sglob->rq );
Debug( LDAP_DEBUG_ARGS, "fm: exiting\n", 0, 0, 0 );
return NULL;
}
RETSIGTYPE
slurp_set_shutdown(int sig)
{
sglob->slurpd_shutdown = 1;
tcp_write( sglob->wake_sds[1], "0", 1);
(void) SIGNAL_REINSTALL( sig, slurp_set_shutdown );
}
RETSIGTYPE
do_nothing(int sig)
{
(void) SIGNAL_REINSTALL( sig, do_nothing );
}
static void
populate_queue(
char *f
)
{
FILE *fp, *lfp;
char *p;
if ( acquire_lock( f, &fp, &lfp ) < 0 ) {
Debug( LDAP_DEBUG_ANY,
"error: can't lock file \"%s\": %s\n",
f, sys_errlist[ errno ], 0 );
return;
}
if ( fseek( fp, sglob->srpos, 0 ) < 0 ) {
Debug( LDAP_DEBUG_ANY,
"error: can't seek to offset %ld in file \"%s\"\n",
sglob->srpos, f, 0 );
} else {
while (( p = get_record( fp )) != NULL ) {
if ( sglob->rq->rq_add( sglob->rq, p ) < 0 ) {
char *t;
if (( t = strchr( p, '\n' )) != NULL ) {
*t = '\0';
}
Debug( LDAP_DEBUG_ANY,
"error: malformed replog entry (begins with \"%s\")\n",
p, 0, 0 );
}
free( p );
ldap_pvt_thread_yield();
}
sglob->srpos = ftell( fp );
}
(void) relinquish_lock( f, fp, lfp );
}
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 )) {
while ( lcur + len + 1 > lmax ) {
lmax += BUFSIZ;
buf = (char *) ch_realloc( buf, lmax );
}
strcpy( buf + lcur, line );
lcur += len;
}
return( buf );
}