]> git.sur5r.net Git - openldap/blob - servers/slurpd/fm.c
8a8f5c4723d73b1872878ce3899641b6a31c5898
[openldap] / servers / slurpd / fm.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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 static RETSIGTYPE set_shutdown LDAP_P((int));
40
41
42 /*
43  * Main file manager routine.  Watches for new data to be appended to the
44  * slapd replication log.  When new data is appended, fm does the following:
45  *  - appends the data to slurpd's private copy of the replication log.
46  *  - truncates the slapd replog
47  *  - adds items to the internal queue of replication work to do
48  *  - signals the replication threads to let them know new work has arrived.
49  */
50 void *
51 fm(
52     void *arg
53 )
54 {
55     int rc;
56
57     /* Set up our signal handlers:
58      * SIG{TERM,INT,HUP} causes a shutdown
59      * LDAP_SIGUSR1 - does nothing, used to wake up sleeping threads.
60          * LDAP_SIGUSR2 - causes a shutdown
61      */
62     (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
63     (void) SIGNAL( LDAP_SIGUSR2, set_shutdown );
64     (void) SIGNAL( SIGTERM, set_shutdown );
65     (void) SIGNAL( SIGINT, set_shutdown );
66 #ifdef SIGHUP
67     (void) SIGNAL( SIGHUP, set_shutdown );
68 #endif
69
70     if ( sglob->one_shot_mode ) {
71         if ( file_nonempty( sglob->slapd_replogfile )) {
72             populate_queue( sglob->slapd_replogfile );
73         }
74         printf( "Processing in one-shot mode:\n" );
75         printf( "%d total replication records in file,\n",
76                 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_ALL ));
77         printf( "%d replication records to process.\n",
78                 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_NZRC ));
79         return NULL;
80     }
81     /*
82      * There may be some leftover replication records in our own
83      * copy of the replication log.  If any exist, add them to the
84      * queue.
85      */
86     if ( file_nonempty( sglob->slurpd_replogfile )) {
87         populate_queue( sglob->slurpd_replogfile );
88     }
89
90
91     while ( !sglob->slurpd_shutdown ) {
92         if ( file_nonempty( sglob->slapd_replogfile )) {
93             /* New work found - copy to slurpd replog file */
94             Debug( LDAP_DEBUG_ARGS, "new work in %s\n",
95                     sglob->slapd_replogfile, 0, 0 );
96             if (( rc = copy_replog( sglob->slapd_replogfile,
97                     sglob->slurpd_replogfile )) == 0 )  {
98                 populate_queue( sglob->slurpd_replogfile );
99             } else {
100                 if ( rc < 0 ) {
101                     Debug( LDAP_DEBUG_ANY,
102                             "Fatal error while copying replication log\n",
103                             0, 0, 0 );
104                     sglob->slurpd_shutdown = 1;
105                 }
106             }
107         } else {
108             ldap_pvt_thread_sleep( sglob->no_work_interval );
109         }
110
111         /* Garbage-collect queue */
112         sglob->rq->rq_gc( sglob->rq );
113
114         /* Trim replication log file, if needed */
115         if ( sglob->rq->rq_needtrim( sglob->rq )) {
116             FILE *fp, *lfp;
117             if (( rc = acquire_lock( sglob->slurpd_replogfile, &fp,
118                     &lfp )) < 0 ) {
119                 Debug( LDAP_DEBUG_ANY,
120                         "Error: cannot acquire lock on \"%s\" for trimming\n",
121                         sglob->slurpd_replogfile, 0, 0 );
122             } else {
123                 sglob->rq->rq_write( sglob->rq, fp );
124                 (void) relinquish_lock( sglob->slurpd_replogfile, fp, lfp );
125             }
126         }
127     }
128     Debug( LDAP_DEBUG_ARGS, "fm: exiting\n", 0, 0, 0 );
129     return NULL;
130 }
131
132
133
134
135 /*
136  * Set a global flag which signals that we're shutting down.
137  */
138 static RETSIGTYPE
139 set_shutdown(int sig)
140 {
141     int i;
142
143     sglob->slurpd_shutdown = 1;                         /* set flag */
144     ldap_pvt_thread_kill( sglob->fm_tid, LDAP_SIGUSR1 );        /* wake up file mgr */
145     sglob->rq->rq_lock( sglob->rq );                    /* lock queue */
146     ldap_pvt_thread_cond_broadcast( &(sglob->rq->rq_more) );    /* wake repl threads */
147     for ( i = 0; i < sglob->num_replicas; i++ ) {
148         (sglob->replicas[ i ])->ri_wake( sglob->replicas[ i ]);
149     }
150     sglob->rq->rq_unlock( sglob->rq );                  /* unlock queue */
151     (void) SIGNAL_REINSTALL( sig, set_shutdown );       /* reinstall handlers */
152 }
153
154
155
156
157 /*
158  * A do-nothing signal handler.
159  */
160 RETSIGTYPE
161 do_nothing(int sig)
162 {
163     (void) SIGNAL_REINSTALL( sig, do_nothing );
164 }
165
166
167
168
169 /*
170  * Open the slurpd replication log, seek to our last known position, and
171  * process any pending replication entries.
172  */
173 static void
174 populate_queue(
175     char *f
176 )
177 {
178     FILE        *fp, *lfp;
179     char        *p;
180
181     if ( acquire_lock( f, &fp, &lfp ) < 0 ) {
182         Debug( LDAP_DEBUG_ANY,
183                 "error: can't lock file \"%s\": %s\n",
184                 f, sys_errlist[ errno ], 0 );
185         return;
186     }
187
188     /*
189      * Read replication records from fp and append them the
190      * the queue.
191      */
192     if ( fseek( fp, sglob->srpos, 0 ) < 0 ) {
193         Debug( LDAP_DEBUG_ANY,
194                 "error: can't seek to offset %ld in file \"%s\"\n",
195                 sglob->srpos, f, 0 );
196     } else {
197     while (( p = get_record( fp )) != NULL ) {
198         if ( sglob->rq->rq_add( sglob->rq, p ) < 0 ) {
199             char *t;
200             /* Print an error message.  Only print first line.  */
201             if (( t = strchr( p, '\n' )) != NULL ) {
202                 *t = '\0';
203             }
204             Debug( LDAP_DEBUG_ANY,
205                     "error: malformed replog entry (begins with \"%s\")\n",
206                     p, 0, 0 );
207         }
208         free( p );
209         ldap_pvt_thread_yield();
210     }
211     sglob->srpos = ftell( fp );
212     }
213     (void) relinquish_lock( f, fp, lfp );
214 }
215     
216
217
218
219 /*
220  * Get the next "record" from the file pointed to by fp.  A "record"
221  * is delimited by two consecutive newlines.  Returns NULL on EOF.
222  */
223 static char *
224 get_record(
225     FILE *fp
226 )
227 {
228     int         len;
229     static char line[BUFSIZ];
230     char        *buf = NULL;
231     static int  lcur, lmax;
232
233     lcur = lmax = 0;
234
235     while (( fgets( line, sizeof(line), fp ) != NULL ) &&
236             (( len = strlen( line )) > 1 )) {
237
238                 while ( lcur + len + 1 > lmax ) {
239                     lmax += BUFSIZ;
240                     buf = (char *) ch_realloc( buf, lmax );
241                 }
242                 strcpy( buf + lcur, line );
243                 lcur += len;
244     }
245     return( buf );
246 }