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.
20 #include <ac/string.h>
21 #include <sys/signal.h>
30 extern void do_admin LDAP_P((void));
31 extern int file_nonempty LDAP_P(( char * ));
32 extern int acquire_lock LDAP_P((char *, FILE **, FILE ** ));
33 extern int relinquish_lock LDAP_P((char *, FILE *, FILE * ));
38 static char *get_record LDAP_P(( FILE * ));
39 static void populate_queue LDAP_P(( char *f ));
40 static void set_shutdown LDAP_P((void));
41 void do_nothing LDAP_P((void));
43 #ifdef DECL_SYS_ERRLIST
44 extern char *sys_errlist[];
45 #endif /* DECL_SYS_ERRLIST */
50 * Main file manager routine. Watches for new data to be appended to the
51 * slapd replication log. When new data is appended, fm does the following:
52 * - appends the data to slurpd's private copy of the replication log.
53 * - truncates the slapd replog
54 * - adds items to the internal queue of replication work to do
55 * - signals the replication threads to let them know new work has arrived.
64 /* Set up our signal handlers:
65 * SIG{TERM,INT,HUP} causes a shutdown
66 * SIG(STKFLT|USR1) - does nothing, used to wake up sleeping threads.
67 * SIG(UNUSED|USR2) - causes slurpd to read its administrative interface file.
68 * (not yet implemented).
71 (void) SIGNAL( SIGSTKFLT, (void *) do_nothing );
73 (void) SIGNAL( SIGUSR1, (void *) do_nothing );
76 (void) SIGNAL( SIGUNUSED, (void *) do_admin );
78 (void) SIGNAL( SIGUSR2, (void *) do_admin );
80 (void) SIGNAL( SIGTERM, (void *) set_shutdown );
81 (void) SIGNAL( SIGINT, (void *) set_shutdown );
82 (void) SIGNAL( SIGHUP, (void *) set_shutdown );
84 if ( sglob->one_shot_mode ) {
85 if ( file_nonempty( sglob->slapd_replogfile )) {
86 populate_queue( sglob->slapd_replogfile );
88 printf( "Processing in one-shot mode:\n" );
89 printf( "%d total replication records in file,\n",
90 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_ALL ));
91 printf( "%d replication records to process.\n",
92 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_NZRC ));
96 * There may be some leftover replication records in our own
97 * copy of the replication log. If any exist, add them to the
100 if ( file_nonempty( sglob->slurpd_replogfile )) {
101 populate_queue( sglob->slurpd_replogfile );
105 while ( !sglob->slurpd_shutdown ) {
106 if ( file_nonempty( sglob->slapd_replogfile )) {
107 /* New work found - copy to slurpd replog file */
108 Debug( LDAP_DEBUG_ARGS, "new work in %s\n",
109 sglob->slapd_replogfile, 0, 0 );
110 if (( rc = copy_replog( sglob->slapd_replogfile,
111 sglob->slurpd_replogfile )) == 0 ) {
112 populate_queue( sglob->slurpd_replogfile );
115 Debug( LDAP_DEBUG_ANY,
116 "Fatal error while copying replication log\n",
118 sglob->slurpd_shutdown = 1;
122 tsleep( sglob->no_work_interval );
125 /* Garbage-collect queue */
126 sglob->rq->rq_gc( sglob->rq );
128 /* Trim replication log file, if needed */
129 if ( sglob->rq->rq_needtrim( sglob->rq )) {
131 if (( rc = acquire_lock( sglob->slurpd_replogfile, &fp,
133 Debug( LDAP_DEBUG_ANY,
134 "Error: cannot acquire lock on \"%s\" for trimming\n",
135 sglob->slurpd_replogfile, 0, 0 );
137 sglob->rq->rq_write( sglob->rq, fp );
138 (void) relinquish_lock( sglob->slurpd_replogfile, fp, lfp );
142 Debug( LDAP_DEBUG_ARGS, "fm: exiting\n", 0, 0, 0 );
149 * Set a global flag which signals that we're shutting down.
156 sglob->slurpd_shutdown = 1; /* set flag */
158 pthread_kill( sglob->fm_tid, SIGSTKFLT ); /* wake up file mgr */
160 pthread_kill( sglob->fm_tid, SIGUSR1 ); /* wake up file mgr */
162 sglob->rq->rq_lock( sglob->rq ); /* lock queue */
163 pthread_cond_broadcast( &(sglob->rq->rq_more) ); /* wake repl threads */
164 for ( i = 0; i < sglob->num_replicas; i++ ) {
165 (sglob->replicas[ i ])->ri_wake( sglob->replicas[ i ]);
167 sglob->rq->rq_unlock( sglob->rq ); /* unlock queue */
168 (void) SIGNAL( SIGTERM, (void *) set_shutdown ); /* reinstall handlers */
169 (void) SIGNAL( SIGINT, (void *) set_shutdown );
170 (void) SIGNAL( SIGHUP, (void *) set_shutdown );
177 * A do-nothing signal handler.
183 (void) SIGNAL( SIGSTKFLT, (void *) do_nothing );
185 (void) SIGNAL( SIGUSR1, (void *) do_nothing );
193 * Open the slurpd replication log, seek to our last known position, and
194 * process any pending replication entries.
205 if ( acquire_lock( f, &fp, &lfp ) < 0 ) {
206 Debug( LDAP_DEBUG_ANY,
207 "error: can't lock file \"%s\": %s\n",
208 f, sys_errlist[ errno ], 0 );
213 * Read replication records from fp and append them the
216 if ( fseek( fp, sglob->srpos, 0 ) < 0 ) {
217 Debug( LDAP_DEBUG_ANY,
218 "error: can't seek to offset %ld in file \"%s\"\n",
219 sglob->srpos, f, 0 );
222 while (( p = get_record( fp )) != NULL ) {
223 if ( sglob->rq->rq_add( sglob->rq, p ) < 0 ) {
225 /* Print an error message. Only print first line. */
226 if (( t = strchr( p, '\n' )) != NULL ) {
229 Debug( LDAP_DEBUG_ANY,
230 "error: malformed replog entry (begins with \"%s\")\n",
236 sglob->srpos = ftell( fp );
237 (void) relinquish_lock( f, fp, lfp );
244 * Get the next "record" from the file pointed to by fp. A "record"
245 * is delimited by two consecutive newlines. Returns NULL on EOF.
253 static char line[BUFSIZ];
255 static int lcur, lmax;
259 while (( fgets( line, sizeof(line), fp ) != NULL ) &&
260 (( len = strlen( line )) > 1 )) {
261 while ( lcur + len + 1 > lmax ) {
263 buf = (char *) ch_realloc( buf, lmax );
265 strcpy( buf + lcur, line );