3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1996 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
19 * fm.c - file management routines.
26 #include <ac/stdlib.h>
27 #include <ac/string.h>
28 #include <ac/signal.h>
37 static char *get_record LDAP_P(( FILE * ));
38 static void populate_queue LDAP_P(( char *f ));
39 static RETSIGTYPE set_shutdown LDAP_P((int));
43 * Main file manager routine. Watches for new data to be appended to the
44 * slapd replication log. When new data is appended, fm does the following:
45 * - appends the data to slurpd's private copy of the replication log.
46 * - truncates the slapd replog
47 * - adds items to the internal queue of replication work to do
48 * - signals the replication threads to let them know new work has arrived.
57 /* Set up our signal handlers:
58 * SIG{TERM,INT,HUP} causes a shutdown
59 * LDAP_SIGUSR1 - does nothing, used to wake up sleeping threads.
60 * LDAP_SIGUSR2 - causes a shutdown
62 (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
63 (void) SIGNAL( LDAP_SIGUSR2, set_shutdown );
64 (void) SIGNAL( SIGTERM, set_shutdown );
65 (void) SIGNAL( SIGINT, set_shutdown );
67 (void) SIGNAL( SIGHUP, set_shutdown );
70 if ( sglob->one_shot_mode ) {
71 if ( file_nonempty( sglob->slapd_replogfile )) {
72 populate_queue( sglob->slapd_replogfile );
74 printf( "Processing in one-shot mode:\n" );
75 printf( "%d total replication records in file,\n",
76 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_ALL ));
77 printf( "%d replication records to process.\n",
78 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_NZRC ));
82 * There may be some leftover replication records in our own
83 * copy of the replication log. If any exist, add them to the
86 if ( file_nonempty( sglob->slurpd_replogfile )) {
87 populate_queue( sglob->slurpd_replogfile );
91 while ( !sglob->slurpd_shutdown ) {
92 if ( file_nonempty( sglob->slapd_replogfile )) {
93 /* New work found - copy to slurpd replog file */
95 LDAP_LOG (( "fm", LDAP_LEVEL_ARGS,
96 "fm: new work in %s\n", sglob->slapd_replogfile ));
98 Debug( LDAP_DEBUG_ARGS, "new work in %s\n",
99 sglob->slapd_replogfile, 0, 0 );
101 if (( rc = copy_replog( sglob->slapd_replogfile,
102 sglob->slurpd_replogfile )) == 0 ) {
103 populate_queue( sglob->slurpd_replogfile );
107 LDAP_LOG (( "fm", LDAP_LEVEL_CRIT,
108 "fm: Fatal error while copying replication log\n" ));
110 Debug( LDAP_DEBUG_ANY,
111 "Fatal error while copying replication log\n",
114 sglob->slurpd_shutdown = 1;
118 ldap_pvt_thread_sleep( sglob->no_work_interval );
121 /* Garbage-collect queue */
122 sglob->rq->rq_gc( sglob->rq );
124 /* Trim replication log file, if needed */
125 if ( sglob->rq->rq_needtrim( sglob->rq )) {
127 if (( rc = acquire_lock( sglob->slurpd_replogfile, &fp,
130 LDAP_LOG (( "fm", LDAP_LEVEL_ERR,
131 "fm: Error: cannot acquire lock on \"%s\" for trimming\n",
132 sglob->slurpd_replogfile ));
134 Debug( LDAP_DEBUG_ANY,
135 "Error: cannot acquire lock on \"%s\" for trimming\n",
136 sglob->slurpd_replogfile, 0, 0 );
139 sglob->rq->rq_write( sglob->rq, fp );
140 (void) relinquish_lock( sglob->slurpd_replogfile, fp, lfp );
145 LDAP_LOG (( "fm", LDAP_LEVEL_RESULTS, "fm: exiting\n" ));
147 Debug( LDAP_DEBUG_ARGS, "fm: exiting\n", 0, 0, 0 );
156 * Set a global flag which signals that we're shutting down.
159 set_shutdown(int sig)
163 sglob->slurpd_shutdown = 1; /* set flag */
164 ldap_pvt_thread_kill( sglob->fm_tid, LDAP_SIGUSR1 ); /* wake up file mgr */
165 sglob->rq->rq_lock( sglob->rq ); /* lock queue */
166 ldap_pvt_thread_cond_broadcast( &(sglob->rq->rq_more) ); /* wake repl threads */
167 for ( i = 0; i < sglob->num_replicas; i++ ) {
168 (sglob->replicas[ i ])->ri_wake( sglob->replicas[ i ]);
170 sglob->rq->rq_unlock( sglob->rq ); /* unlock queue */
171 (void) SIGNAL_REINSTALL( sig, set_shutdown ); /* reinstall handlers */
178 * A do-nothing signal handler.
183 (void) SIGNAL_REINSTALL( sig, do_nothing );
190 * Open the slurpd replication log, seek to our last known position, and
191 * process any pending replication entries.
201 if ( acquire_lock( f, &fp, &lfp ) < 0 ) {
203 LDAP_LOG (( "fm", LDAP_LEVEL_ERR,
204 "populate_queue: error: can't lock file \"%s\": %s\n",
205 f, sys_errlist[ errno ] ));
207 Debug( LDAP_DEBUG_ANY,
208 "error: can't lock file \"%s\": %s\n",
209 f, sys_errlist[ errno ], 0 );
215 * Read replication records from fp and append them the
218 if ( fseek( fp, sglob->srpos, 0 ) < 0 ) {
220 LDAP_LOG (( "fm", LDAP_LEVEL_ERR,
221 "populate_queue: error: can't seek to offset %ld in file \"%s\"\n",
224 Debug( LDAP_DEBUG_ANY,
225 "error: can't seek to offset %ld in file \"%s\"\n",
226 sglob->srpos, f, 0 );
229 while (( p = get_record( fp )) != NULL ) {
230 if ( sglob->rq->rq_add( sglob->rq, p ) < 0 ) {
232 /* Print an error message. Only print first line. */
233 if (( t = strchr( p, '\n' )) != NULL ) {
237 LDAP_LOG (( "fm", LDAP_LEVEL_ERR,
238 "populate_queue: error: malformed replog entry "
239 "(begins with \"%s\")\n", p ));
241 Debug( LDAP_DEBUG_ANY,
242 "error: malformed replog entry (begins with \"%s\")\n",
247 ldap_pvt_thread_yield();
249 sglob->srpos = ftell( fp );
251 (void) relinquish_lock( f, fp, lfp );
258 * Get the next "record" from the file pointed to by fp. A "record"
259 * is delimited by two consecutive newlines. Returns NULL on EOF.
267 static char line[BUFSIZ];
269 static int lcur, lmax;
273 while (( fgets( line, sizeof(line), fp ) != NULL ) &&
274 (( len = strlen( line )) > 1 )) {
276 while ( lcur + len + 1 > lmax ) {
278 buf = (char *) ch_realloc( buf, lmax );
280 strcpy( buf + lcur, line );