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.
24 extern void do_admin();
26 static void set_shutdown();
33 extern int file_nonempty( char * );
34 extern int acquire_lock(char *, FILE **, FILE ** );
35 extern int relinquish_lock(char *, FILE *, FILE * );
36 #else /* NEEDPROTOS */
37 extern int file_nonempty();
38 extern int acquire_lock();
39 extern int relinquish_lock();
40 #endif /* NEEDPROTOS */
46 static char *get_record( FILE * );
47 static void populate_queue( char *f );
48 static void set_shutdown();
50 #else /* NEEDPROTOS */
51 static char *get_record();
52 static void populate_queue();
53 static void set_shutdown();
55 #endif /* NEEDPROTOS */
57 #ifndef SYSERRLIST_IN_STDIO
58 extern char *sys_errlist[];
59 #endif /* SYSERRLIST_IN_STDIO */
64 * Main file manager routine. Watches for new data to be appended to the
65 * slapd replication log. When new data is appended, fm does the following:
66 * - appends the data to slurpd's private copy of the replication log.
67 * - truncates the slapd replog
68 * - adds items to the internal queue of replication work to do
69 * - signals the replication threads to let them know new work has arrived.
78 /* Set up our signal handlers:
79 * SIG{TERM,INT,HUP} causes a shutdown
80 * SIGUSR1 - does nothing, used to wake up sleeping threads.
81 * SIGUSR2 - causes slurpd to read its administrative interface file.
82 * (not yet implemented).
84 (void) SIGNAL( SIGUSR1, (void *) do_nothing );
85 (void) SIGNAL( SIGUSR2, (void *) do_admin );
86 (void) SIGNAL( SIGTERM, (void *) set_shutdown );
87 (void) SIGNAL( SIGINT, (void *) set_shutdown );
88 (void) SIGNAL( SIGHUP, (void *) set_shutdown );
90 if ( sglob->one_shot_mode ) {
91 if ( file_nonempty( sglob->slapd_replogfile )) {
92 populate_queue( sglob->slapd_replogfile );
94 printf( "Processing in one-shot mode:\n" );
95 printf( "%d total replication records in file,\n",
96 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_ALL ));
97 printf( "%d replication records to process.\n",
98 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_NZRC ));
102 * There may be some leftover replication records in our own
103 * copy of the replication log. If any exist, add them to the
106 if ( file_nonempty( sglob->slurpd_replogfile )) {
107 populate_queue( sglob->slurpd_replogfile );
111 while ( !sglob->slurpd_shutdown ) {
112 if ( file_nonempty( sglob->slapd_replogfile )) {
113 /* New work found - copy to slurpd replog file */
114 Debug( LDAP_DEBUG_ARGS, "new work in %s\n",
115 sglob->slapd_replogfile, 0, 0 );
116 if (( rc = copy_replog( sglob->slapd_replogfile,
117 sglob->slurpd_replogfile )) == 0 ) {
118 populate_queue( sglob->slurpd_replogfile );
121 Debug( LDAP_DEBUG_ANY,
122 "Fatal error while copying replication log\n",
124 sglob->slurpd_shutdown = 1;
128 tsleep( sglob->no_work_interval );
131 /* Garbage-collect queue */
132 sglob->rq->rq_gc( sglob->rq );
134 /* Trim replication log file, if needed */
135 if ( sglob->rq->rq_needtrim( sglob->rq )) {
137 if (( rc = acquire_lock( sglob->slurpd_replogfile, &fp,
139 Debug( LDAP_DEBUG_ANY,
140 "Error: cannot acquire lock on \"%s\" for trimming\n",
141 sglob->slurpd_replogfile, 0, 0 );
143 sglob->rq->rq_write( sglob->rq, fp );
144 (void) relinquish_lock( sglob->slurpd_replogfile, fp, lfp );
148 Debug( LDAP_DEBUG_ARGS, "fm: exiting\n", 0, 0, 0 );
155 * Set a global flag which signals that we're shutting down.
162 sglob->slurpd_shutdown = 1; /* set flag */
163 pthread_kill( sglob->fm_tid, SIGUSR1 ); /* wake up file mgr */
164 sglob->rq->rq_lock( sglob->rq ); /* lock queue */
165 pthread_cond_broadcast( &(sglob->rq->rq_more) ); /* wake repl threads */
166 for ( i = 0; i < sglob->num_replicas; i++ ) {
167 (sglob->replicas[ i ])->ri_wake( sglob->replicas[ i ]);
169 sglob->rq->rq_unlock( sglob->rq ); /* unlock queue */
170 (void) SIGNAL( SIGTERM, (void *) set_shutdown ); /* reinstall handlers */
171 (void) SIGNAL( SIGINT, (void *) set_shutdown );
172 (void) SIGNAL( SIGHUP, (void *) set_shutdown );
179 * A do-nothing signal handler.
184 (void) SIGNAL( SIGUSR1, (void *) do_nothing );
191 * Open the slurpd replication log, seek to our last known position, and
192 * process any pending replication entries.
203 if ( acquire_lock( f, &fp, &lfp ) < 0 ) {
204 Debug( LDAP_DEBUG_ANY,
205 "error: can't lock file \"%s\": %s\n",
206 f, sys_errlist[ errno ], 0 );
211 * Read replication records from fp and append them the
214 if ( fseek( fp, sglob->srpos, 0 ) < 0 ) {
215 Debug( LDAP_DEBUG_ANY,
216 "error: can't seek to offset %ld in file \"%s\"\n",
217 sglob->srpos, f, 0 );
220 while (( p = get_record( fp )) != NULL ) {
221 if ( sglob->rq->rq_add( sglob->rq, p ) < 0 ) {
223 /* Print an error message. Only print first line. */
224 if (( t = strchr( p, '\n' )) != NULL ) {
227 Debug( LDAP_DEBUG_ANY,
228 "error: malformed replog entry (begins with \"%s\")\n",
234 sglob->srpos = ftell( fp );
235 (void) relinquish_lock( f, fp, lfp );
242 * Get the next "record" from the file pointed to by fp. A "record"
243 * is delimited by two consecutive newlines. Returns NULL on EOF.
251 static char line[BUFSIZ];
253 static int lcur, lmax;
257 while (( fgets( line, sizeof(line), fp ) != NULL ) &&
258 (( len = strlen( line )) > 1 )) {
259 while ( lcur + len + 1 > lmax ) {
261 buf = (char *) ch_realloc( buf, lmax );
263 strcpy( buf + lcur, line );