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.
17 #define DISABLE_BRIDGE
21 #include <ac/string.h>
22 #include <sys/signal.h>
31 extern void do_admin LDAP_P((void));
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 void set_shutdown LDAP_P((void));
42 void do_nothing LDAP_P((void));
44 #ifdef DECL_SYS_ERRLIST
45 extern char *sys_errlist[];
46 #endif /* DECL_SYS_ERRLIST */
51 * Main file manager routine. Watches for new data to be appended to the
52 * slapd replication log. When new data is appended, fm does the following:
53 * - appends the data to slurpd's private copy of the replication log.
54 * - truncates the slapd replog
55 * - adds items to the internal queue of replication work to do
56 * - signals the replication threads to let them know new work has arrived.
65 /* Set up our signal handlers:
66 * SIG{TERM,INT,HUP} causes a shutdown
67 * SIG(STKFLT|USR1) - does nothing, used to wake up sleeping threads.
68 * SIG(UNUSED|USR2) - causes slurpd to read its administrative interface file.
69 * (not yet implemented).
72 (void) SIGNAL( SIGSTKFLT, (void *) do_nothing );
74 (void) SIGNAL( SIGUSR1, (void *) do_nothing );
77 (void) SIGNAL( SIGUNUSED, (void *) do_admin );
79 (void) SIGNAL( SIGUSR2, (void *) do_admin );
81 (void) SIGNAL( SIGTERM, (void *) set_shutdown );
82 (void) SIGNAL( SIGINT, (void *) set_shutdown );
83 (void) SIGNAL( SIGHUP, (void *) set_shutdown );
85 if ( sglob->one_shot_mode ) {
86 if ( file_nonempty( sglob->slapd_replogfile )) {
87 populate_queue( sglob->slapd_replogfile );
89 printf( "Processing in one-shot mode:\n" );
90 printf( "%d total replication records in file,\n",
91 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_ALL ));
92 printf( "%d replication records to process.\n",
93 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_NZRC ));
97 * There may be some leftover replication records in our own
98 * copy of the replication log. If any exist, add them to the
101 if ( file_nonempty( sglob->slurpd_replogfile )) {
102 populate_queue( sglob->slurpd_replogfile );
106 while ( !sglob->slurpd_shutdown ) {
107 if ( file_nonempty( sglob->slapd_replogfile )) {
108 /* New work found - copy to slurpd replog file */
109 Debug( LDAP_DEBUG_ARGS, "new work in %s\n",
110 sglob->slapd_replogfile, 0, 0 );
111 if (( rc = copy_replog( sglob->slapd_replogfile,
112 sglob->slurpd_replogfile )) == 0 ) {
113 populate_queue( sglob->slurpd_replogfile );
116 Debug( LDAP_DEBUG_ANY,
117 "Fatal error while copying replication log\n",
119 sglob->slurpd_shutdown = 1;
123 tsleep( sglob->no_work_interval );
126 /* Garbage-collect queue */
127 sglob->rq->rq_gc( sglob->rq );
129 /* Trim replication log file, if needed */
130 if ( sglob->rq->rq_needtrim( sglob->rq )) {
132 if (( rc = acquire_lock( sglob->slurpd_replogfile, &fp,
134 Debug( LDAP_DEBUG_ANY,
135 "Error: cannot acquire lock on \"%s\" for trimming\n",
136 sglob->slurpd_replogfile, 0, 0 );
138 sglob->rq->rq_write( sglob->rq, fp );
139 (void) relinquish_lock( sglob->slurpd_replogfile, fp, lfp );
143 Debug( LDAP_DEBUG_ARGS, "fm: exiting\n", 0, 0, 0 );
150 * Set a global flag which signals that we're shutting down.
157 sglob->slurpd_shutdown = 1; /* set flag */
159 pthread_kill( sglob->fm_tid, SIGSTKFLT ); /* wake up file mgr */
161 pthread_kill( sglob->fm_tid, SIGUSR1 ); /* wake up file mgr */
163 sglob->rq->rq_lock( sglob->rq ); /* lock queue */
164 pthread_cond_broadcast( &(sglob->rq->rq_more) ); /* wake repl threads */
165 for ( i = 0; i < sglob->num_replicas; i++ ) {
166 (sglob->replicas[ i ])->ri_wake( sglob->replicas[ i ]);
168 sglob->rq->rq_unlock( sglob->rq ); /* unlock queue */
169 (void) SIGNAL( SIGTERM, (void *) set_shutdown ); /* reinstall handlers */
170 (void) SIGNAL( SIGINT, (void *) set_shutdown );
171 (void) SIGNAL( SIGHUP, (void *) set_shutdown );
178 * A do-nothing signal handler.
184 (void) SIGNAL( SIGSTKFLT, (void *) do_nothing );
186 (void) SIGNAL( SIGUSR1, (void *) do_nothing );
194 * Open the slurpd replication log, seek to our last known position, and
195 * process any pending replication entries.
206 if ( acquire_lock( f, &fp, &lfp ) < 0 ) {
207 Debug( LDAP_DEBUG_ANY,
208 "error: can't lock file \"%s\": %s\n",
209 f, sys_errlist[ errno ], 0 );
214 * Read replication records from fp and append them the
217 if ( fseek( fp, sglob->srpos, 0 ) < 0 ) {
218 Debug( LDAP_DEBUG_ANY,
219 "error: can't seek to offset %ld in file \"%s\"\n",
220 sglob->srpos, f, 0 );
223 while (( p = get_record( fp )) != NULL ) {
224 if ( sglob->rq->rq_add( sglob->rq, p ) < 0 ) {
226 /* Print an error message. Only print first line. */
227 if (( t = strchr( p, '\n' )) != NULL ) {
230 Debug( LDAP_DEBUG_ANY,
231 "error: malformed replog entry (begins with \"%s\")\n",
237 sglob->srpos = ftell( fp );
238 (void) relinquish_lock( f, fp, lfp );
245 * Get the next "record" from the file pointed to by fp. A "record"
246 * is delimited by two consecutive newlines. Returns NULL on EOF.
254 static char line[BUFSIZ];
256 static int lcur, lmax;
260 while (( fgets( line, sizeof(line), fp ) != NULL ) &&
261 (( len = strlen( line )) > 1 )) {
262 while ( lcur + len + 1 > lmax ) {
264 buf = (char *) ch_realloc( buf, lmax );
266 strcpy( buf + lcur, line );