]> git.sur5r.net Git - openldap/blob - servers/slurpd/fm.c
50740f25b5bf225ec1fba51078f7fcdba05edef2
[openldap] / servers / slurpd / fm.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright (c) 1996 Regents of the University of Michigan.
8  * All rights reserved.
9  *
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.
16  */
17
18 /*
19  * fm.c - file management routines.
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25
26 #include <ac/stdlib.h>
27 #include <ac/string.h>
28 #include <ac/signal.h>
29
30 #include "slurp.h"
31 #include "globals.h"
32
33
34 /*
35  * Forward references
36  */
37 static char *get_record LDAP_P(( FILE * ));
38 static void populate_queue LDAP_P(( char *f ));
39
40
41 /*
42  * Main file manager routine.  Watches for new data to be appended to the
43  * slapd replication log.  When new data is appended, fm does the following:
44  *  - appends the data to slurpd's private copy of the replication log.
45  *  - truncates the slapd replog
46  *  - adds items to the internal queue of replication work to do
47  *  - signals the replication threads to let them know new work has arrived.
48  */
49 void *
50 fm(
51     void *arg
52 )
53 {
54     int rc;
55
56     /* Set up our signal handlers:
57      * SIG{TERM,INT,HUP} causes a shutdown
58      * LDAP_SIGUSR1 - does nothing, used to wake up sleeping threads.
59          * LDAP_SIGUSR2 - causes a shutdown
60      */
61     (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
62     (void) SIGNAL( LDAP_SIGUSR2, slurp_set_shutdown );
63     (void) SIGNAL( SIGTERM, slurp_set_shutdown );
64     (void) SIGNAL( SIGINT, slurp_set_shutdown );
65 #ifdef SIGHUP
66     (void) SIGNAL( SIGHUP, slurp_set_shutdown );
67 #endif
68 #ifdef SIGBREAK
69     (void) SIGNAL( SIGBREAK, slurp_set_shutdown );
70 #endif
71
72     if ( sglob->one_shot_mode ) {
73         if ( file_nonempty( sglob->slapd_replogfile )) {
74             populate_queue( sglob->slapd_replogfile );
75         }
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 ));
81         return NULL;
82     }
83     /*
84      * There may be some leftover replication records in our own
85      * copy of the replication log.  If any exist, add them to the
86      * queue.
87      */
88     if ( file_nonempty( sglob->slurpd_replogfile )) {
89         populate_queue( sglob->slurpd_replogfile );
90     }
91
92
93     while ( !sglob->slurpd_shutdown ) {
94         if ( file_nonempty( sglob->slapd_replogfile )) {
95             /* New work found - copy to slurpd replog file */
96 #ifdef NEW_LOGGING
97         LDAP_LOG ( SLURPD, ARGS, 
98                         "fm: new work in %s\n", sglob->slapd_replogfile, 0, 0 );
99 #else
100             Debug( LDAP_DEBUG_ARGS, "new work in %s\n",
101                     sglob->slapd_replogfile, 0, 0 );
102 #endif
103             if (( rc = copy_replog( sglob->slapd_replogfile,
104                     sglob->slurpd_replogfile )) == 0 )  {
105                 populate_queue( sglob->slurpd_replogfile );
106             } else {
107                 if ( rc < 0 ) {
108 #ifdef NEW_LOGGING
109                 LDAP_LOG ( SLURPD, CRIT, 
110                                 "fm: Fatal error while copying replication log\n" , 0, 0, 0);
111 #else
112                     Debug( LDAP_DEBUG_ANY,
113                             "Fatal error while copying replication log\n",
114                             0, 0, 0 );
115 #endif
116                     sglob->slurpd_shutdown = 1;
117                 }
118             }
119         } else {
120             ldap_pvt_thread_sleep( sglob->no_work_interval );
121         }
122
123         /* Garbage-collect queue */
124         sglob->rq->rq_gc( sglob->rq );
125
126         /* Trim replication log file, if needed */
127         if ( sglob->rq->rq_needtrim( sglob->rq )) {
128             FILE *fp, *lfp;
129             if (( rc = acquire_lock( sglob->slurpd_replogfile, &fp,
130                     &lfp )) < 0 ) {
131 #ifdef NEW_LOGGING
132                 LDAP_LOG ( SLURPD, ERR, 
133                         "fm: Error: cannot acquire lock on \"%s\" for trimming\n",
134                         sglob->slurpd_replogfile, 0, 0 );
135 #else
136                 Debug( LDAP_DEBUG_ANY,
137                         "Error: cannot acquire lock on \"%s\" for trimming\n",
138                         sglob->slurpd_replogfile, 0, 0 );
139 #endif
140             } else {
141                 sglob->rq->rq_write( sglob->rq, fp );
142                 (void) relinquish_lock( sglob->slurpd_replogfile, fp, lfp );
143             }
144         }
145     }
146 #ifdef NEW_LOGGING
147         LDAP_LOG ( SLURPD, RESULTS, "fm: exiting\n", 0, 0, 0 );
148 #else
149     Debug( LDAP_DEBUG_ARGS, "fm: exiting\n", 0, 0, 0 );
150 #endif
151     return NULL;
152 }
153
154
155
156
157 /*
158  * Set a global flag which signals that we're shutting down.
159  */
160 RETSIGTYPE
161 slurp_set_shutdown(int sig)
162 {
163     int i;
164
165 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
166     if (sig == SIGBREAK) return do_nothing( sig );
167 #endif
168
169     sglob->slurpd_shutdown = 1;                         /* set flag */
170     ldap_pvt_thread_kill( sglob->fm_tid, LDAP_SIGUSR1 );        /* wake up file mgr */
171     sglob->rq->rq_lock( sglob->rq );                    /* lock queue */
172     ldap_pvt_thread_cond_broadcast( &(sglob->rq->rq_more) );    /* wake repl threads */
173     for ( i = 0; i < sglob->num_replicas; i++ ) {
174         (sglob->replicas[ i ])->ri_wake( sglob->replicas[ i ]);
175     }
176     sglob->rq->rq_unlock( sglob->rq );                  /* unlock queue */
177     (void) SIGNAL_REINSTALL( sig, slurp_set_shutdown ); /* reinstall handlers */
178 }
179
180
181
182
183 /*
184  * A do-nothing signal handler.
185  */
186 RETSIGTYPE
187 do_nothing(int sig)
188 {
189     (void) SIGNAL_REINSTALL( sig, do_nothing );
190 }
191
192
193
194
195 /*
196  * Open the slurpd replication log, seek to our last known position, and
197  * process any pending replication entries.
198  */
199 static void
200 populate_queue(
201     char *f
202 )
203 {
204     FILE        *fp, *lfp;
205     char        *p;
206
207     if ( acquire_lock( f, &fp, &lfp ) < 0 ) {
208 #ifdef NEW_LOGGING
209         LDAP_LOG ( SLURPD, ERR, 
210                 "populate_queue: error: can't lock file \"%s\": %s\n", 
211                 f, sys_errlist[ errno ], 0 );
212 #else
213         Debug( LDAP_DEBUG_ANY,
214                 "error: can't lock file \"%s\": %s\n",
215                 f, sys_errlist[ errno ], 0 );
216 #endif
217         return;
218     }
219
220     /*
221      * Read replication records from fp and append them the
222      * the queue.
223      */
224     if ( fseek( fp, sglob->srpos, 0 ) < 0 ) {
225 #ifdef NEW_LOGGING
226         LDAP_LOG ( SLURPD, ERR, 
227                 "populate_queue: error: can't seek to offset %ld in file \"%s\"\n", 
228                 sglob->srpos, f, 0 );
229 #else
230         Debug( LDAP_DEBUG_ANY,
231                 "error: can't seek to offset %ld in file \"%s\"\n",
232                 sglob->srpos, f, 0 );
233 #endif
234     } else {
235     while (( p = get_record( fp )) != NULL ) {
236         if ( sglob->rq->rq_add( sglob->rq, p ) < 0 ) {
237             char *t;
238             /* Print an error message.  Only print first line.  */
239             if (( t = strchr( p, '\n' )) != NULL ) {
240                 *t = '\0';
241             }
242 #ifdef NEW_LOGGING
243                 LDAP_LOG ( SLURPD, ERR, 
244                         "populate_queue: error: malformed replog entry "
245                         "(begins with \"%s\")\n", p, 0, 0 );
246 #else
247             Debug( LDAP_DEBUG_ANY,
248                     "error: malformed replog entry (begins with \"%s\")\n",
249                     p, 0, 0 );
250 #endif
251         }
252         free( p );
253         ldap_pvt_thread_yield();
254     }
255     sglob->srpos = ftell( fp );
256     }
257     (void) relinquish_lock( f, fp, lfp );
258 }
259     
260
261
262
263 /*
264  * Get the next "record" from the file pointed to by fp.  A "record"
265  * is delimited by two consecutive newlines.  Returns NULL on EOF.
266  */
267 static char *
268 get_record(
269     FILE *fp
270 )
271 {
272     int         len;
273     static char line[BUFSIZ];
274     char        *buf = NULL;
275     static int  lcur, lmax;
276
277     lcur = lmax = 0;
278
279     while (( fgets( line, sizeof(line), fp ) != NULL ) &&
280             (( len = strlen( line )) > 1 )) {
281
282                 while ( lcur + len + 1 > lmax ) {
283                     lmax += BUFSIZ;
284                     buf = (char *) ch_realloc( buf, lmax );
285                 }
286                 strcpy( buf + lcur, line );
287                 lcur += len;
288     }
289     return( buf );
290 }