]> git.sur5r.net Git - openldap/blob - servers/slurpd/fm.c
Happy New Year!
[openldap] / servers / slurpd / fm.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15 /* Portions Copyright (c) 1996 Regents of the University of Michigan.
16  * All rights reserved.
17  *
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.
24  */
25 /* ACKNOWLEDGEMENTS:
26  * This work was originally developed by the University of Michigan
27  * (as part of U-MICH LDAP).
28  */
29
30 /*
31  * fm.c - file management routines.
32  */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37
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>
43
44 #include "slurp.h"
45 #include "globals.h"
46
47
48 /*
49  * Forward references
50  */
51 static char *get_record LDAP_P(( FILE * ));
52 static void populate_queue LDAP_P(( char *f ));
53
54
55 /*
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.
62  */
63 void *
64 fm(
65     void *arg
66 )
67 {
68     int rc;
69     int i;
70     fd_set readfds;
71
72     /* Set up our signal handlers:
73      * SIG{TERM,INT,HUP} causes a shutdown
74      */
75     (void) SIGNAL( SIGTERM, slurp_set_shutdown );
76     (void) SIGNAL( SIGINT, slurp_set_shutdown );
77 #ifdef SIGHUP
78     (void) SIGNAL( SIGHUP, slurp_set_shutdown );
79 #endif
80 #if defined(SIGBREAK) && defined(HAVE_NT_SERVICE_MANAGER)
81     (void) SIGNAL( SIGBREAK, do_nothing );
82 #endif
83
84     if ( sglob->one_shot_mode ) {
85         if ( file_nonempty( sglob->slapd_replogfile )) {
86             populate_queue( sglob->slapd_replogfile );
87         }
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 ));
93         return NULL;
94     }
95     /*
96      * There may be some leftover replication records in our own
97      * copy of the replication log.  If any exist, add them to the
98      * queue.
99      */
100     if ( file_nonempty( sglob->slurpd_replogfile )) {
101         populate_queue( sglob->slurpd_replogfile );
102     }
103
104     FD_ZERO( &readfds );
105
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 );
114             } else {
115                 if ( rc < 0 ) {
116                     Debug( LDAP_DEBUG_ANY,
117                             "Fatal error while copying replication log\n",
118                             0, 0, 0 );
119                     sglob->slurpd_shutdown = 1;
120                 }
121             }
122         } else {
123             struct timeval tv;
124
125             FD_SET( sglob->wake_sds[0], &readfds );
126             tv.tv_sec = sglob->no_work_interval;
127             tv.tv_usec = 0;
128
129             rc = select( sglob->wake_sds[0]+1, &readfds, NULL, NULL, &tv );
130         }
131
132         /* Garbage-collect queue */
133         sglob->rq->rq_gc( sglob->rq );
134
135         /* Trim replication log file, if needed */
136         if ( sglob->rq->rq_needtrim( sglob->rq )) {
137             FILE *fp, *lfp;
138             if (( rc = acquire_lock( sglob->slurpd_replogfile, &fp,
139                     &lfp )) < 0 ) {
140                 Debug( LDAP_DEBUG_ANY,
141                         "Error: cannot acquire lock on \"%s\" for trimming\n",
142                         sglob->slurpd_replogfile, 0, 0 );
143             } else {
144                 sglob->rq->rq_write( sglob->rq, fp );
145                 (void) relinquish_lock( sglob->slurpd_replogfile, fp, lfp );
146             }
147         }
148     }
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 ]);
153     }
154     sglob->rq->rq_unlock( sglob->rq );                  /* unlock queue */
155     Debug( LDAP_DEBUG_ARGS, "fm: exiting\n", 0, 0, 0 );
156     return NULL;
157 }
158
159
160
161
162 /*
163  * Set a global flag which signals that we're shutting down.
164  */
165 RETSIGTYPE
166 slurp_set_shutdown(int sig)
167 {
168     sglob->slurpd_shutdown = 1;                         /* set flag */
169     tcp_write( sglob->wake_sds[1], "0", 1);             /* wake up file mgr */
170
171     (void) SIGNAL_REINSTALL( sig, slurp_set_shutdown ); /* reinstall handlers */
172 }
173
174
175
176
177 /*
178  * A do-nothing signal handler.
179  */
180 RETSIGTYPE
181 do_nothing(int sig)
182 {
183     (void) SIGNAL_REINSTALL( sig, do_nothing );
184 }
185
186
187
188
189 /*
190  * Open the slurpd replication log, seek to our last known position, and
191  * process any pending replication entries.
192  */
193 static void
194 populate_queue(
195     char *f
196 )
197 {
198     FILE        *fp, *lfp;
199     char        *p;
200
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 );
205         return;
206     }
207
208     /*
209      * Read replication records from fp and append them the
210      * the queue.
211      */
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 );
216     } else {
217     while (( p = get_record( fp )) != NULL ) {
218         if ( sglob->rq->rq_add( sglob->rq, p ) < 0 ) {
219             char *t;
220             /* Print an error message.  Only print first line.  */
221             if (( t = strchr( p, '\n' )) != NULL ) {
222                 *t = '\0';
223             }
224             Debug( LDAP_DEBUG_ANY,
225                     "error: malformed replog entry (begins with \"%s\")\n",
226                     p, 0, 0 );
227         }
228         free( p );
229         ldap_pvt_thread_yield();
230     }
231     sglob->srpos = ftell( fp );
232     }
233     (void) relinquish_lock( f, fp, lfp );
234 }
235     
236
237
238
239 /*
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.
242  */
243 static char *
244 get_record(
245     FILE *fp
246 )
247 {
248     int         len;
249     static char line[BUFSIZ];
250     char        *buf = NULL;
251     static int  lcur, lmax;
252
253     lcur = lmax = 0;
254
255     while (( fgets( line, sizeof(line), fp ) != NULL ) &&
256             (( len = strlen( line )) > 1 )) {
257
258                 while ( lcur + len + 1 > lmax ) {
259                     lmax += BUFSIZ;
260                     buf = (char *) ch_realloc( buf, lmax );
261                 }
262                 strcpy( buf + lcur, line );
263                 lcur += len;
264     }
265     return( buf );
266 }