2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 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>
52 static char *get_record LDAP_P(( FILE * ));
53 static void populate_queue LDAP_P(( char *f ));
57 * Main file manager routine. Watches for new data to be appended to the
58 * slapd replication log. When new data is appended, fm does the following:
59 * - appends the data to slurpd's private copy of the replication log.
60 * - truncates the slapd replog
61 * - adds items to the internal queue of replication work to do
62 * - signals the replication threads to let them know new work has arrived.
73 /* Set up our signal handlers:
74 * SIG{TERM,INT,HUP} causes a shutdown
76 (void) SIGNAL( SIGTERM, slurp_set_shutdown );
77 (void) SIGNAL( SIGINT, slurp_set_shutdown );
79 (void) SIGNAL( SIGHUP, slurp_set_shutdown );
82 (void) SIGNAL( SIGBREAK, slurp_set_shutdown );
85 if ( sglob->one_shot_mode ) {
86 if ( file_nonempty( sglob->slapd_replogfile )) {
87 populate_queue( sglob->slapd_replogfile );
89 printf( "Processing in one-shot mode:\n" );
90 printf( "%d total replication records in file,\n",
91 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_ALL ));
92 printf( "%d replication records to process.\n",
93 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_NZRC ));
97 * There may be some leftover replication records in our own
98 * copy of the replication log. If any exist, add them to the
101 if ( file_nonempty( sglob->slurpd_replogfile )) {
102 populate_queue( sglob->slurpd_replogfile );
107 while ( !sglob->slurpd_shutdown ) {
108 if ( file_nonempty( sglob->slapd_replogfile )) {
109 /* New work found - copy to slurpd replog file */
110 Debug( LDAP_DEBUG_ARGS, "new work in %s\n",
111 sglob->slapd_replogfile, 0, 0 );
112 if (( rc = copy_replog( sglob->slapd_replogfile,
113 sglob->slurpd_replogfile )) == 0 ) {
114 populate_queue( sglob->slurpd_replogfile );
117 Debug( LDAP_DEBUG_ANY,
118 "Fatal error while copying replication log\n",
120 sglob->slurpd_shutdown = 1;
126 FD_SET( sglob->wake_sds[0], &readfds );
127 tv.tv_sec = sglob->no_work_interval;
130 rc = select( sglob->wake_sds[0]+1, &readfds, NULL, NULL, &tv );
133 /* Garbage-collect queue */
134 sglob->rq->rq_gc( sglob->rq );
136 /* Trim replication log file, if needed */
137 if ( sglob->rq->rq_needtrim( sglob->rq )) {
139 if (( rc = acquire_lock( sglob->slurpd_replogfile, &fp,
141 Debug( LDAP_DEBUG_ANY,
142 "Error: cannot acquire lock on \"%s\" for trimming\n",
143 sglob->slurpd_replogfile, 0, 0 );
145 sglob->rq->rq_write( sglob->rq, fp );
146 (void) relinquish_lock( sglob->slurpd_replogfile, fp, lfp );
150 sglob->rq->rq_lock( sglob->rq ); /* lock queue */
151 ldap_pvt_thread_cond_broadcast( &(sglob->rq->rq_more) ); /* wake repl threads */
152 for ( i = 0; i < sglob->num_replicas; i++ ) {
153 (sglob->replicas[ i ])->ri_wake( sglob->replicas[ i ]);
155 sglob->rq->rq_unlock( sglob->rq ); /* unlock queue */
156 Debug( LDAP_DEBUG_ARGS, "fm: exiting\n", 0, 0, 0 );
164 * Set a global flag which signals that we're shutting down.
167 slurp_set_shutdown(int sig)
169 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
170 if (is_NT_Service && sig == SIGBREAK) {
175 sglob->slurpd_shutdown = 1; /* set flag */
176 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 ) {
210 Debug( LDAP_DEBUG_ANY,
211 "error: can't lock file \"%s\": %s\n",
212 f, sys_errlist[ errno ], 0 );
217 * Read replication records from fp and append them the
220 if ( fseek( fp, sglob->srpos, 0 ) < 0 ) {
221 Debug( LDAP_DEBUG_ANY,
222 "error: can't seek to offset %ld in file \"%s\"\n",
223 sglob->srpos, f, 0 );
225 while (( p = get_record( fp )) != NULL ) {
226 if ( sglob->rq->rq_add( sglob->rq, p ) < 0 ) {
228 /* Print an error message. Only print first line. */
229 if (( t = strchr( p, '\n' )) != NULL ) {
232 Debug( LDAP_DEBUG_ANY,
233 "error: malformed replog entry (begins with \"%s\")\n",
237 ldap_pvt_thread_yield();
239 sglob->srpos = ftell( fp );
241 (void) relinquish_lock( f, fp, lfp );
248 * Get the next "record" from the file pointed to by fp. A "record"
249 * is delimited by two consecutive newlines. Returns NULL on EOF.
257 static char line[BUFSIZ];
259 static int lcur, lmax;
263 while (( fgets( line, sizeof(line), fp ) != NULL ) &&
264 (( len = strlen( line )) > 1 )) {
266 while ( lcur + len + 1 > lmax ) {
268 buf = (char *) ch_realloc( buf, lmax );
270 strcpy( buf + lcur, line );