2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1996 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
26 * This work was originally developed by the University of Michigan
27 * (as part of U-MICH LDAP).
31 * fm.c - file management routines.
38 #include <ac/stdlib.h>
39 #include <ac/string.h>
40 #include <ac/signal.h>
41 #include <ac/socket.h>
42 #include <ac/unistd.h>
51 static char *get_record LDAP_P(( FILE * ));
52 static void populate_queue LDAP_P(( char *f ));
56 * Main file manager routine. Watches for new data to be appended to the
57 * slapd replication log. When new data is appended, fm does the following:
58 * - appends the data to slurpd's private copy of the replication log.
59 * - truncates the slapd replog
60 * - adds items to the internal queue of replication work to do
61 * - signals the replication threads to let them know new work has arrived.
72 /* Set up our signal handlers:
73 * SIG{TERM,INT,HUP} causes a shutdown
75 (void) SIGNAL( SIGTERM, slurp_set_shutdown );
76 (void) SIGNAL( SIGINT, slurp_set_shutdown );
78 (void) SIGNAL( SIGHUP, slurp_set_shutdown );
80 #if defined(SIGBREAK) && defined(HAVE_NT_SERVICE_MANAGER)
81 (void) SIGNAL( SIGBREAK, do_nothing );
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 );
106 while ( !sglob->slurpd_shutdown ) {
107 if ( file_nonempty( sglob->slapd_replogfile )) {
108 /* New work found - copy to slurpd replog file */
110 LDAP_LOG ( SLURPD, ARGS,
111 "fm: new work in %s\n", sglob->slapd_replogfile, 0, 0 );
113 Debug( LDAP_DEBUG_ARGS, "new work in %s\n",
114 sglob->slapd_replogfile, 0, 0 );
116 if (( rc = copy_replog( sglob->slapd_replogfile,
117 sglob->slurpd_replogfile )) == 0 ) {
118 populate_queue( sglob->slurpd_replogfile );
122 LDAP_LOG ( SLURPD, CRIT,
123 "fm: Fatal error while copying replication log\n" , 0, 0, 0);
125 Debug( LDAP_DEBUG_ANY,
126 "Fatal error while copying replication log\n",
129 sglob->slurpd_shutdown = 1;
135 FD_SET( sglob->wake_sds[0], &readfds );
136 tv.tv_sec = sglob->no_work_interval;
139 rc = select( sglob->wake_sds[0]+1, &readfds, NULL, NULL, &tv );
142 /* Garbage-collect queue */
143 sglob->rq->rq_gc( sglob->rq );
145 /* Trim replication log file, if needed */
146 if ( sglob->rq->rq_needtrim( sglob->rq )) {
148 if (( rc = acquire_lock( sglob->slurpd_replogfile, &fp,
151 LDAP_LOG ( SLURPD, ERR,
152 "fm: Error: cannot acquire lock on \"%s\" for trimming\n",
153 sglob->slurpd_replogfile, 0, 0 );
155 Debug( LDAP_DEBUG_ANY,
156 "Error: cannot acquire lock on \"%s\" for trimming\n",
157 sglob->slurpd_replogfile, 0, 0 );
160 sglob->rq->rq_write( sglob->rq, fp );
161 (void) relinquish_lock( sglob->slurpd_replogfile, fp, lfp );
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 */
172 LDAP_LOG ( SLURPD, RESULTS, "fm: exiting\n", 0, 0, 0 );
174 Debug( LDAP_DEBUG_ARGS, "fm: exiting\n", 0, 0, 0 );
183 * Set a global flag which signals that we're shutting down.
186 slurp_set_shutdown(int sig)
188 sglob->slurpd_shutdown = 1; /* set flag */
189 tcp_write( sglob->wake_sds[1], "0", 1); /* wake up file mgr */
191 (void) SIGNAL_REINSTALL( sig, slurp_set_shutdown ); /* reinstall handlers */
198 * A do-nothing signal handler.
203 (void) SIGNAL_REINSTALL( sig, do_nothing );
210 * Open the slurpd replication log, seek to our last known position, and
211 * process any pending replication entries.
221 if ( acquire_lock( f, &fp, &lfp ) < 0 ) {
223 LDAP_LOG ( SLURPD, ERR,
224 "populate_queue: error: can't lock file \"%s\": %s\n",
225 f, sys_errlist[ errno ], 0 );
227 Debug( LDAP_DEBUG_ANY,
228 "error: can't lock file \"%s\": %s\n",
229 f, sys_errlist[ errno ], 0 );
235 * Read replication records from fp and append them the
238 if ( fseek( fp, sglob->srpos, 0 ) < 0 ) {
240 LDAP_LOG ( SLURPD, ERR,
241 "populate_queue: error: can't seek to offset %ld in file \"%s\"\n",
242 sglob->srpos, f, 0 );
244 Debug( LDAP_DEBUG_ANY,
245 "error: can't seek to offset %ld in file \"%s\"\n",
246 sglob->srpos, f, 0 );
249 while (( p = get_record( fp )) != NULL ) {
250 if ( sglob->rq->rq_add( sglob->rq, p ) < 0 ) {
252 /* Print an error message. Only print first line. */
253 if (( t = strchr( p, '\n' )) != NULL ) {
257 LDAP_LOG ( SLURPD, ERR,
258 "populate_queue: error: malformed replog entry "
259 "(begins with \"%s\")\n", p, 0, 0 );
261 Debug( LDAP_DEBUG_ANY,
262 "error: malformed replog entry (begins with \"%s\")\n",
267 ldap_pvt_thread_yield();
269 sglob->srpos = ftell( fp );
271 (void) relinquish_lock( f, fp, lfp );
278 * Get the next "record" from the file pointed to by fp. A "record"
279 * is delimited by two consecutive newlines. Returns NULL on EOF.
287 static char line[BUFSIZ];
289 static int lcur, lmax;
293 while (( fgets( line, sizeof(line), fp ) != NULL ) &&
294 (( len = strlen( line )) > 1 )) {
296 while ( lcur + len + 1 > lmax ) {
298 buf = (char *) ch_realloc( buf, lmax );
300 strcpy( buf + lcur, line );