3 * Copyright 1998-2003 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>
29 #include <ac/socket.h>
30 #include <ac/unistd.h>
39 static char *get_record LDAP_P(( FILE * ));
40 static void populate_queue LDAP_P(( char *f ));
44 * Main file manager routine. Watches for new data to be appended to the
45 * slapd replication log. When new data is appended, fm does the following:
46 * - appends the data to slurpd's private copy of the replication log.
47 * - truncates the slapd replog
48 * - adds items to the internal queue of replication work to do
49 * - 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
63 (void) SIGNAL( SIGTERM, slurp_set_shutdown );
64 (void) SIGNAL( SIGINT, slurp_set_shutdown );
66 (void) SIGNAL( SIGHUP, slurp_set_shutdown );
68 #if defined(SIGBREAK) && defined(HAVE_NT_SERVICE_MANAGER)
69 (void) SIGNAL( SIGBREAK, do_nothing );
72 if ( sglob->one_shot_mode ) {
73 if ( file_nonempty( sglob->slapd_replogfile )) {
74 populate_queue( sglob->slapd_replogfile );
76 printf( "Processing in one-shot mode:\n" );
77 printf( "%d total replication records in file,\n",
78 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_ALL ));
79 printf( "%d replication records to process.\n",
80 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_NZRC ));
84 * There may be some leftover replication records in our own
85 * copy of the replication log. If any exist, add them to the
88 if ( file_nonempty( sglob->slurpd_replogfile )) {
89 populate_queue( sglob->slurpd_replogfile );
94 while ( !sglob->slurpd_shutdown ) {
95 if ( file_nonempty( sglob->slapd_replogfile )) {
96 /* New work found - copy to slurpd replog file */
98 LDAP_LOG ( SLURPD, ARGS,
99 "fm: new work in %s\n", sglob->slapd_replogfile, 0, 0 );
101 Debug( LDAP_DEBUG_ARGS, "new work in %s\n",
102 sglob->slapd_replogfile, 0, 0 );
104 if (( rc = copy_replog( sglob->slapd_replogfile,
105 sglob->slurpd_replogfile )) == 0 ) {
106 populate_queue( sglob->slurpd_replogfile );
110 LDAP_LOG ( SLURPD, CRIT,
111 "fm: Fatal error while copying replication log\n" , 0, 0, 0);
113 Debug( LDAP_DEBUG_ANY,
114 "Fatal error while copying replication log\n",
117 sglob->slurpd_shutdown = 1;
123 FD_SET( sglob->wake_sds[0], &readfds );
124 tv.tv_sec = sglob->no_work_interval;
127 rc = select( sglob->wake_sds[0]+1, &readfds, NULL, NULL, &tv );
130 /* Garbage-collect queue */
131 sglob->rq->rq_gc( sglob->rq );
133 /* Trim replication log file, if needed */
134 if ( sglob->rq->rq_needtrim( sglob->rq )) {
136 if (( rc = acquire_lock( sglob->slurpd_replogfile, &fp,
139 LDAP_LOG ( SLURPD, ERR,
140 "fm: Error: cannot acquire lock on \"%s\" for trimming\n",
141 sglob->slurpd_replogfile, 0, 0 );
143 Debug( LDAP_DEBUG_ANY,
144 "Error: cannot acquire lock on \"%s\" for trimming\n",
145 sglob->slurpd_replogfile, 0, 0 );
148 sglob->rq->rq_write( sglob->rq, fp );
149 (void) relinquish_lock( sglob->slurpd_replogfile, fp, lfp );
153 sglob->rq->rq_lock( sglob->rq ); /* lock queue */
154 ldap_pvt_thread_cond_broadcast( &(sglob->rq->rq_more) ); /* wake repl threads */
155 for ( i = 0; i < sglob->num_replicas; i++ ) {
156 (sglob->replicas[ i ])->ri_wake( sglob->replicas[ i ]);
158 sglob->rq->rq_unlock( sglob->rq ); /* unlock queue */
160 LDAP_LOG ( SLURPD, RESULTS, "fm: exiting\n", 0, 0, 0 );
162 Debug( LDAP_DEBUG_ARGS, "fm: exiting\n", 0, 0, 0 );
171 * Set a global flag which signals that we're shutting down.
174 slurp_set_shutdown(int sig)
176 sglob->slurpd_shutdown = 1; /* set flag */
177 tcp_write( sglob->wake_sds[1], "0", 1); /* wake up file mgr */
179 (void) SIGNAL_REINSTALL( sig, slurp_set_shutdown ); /* reinstall handlers */
186 * A do-nothing signal handler.
191 (void) SIGNAL_REINSTALL( sig, do_nothing );
198 * Open the slurpd replication log, seek to our last known position, and
199 * process any pending replication entries.
209 if ( acquire_lock( f, &fp, &lfp ) < 0 ) {
211 LDAP_LOG ( SLURPD, ERR,
212 "populate_queue: error: can't lock file \"%s\": %s\n",
213 f, sys_errlist[ errno ], 0 );
215 Debug( LDAP_DEBUG_ANY,
216 "error: can't lock file \"%s\": %s\n",
217 f, sys_errlist[ errno ], 0 );
223 * Read replication records from fp and append them the
226 if ( fseek( fp, sglob->srpos, 0 ) < 0 ) {
228 LDAP_LOG ( SLURPD, ERR,
229 "populate_queue: error: can't seek to offset %ld in file \"%s\"\n",
230 sglob->srpos, f, 0 );
232 Debug( LDAP_DEBUG_ANY,
233 "error: can't seek to offset %ld in file \"%s\"\n",
234 sglob->srpos, f, 0 );
237 while (( p = get_record( fp )) != NULL ) {
238 if ( sglob->rq->rq_add( sglob->rq, p ) < 0 ) {
240 /* Print an error message. Only print first line. */
241 if (( t = strchr( p, '\n' )) != NULL ) {
245 LDAP_LOG ( SLURPD, ERR,
246 "populate_queue: error: malformed replog entry "
247 "(begins with \"%s\")\n", p, 0, 0 );
249 Debug( LDAP_DEBUG_ANY,
250 "error: malformed replog entry (begins with \"%s\")\n",
255 ldap_pvt_thread_yield();
257 sglob->srpos = ftell( fp );
259 (void) relinquish_lock( f, fp, lfp );
266 * Get the next "record" from the file pointed to by fp. A "record"
267 * is delimited by two consecutive newlines. Returns NULL on EOF.
275 static char line[BUFSIZ];
277 static int lcur, lmax;
281 while (( fgets( line, sizeof(line), fp ) != NULL ) &&
282 (( len = strlen( line )) > 1 )) {
284 while ( lcur + len + 1 > lmax ) {
286 buf = (char *) ch_realloc( buf, lmax );
288 strcpy( buf + lcur, line );