/* * 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 ) { #ifdef NEW_LOGGING LDAP_LOG ( SLURPD, ERR, "populate_queue: error: can't lock file \"%s\": %s\n", f, sys_errlist[ errno ], 0 ); #else Debug( LDAP_DEBUG_ANY, "error: can't lock file \"%s\": %s\n", f, sys_errlist[ errno ], 0 ); #endif return; } /* * Read replication records from fp and append them the * the queue. */ if ( fseek( fp, sglob->srpos, 0 ) < 0 ) { #ifdef NEW_LOGGING LDAP_LOG ( SLURPD, ERR, "populate_queue: error: can't seek to offset %ld in file \"%s\"\n", sglob->srpos, f, 0 ); #else Debug( LDAP_DEBUG_ANY, "error: can't seek to offset %ld in file \"%s\"\n", sglob->srpos, f, 0 ); #endif } else { 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'; } #ifdef NEW_LOGGING LDAP_LOG ( SLURPD, ERR, "populate_queue: error: malformed replog entry " "(begins with \"%s\")\n", p, 0, 0 ); #else Debug( LDAP_DEBUG_ANY, "error: malformed replog entry (begins with \"%s\")\n", p, 0, 0 ); #endif } free( p ); ldap_pvt_thread_yield(); } sglob->srpos = ftell( fp ); } (void) relinquish_lock( f, fp, lfp ); }
void unlock() { HPX_ITT_SYNC_RELEASING(this); relinquish_lock(); util::unregister_lock(this); HPX_ITT_SYNC_RELEASED(this); }
/* * Read status information from disk file. */ static int St_read( St *st ) { FILE *fp; FILE *lfp; char buf[ 255 ]; int i; int rc; char *hostname, *port, *timestamp, *seq, *p, *t; int found; if ( st == NULL ) { return -1; } ldap_pvt_thread_mutex_lock( &(st->st_mutex )); if ( access( sglob->slurpd_status_file, F_OK ) < 0 ) { /* * File doesn't exist, so create it and return. */ if (( fp = fopen( sglob->slurpd_status_file, "w" )) == NULL ) { #ifdef NEW_LOGGING LDAP_LOG ( SLURPD, ERR, "St_write: " "Error: cannot create status file \"%s\"\n", sglob->slurpd_status_file, 0, 0 ); #else Debug( LDAP_DEBUG_ANY, "Error: cannot create status file \"%s\"\n", sglob->slurpd_status_file, 0, 0 ); #endif ldap_pvt_thread_mutex_unlock( &(st->st_mutex )); return -1; } (void) fclose( fp ); ldap_pvt_thread_mutex_unlock( &(st->st_mutex )); #ifdef NEW_LOGGING LDAP_LOG ( SLURPD, DETAIL1, "St_write: " "No status file found, defaulting values\n", 0, 0, 0 ); #else Debug( LDAP_DEBUG_ARGS, "No status file found, defaulting values\n", 0, 0, 0 ); #endif return 0; } if (( rc = acquire_lock( sglob->slurpd_status_file, &fp, &lfp)) < 0 ) { ldap_pvt_thread_mutex_unlock( &(st->st_mutex )); return 0; } while ( fgets( buf, sizeof( buf ), fp ) != NULL ) { p = buf; hostname = p; if (( t = strchr( p, ':' )) == NULL ) { goto bad; } *t++ = '\0'; p = t; port = p; if (( t = strchr( p, ':' )) == NULL ) { goto bad; } *t++ = '\0'; p = t; timestamp = p; if (( t = strchr( p, ':' )) == NULL ) { goto bad; } *t++ = '\0'; seq = t; if (( t = strchr( seq, '\n' )) != NULL ) { *t = '\0'; } found = 0; for ( i = 0; i < sglob->st->st_nreplicas; i++ ) { if ( !strcmp( hostname, sglob->st->st_data[ i ]->hostname ) && atoi( port ) == sglob->st->st_data[ i ]->port ) { found = 1; sglob->st->st_data[ i ]->last = atol( timestamp ); sglob->st->st_data[ i ]->seq = atoi( seq ); break; } } if ( found ) { char tbuf[ 255 ]; sprintf( tbuf, "%s:%s (timestamp %s.%s)", hostname, port, timestamp, seq ); #ifdef NEW_LOGGING LDAP_LOG ( SLURPD, DETAIL1, "St_write: " "Retrieved state information for %s\n", tbuf, 0, 0 ); #else Debug( LDAP_DEBUG_ARGS, "Retrieved state information for %s\n", tbuf, 0, 0 ); #endif } else { #ifdef NEW_LOGGING LDAP_LOG ( SLURPD, WARNING, "St_write: " "Warning: saved state for %s:%s, not a known replica\n", hostname, port, 0 ); #else Debug( LDAP_DEBUG_ANY, "Warning: saved state for %s:%s, not a known replica\n", hostname, port, 0 ); #endif } } (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp); ldap_pvt_thread_mutex_unlock( &(st->st_mutex )); return 0; bad: (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp); ldap_pvt_thread_mutex_unlock( &(st->st_mutex )); return -1; }
/* * 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. */ void * fm( void *arg ) { int rc; int i; fd_set readfds; /* Set up our signal handlers: * SIG{TERM,INT,HUP} causes a shutdown */ (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; } /* * 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 ); } FD_ZERO( &readfds ); while ( !sglob->slurpd_shutdown ) { if ( file_nonempty( sglob->slapd_replogfile )) { /* New work found - copy to slurpd replog file */ #ifdef NEW_LOGGING LDAP_LOG ( SLURPD, ARGS, "fm: new work in %s\n", sglob->slapd_replogfile, 0, 0 ); #else Debug( LDAP_DEBUG_ARGS, "new work in %s\n", sglob->slapd_replogfile, 0, 0 ); #endif if (( rc = copy_replog( sglob->slapd_replogfile, sglob->slurpd_replogfile )) == 0 ) { populate_queue( sglob->slurpd_replogfile ); } else { if ( rc < 0 ) { #ifdef NEW_LOGGING LDAP_LOG ( SLURPD, CRIT, "fm: Fatal error while copying replication log\n" , 0, 0, 0); #else Debug( LDAP_DEBUG_ANY, "Fatal error while copying replication log\n", 0, 0, 0 ); #endif 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 ); } /* 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 ) { #ifdef NEW_LOGGING LDAP_LOG ( SLURPD, ERR, "fm: Error: cannot acquire lock on \"%s\" for trimming\n", sglob->slurpd_replogfile, 0, 0 ); #else Debug( LDAP_DEBUG_ANY, "Error: cannot acquire lock on \"%s\" for trimming\n", sglob->slurpd_replogfile, 0, 0 ); #endif } else { sglob->rq->rq_write( sglob->rq, fp ); (void) relinquish_lock( sglob->slurpd_replogfile, fp, lfp ); } } } 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 */ #ifdef NEW_LOGGING LDAP_LOG ( SLURPD, RESULTS, "fm: exiting\n", 0, 0, 0 ); #else Debug( LDAP_DEBUG_ARGS, "fm: exiting\n", 0, 0, 0 ); #endif return NULL; }