2 * Copyright (c) 1996 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
14 * fm.c - file management routines.
21 #include <ac/string.h>
22 #include <ac/signal.h>
31 extern RETSIGTYPE do_admin LDAP_P((int));
32 extern int file_nonempty LDAP_P(( char * ));
33 extern int acquire_lock LDAP_P((char *, FILE **, FILE ** ));
34 extern int relinquish_lock LDAP_P((char *, FILE *, FILE * ));
39 static char *get_record LDAP_P(( FILE * ));
40 static void populate_queue LDAP_P(( char *f ));
41 static RETSIGTYPE set_shutdown LDAP_P((int));
42 RETSIGTYPE do_nothing LDAP_P((int));
46 * Main file manager routine. Watches for new data to be appended to the
47 * slapd replication log. When new data is appended, fm does the following:
48 * - appends the data to slurpd's private copy of the replication log.
49 * - truncates the slapd replog
50 * - adds items to the internal queue of replication work to do
51 * - signals the replication threads to let them know new work has arrived.
60 /* Set up our signal handlers:
61 * SIG{TERM,INT,HUP} causes a shutdown
62 * SIG(STKFLT|USR1) - does nothing, used to wake up sleeping threads.
63 * SIG(UNUSED|USR2) - causes slurpd to read its administrative interface file.
64 * (not yet implemented).
66 #ifdef HAVE_LINUX_THREADS
67 (void) SIGNAL( SIGSTKFLT, do_nothing );
68 (void) SIGNAL( SIGUNUSED, do_admin );
70 (void) SIGNAL( SIGUSR1, do_nothing );
71 (void) SIGNAL( SIGUSR2, do_admin );
73 (void) SIGNAL( SIGTERM, set_shutdown );
74 (void) SIGNAL( SIGINT, set_shutdown );
75 (void) SIGNAL( SIGHUP, set_shutdown );
77 if ( sglob->one_shot_mode ) {
78 if ( file_nonempty( sglob->slapd_replogfile )) {
79 populate_queue( sglob->slapd_replogfile );
81 printf( "Processing in one-shot mode:\n" );
82 printf( "%d total replication records in file,\n",
83 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_ALL ));
84 printf( "%d replication records to process.\n",
85 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_NZRC ));
89 * There may be some leftover replication records in our own
90 * copy of the replication log. If any exist, add them to the
93 if ( file_nonempty( sglob->slurpd_replogfile )) {
94 populate_queue( sglob->slurpd_replogfile );
98 while ( !sglob->slurpd_shutdown ) {
99 if ( file_nonempty( sglob->slapd_replogfile )) {
100 /* New work found - copy to slurpd replog file */
101 Debug( LDAP_DEBUG_ARGS, "new work in %s\n",
102 sglob->slapd_replogfile, 0, 0 );
103 if (( rc = copy_replog( sglob->slapd_replogfile,
104 sglob->slurpd_replogfile )) == 0 ) {
105 populate_queue( sglob->slurpd_replogfile );
108 Debug( LDAP_DEBUG_ANY,
109 "Fatal error while copying replication log\n",
111 sglob->slurpd_shutdown = 1;
115 tsleep( sglob->no_work_interval );
118 /* Garbage-collect queue */
119 sglob->rq->rq_gc( sglob->rq );
121 /* Trim replication log file, if needed */
122 if ( sglob->rq->rq_needtrim( sglob->rq )) {
124 if (( rc = acquire_lock( sglob->slurpd_replogfile, &fp,
126 Debug( LDAP_DEBUG_ANY,
127 "Error: cannot acquire lock on \"%s\" for trimming\n",
128 sglob->slurpd_replogfile, 0, 0 );
130 sglob->rq->rq_write( sglob->rq, fp );
131 (void) relinquish_lock( sglob->slurpd_replogfile, fp, lfp );
135 Debug( LDAP_DEBUG_ARGS, "fm: exiting\n", 0, 0, 0 );
142 * Set a global flag which signals that we're shutting down.
149 sglob->slurpd_shutdown = 1; /* set flag */
150 #ifdef HAVE_LINUX_THREADS
151 pthread_kill( sglob->fm_tid, SIGSTKFLT ); /* wake up file mgr */
153 pthread_kill( sglob->fm_tid, SIGUSR1 ); /* wake up file mgr */
155 sglob->rq->rq_lock( sglob->rq ); /* lock queue */
156 pthread_cond_broadcast( &(sglob->rq->rq_more) ); /* wake repl threads */
157 for ( i = 0; i < sglob->num_replicas; i++ ) {
158 (sglob->replicas[ i ])->ri_wake( sglob->replicas[ i ]);
160 sglob->rq->rq_unlock( sglob->rq ); /* unlock queue */
161 (void) SIGNAL( SIGTERM, set_shutdown ); /* reinstall handlers */
162 (void) SIGNAL( SIGINT, set_shutdown );
163 (void) SIGNAL( SIGHUP, set_shutdown );
170 * A do-nothing signal handler.
175 #ifdef HAVE_LINUX_THREADS
176 (void) SIGNAL( SIGSTKFLT, do_nothing );
178 (void) SIGNAL( SIGUSR1, do_nothing );
186 * Open the slurpd replication log, seek to our last known position, and
187 * process any pending replication entries.
198 if ( acquire_lock( f, &fp, &lfp ) < 0 ) {
199 Debug( LDAP_DEBUG_ANY,
200 "error: can't lock file \"%s\": %s\n",
201 f, sys_errlist[ errno ], 0 );
206 * Read replication records from fp and append them the
209 if ( fseek( fp, sglob->srpos, 0 ) < 0 ) {
210 Debug( LDAP_DEBUG_ANY,
211 "error: can't seek to offset %ld in file \"%s\"\n",
212 sglob->srpos, f, 0 );
215 while (( p = get_record( fp )) != NULL ) {
216 if ( sglob->rq->rq_add( sglob->rq, p ) < 0 ) {
218 /* Print an error message. Only print first line. */
219 if (( t = strchr( p, '\n' )) != NULL ) {
222 Debug( LDAP_DEBUG_ANY,
223 "error: malformed replog entry (begins with \"%s\")\n",
229 sglob->srpos = ftell( fp );
230 (void) relinquish_lock( f, fp, lfp );
237 * Get the next "record" from the file pointed to by fp. A "record"
238 * is delimited by two consecutive newlines. Returns NULL on EOF.
246 static char line[BUFSIZ];
248 static int lcur, lmax;
252 while (( fgets( line, sizeof(line), fp ) != NULL ) &&
253 (( len = strlen( line )) > 1 )) {
254 while ( lcur + len + 1 > lmax ) {
256 buf = (char *) ch_realloc( buf, lmax );
258 strcpy( buf + lcur, line );