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 */
109 Debug( LDAP_DEBUG_ARGS, "new work in %s\n",
110 sglob->slapd_replogfile, 0, 0 );
111 if (( rc = copy_replog( sglob->slapd_replogfile,
112 sglob->slurpd_replogfile )) == 0 ) {
113 populate_queue( sglob->slurpd_replogfile );
116 Debug( LDAP_DEBUG_ANY,
117 "Fatal error while copying replication log\n",
119 sglob->slurpd_shutdown = 1;
125 FD_SET( sglob->wake_sds[0], &readfds );
126 tv.tv_sec = sglob->no_work_interval;
129 rc = select( sglob->wake_sds[0]+1, &readfds, NULL, NULL, &tv );
132 /* Garbage-collect queue */
133 sglob->rq->rq_gc( sglob->rq );
135 /* Trim replication log file, if needed */
136 if ( sglob->rq->rq_needtrim( sglob->rq )) {
138 if (( rc = acquire_lock( sglob->slurpd_replogfile, &fp,
140 Debug( LDAP_DEBUG_ANY,
141 "Error: cannot acquire lock on \"%s\" for trimming\n",
142 sglob->slurpd_replogfile, 0, 0 );
144 sglob->rq->rq_write( sglob->rq, fp );
145 (void) relinquish_lock( sglob->slurpd_replogfile, fp, lfp );
149 sglob->rq->rq_lock( sglob->rq ); /* lock queue */
150 ldap_pvt_thread_cond_broadcast( &(sglob->rq->rq_more) ); /* wake repl threads */
151 for ( i = 0; i < sglob->num_replicas; i++ ) {
152 (sglob->replicas[ i ])->ri_wake( sglob->replicas[ i ]);
154 sglob->rq->rq_unlock( sglob->rq ); /* unlock queue */
155 Debug( LDAP_DEBUG_ARGS, "fm: exiting\n", 0, 0, 0 );
163 * Set a global flag which signals that we're shutting down.
166 slurp_set_shutdown(int sig)
168 sglob->slurpd_shutdown = 1; /* set flag */
169 tcp_write( sglob->wake_sds[1], "0", 1); /* wake up file mgr */
171 (void) SIGNAL_REINSTALL( sig, slurp_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 ) {
202 Debug( LDAP_DEBUG_ANY,
203 "error: can't lock file \"%s\": %s\n",
204 f, sys_errlist[ errno ], 0 );
209 * Read replication records from fp and append them the
212 if ( fseek( fp, sglob->srpos, 0 ) < 0 ) {
213 Debug( LDAP_DEBUG_ANY,
214 "error: can't seek to offset %ld in file \"%s\"\n",
215 sglob->srpos, f, 0 );
217 while (( p = get_record( fp )) != NULL ) {
218 if ( sglob->rq->rq_add( sglob->rq, p ) < 0 ) {
220 /* Print an error message. Only print first line. */
221 if (( t = strchr( p, '\n' )) != NULL ) {
224 Debug( LDAP_DEBUG_ANY,
225 "error: malformed replog entry (begins with \"%s\")\n",
229 ldap_pvt_thread_yield();
231 sglob->srpos = ftell( fp );
233 (void) relinquish_lock( f, fp, lfp );
240 * Get the next "record" from the file pointed to by fp. A "record"
241 * is delimited by two consecutive newlines. Returns NULL on EOF.
249 static char line[BUFSIZ];
251 static int lcur, lmax;
255 while (( fgets( line, sizeof(line), fp ) != NULL ) &&
256 (( len = strlen( line )) > 1 )) {
258 while ( lcur + len + 1 > lmax ) {
260 buf = (char *) ch_realloc( buf, lmax );
262 strcpy( buf + lcur, line );