]> git.sur5r.net Git - openldap/blob - servers/slurpd/fm.c
164f407283c9f08948ce08749ed903de91049aa9
[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 #include <ac/socket.h>
30 #include <ac/unistd.h>
31
32 #include "slurp.h"
33 #include "globals.h"
34
35
36 /*
37  * Forward references
38  */
39 static char *get_record LDAP_P(( FILE * ));
40 static void populate_queue LDAP_P(( char *f ));
41
42
43 /*
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.
50  */
51 void *
52 fm(
53     void *arg
54 )
55 {
56     int rc;
57     int i;
58     fd_set readfds;
59
60     /* Set up our signal handlers:
61      * SIG{TERM,INT,HUP} causes a shutdown
62      */
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 #if defined(SIGBREAK) && defined(HAVE_NT_SERVICE_MANAGER)
69     (void) SIGNAL( SIGBREAK, do_nothing );
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     FD_ZERO( &readfds );
93
94     while ( !sglob->slurpd_shutdown ) {
95         if ( file_nonempty( sglob->slapd_replogfile )) {
96             /* New work found - copy to slurpd replog file */
97 #ifdef NEW_LOGGING
98         LDAP_LOG ( SLURPD, ARGS, 
99                         "fm: new work in %s\n", sglob->slapd_replogfile, 0, 0 );
100 #else
101             Debug( LDAP_DEBUG_ARGS, "new work in %s\n",
102                     sglob->slapd_replogfile, 0, 0 );
103 #endif
104             if (( rc = copy_replog( sglob->slapd_replogfile,
105                     sglob->slurpd_replogfile )) == 0 )  {
106                 populate_queue( sglob->slurpd_replogfile );
107             } else {
108                 if ( rc < 0 ) {
109 #ifdef NEW_LOGGING
110                 LDAP_LOG ( SLURPD, CRIT, 
111                                 "fm: Fatal error while copying replication log\n" , 0, 0, 0);
112 #else
113                     Debug( LDAP_DEBUG_ANY,
114                             "Fatal error while copying replication log\n",
115                             0, 0, 0 );
116 #endif
117                     sglob->slurpd_shutdown = 1;
118                 }
119             }
120         } else {
121             struct timeval tv;
122
123             FD_SET( sglob->wake_sds[0], &readfds );
124             tv.tv_sec = sglob->no_work_interval;
125             tv.tv_usec = 0;
126
127             rc = select( sglob->wake_sds[0]+1, &readfds, NULL, NULL, &tv );
128         }
129
130         /* Garbage-collect queue */
131         sglob->rq->rq_gc( sglob->rq );
132
133         /* Trim replication log file, if needed */
134         if ( sglob->rq->rq_needtrim( sglob->rq )) {
135             FILE *fp, *lfp;
136             if (( rc = acquire_lock( sglob->slurpd_replogfile, &fp,
137                     &lfp )) < 0 ) {
138 #ifdef NEW_LOGGING
139                 LDAP_LOG ( SLURPD, ERR, 
140                         "fm: Error: cannot acquire lock on \"%s\" for trimming\n",
141                         sglob->slurpd_replogfile, 0, 0 );
142 #else
143                 Debug( LDAP_DEBUG_ANY,
144                         "Error: cannot acquire lock on \"%s\" for trimming\n",
145                         sglob->slurpd_replogfile, 0, 0 );
146 #endif
147             } else {
148                 sglob->rq->rq_write( sglob->rq, fp );
149                 (void) relinquish_lock( sglob->slurpd_replogfile, fp, lfp );
150             }
151         }
152     }
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 ]);
157     }
158     sglob->rq->rq_unlock( sglob->rq );                  /* unlock queue */
159 #ifdef NEW_LOGGING
160         LDAP_LOG ( SLURPD, RESULTS, "fm: exiting\n", 0, 0, 0 );
161 #else
162     Debug( LDAP_DEBUG_ARGS, "fm: exiting\n", 0, 0, 0 );
163 #endif
164     return NULL;
165 }
166
167
168
169
170 /*
171  * Set a global flag which signals that we're shutting down.
172  */
173 RETSIGTYPE
174 slurp_set_shutdown(int sig)
175 {
176     sglob->slurpd_shutdown = 1;                         /* set flag */
177     tcp_write( sglob->wake_sds[1], "0", 1);             /* wake up file mgr */
178
179     (void) SIGNAL_REINSTALL( sig, slurp_set_shutdown ); /* reinstall handlers */
180 }
181
182
183
184
185 /*
186  * A do-nothing signal handler.
187  */
188 RETSIGTYPE
189 do_nothing(int sig)
190 {
191     (void) SIGNAL_REINSTALL( sig, do_nothing );
192 }
193
194
195
196
197 /*
198  * Open the slurpd replication log, seek to our last known position, and
199  * process any pending replication entries.
200  */
201 static void
202 populate_queue(
203     char *f
204 )
205 {
206     FILE        *fp, *lfp;
207     char        *p;
208
209     if ( acquire_lock( f, &fp, &lfp ) < 0 ) {
210 #ifdef NEW_LOGGING
211         LDAP_LOG ( SLURPD, ERR, 
212                 "populate_queue: error: can't lock file \"%s\": %s\n", 
213                 f, sys_errlist[ errno ], 0 );
214 #else
215         Debug( LDAP_DEBUG_ANY,
216                 "error: can't lock file \"%s\": %s\n",
217                 f, sys_errlist[ errno ], 0 );
218 #endif
219         return;
220     }
221
222     /*
223      * Read replication records from fp and append them the
224      * the queue.
225      */
226     if ( fseek( fp, sglob->srpos, 0 ) < 0 ) {
227 #ifdef NEW_LOGGING
228         LDAP_LOG ( SLURPD, ERR, 
229                 "populate_queue: error: can't seek to offset %ld in file \"%s\"\n", 
230                 sglob->srpos, f, 0 );
231 #else
232         Debug( LDAP_DEBUG_ANY,
233                 "error: can't seek to offset %ld in file \"%s\"\n",
234                 sglob->srpos, f, 0 );
235 #endif
236     } else {
237     while (( p = get_record( fp )) != NULL ) {
238         if ( sglob->rq->rq_add( sglob->rq, p ) < 0 ) {
239             char *t;
240             /* Print an error message.  Only print first line.  */
241             if (( t = strchr( p, '\n' )) != NULL ) {
242                 *t = '\0';
243             }
244 #ifdef NEW_LOGGING
245                 LDAP_LOG ( SLURPD, ERR, 
246                         "populate_queue: error: malformed replog entry "
247                         "(begins with \"%s\")\n", p, 0, 0 );
248 #else
249             Debug( LDAP_DEBUG_ANY,
250                     "error: malformed replog entry (begins with \"%s\")\n",
251                     p, 0, 0 );
252 #endif
253         }
254         free( p );
255         ldap_pvt_thread_yield();
256     }
257     sglob->srpos = ftell( fp );
258     }
259     (void) relinquish_lock( f, fp, lfp );
260 }
261     
262
263
264
265 /*
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.
268  */
269 static char *
270 get_record(
271     FILE *fp
272 )
273 {
274     int         len;
275     static char line[BUFSIZ];
276     char        *buf = NULL;
277     static int  lcur, lmax;
278
279     lcur = lmax = 0;
280
281     while (( fgets( line, sizeof(line), fp ) != NULL ) &&
282             (( len = strlen( line )) > 1 )) {
283
284                 while ( lcur + len + 1 > lmax ) {
285                     lmax += BUFSIZ;
286                     buf = (char *) ch_realloc( buf, lmax );
287                 }
288                 strcpy( buf + lcur, line );
289                 lcur += len;
290     }
291     return( buf );
292 }